Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

compilers.js 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. 'use strict';
  2. var utils = require('./utils');
  3. module.exports = function(braces, options) {
  4. braces.compiler
  5. /**
  6. * bos
  7. */
  8. .set('bos', function() {
  9. if (this.output) return;
  10. this.ast.queue = isEscaped(this.ast) ? [this.ast.val] : [];
  11. this.ast.count = 1;
  12. })
  13. /**
  14. * Square brackets
  15. */
  16. .set('bracket', function(node) {
  17. var close = node.close;
  18. var open = !node.escaped ? '[' : '\\[';
  19. var negated = node.negated;
  20. var inner = node.inner;
  21. inner = inner.replace(/\\(?=[\\\w]|$)/g, '\\\\');
  22. if (inner === ']-') {
  23. inner = '\\]\\-';
  24. }
  25. if (negated && inner.indexOf('.') === -1) {
  26. inner += '.';
  27. }
  28. if (negated && inner.indexOf('/') === -1) {
  29. inner += '/';
  30. }
  31. var val = open + negated + inner + close;
  32. var queue = node.parent.queue;
  33. var last = utils.arrayify(queue.pop());
  34. queue.push(utils.join(last, val));
  35. queue.push.apply(queue, []);
  36. })
  37. /**
  38. * Brace
  39. */
  40. .set('brace', function(node) {
  41. node.queue = isEscaped(node) ? [node.val] : [];
  42. node.count = 1;
  43. return this.mapVisit(node.nodes);
  44. })
  45. /**
  46. * Open
  47. */
  48. .set('brace.open', function(node) {
  49. node.parent.open = node.val;
  50. })
  51. /**
  52. * Inner
  53. */
  54. .set('text', function(node) {
  55. var queue = node.parent.queue;
  56. var escaped = node.escaped;
  57. var segs = [node.val];
  58. if (node.optimize === false) {
  59. options = utils.extend({}, options, {optimize: false});
  60. }
  61. if (node.multiplier > 1) {
  62. node.parent.count *= node.multiplier;
  63. }
  64. if (options.quantifiers === true && utils.isQuantifier(node.val)) {
  65. escaped = true;
  66. } else if (node.val.length > 1) {
  67. if (isType(node.parent, 'brace') && !isEscaped(node)) {
  68. var expanded = utils.expand(node.val, options);
  69. segs = expanded.segs;
  70. if (expanded.isOptimized) {
  71. node.parent.isOptimized = true;
  72. }
  73. // if nothing was expanded, we probably have a literal brace
  74. if (!segs.length) {
  75. var val = (expanded.val || node.val);
  76. if (options.unescape !== false) {
  77. // unescape unexpanded brace sequence/set separators
  78. val = val.replace(/\\([,.])/g, '$1');
  79. // strip quotes
  80. val = val.replace(/["'`]/g, '');
  81. }
  82. segs = [val];
  83. escaped = true;
  84. }
  85. }
  86. } else if (node.val === ',') {
  87. if (options.expand) {
  88. node.parent.queue.push(['']);
  89. segs = [''];
  90. } else {
  91. segs = ['|'];
  92. }
  93. } else {
  94. escaped = true;
  95. }
  96. if (escaped && isType(node.parent, 'brace')) {
  97. if (node.parent.nodes.length <= 4 && node.parent.count === 1) {
  98. node.parent.escaped = true;
  99. } else if (node.parent.length <= 3) {
  100. node.parent.escaped = true;
  101. }
  102. }
  103. if (!hasQueue(node.parent)) {
  104. node.parent.queue = segs;
  105. return;
  106. }
  107. var last = utils.arrayify(queue.pop());
  108. if (node.parent.count > 1 && options.expand) {
  109. last = multiply(last, node.parent.count);
  110. node.parent.count = 1;
  111. }
  112. queue.push(utils.join(utils.flatten(last), segs.shift()));
  113. queue.push.apply(queue, segs);
  114. })
  115. /**
  116. * Close
  117. */
  118. .set('brace.close', function(node) {
  119. var queue = node.parent.queue;
  120. var prev = node.parent.parent;
  121. var last = prev.queue.pop();
  122. var open = node.parent.open;
  123. var close = node.val;
  124. if (open && close && isOptimized(node, options)) {
  125. open = '(';
  126. close = ')';
  127. }
  128. // if a close brace exists, and the previous segment is one character
  129. // don't wrap the result in braces or parens
  130. var ele = utils.last(queue);
  131. if (node.parent.count > 1 && options.expand) {
  132. ele = multiply(queue.pop(), node.parent.count);
  133. node.parent.count = 1;
  134. queue.push(ele);
  135. }
  136. if (close && typeof ele === 'string' && ele.length === 1) {
  137. open = '';
  138. close = '';
  139. }
  140. if ((isLiteralBrace(node, options) || noInner(node)) && !node.parent.hasEmpty) {
  141. queue.push(utils.join(open, queue.pop() || ''));
  142. queue = utils.flatten(utils.join(queue, close));
  143. }
  144. if (typeof last === 'undefined') {
  145. prev.queue = [queue];
  146. } else {
  147. prev.queue.push(utils.flatten(utils.join(last, queue)));
  148. }
  149. })
  150. /**
  151. * eos
  152. */
  153. .set('eos', function(node) {
  154. if (this.input) return;
  155. if (options.optimize !== false) {
  156. this.output = utils.last(utils.flatten(this.ast.queue));
  157. } else if (Array.isArray(utils.last(this.ast.queue))) {
  158. this.output = utils.flatten(this.ast.queue.pop());
  159. } else {
  160. this.output = utils.flatten(this.ast.queue);
  161. }
  162. if (node.parent.count > 1 && options.expand) {
  163. this.output = multiply(this.output, node.parent.count);
  164. }
  165. this.output = utils.arrayify(this.output);
  166. this.ast.queue = [];
  167. });
  168. };
  169. /**
  170. * Multiply the segments in the current brace level
  171. */
  172. function multiply(queue, n, options) {
  173. return utils.flatten(utils.repeat(utils.arrayify(queue), n));
  174. }
  175. /**
  176. * Return true if `node` is escaped
  177. */
  178. function isEscaped(node) {
  179. return node.escaped === true;
  180. }
  181. /**
  182. * Returns true if regex parens should be used for sets. If the parent `type`
  183. * is not `brace`, then we're on a root node, which means we should never
  184. * expand segments and open/close braces should be `{}` (since this indicates
  185. * a brace is missing from the set)
  186. */
  187. function isOptimized(node, options) {
  188. if (node.parent.isOptimized) return true;
  189. return isType(node.parent, 'brace')
  190. && !isEscaped(node.parent)
  191. && options.expand !== true;
  192. }
  193. /**
  194. * Returns true if the value in `node` should be wrapped in a literal brace.
  195. * @return {Boolean}
  196. */
  197. function isLiteralBrace(node, options) {
  198. return isEscaped(node.parent) || options.optimize !== false;
  199. }
  200. /**
  201. * Returns true if the given `node` does not have an inner value.
  202. * @return {Boolean}
  203. */
  204. function noInner(node, type) {
  205. if (node.parent.queue.length === 1) {
  206. return true;
  207. }
  208. var nodes = node.parent.nodes;
  209. return nodes.length === 3
  210. && isType(nodes[0], 'brace.open')
  211. && !isType(nodes[1], 'text')
  212. && isType(nodes[2], 'brace.close');
  213. }
  214. /**
  215. * Returns true if the given `node` is the given `type`
  216. * @return {Boolean}
  217. */
  218. function isType(node, type) {
  219. return typeof node !== 'undefined' && node.type === type;
  220. }
  221. /**
  222. * Returns true if the given `node` has a non-empty queue.
  223. * @return {Boolean}
  224. */
  225. function hasQueue(node) {
  226. return Array.isArray(node.queue) && node.queue.length;
  227. }