@witchcraft/expressit 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (307) hide show
  1. package/README.md +86 -0
  2. package/dist/ast/builders/array.d.ts +8 -0
  3. package/dist/ast/builders/array.d.ts.map +1 -0
  4. package/dist/ast/builders/array.js +31 -0
  5. package/dist/ast/builders/condition.d.ts +20 -0
  6. package/dist/ast/builders/condition.d.ts.map +1 -0
  7. package/dist/ast/builders/condition.js +28 -0
  8. package/dist/ast/builders/delim.d.ts +11 -0
  9. package/dist/ast/builders/delim.d.ts.map +1 -0
  10. package/dist/ast/builders/delim.js +20 -0
  11. package/dist/ast/builders/error.d.ts +9 -0
  12. package/dist/ast/builders/error.d.ts.map +1 -0
  13. package/dist/ast/builders/error.js +16 -0
  14. package/dist/ast/builders/expression.d.ts +12 -0
  15. package/dist/ast/builders/expression.d.ts.map +1 -0
  16. package/dist/ast/builders/expression.js +31 -0
  17. package/dist/ast/builders/group.d.ts +20 -0
  18. package/dist/ast/builders/group.d.ts.map +1 -0
  19. package/dist/ast/builders/group.js +47 -0
  20. package/dist/ast/builders/index.d.ts +12 -0
  21. package/dist/ast/builders/index.d.ts.map +1 -0
  22. package/dist/ast/builders/index.js +24 -0
  23. package/dist/ast/builders/isFullPos.d.ts +6 -0
  24. package/dist/ast/builders/isFullPos.d.ts.map +1 -0
  25. package/dist/ast/builders/isFullPos.js +6 -0
  26. package/dist/ast/builders/pos.d.ts +21 -0
  27. package/dist/ast/builders/pos.d.ts.map +1 -0
  28. package/dist/ast/builders/pos.js +28 -0
  29. package/dist/ast/builders/token.d.ts +12 -0
  30. package/dist/ast/builders/token.d.ts.map +1 -0
  31. package/dist/ast/builders/token.js +26 -0
  32. package/dist/ast/builders/type.d.ts +6 -0
  33. package/dist/ast/builders/type.d.ts.map +1 -0
  34. package/dist/ast/builders/type.js +37 -0
  35. package/dist/ast/builders/variable.d.ts +17 -0
  36. package/dist/ast/builders/variable.d.ts.map +1 -0
  37. package/dist/ast/builders/variable.js +62 -0
  38. package/dist/ast/classes/ArrayNode.d.ts +18 -0
  39. package/dist/ast/classes/ArrayNode.d.ts.map +1 -0
  40. package/dist/ast/classes/ArrayNode.js +55 -0
  41. package/dist/ast/classes/Condition.d.ts +13 -0
  42. package/dist/ast/classes/Condition.d.ts.map +1 -0
  43. package/dist/ast/classes/Condition.js +21 -0
  44. package/dist/ast/classes/ConditionNode.d.ts +73 -0
  45. package/dist/ast/classes/ConditionNode.d.ts.map +1 -0
  46. package/dist/ast/classes/ConditionNode.js +101 -0
  47. package/dist/ast/classes/ErrorToken.d.ts +27 -0
  48. package/dist/ast/classes/ErrorToken.d.ts.map +1 -0
  49. package/dist/ast/classes/ErrorToken.js +47 -0
  50. package/dist/ast/classes/Expression.d.ts +13 -0
  51. package/dist/ast/classes/Expression.d.ts.map +1 -0
  52. package/dist/ast/classes/Expression.js +19 -0
  53. package/dist/ast/classes/ExpressionNode.d.ts +21 -0
  54. package/dist/ast/classes/ExpressionNode.d.ts.map +1 -0
  55. package/dist/ast/classes/ExpressionNode.js +57 -0
  56. package/dist/ast/classes/GroupNode.d.ts +64 -0
  57. package/dist/ast/classes/GroupNode.d.ts.map +1 -0
  58. package/dist/ast/classes/GroupNode.js +69 -0
  59. package/dist/ast/classes/Node.d.ts +22 -0
  60. package/dist/ast/classes/Node.d.ts.map +1 -0
  61. package/dist/ast/classes/Node.js +28 -0
  62. package/dist/ast/classes/Token.d.ts +27 -0
  63. package/dist/ast/classes/Token.d.ts.map +1 -0
  64. package/dist/ast/classes/Token.js +28 -0
  65. package/dist/ast/classes/ValidToken.d.ts +26 -0
  66. package/dist/ast/classes/ValidToken.d.ts.map +1 -0
  67. package/dist/ast/classes/ValidToken.js +49 -0
  68. package/dist/ast/classes/VariableNode.d.ts +33 -0
  69. package/dist/ast/classes/VariableNode.d.ts.map +1 -0
  70. package/dist/ast/classes/VariableNode.js +58 -0
  71. package/dist/ast/classes/index.d.ts +12 -0
  72. package/dist/ast/classes/index.d.ts.map +1 -0
  73. package/dist/ast/classes/index.js +24 -0
  74. package/dist/ast/handlers.d.ts +42 -0
  75. package/dist/ast/handlers.d.ts.map +1 -0
  76. package/dist/ast/handlers.js +150 -0
  77. package/dist/ast/index.d.ts +4 -0
  78. package/dist/ast/index.d.ts.map +1 -0
  79. package/dist/ast/index.js +8 -0
  80. package/dist/examples/advancedValueComparer.d.ts +3 -0
  81. package/dist/examples/advancedValueComparer.d.ts.map +1 -0
  82. package/dist/examples/advancedValueComparer.js +28 -0
  83. package/dist/examples/shortcutContextParser.d.ts +22 -0
  84. package/dist/examples/shortcutContextParser.d.ts.map +1 -0
  85. package/dist/examples/shortcutContextParser.js +126 -0
  86. package/dist/global.d.js +1 -0
  87. package/dist/grammar/ParserBase.d.ts +51 -0
  88. package/dist/grammar/ParserBase.d.ts.map +1 -0
  89. package/dist/grammar/ParserBase.js +516 -0
  90. package/dist/grammar/createTokens.d.ts +56 -0
  91. package/dist/grammar/createTokens.d.ts.map +1 -0
  92. package/dist/grammar/createTokens.js +843 -0
  93. package/dist/grammar/index.d.ts +3 -0
  94. package/dist/grammar/index.d.ts.map +1 -0
  95. package/dist/grammar/index.js +6 -0
  96. package/dist/helpers/errors.d.ts +9 -0
  97. package/dist/helpers/errors.d.ts.map +1 -0
  98. package/dist/helpers/errors.js +41 -0
  99. package/dist/helpers/general/applyBoolean.d.ts +3 -0
  100. package/dist/helpers/general/applyBoolean.d.ts.map +1 -0
  101. package/dist/helpers/general/applyBoolean.js +17 -0
  102. package/dist/helpers/general/applyPrefix.d.ts +4 -0
  103. package/dist/helpers/general/applyPrefix.d.ts.map +1 -0
  104. package/dist/helpers/general/applyPrefix.js +9 -0
  105. package/dist/helpers/general/defaultConditionNormalizer.d.ts +3 -0
  106. package/dist/helpers/general/defaultConditionNormalizer.d.ts.map +1 -0
  107. package/dist/helpers/general/defaultConditionNormalizer.js +6 -0
  108. package/dist/helpers/general/defaultKeyParser.d.ts +3 -0
  109. package/dist/helpers/general/defaultKeyParser.d.ts.map +1 -0
  110. package/dist/helpers/general/defaultKeyParser.js +8 -0
  111. package/dist/helpers/general/defaultPrefixApplier.d.ts +3 -0
  112. package/dist/helpers/general/defaultPrefixApplier.d.ts.map +1 -0
  113. package/dist/helpers/general/defaultPrefixApplier.js +6 -0
  114. package/dist/helpers/general/defaultValueComparer.d.ts +3 -0
  115. package/dist/helpers/general/defaultValueComparer.d.ts.map +1 -0
  116. package/dist/helpers/general/defaultValueComparer.js +6 -0
  117. package/dist/helpers/general/index.d.ts +7 -0
  118. package/dist/helpers/general/index.d.ts.map +1 -0
  119. package/dist/helpers/general/index.js +14 -0
  120. package/dist/helpers/index.d.ts +4 -0
  121. package/dist/helpers/index.d.ts.map +1 -0
  122. package/dist/helpers/index.js +8 -0
  123. package/dist/helpers/parser/assignParents.d.ts +4 -0
  124. package/dist/helpers/parser/assignParents.d.ts.map +1 -0
  125. package/dist/helpers/parser/assignParents.js +71 -0
  126. package/dist/helpers/parser/checkParserOpts.d.ts +3 -0
  127. package/dist/helpers/parser/checkParserOpts.d.ts.map +1 -0
  128. package/dist/helpers/parser/checkParserOpts.js +126 -0
  129. package/dist/helpers/parser/extractPosition.d.ts +9 -0
  130. package/dist/helpers/parser/extractPosition.d.ts.map +1 -0
  131. package/dist/helpers/parser/extractPosition.js +9 -0
  132. package/dist/helpers/parser/getUnclosedRightParenCount.d.ts +5 -0
  133. package/dist/helpers/parser/getUnclosedRightParenCount.d.ts.map +1 -0
  134. package/dist/helpers/parser/getUnclosedRightParenCount.js +20 -0
  135. package/dist/helpers/parser/index.d.ts +9 -0
  136. package/dist/helpers/parser/index.d.ts.map +1 -0
  137. package/dist/helpers/parser/index.js +18 -0
  138. package/dist/helpers/parser/parseParserOptions.d.ts +4 -0
  139. package/dist/helpers/parser/parseParserOptions.d.ts.map +1 -0
  140. package/dist/helpers/parser/parseParserOptions.js +45 -0
  141. package/dist/helpers/parser/seal.d.ts +8 -0
  142. package/dist/helpers/parser/seal.d.ts.map +1 -0
  143. package/dist/helpers/parser/seal.js +10 -0
  144. package/dist/helpers/parser/setParent.d.ts +6 -0
  145. package/dist/helpers/parser/setParent.d.ts.map +1 -0
  146. package/dist/helpers/parser/setParent.js +4 -0
  147. package/dist/helpers/parser/unescape.d.ts +3 -0
  148. package/dist/helpers/parser/unescape.d.ts.map +1 -0
  149. package/dist/helpers/parser/unescape.js +6 -0
  150. package/dist/index.d.ts +6 -0
  151. package/dist/index.d.ts.map +1 -0
  152. package/dist/index.js +17 -0
  153. package/dist/methods/autocomplete.d.ts +18 -0
  154. package/dist/methods/autocomplete.d.ts.map +1 -0
  155. package/dist/methods/autocomplete.js +109 -0
  156. package/dist/methods/autoreplace.d.ts +13 -0
  157. package/dist/methods/autoreplace.d.ts.map +1 -0
  158. package/dist/methods/autoreplace.js +36 -0
  159. package/dist/methods/autosuggest.d.ts +28 -0
  160. package/dist/methods/autosuggest.d.ts.map +1 -0
  161. package/dist/methods/autosuggest.js +371 -0
  162. package/dist/methods/evaluate.d.ts +11 -0
  163. package/dist/methods/evaluate.d.ts.map +1 -0
  164. package/dist/methods/evaluate.js +30 -0
  165. package/dist/methods/getBestIndex.d.ts +19 -0
  166. package/dist/methods/getBestIndex.d.ts.map +1 -0
  167. package/dist/methods/getBestIndex.js +53 -0
  168. package/dist/methods/getIndexes.d.ts +17 -0
  169. package/dist/methods/getIndexes.d.ts.map +1 -0
  170. package/dist/methods/getIndexes.js +97 -0
  171. package/dist/methods/index.d.ts +9 -0
  172. package/dist/methods/index.d.ts.map +1 -0
  173. package/dist/methods/index.js +18 -0
  174. package/dist/methods/normalize.d.ts +12 -0
  175. package/dist/methods/normalize.d.ts.map +1 -0
  176. package/dist/methods/normalize.js +99 -0
  177. package/dist/methods/validate.d.ts +11 -0
  178. package/dist/methods/validate.d.ts.map +1 -0
  179. package/dist/methods/validate.js +111 -0
  180. package/dist/package.js +7 -0
  181. package/dist/package.json.js +193 -0
  182. package/dist/parser.d.ts +58 -0
  183. package/dist/parser.d.ts.map +1 -0
  184. package/dist/parser.js +136 -0
  185. package/dist/types/ast.d.ts +70 -0
  186. package/dist/types/ast.d.ts.map +1 -0
  187. package/dist/types/ast.js +29 -0
  188. package/dist/types/autocomplete.d.ts +143 -0
  189. package/dist/types/autocomplete.d.ts.map +1 -0
  190. package/dist/types/autocomplete.js +24 -0
  191. package/dist/types/errors.d.ts +34 -0
  192. package/dist/types/errors.d.ts.map +1 -0
  193. package/dist/types/errors.js +10 -0
  194. package/dist/types/index.d.ts +5 -0
  195. package/dist/types/index.d.ts.map +1 -0
  196. package/dist/types/index.js +9 -0
  197. package/dist/types/parser.d.ts +451 -0
  198. package/dist/types/parser.d.ts.map +1 -0
  199. package/dist/types/parser.js +1 -0
  200. package/dist/utils/extractTokens.d.ts +8 -0
  201. package/dist/utils/extractTokens.d.ts.map +1 -0
  202. package/dist/utils/extractTokens.js +50 -0
  203. package/dist/utils/getCursorInfo.d.ts +7 -0
  204. package/dist/utils/getCursorInfo.d.ts.map +1 -0
  205. package/dist/utils/getCursorInfo.js +86 -0
  206. package/dist/utils/getOppositeDelimiter.d.ts +6 -0
  207. package/dist/utils/getOppositeDelimiter.d.ts.map +1 -0
  208. package/dist/utils/getOppositeDelimiter.js +35 -0
  209. package/dist/utils/getSurroundingErrors.d.ts +25 -0
  210. package/dist/utils/getSurroundingErrors.d.ts.map +1 -0
  211. package/dist/utils/getSurroundingErrors.js +37 -0
  212. package/dist/utils/index.d.ts +10 -0
  213. package/dist/utils/index.d.ts.map +1 -0
  214. package/dist/utils/index.js +20 -0
  215. package/dist/utils/isBracket.d.ts +3 -0
  216. package/dist/utils/isBracket.d.ts.map +1 -0
  217. package/dist/utils/isBracket.js +7 -0
  218. package/dist/utils/isDelimiter.d.ts +6 -0
  219. package/dist/utils/isDelimiter.d.ts.map +1 -0
  220. package/dist/utils/isDelimiter.js +17 -0
  221. package/dist/utils/isParen.d.ts +3 -0
  222. package/dist/utils/isParen.d.ts.map +1 -0
  223. package/dist/utils/isParen.js +7 -0
  224. package/dist/utils/isQuote.d.ts +4 -0
  225. package/dist/utils/isQuote.d.ts.map +1 -0
  226. package/dist/utils/isQuote.js +7 -0
  227. package/dist/utils/prettyAst.d.ts +35 -0
  228. package/dist/utils/prettyAst.d.ts.map +1 -0
  229. package/dist/utils/prettyAst.js +112 -0
  230. package/package.json +152 -0
  231. package/src/ast/builders/array.ts +45 -0
  232. package/src/ast/builders/condition.ts +56 -0
  233. package/src/ast/builders/delim.ts +39 -0
  234. package/src/ast/builders/error.ts +22 -0
  235. package/src/ast/builders/expression.ts +66 -0
  236. package/src/ast/builders/group.ts +79 -0
  237. package/src/ast/builders/index.ts +13 -0
  238. package/src/ast/builders/isFullPos.ts +10 -0
  239. package/src/ast/builders/pos.ts +57 -0
  240. package/src/ast/builders/token.ts +46 -0
  241. package/src/ast/builders/type.ts +32 -0
  242. package/src/ast/builders/variable.ts +89 -0
  243. package/src/ast/classes/ArrayNode.ts +46 -0
  244. package/src/ast/classes/Condition.ts +22 -0
  245. package/src/ast/classes/ConditionNode.ts +141 -0
  246. package/src/ast/classes/ErrorToken.ts +49 -0
  247. package/src/ast/classes/Expression.ts +26 -0
  248. package/src/ast/classes/ExpressionNode.ts +62 -0
  249. package/src/ast/classes/GroupNode.ts +127 -0
  250. package/src/ast/classes/Node.ts +47 -0
  251. package/src/ast/classes/Token.ts +59 -0
  252. package/src/ast/classes/ValidToken.ts +56 -0
  253. package/src/ast/classes/VariableNode.ts +67 -0
  254. package/src/ast/classes/index.ts +13 -0
  255. package/src/ast/handlers.ts +190 -0
  256. package/src/ast/index.ts +5 -0
  257. package/src/examples/advancedValueComparer.ts +31 -0
  258. package/src/examples/shortcutContextParser.ts +140 -0
  259. package/src/global.d.ts +4 -0
  260. package/src/grammar/ParserBase.ts +715 -0
  261. package/src/grammar/createTokens.ts +512 -0
  262. package/src/grammar/index.ts +4 -0
  263. package/src/helpers/errors.ts +45 -0
  264. package/src/helpers/general/applyBoolean.ts +9 -0
  265. package/src/helpers/general/applyPrefix.ts +7 -0
  266. package/src/helpers/general/defaultConditionNormalizer.ts +9 -0
  267. package/src/helpers/general/defaultKeyParser.ts +8 -0
  268. package/src/helpers/general/defaultPrefixApplier.ts +7 -0
  269. package/src/helpers/general/defaultValueComparer.ts +7 -0
  270. package/src/helpers/general/index.ts +8 -0
  271. package/src/helpers/index.ts +5 -0
  272. package/src/helpers/parser/assignParents.ts +51 -0
  273. package/src/helpers/parser/checkParserOpts.ts +143 -0
  274. package/src/helpers/parser/extractPosition.ts +15 -0
  275. package/src/helpers/parser/getUnclosedRightParenCount.ts +22 -0
  276. package/src/helpers/parser/index.ts +10 -0
  277. package/src/helpers/parser/parseParserOptions.ts +54 -0
  278. package/src/helpers/parser/seal.ts +14 -0
  279. package/src/helpers/parser/setParent.ts +5 -0
  280. package/src/helpers/parser/unescape.ts +4 -0
  281. package/src/index.ts +7 -0
  282. package/src/methods/autocomplete.ts +128 -0
  283. package/src/methods/autoreplace.ts +46 -0
  284. package/src/methods/autosuggest.ts +543 -0
  285. package/src/methods/evaluate.ts +37 -0
  286. package/src/methods/getBestIndex.ts +53 -0
  287. package/src/methods/getIndexes.ts +99 -0
  288. package/src/methods/index.ts +10 -0
  289. package/src/methods/normalize.ts +138 -0
  290. package/src/methods/validate.ts +141 -0
  291. package/src/package.js +11 -0
  292. package/src/parser.ts +183 -0
  293. package/src/types/ast.ts +148 -0
  294. package/src/types/autocomplete.ts +152 -0
  295. package/src/types/errors.ts +40 -0
  296. package/src/types/index.ts +6 -0
  297. package/src/types/parser.ts +479 -0
  298. package/src/utils/extractTokens.ts +67 -0
  299. package/src/utils/getCursorInfo.ts +106 -0
  300. package/src/utils/getOppositeDelimiter.ts +36 -0
  301. package/src/utils/getSurroundingErrors.ts +57 -0
  302. package/src/utils/index.ts +11 -0
  303. package/src/utils/isBracket.ts +6 -0
  304. package/src/utils/isDelimiter.ts +18 -0
  305. package/src/utils/isParen.ts +6 -0
  306. package/src/utils/isQuote.ts +6 -0
  307. package/src/utils/prettyAst.ts +152 -0
