parse.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. 'use strict';
  2. const stringify = require('./stringify');
  3. /**
  4. * Constants
  5. */
  6. const {
  7. MAX_LENGTH,
  8. CHAR_BACKSLASH, /* \ */
  9. CHAR_BACKTICK, /* ` */
  10. CHAR_COMMA, /* , */
  11. CHAR_DOT, /* . */
  12. CHAR_LEFT_PARENTHESES, /* ( */
  13. CHAR_RIGHT_PARENTHESES, /* ) */
  14. CHAR_LEFT_CURLY_BRACE, /* { */
  15. CHAR_RIGHT_CURLY_BRACE, /* } */
  16. CHAR_LEFT_SQUARE_BRACKET, /* [ */
  17. CHAR_RIGHT_SQUARE_BRACKET, /* ] */
  18. CHAR_DOUBLE_QUOTE, /* " */
  19. CHAR_SINGLE_QUOTE, /* ' */
  20. CHAR_NO_BREAK_SPACE,
  21. CHAR_ZERO_WIDTH_NOBREAK_SPACE
  22. } = require('./constants');
  23. /**
  24. * parse
  25. */
  26. const parse = (input, options = {}) => {
  27. if (typeof input !== 'string') {
  28. throw new TypeError('Expected a string');
  29. }
  30. let opts = options || {};
  31. let max = typeof opts.maxLength === 'number' ? Math.min(MAX_LENGTH, opts.maxLength) : MAX_LENGTH;
  32. if (input.length > max) {
  33. throw new SyntaxError(`Input length (${input.length}), exceeds max characters (${max})`);
  34. }
  35. let ast = { type: 'root', input, nodes: [] };
  36. let stack = [ast];
  37. let block = ast;
  38. let prev = ast;
  39. let brackets = 0;
  40. let length = input.length;
  41. let index = 0;
  42. let depth = 0;
  43. let value;
  44. let memo = {};
  45. /**
  46. * Helpers
  47. */
  48. const advance = () => input[index++];
  49. const push = node => {
  50. if (node.type === 'text' && prev.type === 'dot') {
  51. prev.type = 'text';
  52. }
  53. if (prev && prev.type === 'text' && node.type === 'text') {
  54. prev.value += node.value;
  55. return;
  56. }
  57. block.nodes.push(node);
  58. node.parent = block;
  59. node.prev = prev;
  60. prev = node;
  61. return node;
  62. };
  63. push({ type: 'bos' });
  64. while (index < length) {
  65. block = stack[stack.length - 1];
  66. value = advance();
  67. /**
  68. * Invalid chars
  69. */
  70. if (value === CHAR_ZERO_WIDTH_NOBREAK_SPACE || value === CHAR_NO_BREAK_SPACE) {
  71. continue;
  72. }
  73. /**
  74. * Escaped chars
  75. */
  76. if (value === CHAR_BACKSLASH) {
  77. push({ type: 'text', value: (options.keepEscaping ? value : '') + advance() });
  78. continue;
  79. }
  80. /**
  81. * Right square bracket (literal): ']'
  82. */
  83. if (value === CHAR_RIGHT_SQUARE_BRACKET) {
  84. push({ type: 'text', value: '\\' + value });
  85. continue;
  86. }
  87. /**
  88. * Left square bracket: '['
  89. */
  90. if (value === CHAR_LEFT_SQUARE_BRACKET) {
  91. brackets++;
  92. let closed = true;
  93. let next;
  94. while (index < length && (next = advance())) {
  95. value += next;
  96. if (next === CHAR_LEFT_SQUARE_BRACKET) {
  97. brackets++;
  98. continue;
  99. }
  100. if (next === CHAR_BACKSLASH) {
  101. value += advance();
  102. continue;
  103. }
  104. if (next === CHAR_RIGHT_SQUARE_BRACKET) {
  105. brackets--;
  106. if (brackets === 0) {
  107. break;
  108. }
  109. }
  110. }
  111. push({ type: 'text', value });
  112. continue;
  113. }
  114. /**
  115. * Parentheses
  116. */
  117. if (value === CHAR_LEFT_PARENTHESES) {
  118. block = push({ type: 'paren', nodes: [] });
  119. stack.push(block);
  120. push({ type: 'text', value });
  121. continue;
  122. }
  123. if (value === CHAR_RIGHT_PARENTHESES) {
  124. if (block.type !== 'paren') {
  125. push({ type: 'text', value });
  126. continue;
  127. }
  128. block = stack.pop();
  129. push({ type: 'text', value });
  130. block = stack[stack.length - 1];
  131. continue;
  132. }
  133. /**
  134. * Quotes: '|"|`
  135. */
  136. if (value === CHAR_DOUBLE_QUOTE || value === CHAR_SINGLE_QUOTE || value === CHAR_BACKTICK) {
  137. let open = value;
  138. let next;
  139. if (options.keepQuotes !== true) {
  140. value = '';
  141. }
  142. while (index < length && (next = advance())) {
  143. if (next === CHAR_BACKSLASH) {
  144. value += next + advance();
  145. continue;
  146. }
  147. if (next === open) {
  148. if (options.keepQuotes === true) value += next;
  149. break;
  150. }
  151. value += next;
  152. }
  153. push({ type: 'text', value });
  154. continue;
  155. }
  156. /**
  157. * Left curly brace: '{'
  158. */
  159. if (value === CHAR_LEFT_CURLY_BRACE) {
  160. depth++;
  161. let dollar = prev.value && prev.value.slice(-1) === '$' || block.dollar === true;
  162. let brace = {
  163. type: 'brace',
  164. open: true,
  165. close: false,
  166. dollar,
  167. depth,
  168. commas: 0,
  169. ranges: 0,
  170. nodes: []
  171. };
  172. block = push(brace);
  173. stack.push(block);
  174. push({ type: 'open', value });
  175. continue;
  176. }
  177. /**
  178. * Right curly brace: '}'
  179. */
  180. if (value === CHAR_RIGHT_CURLY_BRACE) {
  181. if (block.type !== 'brace') {
  182. push({ type: 'text', value });
  183. continue;
  184. }
  185. let type = 'close';
  186. block = stack.pop();
  187. block.close = true;
  188. push({ type, value });
  189. depth--;
  190. block = stack[stack.length - 1];
  191. continue;
  192. }
  193. /**
  194. * Comma: ','
  195. */
  196. if (value === CHAR_COMMA && depth > 0) {
  197. if (block.ranges > 0) {
  198. block.ranges = 0;
  199. let open = block.nodes.shift();
  200. block.nodes = [open, { type: 'text', value: stringify(block) }];
  201. }
  202. push({ type: 'comma', value });
  203. block.commas++;
  204. continue;
  205. }
  206. /**
  207. * Dot: '.'
  208. */
  209. if (value === CHAR_DOT && depth > 0 && block.commas === 0) {
  210. let siblings = block.nodes;
  211. if (depth === 0 || siblings.length === 0) {
  212. push({ type: 'text', value });
  213. continue;
  214. }
  215. if (prev.type === 'dot') {
  216. block.range = [];
  217. prev.value += value;
  218. prev.type = 'range';
  219. if (block.nodes.length !== 3 && block.nodes.length !== 5) {
  220. block.invalid = true;
  221. block.ranges = 0;
  222. prev.type = 'text';
  223. continue;
  224. }
  225. block.ranges++;
  226. block.args = [];
  227. continue;
  228. }
  229. if (prev.type === 'range') {
  230. siblings.pop();
  231. let before = siblings[siblings.length - 1];
  232. before.value += prev.value + value;
  233. prev = before;
  234. block.ranges--;
  235. continue;
  236. }
  237. push({ type: 'dot', value });
  238. continue;
  239. }
  240. /**
  241. * Text
  242. */
  243. push({ type: 'text', value });
  244. }
  245. // Mark imbalanced braces and brackets as invalid
  246. do {
  247. block = stack.pop();
  248. if (block.type !== 'root') {
  249. block.nodes.forEach(node => {
  250. if (!node.nodes) {
  251. if (node.type === 'open') node.isOpen = true;
  252. if (node.type === 'close') node.isClose = true;
  253. if (!node.nodes) node.type = 'text';
  254. node.invalid = true;
  255. }
  256. });
  257. // get the location of the block on parent.nodes (block's siblings)
  258. let parent = stack[stack.length - 1];
  259. let index = parent.nodes.indexOf(block);
  260. // replace the (invalid) block with it's nodes
  261. parent.nodes.splice(index, 1, ...block.nodes);
  262. }
  263. } while (stack.length > 0);
  264. push({ type: 'eos' });
  265. return ast;
  266. };
  267. module.exports = parse;