説明なし

parsers.js 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. 'use strict';
  2. var Node = require('snapdragon-node');
  3. var utils = require('./utils');
  4. /**
  5. * Braces parsers
  6. */
  7. module.exports = function(braces, options) {
  8. braces.parser
  9. .set('bos', function() {
  10. if (!this.parsed) {
  11. this.ast = this.nodes[0] = new Node(this.ast);
  12. }
  13. })
  14. /**
  15. * Character parsers
  16. */
  17. .set('escape', function() {
  18. var pos = this.position();
  19. var m = this.match(/^(?:\\(.)|\$\{)/);
  20. if (!m) return;
  21. var prev = this.prev();
  22. var last = utils.last(prev.nodes);
  23. var node = pos(new Node({
  24. type: 'text',
  25. multiplier: 1,
  26. val: m[0]
  27. }));
  28. if (node.val === '\\\\') {
  29. return node;
  30. }
  31. if (node.val === '${') {
  32. var str = this.input;
  33. var idx = -1;
  34. var ch;
  35. while ((ch = str[++idx])) {
  36. this.consume(1);
  37. node.val += ch;
  38. if (ch === '\\') {
  39. node.val += str[++idx];
  40. continue;
  41. }
  42. if (ch === '}') {
  43. break;
  44. }
  45. }
  46. }
  47. if (this.options.unescape !== false) {
  48. node.val = node.val.replace(/\\([{}])/g, '$1');
  49. }
  50. if (last.val === '"' && this.input.charAt(0) === '"') {
  51. last.val = node.val;
  52. this.consume(1);
  53. return;
  54. }
  55. return concatNodes.call(this, pos, node, prev, options);
  56. })
  57. /**
  58. * Brackets: "[...]" (basic, this is overridden by
  59. * other parsers in more advanced implementations)
  60. */
  61. .set('bracket', function() {
  62. var isInside = this.isInside('brace');
  63. var pos = this.position();
  64. var m = this.match(/^(?:\[([!^]?)([^\]]{2,}|\]\-)(\]|[^*+?]+)|\[)/);
  65. if (!m) return;
  66. var prev = this.prev();
  67. var val = m[0];
  68. var negated = m[1] ? '^' : '';
  69. var inner = m[2] || '';
  70. var close = m[3] || '';
  71. if (isInside && prev.type === 'brace') {
  72. prev.text = prev.text || '';
  73. prev.text += val;
  74. }
  75. var esc = this.input.slice(0, 2);
  76. if (inner === '' && esc === '\\]') {
  77. inner += esc;
  78. this.consume(2);
  79. var str = this.input;
  80. var idx = -1;
  81. var ch;
  82. while ((ch = str[++idx])) {
  83. this.consume(1);
  84. if (ch === ']') {
  85. close = ch;
  86. break;
  87. }
  88. inner += ch;
  89. }
  90. }
  91. return pos(new Node({
  92. type: 'bracket',
  93. val: val,
  94. escaped: close !== ']',
  95. negated: negated,
  96. inner: inner,
  97. close: close
  98. }));
  99. })
  100. /**
  101. * Empty braces (we capture these early to
  102. * speed up processing in the compiler)
  103. */
  104. .set('multiplier', function() {
  105. var isInside = this.isInside('brace');
  106. var pos = this.position();
  107. var m = this.match(/^\{(,+(?:(\{,+\})*),*|,*(?:(\{,+\})*),+)\}/);
  108. if (!m) return;
  109. this.multiplier = true;
  110. var prev = this.prev();
  111. var val = m[0];
  112. if (isInside && prev.type === 'brace') {
  113. prev.text = prev.text || '';
  114. prev.text += val;
  115. }
  116. var node = pos(new Node({
  117. type: 'text',
  118. multiplier: 1,
  119. match: m,
  120. val: val
  121. }));
  122. return concatNodes.call(this, pos, node, prev, options);
  123. })
  124. /**
  125. * Open
  126. */
  127. .set('brace.open', function() {
  128. var pos = this.position();
  129. var m = this.match(/^\{(?!(?:[^\\}]?|,+)\})/);
  130. if (!m) return;
  131. var prev = this.prev();
  132. var last = utils.last(prev.nodes);
  133. // if the last parsed character was an extglob character
  134. // we need to _not optimize_ the brace pattern because
  135. // it might be mistaken for an extglob by a downstream parser
  136. if (last && last.val && isExtglobChar(last.val.slice(-1))) {
  137. last.optimize = false;
  138. }
  139. var open = pos(new Node({
  140. type: 'brace.open',
  141. val: m[0]
  142. }));
  143. var node = pos(new Node({
  144. type: 'brace',
  145. nodes: []
  146. }));
  147. node.push(open);
  148. prev.push(node);
  149. this.push('brace', node);
  150. })
  151. /**
  152. * Close
  153. */
  154. .set('brace.close', function() {
  155. var pos = this.position();
  156. var m = this.match(/^\}/);
  157. if (!m || !m[0]) return;
  158. var brace = this.pop('brace');
  159. var node = pos(new Node({
  160. type: 'brace.close',
  161. val: m[0]
  162. }));
  163. if (!this.isType(brace, 'brace')) {
  164. if (this.options.strict) {
  165. throw new Error('missing opening "{"');
  166. }
  167. node.type = 'text';
  168. node.multiplier = 0;
  169. node.escaped = true;
  170. return node;
  171. }
  172. var prev = this.prev();
  173. var last = utils.last(prev.nodes);
  174. if (last.text) {
  175. var lastNode = utils.last(last.nodes);
  176. if (lastNode.val === ')' && /[!@*?+]\(/.test(last.text)) {
  177. var open = last.nodes[0];
  178. var text = last.nodes[1];
  179. if (open.type === 'brace.open' && text && text.type === 'text') {
  180. text.optimize = false;
  181. }
  182. }
  183. }
  184. if (brace.nodes.length > 2) {
  185. var first = brace.nodes[1];
  186. if (first.type === 'text' && first.val === ',') {
  187. brace.nodes.splice(1, 1);
  188. brace.nodes.push(first);
  189. }
  190. }
  191. brace.push(node);
  192. })
  193. /**
  194. * Capture boundary characters
  195. */
  196. .set('boundary', function() {
  197. var pos = this.position();
  198. var m = this.match(/^[$^](?!\{)/);
  199. if (!m) return;
  200. return pos(new Node({
  201. type: 'text',
  202. val: m[0]
  203. }));
  204. })
  205. /**
  206. * One or zero, non-comma characters wrapped in braces
  207. */
  208. .set('nobrace', function() {
  209. var isInside = this.isInside('brace');
  210. var pos = this.position();
  211. var m = this.match(/^\{[^,]?\}/);
  212. if (!m) return;
  213. var prev = this.prev();
  214. var val = m[0];
  215. if (isInside && prev.type === 'brace') {
  216. prev.text = prev.text || '';
  217. prev.text += val;
  218. }
  219. return pos(new Node({
  220. type: 'text',
  221. multiplier: 0,
  222. val: val
  223. }));
  224. })
  225. /**
  226. * Text
  227. */
  228. .set('text', function() {
  229. var isInside = this.isInside('brace');
  230. var pos = this.position();
  231. var m = this.match(/^((?!\\)[^${}\[\]])+/);
  232. if (!m) return;
  233. var prev = this.prev();
  234. var val = m[0];
  235. if (isInside && prev.type === 'brace') {
  236. prev.text = prev.text || '';
  237. prev.text += val;
  238. }
  239. var node = pos(new Node({
  240. type: 'text',
  241. multiplier: 1,
  242. val: val
  243. }));
  244. return concatNodes.call(this, pos, node, prev, options);
  245. });
  246. };
  247. /**
  248. * Returns true if the character is an extglob character.
  249. */
  250. function isExtglobChar(ch) {
  251. return ch === '!' || ch === '@' || ch === '*' || ch === '?' || ch === '+';
  252. }
  253. /**
  254. * Combine text nodes, and calculate empty sets (`{,,}`)
  255. * @param {Function} `pos` Function to calculate node position
  256. * @param {Object} `node` AST node
  257. * @return {Object}
  258. */
  259. function concatNodes(pos, node, parent, options) {
  260. node.orig = node.val;
  261. var prev = this.prev();
  262. var last = utils.last(prev.nodes);
  263. var isEscaped = false;
  264. if (node.val.length > 1) {
  265. var a = node.val.charAt(0);
  266. var b = node.val.slice(-1);
  267. isEscaped = (a === '"' && b === '"')
  268. || (a === "'" && b === "'")
  269. || (a === '`' && b === '`');
  270. }
  271. if (isEscaped && options.unescape !== false) {
  272. node.val = node.val.slice(1, node.val.length - 1);
  273. node.escaped = true;
  274. }
  275. if (node.match) {
  276. var match = node.match[1];
  277. if (!match || match.indexOf('}') === -1) {
  278. match = node.match[0];
  279. }
  280. // replace each set with a single ","
  281. var val = match.replace(/\{/g, ',').replace(/\}/g, '');
  282. node.multiplier *= val.length;
  283. node.val = '';
  284. }
  285. var simpleText = last.type === 'text'
  286. && last.multiplier === 1
  287. && node.multiplier === 1
  288. && node.val;
  289. if (simpleText) {
  290. last.val += node.val;
  291. return;
  292. }
  293. prev.push(node);
  294. }