match.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. 'use strict';
  2. var hasOwnProperty = Object.prototype.hasOwnProperty;
  3. var matchGraph = require('./match-graph');
  4. var MATCH = matchGraph.MATCH;
  5. var MISMATCH = matchGraph.MISMATCH;
  6. var DISALLOW_EMPTY = matchGraph.DISALLOW_EMPTY;
  7. var TOKEN = 1;
  8. var OPEN_SYNTAX = 2;
  9. var CLOSE_SYNTAX = 3;
  10. var EXIT_REASON_MATCH = 'Match';
  11. var EXIT_REASON_MISMATCH = 'Mismatch';
  12. var EXIT_REASON_ITERATION_LIMIT = 'Maximum iteration number exceeded (please fill an issue on https://github.com/csstree/csstree/issues)';
  13. var ITERATION_LIMIT = 10000;
  14. var totalIterationCount = 0;
  15. function mapList(list, fn) {
  16. var result = [];
  17. while (list) {
  18. result.unshift(fn(list));
  19. list = list.prev;
  20. }
  21. return result;
  22. }
  23. function isCommaContextStart(token) {
  24. if (token === null) {
  25. return true;
  26. }
  27. token = token.value.charAt(token.value.length - 1);
  28. return (
  29. token === ',' ||
  30. token === '(' ||
  31. token === '[' ||
  32. token === '/'
  33. );
  34. }
  35. function isCommaContextEnd(token) {
  36. if (token === null) {
  37. return true;
  38. }
  39. token = token.value.charAt(0);
  40. return (
  41. token === ')' ||
  42. token === ']' ||
  43. token === '/'
  44. );
  45. }
  46. function internalMatch(tokens, syntax, syntaxes) {
  47. function moveToNextToken() {
  48. do {
  49. tokenCursor++;
  50. token = tokenCursor < tokens.length ? tokens[tokenCursor] : null;
  51. } while (token !== null && !/\S/.test(token.value));
  52. }
  53. function getNextToken(offset) {
  54. var nextIndex = tokenCursor + offset;
  55. return nextIndex < tokens.length ? tokens[nextIndex] : null;
  56. }
  57. function pushThenStack(nextSyntax) {
  58. thenStack = {
  59. nextSyntax: nextSyntax,
  60. matchStack: matchStack,
  61. syntaxStack: syntaxStack,
  62. prev: thenStack
  63. };
  64. }
  65. function pushElseStack(nextSyntax) {
  66. elseStack = {
  67. nextSyntax: nextSyntax,
  68. matchStack: matchStack,
  69. syntaxStack: syntaxStack,
  70. thenStack: thenStack,
  71. tokenCursor: tokenCursor,
  72. token: token,
  73. prev: elseStack
  74. };
  75. }
  76. function addTokenToMatch() {
  77. matchStack = {
  78. type: TOKEN,
  79. syntax: syntax.syntax,
  80. token: token,
  81. prev: matchStack
  82. };
  83. moveToNextToken();
  84. if (tokenCursor > longestMatch) {
  85. longestMatch = tokenCursor;
  86. }
  87. return matchStack.token;
  88. }
  89. function openSyntax() {
  90. syntaxStack = {
  91. syntax: syntax,
  92. prev: syntaxStack
  93. };
  94. matchStack = {
  95. type: OPEN_SYNTAX,
  96. syntax: syntax.syntax,
  97. token: matchStack.token,
  98. prev: matchStack
  99. };
  100. }
  101. function closeSyntax() {
  102. if (matchStack.type === OPEN_SYNTAX) {
  103. matchStack = matchStack.prev;
  104. } else {
  105. matchStack = {
  106. type: CLOSE_SYNTAX,
  107. syntax: syntaxStack.syntax,
  108. token: matchStack.token,
  109. prev: matchStack
  110. };
  111. }
  112. syntaxStack = syntaxStack.prev;
  113. }
  114. var syntaxStack = null;
  115. var thenStack = null;
  116. var elseStack = null;
  117. var iterationCount = 0;
  118. var exitReason = EXIT_REASON_MATCH;
  119. var matchStack = { type: 'Stub', syntax: null, token: null, tokenCursor: -1, prev: null };
  120. var longestMatch = 0;
  121. var tokenCursor = -1;
  122. var token = null;
  123. moveToNextToken();
  124. while (true) {
  125. // console.log('--\n',
  126. // '#' + iterationCount,
  127. // require('util').inspect({
  128. // match: mapList(matchStack, x => x.type === TOKEN ? x.token && x.token.value : x.syntax ? x.type + '!' + x.syntax.name : null),
  129. // elseStack: mapList(elseStack, x => x.id),
  130. // thenStack: mapList(thenStack, x => x.id),
  131. // token: token && token.value,
  132. // tokenCursor,
  133. // syntax
  134. // }, { depth: null })
  135. // );
  136. // prevent infinite loop
  137. if (++iterationCount === ITERATION_LIMIT) {
  138. console.warn('[csstree-match] BREAK after ' + ITERATION_LIMIT + ' iterations');
  139. exitReason = EXIT_REASON_ITERATION_LIMIT;
  140. break;
  141. }
  142. if (syntax === MATCH) {
  143. if (thenStack === null) {
  144. // turn to MISMATCH when some tokens left unmatched
  145. if (token !== null) {
  146. // doesn't mismatch if just one token left and it's an IE hack
  147. if (tokenCursor !== tokens.length - 1 || (token.value !== '\\0' && token.value !== '\\9')) {
  148. syntax = MISMATCH;
  149. continue;
  150. }
  151. }
  152. // break the main loop, return a result - MATCH
  153. exitReason = EXIT_REASON_MATCH;
  154. break;
  155. }
  156. // go to next syntax (`then` branch)
  157. syntax = thenStack.nextSyntax;
  158. // check match is not empty
  159. if (syntax === DISALLOW_EMPTY) {
  160. if (thenStack.matchStack.token === matchStack.token) {
  161. syntax = MISMATCH;
  162. continue;
  163. } else {
  164. syntax = MATCH;
  165. }
  166. }
  167. // close syntax if needed
  168. while (syntaxStack !== null && thenStack.syntaxStack !== syntaxStack) {
  169. closeSyntax();
  170. }
  171. // pop stack
  172. thenStack = thenStack.prev;
  173. continue;
  174. }
  175. if (syntax === MISMATCH) {
  176. if (elseStack === null) {
  177. // break the main loop, return a result - MISMATCH
  178. exitReason = EXIT_REASON_MISMATCH;
  179. break;
  180. }
  181. // go to next syntax (`else` branch)
  182. syntax = elseStack.nextSyntax;
  183. // restore all the rest stack states
  184. thenStack = elseStack.thenStack;
  185. syntaxStack = elseStack.syntaxStack;
  186. matchStack = elseStack.matchStack;
  187. tokenCursor = elseStack.tokenCursor;
  188. token = elseStack.token;
  189. // pop stack
  190. elseStack = elseStack.prev;
  191. continue;
  192. }
  193. switch (syntax.type) {
  194. case 'MatchGraph':
  195. syntax = syntax.match;
  196. break;
  197. case 'If':
  198. // IMPORTANT: else stack push must go first,
  199. // since it stores the state of thenStack before changes
  200. if (syntax.else !== MISMATCH) {
  201. pushElseStack(syntax.else);
  202. }
  203. if (syntax.then !== MATCH) {
  204. pushThenStack(syntax.then);
  205. }
  206. syntax = syntax.match;
  207. break;
  208. case 'MatchOnce':
  209. syntax = {
  210. type: 'MatchOnceBuffer',
  211. terms: syntax.terms,
  212. all: syntax.all,
  213. matchStack: matchStack,
  214. index: 0,
  215. mask: 0
  216. };
  217. break;
  218. case 'MatchOnceBuffer':
  219. if (syntax.index === syntax.terms.length) {
  220. // if no matches during a cycle
  221. if (syntax.matchStack === matchStack) {
  222. // no matches at all or it's required all terms to be matched
  223. if (syntax.mask === 0 || syntax.all) {
  224. syntax = MISMATCH;
  225. break;
  226. }
  227. // a partial match is ok
  228. syntax = MATCH;
  229. break;
  230. } else {
  231. // start trying to match from the start
  232. syntax.index = 0;
  233. syntax.matchStack = matchStack;
  234. }
  235. }
  236. for (; syntax.index < syntax.terms.length; syntax.index++) {
  237. if ((syntax.mask & (1 << syntax.index)) === 0) {
  238. // IMPORTANT: else stack push must go first,
  239. // since it stores the state of thenStack before changes
  240. pushElseStack(syntax);
  241. pushThenStack({
  242. type: 'AddMatchOnce',
  243. buffer: syntax
  244. });
  245. // match
  246. syntax = syntax.terms[syntax.index++];
  247. break;
  248. }
  249. }
  250. break;
  251. case 'AddMatchOnce':
  252. syntax = syntax.buffer;
  253. var newMask = syntax.mask | (1 << (syntax.index - 1));
  254. // all terms are matched
  255. if (newMask === (1 << syntax.terms.length) - 1) {
  256. syntax = MATCH;
  257. continue;
  258. }
  259. syntax = {
  260. type: 'MatchOnceBuffer',
  261. terms: syntax.terms,
  262. all: syntax.all,
  263. matchStack: syntax.matchStack,
  264. index: syntax.index,
  265. mask: newMask
  266. };
  267. break;
  268. case 'Enum':
  269. var name = token !== null ? token.value.toLowerCase() : '';
  270. // drop \0 and \9 hack from keyword name
  271. if (name.indexOf('\\') !== -1) {
  272. name = name.replace(/\\[09].*$/, '');
  273. }
  274. if (hasOwnProperty.call(syntax.map, name)) {
  275. syntax = syntax.map[name];
  276. } else {
  277. syntax = MISMATCH;
  278. }
  279. break;
  280. case 'Generic':
  281. syntax = syntax.fn(token, addTokenToMatch, getNextToken) ? MATCH : MISMATCH;
  282. break;
  283. case 'Type':
  284. case 'Property':
  285. openSyntax();
  286. var syntaxDict = syntax.type === 'Type' ? 'types' : 'properties';
  287. if (hasOwnProperty.call(syntaxes, syntaxDict) && syntaxes[syntaxDict][syntax.name]) {
  288. syntax = syntaxes[syntaxDict][syntax.name].match;
  289. } else {
  290. syntax = undefined;
  291. }
  292. if (!syntax) {
  293. throw new Error(
  294. 'Bad syntax reference: ' +
  295. (syntaxStack.syntax.type === 'Type'
  296. ? '<' + syntaxStack.syntax.name + '>'
  297. : '<\'' + syntaxStack.syntax.name + '\'>')
  298. );
  299. }
  300. break;
  301. case 'Keyword':
  302. var name = syntax.name;
  303. if (token !== null) {
  304. var keywordName = token.value;
  305. // drop \0 and \9 hack from keyword name
  306. if (keywordName.indexOf('\\') !== -1) {
  307. keywordName = keywordName.replace(/\\[09].*$/, '');
  308. }
  309. if (keywordName.toLowerCase() === name) {
  310. addTokenToMatch();
  311. syntax = MATCH;
  312. break;
  313. }
  314. }
  315. syntax = MISMATCH;
  316. break;
  317. case 'AtKeyword':
  318. case 'Function':
  319. if (token !== null && token.value.toLowerCase() === syntax.name) {
  320. addTokenToMatch();
  321. syntax = MATCH;
  322. break;
  323. }
  324. syntax = MISMATCH;
  325. break;
  326. case 'Token':
  327. if (token !== null && token.value === syntax.value) {
  328. addTokenToMatch();
  329. syntax = MATCH;
  330. break;
  331. }
  332. syntax = MISMATCH;
  333. break;
  334. case 'Comma':
  335. if (token !== null && token.value === ',') {
  336. if (isCommaContextStart(matchStack.token)) {
  337. syntax = MISMATCH;
  338. } else {
  339. addTokenToMatch();
  340. syntax = isCommaContextEnd(token) ? MISMATCH : MATCH;
  341. }
  342. } else {
  343. syntax = isCommaContextStart(matchStack.token) || isCommaContextEnd(token) ? MATCH : MISMATCH;
  344. }
  345. break;
  346. // case 'String':
  347. // TODO: strings with length other than 1 char
  348. default:
  349. throw new Error('Unknown node type: ' + syntax.type);
  350. }
  351. }
  352. totalIterationCount += iterationCount;
  353. if (exitReason === EXIT_REASON_MATCH) {
  354. while (syntaxStack !== null) {
  355. closeSyntax();
  356. }
  357. } else {
  358. matchStack = null;
  359. }
  360. return {
  361. tokens: tokens,
  362. reason: exitReason,
  363. iterations: iterationCount,
  364. match: matchStack,
  365. longestMatch: longestMatch
  366. };
  367. }
  368. function matchAsList(tokens, matchGraph, syntaxes) {
  369. var matchResult = internalMatch(tokens, matchGraph, syntaxes || {});
  370. if (matchResult.match !== null) {
  371. matchResult.match = mapList(matchResult.match, function(item) {
  372. if (item.type === OPEN_SYNTAX || item.type === CLOSE_SYNTAX) {
  373. return { type: item.type, syntax: item.syntax };
  374. }
  375. return {
  376. syntax: item.syntax,
  377. token: item.token && item.token.value,
  378. node: item.token && item.token.node
  379. };
  380. }).slice(1);
  381. }
  382. return matchResult;
  383. }
  384. function matchAsTree(tokens, matchGraph, syntaxes) {
  385. var matchResult = internalMatch(tokens, matchGraph, syntaxes || {});
  386. if (matchResult.match === null) {
  387. return matchResult;
  388. }
  389. var cursor = matchResult.match;
  390. var host = matchResult.match = {
  391. syntax: matchGraph.syntax || null,
  392. match: []
  393. };
  394. var stack = [host];
  395. // revert a list
  396. var prev = null;
  397. var next = null;
  398. while (cursor !== null) {
  399. next = cursor.prev;
  400. cursor.prev = prev;
  401. prev = cursor;
  402. cursor = next;
  403. }
  404. // init the cursor to start with 2nd item since 1st is a stub item
  405. cursor = prev.prev;
  406. // build a tree
  407. while (cursor !== null && cursor.syntax !== null) {
  408. var entry = cursor;
  409. switch (entry.type) {
  410. case OPEN_SYNTAX:
  411. host.match.push(host = {
  412. syntax: entry.syntax,
  413. match: []
  414. });
  415. stack.push(host);
  416. break;
  417. case CLOSE_SYNTAX:
  418. stack.pop();
  419. host = stack[stack.length - 1];
  420. break;
  421. default:
  422. host.match.push({
  423. syntax: entry.syntax || null,
  424. token: entry.token.value,
  425. node: entry.token.node
  426. });
  427. }
  428. cursor = cursor.prev;
  429. }
  430. return matchResult;
  431. }
  432. module.exports = {
  433. matchAsList: matchAsList,
  434. matchAsTree: matchAsTree,
  435. getTotalIterationCount: function() {
  436. return totalIterationCount;
  437. }
  438. };