parse.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. var SPACE = /\s/
  2. function flatten(array) {
  3. if (!Array.isArray(array)) return [array]
  4. // Iterative flatten: `reduce`+`concat` copies the accumulator on every step,
  5. // which is O(n²) once a query resolves to many nodes.
  6. var result = []
  7. var stack = [array]
  8. while (stack.length) {
  9. var item = stack.pop()
  10. if (Array.isArray(item)) {
  11. for (var i = item.length - 1; i >= 0; i--) {
  12. stack.push(item[i])
  13. }
  14. } else {
  15. result.push(item)
  16. }
  17. }
  18. return result
  19. }
  20. function matchQuery(all, query) {
  21. var node = { query: query }
  22. if (query.indexOf('not ') === 0) {
  23. node.not = true
  24. query = query.slice(4)
  25. }
  26. for (var name in all) {
  27. var type = all[name]
  28. var match = query.match(type.regexp)
  29. if (match) {
  30. node.type = name
  31. for (var i = 0; i < type.matches.length; i++) {
  32. node[type.matches[i]] = match[i + 1]
  33. }
  34. return node
  35. }
  36. }
  37. node.type = 'unknown'
  38. return node
  39. }
  40. function pushClause(all, qs, text, compose) {
  41. var node = matchQuery(all, text.trim())
  42. node.compose = compose
  43. qs.push(node)
  44. }
  45. // Splits a query block into clauses on the `\s+and\s+`, `\s+or\s+` and `,\s*`
  46. // delimiters in a single left-to-right pass. The previous implementation grew
  47. // a suffix one character at a time and re-tested anchored `\s+…` regexps at
  48. // every length, which backtracks across whitespace runs and is O(n²) — a
  49. // small padded query could freeze the event loop for tens of seconds.
  50. function parseBlock(all, block, qs) {
  51. if (block.length === 0) return
  52. var len = block.length
  53. var clauseStart = 0
  54. // Compose for the clause currently being read; the leftmost clause is `or`.
  55. var compose = 'or'
  56. var i = 0
  57. while (i < len) {
  58. var ch = block[i]
  59. if (ch === ',') {
  60. // `,\s*` delimiter. A delimiter at the very start (i === 0) has no left
  61. // clause — the original never emits one there.
  62. if (i !== 0) pushClause(all, qs, block.slice(clauseStart, i), compose)
  63. i++
  64. while (i < len && SPACE.test(block[i])) i++
  65. compose = 'or'
  66. clauseStart = i
  67. continue
  68. }
  69. if (SPACE.test(ch)) {
  70. // Possible `\s+and\s+` or `\s+or\s+`. Scan the whitespace run once
  71. // (linear, no backtracking), then check the following keyword.
  72. var q = i
  73. while (q < len && SPACE.test(block[q])) q++
  74. if (
  75. q + 3 < len &&
  76. (block[q] === 'a' || block[q] === 'A') &&
  77. (block[q + 1] === 'n' || block[q + 1] === 'N') &&
  78. (block[q + 2] === 'd' || block[q + 2] === 'D') &&
  79. SPACE.test(block[q + 3])
  80. ) {
  81. // The leading `\s+` of `\s+and\s+` absorbs whitespace at the block
  82. // start, so a delimiter at i === 0 has no left clause.
  83. if (i !== 0) pushClause(all, qs, block.slice(clauseStart, i), compose)
  84. var afterAnd = q + 3
  85. while (afterAnd < len && SPACE.test(block[afterAnd])) afterAnd++
  86. compose = 'and'
  87. i = afterAnd
  88. clauseStart = afterAnd
  89. continue
  90. } else if (
  91. q + 2 < len &&
  92. (block[q] === 'o' || block[q] === 'O') &&
  93. (block[q + 1] === 'r' || block[q + 1] === 'R') &&
  94. SPACE.test(block[q + 2])
  95. ) {
  96. if (i !== 0) pushClause(all, qs, block.slice(clauseStart, i), compose)
  97. var afterOr = q + 2
  98. while (afterOr < len && SPACE.test(block[afterOr])) afterOr++
  99. compose = 'or'
  100. i = afterOr
  101. clauseStart = afterOr
  102. continue
  103. } else {
  104. // Whitespace inside a clause; skip the run and keep reading.
  105. i = q
  106. continue
  107. }
  108. }
  109. i++
  110. }
  111. pushClause(all, qs, block.slice(clauseStart), compose)
  112. }
  113. module.exports = function parse(all, queries) {
  114. if (!Array.isArray(queries)) queries = [queries]
  115. return flatten(
  116. queries.map(function (block) {
  117. var qs = []
  118. parseBlock(all, block, qs)
  119. return qs
  120. })
  121. )
  122. }