@@ -0,0 +1,141 @@
1
+ import type { ArrayNode } from "./ArrayNode.js"
2
+ import type { ErrorToken } from "./ErrorToken.js"
3
+ import type { ExpressionNode } from "./ExpressionNode.js"
4
+ import type { GroupNode } from "./GroupNode.js"
5
+ import { Node } from "./Node.js"
6
+ import { ValidToken } from "./ValidToken.js"
7
+ import { VariableNode } from "./VariableNode.js"
8
+
9
+ import { type AnyToken, AST_TYPE, type TOKEN_TYPE } from "../../types/ast.js"
10
+
11
+
12
+ /**
13
+ * A condition is composed of a `variable`, @see Variable , and, if it's negated, a "not" `operator` token, @see ValidToken .
14
+ *
15
+ * The `value` property refers to the boolean value of the condition (not to the string value of the variable). See the `operator` property.
16
+ */
17
+ export class ConditionNode<
18
+ TValid extends boolean = boolean,
19
+ TOperator extends
20
+ ValidToken<TOKEN_TYPE.NOT> | undefined =
21
+ ValidToken<TOKEN_TYPE.NOT> | undefined,
22
+ > extends Node<AST_TYPE.CONDITION> {
23
+ /**
24
+ * Contains a value node which could be a variable, an array node (if enabled), or a group.
25
+ *
26
+ * Might be an error in cases like:
27
+ * - just passing a negation operator
28
+ * - if condition property operators are used and you have an input like just `[SEP]`, `op[SEP]`, `prop[SEP]op[SEP]`, `[CUSTOM OP]`, or `prop[CUSTOM OP]` where the variable is missing.
29
+ */
30
+ readonly value:
31
+ | VariableNode<TValid>
32
+ | ArrayNode<TValid>
33
+ | GroupNode<TValid>
34
+ | (TValid extends false ? ErrorToken<TOKEN_TYPE.VALUE> : never)
35
+
36
+ /**
37
+ * If the condition was negated, contains the "not" token, @see ValidToken , the condition was negated with.
38
+ */
39
+ readonly operator?: ValidToken<TOKEN_TYPE.NOT>
40
+
41
+ /**
42
+ * If condition property operators are used, this will contain the property (as a variable), or an error token if it was missing (but some separator or operator was passed).
43
+ *
44
+ * While the property is a variable and can be a quoted variable, it cannot be a prefixed variable string.
45
+ *
46
+ * See the corresponding @see ParserOptions for more details.
47
+ */
48
+ readonly property?: VariableNode | (TValid extends false ? ErrorToken<TOKEN_TYPE.VALUE> : never)
49
+
50
+ /**
51
+ * If condition property operators are used, this will contain the operator.
52
+ *
53
+ * If a "short" form was used, this will contain an `OP_CUSTOM` type token, and the condition's `sep` will always be undefined.
54
+ *
55
+ * If a "long/expanded" form was used, this will contain a `VALUE` type token, and at least one of condition's `sep` tokens will be defined.
56
+ *
57
+ * See the corresponding @see ParserOptions for more details.
58
+ */
59
+ readonly propertyOperator?: AnyToken<TOKEN_TYPE.OP_CUSTOM | TOKEN_TYPE.VALUE>
60
+
61
+ /**
62
+ * If "long/expanded" form condition property operators are used, this will contain the separators, otherwise it is undefined.
63
+ *
64
+ * If it's defined, either both side will be valid tokens, or only the left, while the right might be undefined or an error token.
65
+ *
66
+ * This is because given a string like `[SEP]val` which would produce an error like `[MISSING PROPERTY ERROR][SEP]var`, the separator is always interpreted as being the left one. And even if we have a situation like `op[SEP]var`, it is always interpreted by the parser as `prop[SEP]var`.
67
+ *
68
+ * Why might the right be undefined instead of an error token? This is because we don't need a separator between the operator and a group, `prop[SEP]op(group)`, but we do between a variable in cases like `prop[SEP]op"var"` which would produce an error token on the right side (we could parse this but it just looks inconsistent).
69
+ *
70
+ * See the corresponding @see ParserOptions for more details.
71
+ */
72
+ readonly sep?: {
73
+ left?: AnyToken<TOKEN_TYPE.OP_EXPANDED_SEP>
74
+ right?: AnyToken<TOKEN_TYPE.OP_EXPANDED_SEP>
75
+ }
76
+
77
+ #parent: any
78
+
79
+ #setParent: boolean = false
80
+
81
+ get parent(): GroupNode |
82
+ ExpressionNode |
83
+ undefined {
84
+ return this.#parent
85
+ }
86
+
87
+ set parent(value: GroupNode |
88
+ ExpressionNode |
89
+ undefined
90
+ ) {
91
+ if (this.#setParent) {throw new Error("parent property is readonly")}
92
+ this.#parent = value
93
+ this.#setParent = true
94
+ }
95
+
96
+ constructor({ property, propertyOperator, sep, value, start, end, operator }: {
97
+ operator?: TOperator
98
+ property?: ConditionNode<TValid>["property"]
99
+ propertyOperator?: ConditionNode<TValid>["propertyOperator"]
100
+ sep?: ConditionNode<TValid>["sep"]
101
+ value: ConditionNode<TValid>["value"]
102
+ start: number
103
+ end: number
104
+ }) {
105
+ super(AST_TYPE.CONDITION, start, end)
106
+ this.value = value
107
+ this.operator = operator
108
+ this.property = property
109
+ this.propertyOperator = propertyOperator
110
+ this.sep = sep
111
+ this.operator = operator
112
+ // @ts-expect-error ignore readonly
113
+ this.valid = ((
114
+ this.operator === undefined ||
115
+ this.operator instanceof ValidToken
116
+ ) &&
117
+ this.value instanceof Node &&
118
+ this.value.valid
119
+ ) && (
120
+ (
121
+ this.property === undefined &&
122
+ this.propertyOperator === undefined &&
123
+ this.sep === undefined
124
+ ) || (
125
+ this.property instanceof VariableNode &&
126
+ (
127
+ this.propertyOperator instanceof ValidToken ||
128
+ (
129
+ this.sep?.left instanceof ValidToken &&
130
+ (
131
+ this.sep.right === undefined ||
132
+ this.sep?.right instanceof ValidToken
133
+ )
134
+ )
135
+ )
136
+ )
137
+
138
+ ) as TValid
139
+ }
140
+ }
141
+
@@ -0,0 +1,49 @@
1
+ import type { ConditionNode } from "./ConditionNode.js"
2
+ import type { ExpressionNode } from "./ExpressionNode.js"
3
+ import type { GroupNode } from "./GroupNode.js"
4
+ import { Token } from "./Token.js"
5
+ import type { VariableNode } from "./VariableNode.js"
6
+
7
+ import type { TOKEN_TYPE } from "../../types/ast.js"
8
+
9
+ /**
10
+ * The class for invalid recovery tokens.
11
+ *
12
+ * Unlike valid tokens, error tokens:
13
+ *
14
+ * - Have no value.
15
+ * - Contain an extra property, `expected` with an array of tokens *that would have fixed the issue* (NOT every possible token that could be there).
16
+ * - The start end positions will always be equal. An invalid token has no length.
17
+ */
18
+ export class ErrorToken<
19
+ TExpected extends TOKEN_TYPE = TOKEN_TYPE,
20
+ > extends Token<false, never, never, TExpected[]> {
21
+ readonly expected: TExpected[]
22
+
23
+ #parent: any
24
+
25
+ get parent(): VariableNode |
26
+ GroupNode |
27
+ ExpressionNode |
28
+ ConditionNode {
29
+ return this.#parent
30
+ }
31
+
32
+ set parent(value: VariableNode |
33
+ GroupNode |
34
+ ExpressionNode |
35
+ ConditionNode
36
+ ) {
37
+ if (this.#parent) {throw new Error("parent property is readonly")}
38
+ this.#parent = value
39
+ }
40
+
41
+ constructor({ expected, start, end }: {
42
+ expected: TExpected[]
43
+ start: number
44
+ end: number
45
+ }) {
46
+ super(start, end)
47
+ this.expected = expected
48
+ }
49
+ }
@@ -0,0 +1,26 @@
1
+ import type { Condition } from "./Condition.js"
2
+
3
+ import type { TokenBooleanTypes } from "../../types/ast.js"
4
+
5
+
6
+ export class Expression<TType extends string = string, TValue = any> {
7
+ readonly left:
8
+ | Expression<TType, TValue>
9
+ | Condition<TType, TValue>
10
+
11
+ readonly right:
12
+ | Expression<TType, TValue>
13
+ | Condition<TType, TValue>
14
+
15
+ readonly operator: TokenBooleanTypes
16
+
17
+ constructor({ operator, left, right }: {
18
+ operator: Expression<TType, TValue>["operator"]
19
+ right: Expression<TType, TValue>["right"]
20
+ left: Expression<TType, TValue>["left"]
21
+ }) {
22
+ this.operator = operator
23
+ this.right = right
24
+ this.left = left
25
+ }
26
+ }
@@ -0,0 +1,62 @@
1
+ import type { ConditionNode } from "./ConditionNode.js"
2
+ import type { ErrorToken } from "./ErrorToken.js"
3
+ import type { GroupNode } from "./GroupNode.js"
4
+ import { Node } from "./Node.js"
5
+ import { ValidToken } from "./ValidToken.js"
6
+
7
+ import { type AnyToken, AST_TYPE, type TOKEN_TYPE, type TokenBooleanTypes } from "../../types/ast.js"
8
+
9
+
10
+ export class ExpressionNode<
11
+ TValid extends boolean = boolean,
12
+ >
13
+ extends Node<AST_TYPE.EXPRESSION> {
14
+ readonly operator: AnyToken<TokenBooleanTypes>
15
+
16
+ readonly left:
17
+ | ExpressionNode<TValid>
18
+ | ConditionNode<TValid>
19
+ | GroupNode<TValid>
20
+ | (TValid extends false ? ErrorToken<TOKEN_TYPE.VALUE> : never)
21
+
22
+ readonly right:
23
+ | ExpressionNode<TValid>
24
+ | ConditionNode<TValid>
25
+ | GroupNode<TValid>
26
+ | (TValid extends false ? ErrorToken<TOKEN_TYPE.VALUE> : never)
27
+
28
+ #parent: any
29
+
30
+ #setParent: boolean = false
31
+
32
+ get parent(): GroupNode | undefined {
33
+ return this.#parent
34
+ }
35
+
36
+ set parent(value: GroupNode | undefined) {
37
+ if (this.#setParent) {throw new Error("parent property is readonly")}
38
+ this.#parent = value
39
+ this.#setParent = true
40
+ }
41
+
42
+ constructor({ operator, left, right, start, end }: {
43
+ operator: ExpressionNode<TValid>["operator"]
44
+ right: ExpressionNode<TValid>["right"]
45
+ left: ExpressionNode<TValid>["left"]
46
+ start: number
47
+ end: number
48
+ }) {
49
+ super(AST_TYPE.EXPRESSION, start, end)
50
+ this.operator = operator
51
+ this.right = right
52
+ this.left = left
53
+ // @ts-expect-error ignore readonly
54
+ this.valid = (
55
+ this.operator instanceof ValidToken &&
56
+ this.left instanceof Node &&
57
+ this.left.valid &&
58
+ this.right instanceof Node &&
59
+ this.right.valid
60
+ ) as TValid
61
+ }
62
+ }
@@ -0,0 +1,127 @@
1
+ import type { ConditionNode } from "./ConditionNode.js"
2
+ import type { ErrorToken } from "./ErrorToken.js"
3
+ import type { ExpressionNode } from "./ExpressionNode.js"
4
+ import { Node } from "./Node.js"
5
+ import { ValidToken } from "./ValidToken.js"
6
+
7
+ import { AST_TYPE, type NodeDelimiters, type TOKEN_TYPE } from "../../types/ast.js"
8
+
9
+
10
+ /**
11
+ * A node that symbolizes a parenthesized expression (might be negated), and if `prefixableGroups` is enabled, a prefixed expression @see ParserOptions .
12
+ *
13
+ * ```txt
14
+ * (a || b)
15
+ * |----| ExpressionNode
16
+ * |------| GroupNode
17
+ *
18
+ * !(a || b)
19
+ * ^ prefix: "not" Token
20
+ * |----| ExpressionNode
21
+ * |-------| GroupNode
22
+ * ```
23
+ *
24
+ * If `prefixableGroups` is enabled:
25
+ * ```txt
26
+ * prefix(a || b)
27
+ * |----| prefix: ConditionNode
28
+ * |----| ExpressionNode
29
+ * |------------| GroupNode
30
+ *
31
+ * !prefix(a || b)
32
+ * |-----| prefix: ConditionNode
33
+ * |----| ExpressionNode
34
+ * |-------------| GroupNode
35
+ *
36
+ * !(a || b)
37
+ * ^ prefix: STILL a "not" Token
38
+ * ```
39
+ */
40
+
41
+ export class GroupNode<
42
+ TValid extends boolean = boolean,
43
+ TPrefixable extends boolean = true,
44
+ TPrefix extends
45
+ TPrefixable extends true
46
+ ? ConditionNode<TValid> |
47
+ ValidToken<TOKEN_TYPE.NOT> |
48
+ undefined
49
+ : ValidToken<TOKEN_TYPE.NOT> |
50
+ undefined =
51
+ TPrefixable extends true
52
+ ? ConditionNode<TValid> |
53
+ ValidToken<TOKEN_TYPE.NOT> |
54
+ undefined
55
+ : ValidToken<TOKEN_TYPE.NOT>,
56
+ >
57
+ extends Node<AST_TYPE.GROUP> {
58
+ /**
59
+ * If the condition is negated this will contain a "not" **token**, @see ValidToken .
60
+ *
61
+ * If `prefixableGroups` is enabled and the group is prefixed by a **condition**, this will be an @see ConditionNode.
62
+ *
63
+ * **Careful:** `!(a)` here this would not be a **condition** since it's just a regular negation.
64
+ *
65
+ * See examples at @see GroupNode .
66
+ */
67
+ readonly prefix: TPrefix
68
+
69
+ expression:
70
+ | ConditionNode<TValid>
71
+ | GroupNode<TValid>
72
+ | ExpressionNode<TValid>
73
+ | (TValid extends false ? ErrorToken<TOKEN_TYPE.VALUE> : never)
74
+
75
+ /**
76
+ * The parenthesis tokens, @see ValidToken . These will always be defined (although not necessarily with valid tokens).
77
+ */
78
+ readonly paren: NodeDelimiters<TOKEN_TYPE.PARENL, TOKEN_TYPE.PARENR>
79
+
80
+ #parent: any
81
+
82
+ #setParent: boolean = false
83
+
84
+ get parent(): ExpressionNode | undefined {
85
+ return this.#parent
86
+ }
87
+
88
+ set parent(value: ExpressionNode | undefined) {
89
+ if (this.#setParent) {throw new Error("parent property is readonly")}
90
+ this.#parent = value
91
+ this.#setParent = true
92
+ }
93
+
94
+ constructor({ prefix, expression, paren, start, end }: {
95
+ prefix: TPrefix
96
+ expression: GroupNode<TValid>["expression"]
97
+ paren: GroupNode<TValid>["paren"]
98
+ start: number
99
+ end: number
100
+ }) {
101
+ super(AST_TYPE.GROUP, start, end)
102
+ this.prefix = prefix
103
+ this.expression = expression
104
+ this.paren = paren
105
+ // @ts-expect-error ignore readonly
106
+ this.valid = (
107
+ (
108
+ this.prefix === undefined ||
109
+ this.prefix instanceof ValidToken ||
110
+ (
111
+ this.prefix instanceof Node &&
112
+ this.prefix.valid
113
+ )
114
+ ) &&
115
+ this.expression instanceof Node &&
116
+ this.expression.valid &&
117
+ (
118
+ this.paren === undefined ||
119
+ (
120
+ this.paren.left instanceof ValidToken &&
121
+ this.paren.right instanceof ValidToken
122
+ )
123
+ )
124
+ ) as TValid
125
+ }
126
+ }
127
+
@@ -0,0 +1,47 @@
1
+ import type { ArrayNode } from "./ArrayNode.js"
2
+ import type { ConditionNode } from "./ConditionNode.js"
3
+ import type { ExpressionNode } from "./ExpressionNode.js"
4
+ import type { GroupNode } from "./GroupNode.js"
5
+
6
+ import { BooleanParserLibraryError } from "../../helpers/errors.js"
7
+ import type { AST_TYPE } from "../../types/ast.js"
8
+ import { ERROR_CODES } from "../../types/errors.js"
9
+
10
+ /**
11
+ * The base AST node class all node types extend from. Can be used to check if an object is an ast node.
12
+ * ```ts
13
+ * if (group.prefix instanceof Node) {
14
+ * //...
15
+ * }
16
+ * ```
17
+ */
18
+ export class Node<
19
+ TType extends AST_TYPE = AST_TYPE,
20
+ TValid extends boolean = boolean,
21
+ > {
22
+ readonly type: TType
23
+
24
+ readonly start: number
25
+
26
+ readonly end: number
27
+
28
+ readonly valid!: TValid
29
+
30
+ get parent():
31
+ ConditionNode |
32
+ GroupNode |
33
+ ExpressionNode |
34
+ ArrayNode |
35
+ undefined {
36
+ return undefined as any
37
+ }
38
+
39
+ constructor(type: TType, start: number, end: number) {
40
+ this.type = type
41
+ if (start === undefined || end === undefined) {
42
+ throw new BooleanParserLibraryError(ERROR_CODES.PARSER_POSITION_ERROR, { start, end })
43
+ }
44
+ this.start = start
45
+ this.end = end
46
+ }
47
+ }
@@ -0,0 +1,59 @@
1
+ import type { ArrayNode } from "./ArrayNode.js"
2
+ import type { ConditionNode } from "./ConditionNode.js"
3
+ import type { ExpressionNode } from "./ExpressionNode.js"
4
+ import type { GroupNode } from "./GroupNode.js"
5
+ import type { VariableNode } from "./VariableNode.js"
6
+
7
+ import { BooleanParserLibraryError } from "../../helpers/errors.js"
8
+ import type { TOKEN_TYPE } from "../../types/ast.js"
9
+ import { ERROR_CODES } from "../../types/errors.js"
10
+
11
+ /**
12
+ * The base class from which @see ValidToken and @see ErrorToken extend.
13
+ *
14
+ * Mostly for internal use, and I would suggest using @see AnyToken instead of this for typing things, but can be used to check whether an object is a token:
15
+ * ```ts
16
+ * if (group.prefix instanceof Token) {
17
+ * //...
18
+ * }
19
+ * ```
20
+ * Only really takes care of setting the start/end position.
21
+ */
22
+ export class Token<
23
+ TValid extends boolean = boolean,
24
+ TType extends
25
+ TValid extends true ? TOKEN_TYPE : never =
26
+ TValid extends true ? TOKEN_TYPE : never,
27
+ TValue extends
28
+ TValid extends true ? string : never =
29
+ TValid extends true ? string : never,
30
+ TExpected extends
31
+ TValid extends false ? TOKEN_TYPE[] : never =
32
+ TValid extends false ? TOKEN_TYPE[] : never,
33
+ > {
34
+ readonly type!: TType
35
+
36
+ readonly value!: TValue
37
+
38
+ readonly expected!: TExpected
39
+
40
+ readonly start!: number
41
+
42
+ readonly end!: number
43
+
44
+ get parent(): VariableNode |
45
+ GroupNode |
46
+ ExpressionNode |
47
+ ArrayNode |
48
+ ConditionNode {
49
+ return undefined as any
50
+ }
51
+
52
+ constructor(start: number, end: number) {
53
+ if (start === undefined || end === undefined) {
54
+ throw new BooleanParserLibraryError(ERROR_CODES.PARSER_POSITION_ERROR, { start, end })
55
+ }
56
+ this.start = start
57
+ this.end = end
58
+ }
59
+ }
@@ -0,0 +1,56 @@
1
+ import type { ArrayNode } from "./ArrayNode.js"
2
+ import type { ConditionNode } from "./ConditionNode.js"
3
+ import type { ExpressionNode } from "./ExpressionNode.js"
4
+ import type { GroupNode } from "./GroupNode.js"
5
+ import { Token } from "./Token.js"
6
+ import type { VariableNode } from "./VariableNode.js"
7
+
8
+ import type { TOKEN_TYPE } from "../../types/ast.js"
9
+
10
+
11
+ /**
12
+ * The class for all *valid* tokens.
13
+ *
14
+ * Valid tokens always have a value, even if it might be an empty string.
15
+ */
16
+ export class ValidToken<
17
+ TType extends TOKEN_TYPE = TOKEN_TYPE,
18
+ > extends Token<true, TType, string, never> {
19
+ readonly type: TType
20
+
21
+ readonly value: string
22
+
23
+ #parent: any
24
+
25
+ get parent():
26
+ | VariableNode
27
+ | GroupNode
28
+ | ExpressionNode
29
+ | ConditionNode
30
+ | ArrayNode {
31
+ return this.#parent
32
+ }
33
+
34
+ set parent(value:
35
+ | VariableNode
36
+ | GroupNode
37
+ | ExpressionNode
38
+ | ConditionNode
39
+ | ArrayNode
40
+ ) {
41
+ if (this.#parent) {throw new Error("parent property is readonly")}
42
+ this.#parent = value
43
+ }
44
+
45
+ constructor({ type, value, start, end }: {
46
+ type: TType
47
+ value: string
48
+ start: number
49
+ end: number
50
+ }
51
+ ) {
52
+ super(start, end)
53
+ this.type = type
54
+ this.value = value
55
+ }
56
+ }
@@ -0,0 +1,67 @@
1
+ import type { ArrayNode } from "./ArrayNode.js"
2
+ import type { ConditionNode } from "./ConditionNode.js"
3
+ import type { ErrorToken } from "./ErrorToken.js"
4
+ import { Node } from "./Node.js"
5
+ import { ValidToken } from "./ValidToken.js"
6
+
7
+ import { type AnyToken, AST_TYPE, type NodeDelimiters, type TOKEN_TYPE, type TokenQuoteTypes } from "../../types/ast.js"
8
+
9
+ /**
10
+ * A variable represents **just** a string value (NOT it's boolean value).
11
+ *
12
+ * The parser will never return just a variable, they are always wrapped in an @see ConditionNode to give them a boolean value.
13
+ *
14
+ * A variable might or might not be quoted. If it is, the `quote` property will contain the quote tokens, @see ValidToken . While they will always be of the same quote type, one might be valid while the other might not (@see ErrorToken).
15
+ *
16
+ * `value` will contain the string value of the variable. The text is not processed and might still contain escaped characters (even if unquoted!) that need to be removed to get the intended value. This is so that the position of the node is accurate. @see evaluate will take care of unescaping when needed.
17
+ *
18
+ * If `prefixableStrings` is true, the `prefix` property might contain a value token.
19
+ */
20
+ export class VariableNode<
21
+ TValid extends boolean = boolean,
22
+ > extends Node<AST_TYPE.VARIABLE, TValid> {
23
+ readonly value: TValid extends boolean
24
+ ? AnyToken<TOKEN_TYPE.VALUE>
25
+ : TValid extends true
26
+ ? ValidToken<TOKEN_TYPE.VALUE>
27
+ : ErrorToken<TOKEN_TYPE.VALUE>
28
+
29
+ readonly prefix?: ValidToken<TOKEN_TYPE.VALUE> // todo
30
+
31
+ readonly quote?: NodeDelimiters<TokenQuoteTypes, TokenQuoteTypes>
32
+
33
+ #parent: any
34
+
35
+ #setParent: boolean = false
36
+
37
+ get parent(): ConditionNode | ArrayNode | undefined {
38
+ return this.#parent
39
+ }
40
+
41
+ set parent(value: ConditionNode | ArrayNode | undefined) {
42
+ if (this.#setParent) {throw new Error("parent property is readonly")}
43
+ this.#parent = value
44
+ this.#setParent = true
45
+ }
46
+
47
+ constructor({ prefix, value, quote, start, end }: {
48
+ prefix?: VariableNode<TValid>["prefix"]
49
+ value: VariableNode<TValid>["value"]
50
+ quote?: VariableNode<TValid>["quote"]
51
+ start: number
52
+ end: number
53
+ }) {
54
+ super(AST_TYPE.VARIABLE, start, end)
55
+ this.prefix = prefix
56
+ this.value = value
57
+ this.quote = quote
58
+ // @ts-expect-error ignore readonly
59
+ this.valid = (this.value instanceof ValidToken && (
60
+ this.quote === undefined ||
61
+ (
62
+ this.quote.left instanceof ValidToken &&
63
+ this.quote.right instanceof ValidToken
64
+ )
65
+ )) as TValid
66
+ }
67
+ }
@@ -0,0 +1,13 @@
1
+ /* Autogenerated Index */
2
+
3
+ export { ArrayNode } from "./ArrayNode.js"
4
+ export { Condition } from "./Condition.js"
5
+ export { ConditionNode } from "./ConditionNode.js"
6
+ export { ErrorToken } from "./ErrorToken.js"
7
+ export { Expression } from "./Expression.js"
8
+ export { ExpressionNode } from "./ExpressionNode.js"
9
+ export { GroupNode } from "./GroupNode.js"
10
+ export { Node } from "./Node.js"
11
+ export { Token } from "./Token.js"
12
+ export { ValidToken } from "./ValidToken.js"
13
+ export { VariableNode } from "./VariableNode.js"