You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

parse.js 6.7KB

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