@witchcraft/expressit 0.1.3 → 0.2.1

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 (288) hide show
  1. package/README.md +7 -5
  2. package/dist/Lexer.js +21 -42
  3. package/dist/Parser.d.ts +14 -29
  4. package/dist/Parser.d.ts.map +1 -1
  5. package/dist/Parser.js +151 -197
  6. package/dist/ast/builders/array.d.ts +1 -3
  7. package/dist/ast/builders/array.d.ts.map +1 -1
  8. package/dist/ast/builders/array.js +3 -5
  9. package/dist/ast/builders/condition.d.ts +5 -11
  10. package/dist/ast/builders/condition.d.ts.map +1 -1
  11. package/dist/ast/builders/condition.js +4 -7
  12. package/dist/ast/builders/delim.d.ts +1 -1
  13. package/dist/ast/builders/delim.js +2 -4
  14. package/dist/ast/builders/error.d.ts +3 -4
  15. package/dist/ast/builders/error.d.ts.map +1 -1
  16. package/dist/ast/builders/error.js +6 -2
  17. package/dist/ast/builders/expression.d.ts +3 -7
  18. package/dist/ast/builders/expression.d.ts.map +1 -1
  19. package/dist/ast/builders/expression.js +4 -7
  20. package/dist/ast/builders/group.d.ts +3 -8
  21. package/dist/ast/builders/group.d.ts.map +1 -1
  22. package/dist/ast/builders/group.js +3 -3
  23. package/dist/ast/builders/pos.d.ts +1 -2
  24. package/dist/ast/builders/pos.d.ts.map +1 -1
  25. package/dist/ast/builders/pos.js +3 -6
  26. package/dist/ast/builders/token.d.ts +3 -5
  27. package/dist/ast/builders/token.d.ts.map +1 -1
  28. package/dist/ast/builders/token.js +3 -4
  29. package/dist/ast/builders/type.d.ts +1 -1
  30. package/dist/ast/builders/variable.d.ts +3 -5
  31. package/dist/ast/builders/variable.d.ts.map +1 -1
  32. package/dist/ast/builders/variable.js +4 -7
  33. package/dist/ast/createArrayNode.d.ts +3 -0
  34. package/dist/ast/createArrayNode.d.ts.map +1 -0
  35. package/dist/ast/createArrayNode.js +12 -0
  36. package/dist/ast/createConditionNode.d.ts +11 -0
  37. package/dist/ast/createConditionNode.d.ts.map +1 -0
  38. package/dist/ast/createConditionNode.js +18 -0
  39. package/dist/ast/createExpressionNode.d.ts +3 -0
  40. package/dist/ast/createExpressionNode.d.ts.map +1 -0
  41. package/dist/ast/createExpressionNode.js +14 -0
  42. package/dist/ast/createGroupNode.d.ts +5 -0
  43. package/dist/ast/createGroupNode.d.ts.map +1 -0
  44. package/dist/ast/createGroupNode.js +14 -0
  45. package/dist/ast/createNormalizedCondition.d.ts +3 -0
  46. package/dist/ast/createNormalizedCondition.d.ts.map +1 -0
  47. package/dist/ast/createNormalizedCondition.js +10 -0
  48. package/dist/ast/createNormalizedExpression.d.ts +3 -0
  49. package/dist/ast/createNormalizedExpression.d.ts.map +1 -0
  50. package/dist/ast/createNormalizedExpression.js +10 -0
  51. package/dist/ast/createToken.d.ts +6 -0
  52. package/dist/ast/createToken.d.ts.map +1 -0
  53. package/dist/ast/createToken.js +15 -0
  54. package/dist/ast/createVariableNode.d.ts +3 -0
  55. package/dist/ast/createVariableNode.d.ts.map +1 -0
  56. package/dist/ast/createVariableNode.js +12 -0
  57. package/dist/ast/error.d.ts +3 -0
  58. package/dist/ast/error.d.ts.map +1 -0
  59. package/dist/ast/error.js +8 -0
  60. package/dist/ast/handlers.d.ts +5 -12
  61. package/dist/ast/handlers.d.ts.map +1 -1
  62. package/dist/ast/handlers.js +25 -37
  63. package/dist/ast/index.d.ts +9 -1
  64. package/dist/ast/index.d.ts.map +1 -1
  65. package/dist/ast/index.js +18 -2
  66. package/dist/defaults/defaultConditionNormalizer.d.ts +3 -0
  67. package/dist/defaults/defaultConditionNormalizer.d.ts.map +1 -0
  68. package/dist/defaults/defaultKeyParser.d.ts +3 -0
  69. package/dist/defaults/defaultKeyParser.d.ts.map +1 -0
  70. package/dist/{helpers/general → defaults}/defaultKeyParser.js +1 -2
  71. package/dist/defaults/defaultPrefixApplier.d.ts +3 -0
  72. package/dist/defaults/defaultPrefixApplier.d.ts.map +1 -0
  73. package/dist/defaults/defaultValueComparer.d.ts +3 -0
  74. package/dist/defaults/defaultValueComparer.d.ts.map +1 -0
  75. package/dist/{helpers/general → defaults}/index.d.ts +0 -2
  76. package/dist/defaults/index.d.ts.map +1 -0
  77. package/dist/{helpers/general → defaults}/index.js +0 -4
  78. package/dist/examples/shortcutContextParser.js +4 -10
  79. package/dist/index.d.ts +2 -1
  80. package/dist/index.d.ts.map +1 -1
  81. package/dist/index.js +3 -1
  82. package/dist/{helpers/errors.d.ts → internal/ExpressitError.d.ts} +3 -2
  83. package/dist/internal/ExpressitError.d.ts.map +1 -0
  84. package/dist/{helpers/errors.js → internal/ExpressitError.js} +3 -6
  85. package/dist/internal/applyBoolean.d.ts.map +1 -0
  86. package/dist/{helpers/general → internal}/applyPrefix.d.ts +1 -1
  87. package/dist/internal/applyPrefix.d.ts.map +1 -0
  88. package/dist/{helpers/parser → internal}/checkParserOpts.d.ts +2 -1
  89. package/dist/internal/checkParserOpts.d.ts.map +1 -0
  90. package/dist/{helpers/parser → internal}/checkParserOpts.js +22 -30
  91. package/dist/internal/escapeVariableOrPrefix.d.ts +2 -0
  92. package/dist/internal/escapeVariableOrPrefix.d.ts.map +1 -0
  93. package/dist/internal/escapeVariableOrPrefix.js +34 -0
  94. package/dist/internal/extractPosition.d.ts +5 -0
  95. package/dist/internal/extractPosition.d.ts.map +1 -0
  96. package/dist/internal/getUnclosedRightParenCount.d.ts +4 -0
  97. package/dist/internal/getUnclosedRightParenCount.d.ts.map +1 -0
  98. package/dist/{helpers/parser → internal}/getUnclosedRightParenCount.js +1 -1
  99. package/dist/{helpers/parser → internal}/parseParserOptions.d.ts +1 -1
  100. package/dist/internal/parseParserOptions.d.ts.map +1 -0
  101. package/dist/{helpers/parser → internal}/parseParserOptions.js +4 -4
  102. package/dist/internal/unescape.d.ts.map +1 -0
  103. package/dist/package.json.js +7 -7
  104. package/dist/types/ast.d.ts +195 -6
  105. package/dist/types/ast.d.ts.map +1 -1
  106. package/dist/types/ast.js +2 -0
  107. package/dist/types/autocomplete.d.ts +5 -6
  108. package/dist/types/autocomplete.d.ts.map +1 -1
  109. package/dist/types/parser.d.ts +17 -22
  110. package/dist/types/parser.d.ts.map +1 -1
  111. package/dist/utils/extractTokens.d.ts +2 -4
  112. package/dist/utils/extractTokens.d.ts.map +1 -1
  113. package/dist/utils/extractTokens.js +14 -18
  114. package/dist/utils/generateParentsMap.d.ts +10 -0
  115. package/dist/utils/generateParentsMap.d.ts.map +1 -0
  116. package/dist/utils/generateParentsMap.js +53 -0
  117. package/dist/utils/getCursorInfo.d.ts +1 -1
  118. package/dist/utils/getCursorInfo.d.ts.map +1 -1
  119. package/dist/utils/getCursorInfo.js +11 -21
  120. package/dist/utils/getOppositeDelimiter.d.ts +2 -1
  121. package/dist/utils/getOppositeDelimiter.d.ts.map +1 -1
  122. package/dist/utils/getOppositeDelimiter.js +9 -11
  123. package/dist/utils/getParent.d.ts +5 -0
  124. package/dist/utils/getParent.d.ts.map +1 -0
  125. package/dist/utils/getParent.js +6 -0
  126. package/dist/utils/getSurroundingErrors.d.ts +3 -4
  127. package/dist/utils/getSurroundingErrors.d.ts.map +1 -1
  128. package/dist/utils/getSurroundingErrors.js +3 -3
  129. package/dist/utils/index.d.ts +4 -0
  130. package/dist/utils/index.d.ts.map +1 -1
  131. package/dist/utils/index.js +8 -0
  132. package/dist/utils/isNode.d.ts +10 -0
  133. package/dist/utils/isNode.d.ts.map +1 -0
  134. package/dist/utils/isNode.js +6 -0
  135. package/dist/utils/isToken.d.ts +8 -0
  136. package/dist/utils/isToken.d.ts.map +1 -0
  137. package/dist/utils/isToken.js +6 -0
  138. package/dist/utils/prettyAst.d.ts +1 -2
  139. package/dist/utils/prettyAst.d.ts.map +1 -1
  140. package/dist/utils/prettyAst.js +20 -28
  141. package/package.json +7 -7
  142. package/src/Lexer.ts +2 -2
  143. package/src/Parser.ts +158 -165
  144. package/src/ast/builders/array.ts +4 -9
  145. package/src/ast/builders/condition.ts +10 -15
  146. package/src/ast/builders/delim.ts +1 -1
  147. package/src/ast/builders/error.ts +10 -6
  148. package/src/ast/builders/expression.ts +17 -23
  149. package/src/ast/builders/group.ts +7 -11
  150. package/src/ast/builders/pos.ts +2 -3
  151. package/src/ast/builders/token.ts +6 -7
  152. package/src/ast/builders/type.ts +1 -1
  153. package/src/ast/builders/variable.ts +6 -8
  154. package/src/ast/createArrayNode.ts +17 -0
  155. package/src/ast/createConditionNode.ts +58 -0
  156. package/src/ast/createExpressionNode.ts +21 -0
  157. package/src/ast/createGroupNode.ts +46 -0
  158. package/src/ast/createNormalizedCondition.ts +12 -0
  159. package/src/ast/createNormalizedExpression.ts +9 -0
  160. package/src/ast/createToken.ts +26 -0
  161. package/src/ast/createVariableNode.ts +18 -0
  162. package/src/ast/error.ts +9 -0
  163. package/src/ast/handlers.ts +26 -37
  164. package/src/ast/index.ts +9 -1
  165. package/src/{helpers/general → defaults}/defaultConditionNormalizer.ts +1 -1
  166. package/src/{helpers/general → defaults}/defaultKeyParser.ts +1 -1
  167. package/src/{helpers/general → defaults}/defaultPrefixApplier.ts +1 -1
  168. package/src/{helpers/general → defaults}/defaultValueComparer.ts +1 -1
  169. package/src/{helpers/general → defaults}/index.ts +0 -2
  170. package/src/index.ts +2 -1
  171. package/src/{helpers/errors.ts → internal/ExpressitError.ts} +2 -2
  172. package/src/{helpers/general → internal}/applyPrefix.ts +1 -1
  173. package/src/{helpers/parser → internal}/checkParserOpts.ts +16 -15
  174. package/src/internal/escapeVariableOrPrefix.ts +36 -0
  175. package/src/{helpers/parser → internal}/extractPosition.ts +2 -2
  176. package/src/{helpers/parser → internal}/getUnclosedRightParenCount.ts +1 -2
  177. package/src/{helpers/parser → internal}/parseParserOptions.ts +5 -5
  178. package/src/types/ast.ts +292 -7
  179. package/src/types/autocomplete.ts +5 -7
  180. package/src/types/parser.ts +17 -22
  181. package/src/utils/extractTokens.ts +16 -20
  182. package/src/utils/generateParentsMap.ts +60 -0
  183. package/src/utils/getCursorInfo.ts +4 -6
  184. package/src/utils/getOppositeDelimiter.ts +11 -10
  185. package/src/utils/getParent.ts +16 -0
  186. package/src/utils/getSurroundingErrors.ts +10 -9
  187. package/src/utils/index.ts +4 -0
  188. package/src/utils/isNode.ts +12 -0
  189. package/src/utils/isToken.ts +10 -0
  190. package/src/utils/prettyAst.ts +20 -17
  191. package/dist/ast/classes/ArrayNode.d.ts +0 -18
  192. package/dist/ast/classes/ArrayNode.d.ts.map +0 -1
  193. package/dist/ast/classes/ArrayNode.js +0 -55
  194. package/dist/ast/classes/Condition.d.ts +0 -13
  195. package/dist/ast/classes/Condition.d.ts.map +0 -1
  196. package/dist/ast/classes/Condition.js +0 -21
  197. package/dist/ast/classes/ConditionNode.d.ts +0 -73
  198. package/dist/ast/classes/ConditionNode.d.ts.map +0 -1
  199. package/dist/ast/classes/ConditionNode.js +0 -101
  200. package/dist/ast/classes/ErrorToken.d.ts +0 -27
  201. package/dist/ast/classes/ErrorToken.d.ts.map +0 -1
  202. package/dist/ast/classes/ErrorToken.js +0 -47
  203. package/dist/ast/classes/Expression.d.ts +0 -13
  204. package/dist/ast/classes/Expression.d.ts.map +0 -1
  205. package/dist/ast/classes/Expression.js +0 -19
  206. package/dist/ast/classes/ExpressionNode.d.ts +0 -21
  207. package/dist/ast/classes/ExpressionNode.d.ts.map +0 -1
  208. package/dist/ast/classes/ExpressionNode.js +0 -57
  209. package/dist/ast/classes/GroupNode.d.ts +0 -64
  210. package/dist/ast/classes/GroupNode.d.ts.map +0 -1
  211. package/dist/ast/classes/GroupNode.js +0 -69
  212. package/dist/ast/classes/Node.d.ts +0 -22
  213. package/dist/ast/classes/Node.d.ts.map +0 -1
  214. package/dist/ast/classes/Node.js +0 -28
  215. package/dist/ast/classes/Token.d.ts +0 -27
  216. package/dist/ast/classes/Token.d.ts.map +0 -1
  217. package/dist/ast/classes/Token.js +0 -28
  218. package/dist/ast/classes/ValidToken.d.ts +0 -26
  219. package/dist/ast/classes/ValidToken.d.ts.map +0 -1
  220. package/dist/ast/classes/ValidToken.js +0 -49
  221. package/dist/ast/classes/VariableNode.d.ts +0 -33
  222. package/dist/ast/classes/VariableNode.d.ts.map +0 -1
  223. package/dist/ast/classes/VariableNode.js +0 -58
  224. package/dist/ast/classes/index.d.ts +0 -12
  225. package/dist/ast/classes/index.d.ts.map +0 -1
  226. package/dist/ast/classes/index.js +0 -24
  227. package/dist/helpers/errors.d.ts.map +0 -1
  228. package/dist/helpers/general/applyBoolean.d.ts.map +0 -1
  229. package/dist/helpers/general/applyPrefix.d.ts.map +0 -1
  230. package/dist/helpers/general/defaultConditionNormalizer.d.ts +0 -3
  231. package/dist/helpers/general/defaultConditionNormalizer.d.ts.map +0 -1
  232. package/dist/helpers/general/defaultKeyParser.d.ts +0 -3
  233. package/dist/helpers/general/defaultKeyParser.d.ts.map +0 -1
  234. package/dist/helpers/general/defaultPrefixApplier.d.ts +0 -3
  235. package/dist/helpers/general/defaultPrefixApplier.d.ts.map +0 -1
  236. package/dist/helpers/general/defaultValueComparer.d.ts +0 -3
  237. package/dist/helpers/general/defaultValueComparer.d.ts.map +0 -1
  238. package/dist/helpers/general/index.d.ts.map +0 -1
  239. package/dist/helpers/index.d.ts +0 -4
  240. package/dist/helpers/index.d.ts.map +0 -1
  241. package/dist/helpers/index.js +0 -8
  242. package/dist/helpers/parser/assignParents.d.ts +0 -4
  243. package/dist/helpers/parser/assignParents.d.ts.map +0 -1
  244. package/dist/helpers/parser/assignParents.js +0 -71
  245. package/dist/helpers/parser/checkParserOpts.d.ts.map +0 -1
  246. package/dist/helpers/parser/extractPosition.d.ts +0 -5
  247. package/dist/helpers/parser/extractPosition.d.ts.map +0 -1
  248. package/dist/helpers/parser/getUnclosedRightParenCount.d.ts +0 -4
  249. package/dist/helpers/parser/getUnclosedRightParenCount.d.ts.map +0 -1
  250. package/dist/helpers/parser/index.d.ts +0 -9
  251. package/dist/helpers/parser/index.d.ts.map +0 -1
  252. package/dist/helpers/parser/index.js +0 -18
  253. package/dist/helpers/parser/parseParserOptions.d.ts.map +0 -1
  254. package/dist/helpers/parser/seal.d.ts +0 -8
  255. package/dist/helpers/parser/seal.d.ts.map +0 -1
  256. package/dist/helpers/parser/seal.js +0 -10
  257. package/dist/helpers/parser/setParent.d.ts +0 -6
  258. package/dist/helpers/parser/setParent.d.ts.map +0 -1
  259. package/dist/helpers/parser/setParent.js +0 -4
  260. package/dist/helpers/parser/unescape.d.ts.map +0 -1
  261. package/src/ast/classes/ArrayNode.ts +0 -46
  262. package/src/ast/classes/Condition.ts +0 -22
  263. package/src/ast/classes/ConditionNode.ts +0 -141
  264. package/src/ast/classes/ErrorToken.ts +0 -49
  265. package/src/ast/classes/Expression.ts +0 -26
  266. package/src/ast/classes/ExpressionNode.ts +0 -62
  267. package/src/ast/classes/GroupNode.ts +0 -127
  268. package/src/ast/classes/Node.ts +0 -47
  269. package/src/ast/classes/Token.ts +0 -59
  270. package/src/ast/classes/ValidToken.ts +0 -56
  271. package/src/ast/classes/VariableNode.ts +0 -67
  272. package/src/ast/classes/index.ts +0 -13
  273. package/src/helpers/index.ts +0 -6
  274. package/src/helpers/parser/assignParents.ts +0 -51
  275. package/src/helpers/parser/index.ts +0 -10
  276. package/src/helpers/parser/seal.ts +0 -14
  277. package/src/helpers/parser/setParent.ts +0 -5
  278. /package/dist/{helpers/general → defaults}/defaultConditionNormalizer.js +0 -0
  279. /package/dist/{helpers/general → defaults}/defaultPrefixApplier.js +0 -0
  280. /package/dist/{helpers/general → defaults}/defaultValueComparer.js +0 -0
  281. /package/dist/{helpers/general → internal}/applyBoolean.d.ts +0 -0
  282. /package/dist/{helpers/general → internal}/applyBoolean.js +0 -0
  283. /package/dist/{helpers/general → internal}/applyPrefix.js +0 -0
  284. /package/dist/{helpers/parser → internal}/extractPosition.js +0 -0
  285. /package/dist/{helpers/parser → internal}/unescape.d.ts +0 -0
  286. /package/dist/{helpers/parser → internal}/unescape.js +0 -0
  287. /package/src/{helpers/general → internal}/applyBoolean.ts +0 -0
  288. /package/src/{helpers/parser → internal}/unescape.ts +0 -0
@@ -1,25 +1,21 @@
1
1
  import { unreachable } from "@alanscodelog/utils/unreachable.js";
2
- import { ArrayNode } from "../ast/classes/ArrayNode.js";
3
- import { ConditionNode } from "../ast/classes/ConditionNode.js";
4
- import { ErrorToken } from "../ast/classes/ErrorToken.js";
5
- import { ExpressionNode } from "../ast/classes/ExpressionNode.js";
6
- import { GroupNode } from "../ast/classes/GroupNode.js";
7
- import { Node } from "../ast/classes/Node.js";
8
- import { VariableNode } from "../ast/classes/VariableNode.js";
2
+ import { isNode } from "./isNode.js";
3
+ import { isToken } from "./isToken.js";
4
+ import { AST_TYPE } from "../types/ast.js";
9
5
  function extractTokens(ast) {
10
6
  var _a, _b, _c;
11
- if (ast instanceof ErrorToken) {
7
+ if (isToken(ast) && !ast.valid) {
12
8
  return [ast];
13
9
  }
14
- if (ast instanceof VariableNode) {
10
+ if (ast.type === AST_TYPE.VARIABLE) {
15
11
  const prefix = ast.prefix ? [ast.prefix] : [];
16
12
  const quoteR = ast.quote ? [ast.quote.right] : [];
17
13
  const quoteL = ast.quote ? [ast.quote.left] : [];
18
14
  const quoteFlags = ((_a = ast.quote) == null ? void 0 : _a.flags) ? [ast.quote.flags] : [];
19
15
  return [...prefix, ...quoteL, ast.value, ...quoteR, ...quoteFlags];
20
16
  }
21
- if (ast instanceof ConditionNode) {
22
- const value = ast.value instanceof Node ? extractTokens(ast.value) : [ast.value];
17
+ if (ast.type === AST_TYPE.CONDITION) {
18
+ const value = isNode(ast.value) ? extractTokens(ast.value) : [ast.value];
23
19
  const operator = ast.operator ? [ast.operator] : [];
24
20
  const property = ast.property ? extractTokens(ast.property) : [];
25
21
  const propertyOperator = ast.propertyOperator ? [ast.propertyOperator] : [];
@@ -27,20 +23,20 @@ function extractTokens(ast) {
27
23
  const sepR = ((_c = ast.sep) == null ? void 0 : _c.right) ? [ast.sep.right] : [];
28
24
  return [...operator, ...property, ...sepL, ...propertyOperator, ...sepR, ...value];
29
25
  }
30
- if (ast instanceof GroupNode) {
31
- const prefix = ast.prefix ? ast.prefix instanceof Node ? extractTokens(ast.prefix) : [ast.prefix] : [];
26
+ if (ast.type === AST_TYPE.GROUP) {
27
+ const prefix = ast.prefix ? isNode(ast.prefix) ? extractTokens(ast.prefix) : [ast.prefix] : [];
32
28
  const parenL = ast.paren ? [ast.paren.left] : [];
33
29
  const parenR = ast.paren ? [ast.paren.right] : [];
34
- const expression = ast.expression instanceof Node ? extractTokens(ast.expression) : [ast.expression];
30
+ const expression = isNode(ast.expression) ? extractTokens(ast.expression) : [ast.expression];
35
31
  return [...prefix, ...parenL, ...expression, ...parenR];
36
32
  }
37
- if (ast instanceof ArrayNode) {
33
+ if (ast.type === AST_TYPE.ARRAY) {
38
34
  const values = ast.values.map((val) => extractTokens(val));
39
35
  return [ast.bracket.left, ...values.flat(), ast.bracket.right];
40
36
  }
41
- if (ast instanceof ExpressionNode) {
42
- const right = ast.right instanceof Node ? extractTokens(ast.right) : [ast.right];
43
- const left = ast.left instanceof Node ? extractTokens(ast.left) : [ast.left];
37
+ if (ast.type === AST_TYPE.EXPRESSION) {
38
+ const right = isNode(ast.right) ? extractTokens(ast.right) : [ast.right];
39
+ const left = isNode(ast.left) ? extractTokens(ast.left) : [ast.left];
44
40
  return [...left, ast.operator, ...right];
45
41
  }
46
42
  return unreachable();
@@ -0,0 +1,10 @@
1
+ import { type AnyToken, type Nodes } from "../types/ast.js";
2
+ /**
3
+ * Creates a map like Map<Node, Parent>, for easily accessing parents of nodes.
4
+ *
5
+ * This is done like this to avoid having circular structures in the ast.
6
+ *
7
+ * To get a properly typed parent, use {@link getParent} which you will need to pass a generated map.
8
+ */
9
+ export declare function generateParentsMap(ast: Nodes | AnyToken): Map<Nodes | AnyToken, Nodes>;
10
+ //# sourceMappingURL=generateParentsMap.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generateParentsMap.d.ts","sourceRoot":"","sources":["../../src/utils/generateParentsMap.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,QAAQ,EAAY,KAAK,KAAK,EAAG,MAAM,iBAAiB,CAAA;AAEtE;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,KAAK,GAAG,QAAQ,GAAG,GAAG,CAAC,KAAK,GAAG,QAAQ,EAAE,KAAK,CAAC,CA8CtF"}
@@ -0,0 +1,53 @@
1
+ import "@alanscodelog/utils/types";
2
+ import { isNode } from "./isNode.js";
3
+ import { AST_TYPE } from "../types/ast.js";
4
+ function generateParentsMap(ast) {
5
+ var _a, _b, _c, _d, _e, _f, _g;
6
+ const recursiveMap = arguments[1];
7
+ const map = recursiveMap ?? /* @__PURE__ */ new Map();
8
+ const self = generateParentsMap;
9
+ if (ast.type === AST_TYPE.VARIABLE) {
10
+ if (ast.prefix) map.set(ast.prefix, ast);
11
+ if ((_a = ast.quote) == null ? void 0 : _a.left) map.set(ast.quote.left, ast);
12
+ if ((_b = ast.quote) == null ? void 0 : _b.right) map.set(ast.quote.right, ast);
13
+ if ((_c = ast.quote) == null ? void 0 : _c.flags) map.set(ast.quote.flags, ast);
14
+ map.set(ast.value, ast);
15
+ } else if (ast.type === AST_TYPE.CONDITION) {
16
+ if (ast.operator) map.set(ast.operator, ast);
17
+ if (ast.property) {
18
+ map.set(ast.property, ast);
19
+ if (isNode(ast.property)) self(ast.property, map);
20
+ }
21
+ if (ast.propertyOperator) map.set(ast.propertyOperator, ast);
22
+ if ((_d = ast.sep) == null ? void 0 : _d.left) map.set(ast.sep.left, ast);
23
+ if ((_e = ast.sep) == null ? void 0 : _e.right) map.set(ast.sep.right, ast);
24
+ map.set(ast.value, ast);
25
+ if (isNode(ast.value)) self(ast.value, map);
26
+ } else if (ast.type === AST_TYPE.EXPRESSION) {
27
+ if (ast.operator) map.set(ast.operator, ast);
28
+ map.set(ast.right, ast);
29
+ if (isNode(ast.right)) self(ast.right, map);
30
+ map.set(ast.left, ast);
31
+ if (isNode(ast.left)) self(ast.left, map);
32
+ } else if (ast.type === AST_TYPE.GROUP) {
33
+ if (ast.prefix) {
34
+ map.set(ast.prefix, ast);
35
+ if (isNode(ast.prefix)) self(ast.prefix, map);
36
+ }
37
+ if ((_f = ast.paren) == null ? void 0 : _f.left) map.set(ast.paren.left, ast);
38
+ if ((_g = ast.paren) == null ? void 0 : _g.right) map.set(ast.paren.right, ast);
39
+ map.set(ast.expression, ast);
40
+ if (isNode(ast.expression)) self(ast.expression, map);
41
+ } else if (ast.type === AST_TYPE.ARRAY) {
42
+ if (ast.bracket.left) map.set(ast.bracket.left, ast);
43
+ if (ast.bracket.right) map.set(ast.bracket.right, ast);
44
+ for (const val of ast.values) {
45
+ map.set(val, ast);
46
+ if (isNode(val)) self(val, map);
47
+ }
48
+ }
49
+ return map;
50
+ }
51
+ export {
52
+ generateParentsMap
53
+ };
@@ -1,7 +1,7 @@
1
1
  import { type AnyToken, type ParserResults } from "../types/ast.js";
2
2
  import { type CursorInfo } from "../types/autocomplete.js";
3
3
  /**
4
- * Returns a @see CursorInfo object, see it for details.
4
+ * Returns a {@link CursorInfo} object, see it for details.
5
5
  */
6
6
  export declare function getCursorInfo(input: string, ast: ParserResults | AnyToken[], index: number): CursorInfo;
7
7
  //# sourceMappingURL=getCursorInfo.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"getCursorInfo.d.ts","sourceRoot":"","sources":["../../src/utils/getCursorInfo.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,KAAK,QAAQ,EAAE,KAAK,aAAa,EAAE,MAAM,iBAAiB,CAAA;AACnE,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,0BAA0B,CAAA;AAG1D;;GAEG;AACH,wBAAgB,aAAa,CAC5B,KAAK,EAAE,MAAM,EACb,GAAG,EAAE,aAAa,GAAG,QAAQ,EAAE,EAC/B,KAAK,EAAE,MAAM,GACX,UAAU,CAoDZ"}
1
+ {"version":3,"file":"getCursorInfo.d.ts","sourceRoot":"","sources":["../../src/utils/getCursorInfo.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,KAAK,QAAQ,EAAE,KAAK,aAAa,EAAE,MAAM,iBAAiB,CAAA;AACnE,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,0BAA0B,CAAA;AAG1D;;GAEG;AACH,wBAAgB,aAAa,CAC5B,KAAK,EAAE,MAAM,EACb,GAAG,EAAE,aAAa,GAAG,QAAQ,EAAE,EAC/B,KAAK,EAAE,MAAM,GACX,UAAU,CAoDZ"}
@@ -1,8 +1,6 @@
1
1
  import { isArray } from "@alanscodelog/utils/isArray.js";
2
2
  import { unreachable } from "@alanscodelog/utils/unreachable.js";
3
3
  import { extractTokens } from "./extractTokens.js";
4
- import { ErrorToken } from "../ast/classes/ErrorToken.js";
5
- import { ValidToken } from "../ast/classes/ValidToken.js";
6
4
  function getCursorInfo(input, ast, index) {
7
5
  const tokens = isArray(ast) ? ast : extractTokens(ast);
8
6
  if (input.length < index || index < 0) {
@@ -24,7 +22,7 @@ function getCursorInfo(input, ast, index) {
24
22
  };
25
23
  for (const token of tokens) {
26
24
  if (token.end <= index) {
27
- if (token instanceof ValidToken) {
25
+ if (token.valid) {
28
26
  info.prev = token;
29
27
  info.valid.prev = token;
30
28
  } else if (token.end !== index) {
@@ -32,7 +30,7 @@ function getCursorInfo(input, ast, index) {
32
30
  }
33
31
  }
34
32
  if (token.start < index && token.end > index) {
35
- if (token instanceof ErrorToken) {
33
+ if (!token.valid) {
36
34
  unreachable();
37
35
  } else {
38
36
  info.at = token;
@@ -40,7 +38,7 @@ function getCursorInfo(input, ast, index) {
40
38
  }
41
39
  if (token.start >= index) {
42
40
  info.next || (info.next = token);
43
- if (token instanceof ValidToken && !info.valid.next) {
41
+ if (token.valid && !info.valid.next) {
44
42
  info.valid.next = token;
45
43
  break;
46
44
  }
@@ -57,28 +55,20 @@ function setWhitespaceBetween(input, index, side, info) {
57
55
  if (info.at) {
58
56
  if (info.valid[side]) {
59
57
  const nextTokStart = info.valid[side][oppositePos];
60
- if (info.at[pos] !== nextTokStart)
61
- info.whitespace[side] = true;
62
- else
63
- return;
58
+ if (info.at[pos] !== nextTokStart) info.whitespace[side] = true;
59
+ else return;
64
60
  } else {
65
- if (info.at[pos] !== limit)
66
- info.whitespace[side] = true;
67
- else
68
- return;
61
+ if (info.at[pos] !== limit) info.whitespace[side] = true;
62
+ else return;
69
63
  }
70
64
  } else {
71
65
  if (info.valid[side]) {
72
66
  const nextTokStart = info.valid[side][oppositePos];
73
- if (index !== nextTokStart)
74
- info.whitespace[side] = true;
75
- else
76
- return;
67
+ if (index !== nextTokStart) info.whitespace[side] = true;
68
+ else return;
77
69
  } else {
78
- if (index !== limit)
79
- info.whitespace[side] = true;
80
- else
81
- return;
70
+ if (index !== limit) info.whitespace[side] = true;
71
+ else return;
82
72
  }
83
73
  }
84
74
  }
@@ -1,6 +1,7 @@
1
+ import type { generateParentsMap } from "./generateParentsMap.js";
1
2
  import { type AnyToken, type TokenDelimiterTypes } from "../types/ast.js";
2
3
  /**
3
4
  * Given a delimiter token, returns it's opposite pair, or undefined if the type passed was not a delimiter token (so you can pass any type without checking).
4
5
  */
5
- export declare function getOppositeDelimiter(token: AnyToken): AnyToken<TokenDelimiterTypes> | undefined;
6
+ export declare function getOppositeDelimiter(token: AnyToken, parentsMap: ReturnType<typeof generateParentsMap>): AnyToken<TokenDelimiterTypes> | undefined;
6
7
  //# sourceMappingURL=getOppositeDelimiter.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"getOppositeDelimiter.d.ts","sourceRoot":"","sources":["../../src/utils/getOppositeDelimiter.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,KAAK,QAAQ,EAAc,KAAK,mBAAmB,EAAE,MAAM,iBAAiB,CAAA;AAErF;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,QAAQ,GAAG,QAAQ,CAAC,mBAAmB,CAAC,GAAG,SAAS,CAsB/F"}
1
+ {"version":3,"file":"getOppositeDelimiter.d.ts","sourceRoot":"","sources":["../../src/utils/getOppositeDelimiter.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AAMjE,OAAO,EAAE,KAAK,QAAQ,EAAiE,KAAK,mBAAmB,EAAqB,MAAM,iBAAiB,CAAA;AAE3J;;GAEG;AACH,wBAAgB,oBAAoB,CACnC,KAAK,EAAE,QAAQ,EACf,UAAU,EAAE,UAAU,CAAC,OAAO,kBAAkB,CAAC,GAC/C,QAAQ,CAAC,mBAAmB,CAAC,GAAG,SAAS,CAuB3C"}
@@ -4,27 +4,25 @@ import { isDelimiter } from "./isDelimiter.js";
4
4
  import { isParen } from "./isParen.js";
5
5
  import { isQuote } from "./isQuote.js";
6
6
  import { TOKEN_TYPE } from "../types/ast.js";
7
- function getOppositeDelimiter(token) {
8
- if (!isDelimiter(token))
9
- throw new Error("Token is not a delimiter type.");
7
+ function getOppositeDelimiter(token, parentsMap) {
8
+ const parent = parentsMap.get(token);
9
+ if (!isDelimiter(token)) throw new Error("Token is not a delimiter type.");
10
10
  if (isParen(token)) {
11
- const paren = token.parent.paren;
11
+ const paren = parent.paren;
12
12
  const opposite = paren.left === token ? "right" : "left";
13
13
  return paren[opposite];
14
14
  } else if (isBracket(token)) {
15
- const bracket = token.parent.bracket;
15
+ const bracket = parent.bracket;
16
16
  const opposite = bracket.left === token ? "right" : "left";
17
17
  return bracket[opposite];
18
18
  } else if (isQuote(token)) {
19
- const quotes = token.parent.quote;
20
- if (quotes === void 0)
21
- unreachable();
19
+ const quotes = parent.quote;
20
+ if (quotes === void 0) unreachable();
22
21
  const opposite = quotes.left === token ? "right" : "left";
23
22
  return quotes[opposite];
24
23
  } else if (token.type === TOKEN_TYPE.OP_EXPANDED_SEP) {
25
- const sep = token.parent.sep;
26
- if (sep === void 0)
27
- unreachable();
24
+ const sep = parent.sep;
25
+ if (sep === void 0) unreachable();
28
26
  const opposite = sep.left === token ? "right" : "left";
29
27
  return sep[opposite];
30
28
  }
@@ -0,0 +1,5 @@
1
+ import type { generateParentsMap } from "./generateParentsMap.js";
2
+ import type { AnyToken, ArrayNode, ConditionNode, ExpressionNode, GroupNode, ParentTypes, VariableNode } from "../types/ast.js";
3
+ /** See {@link generateParentsMap} */
4
+ export declare function getParent<T extends ExpressionNode | ConditionNode | GroupNode | VariableNode | ArrayNode | AnyToken | undefined>(node: T, map: ReturnType<typeof generateParentsMap>): ParentTypes<T> | undefined;
5
+ //# sourceMappingURL=getParent.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getParent.d.ts","sourceRoot":"","sources":["../../src/utils/getParent.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AAEjE,OAAO,KAAK,EAAE,QAAQ,EAAE,SAAS,EAAE,aAAa,EAAE,cAAc,EAAE,SAAS,EAAC,WAAW,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAC9H,qCAAqC;AACrC,wBAAgB,SAAS,CACxB,CAAC,SACC,cAAc,GACd,aAAa,GACb,SAAS,GACT,YAAY,GACZ,SAAS,GACT,QAAQ,GACR,SAAS,EACV,IAAI,EAAE,CAAC,EAAE,GAAG,EAAE,UAAU,CAAC,OAAO,kBAAkB,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,GAAG,SAAS,CAEjF"}
@@ -0,0 +1,6 @@
1
+ function getParent(node, map) {
2
+ return map.get(node);
3
+ }
4
+ export {
5
+ getParent
6
+ };
@@ -1,9 +1,9 @@
1
1
  /**
2
- * Mostly for internal use by @see autosuggest.
2
+ * Mostly for internal use by {@link autosuggest}.
3
3
  *
4
4
  * Returns all error tokens immediately before/after cursor (since there might be multiple error tokens one after the other).
5
5
  *
6
- * The errors are sorted by closeness to the given cursor (inside @see CursorInfo), with quote errors having priority, then paren errors, then any other errors. They can be sorted by closeness because although two errors might follow each other, their positions might be different because of whitespace, but they can still be fixed from any cursor position between their ends.
6
+ * The errors are sorted by closeness to the given cursor (inside {@link CursorInfo}), with quote errors having priority, then paren errors, then any other errors. They can be sorted by closeness because although two errors might follow each other, their positions might be different because of whitespace, but they can still be fixed from any cursor position between their ends.
7
7
  *
8
8
  * For example:
9
9
  * ```
@@ -18,8 +18,7 @@
18
18
  * either insertion would fix the issue for either position would fix the issue, albeit with different results
19
19
  * ```
20
20
  */
21
- import { ErrorToken } from "../ast/classes/ErrorToken.js";
22
- import { type AnyToken } from "../types/ast.js";
21
+ import { type AnyToken, type ErrorToken } from "../types/ast.js";
23
22
  import type { CursorInfo } from "../types/autocomplete.js";
24
23
  export declare function getSurroundingErrors(tokens: AnyToken[], token: CursorInfo): ErrorToken[];
25
24
  //# sourceMappingURL=getSurroundingErrors.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"getSurroundingErrors.d.ts","sourceRoot":"","sources":["../../src/utils/getSurroundingErrors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAA;AACzD,OAAO,EAAE,KAAK,QAAQ,EAAc,MAAM,iBAAiB,CAAA;AAC3D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAA;AAG1D,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,QAAQ,EAAE,EAAE,KAAK,EAAE,UAAU,GAAG,UAAU,EAAE,CA8BxF"}
1
+ {"version":3,"file":"getSurroundingErrors.d.ts","sourceRoot":"","sources":["../../src/utils/getSurroundingErrors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,EAAE,KAAK,QAAQ,EAAC,KAAK,UAAU,EAAa,MAAM,iBAAiB,CAAA;AAC1E,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAA;AAG1D,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,QAAQ,EAAE,EAAE,KAAK,EAAE,UAAU,GAAG,UAAU,EAAE,CAgCxF"}
@@ -1,6 +1,6 @@
1
- import { ErrorToken } from "../ast/classes/ErrorToken.js";
2
1
  import { TOKEN_TYPE } from "../types/ast.js";
3
2
  function getSurroundingErrors(tokens, token) {
3
+ var _a, _b;
4
4
  if (token.at) {
5
5
  return [];
6
6
  }
@@ -8,11 +8,11 @@ function getSurroundingErrors(tokens, token) {
8
8
  let iNext = tokens[i] === token.next ? i : i + 1;
9
9
  let iPrev = tokens[i] === token.next ? i - 1 : i;
10
10
  const errors = [];
11
- while (tokens[iNext] instanceof ErrorToken) {
11
+ while (((_a = tokens[iNext]) == null ? void 0 : _a.valid) === false) {
12
12
  errors.push(tokens[iNext]);
13
13
  iNext++;
14
14
  }
15
- while (tokens[iPrev] instanceof ErrorToken) {
15
+ while (((_b = tokens[iPrev]) == null ? void 0 : _b.valid) === false) {
16
16
  errors.push(tokens[iPrev]);
17
17
  iPrev--;
18
18
  }
@@ -1,10 +1,14 @@
1
1
  export { extractTokens } from "./extractTokens.js";
2
+ export { generateParentsMap } from "./generateParentsMap.js";
2
3
  export { getCursorInfo } from "./getCursorInfo.js";
3
4
  export { getOppositeDelimiter } from "./getOppositeDelimiter.js";
5
+ export { getParent } from "./getParent.js";
4
6
  export { getSurroundingErrors } from "./getSurroundingErrors.js";
5
7
  export { isBracket } from "./isBracket.js";
6
8
  export { isDelimiter } from "./isDelimiter.js";
9
+ export { isNode } from "./isNode.js";
7
10
  export { isParen } from "./isParen.js";
8
11
  export { isQuote } from "./isQuote.js";
12
+ export { isToken } from "./isToken.js";
9
13
  export { prettyAst } from "./prettyAst.js";
10
14
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAClD,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAClD,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAA;AAChE,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAA;AAChE,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1C,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAC9C,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AACtC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AACtC,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAClD,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AAC5D,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAClD,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAA;AAChE,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1C,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAA;AAChE,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1C,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAC9C,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACpC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AACtC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AACtC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AACtC,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA"}
@@ -1,20 +1,28 @@
1
1
  import { extractTokens } from "./extractTokens.js";
2
+ import { generateParentsMap } from "./generateParentsMap.js";
2
3
  import { getCursorInfo } from "./getCursorInfo.js";
3
4
  import { getOppositeDelimiter } from "./getOppositeDelimiter.js";
5
+ import { getParent } from "./getParent.js";
4
6
  import { getSurroundingErrors } from "./getSurroundingErrors.js";
5
7
  import { isBracket } from "./isBracket.js";
6
8
  import { isDelimiter } from "./isDelimiter.js";
9
+ import { isNode } from "./isNode.js";
7
10
  import { isParen } from "./isParen.js";
8
11
  import { isQuote } from "./isQuote.js";
12
+ import { isToken } from "./isToken.js";
9
13
  import { prettyAst } from "./prettyAst.js";
10
14
  export {
11
15
  extractTokens,
16
+ generateParentsMap,
12
17
  getCursorInfo,
13
18
  getOppositeDelimiter,
19
+ getParent,
14
20
  getSurroundingErrors,
15
21
  isBracket,
16
22
  isDelimiter,
23
+ isNode,
17
24
  isParen,
18
25
  isQuote,
26
+ isToken,
19
27
  prettyAst
20
28
  };
@@ -0,0 +1,10 @@
1
+ import type { Node } from "../types/ast.js";
2
+ /**
3
+ * A simple wrapper around checking an object's `isNode` property that casts the object to a {@link Nodes} type for typescript.
4
+ *
5
+ * Does not actually do any checking of the object, and assumes the object was created using one of the `create*` functions.
6
+ *
7
+ * Also note that passing a NormalizedCondition or NormalizedExpression will return false even though they have their own {@link AST_TYPE} since they are not technically nodes (with positions).
8
+ */
9
+ export declare function isNode(node: any): node is Node;
10
+ //# sourceMappingURL=isNode.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"isNode.d.ts","sourceRoot":"","sources":["../../src/utils/isNode.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAA;AAC3C;;;;;;GAMG;AACH,wBAAgB,MAAM,CAAC,IAAI,EAAE,GAAG,GAAG,IAAI,IAAI,IAAI,CAE9C"}
@@ -0,0 +1,6 @@
1
+ function isNode(node) {
2
+ return (node == null ? void 0 : node.isNode) === true;
3
+ }
4
+ export {
5
+ isNode
6
+ };
@@ -0,0 +1,8 @@
1
+ import type { ErrorToken, ValidToken } from "../types/ast.js";
2
+ /**
3
+ * A simple wrapper around checking an object's `isToken` property that casts the object to a {@link ValidToken} or {@link ErrorToken} for typescript.
4
+ *
5
+ * Does not actually do any checking of the object, and assumes the object was created using one of the `create*` functions.
6
+ */
7
+ export declare function isToken(token: any): token is ValidToken | ErrorToken;
8
+ //# sourceMappingURL=isToken.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"isToken.d.ts","sourceRoot":"","sources":["../../src/utils/isToken.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAE7D;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,GAAG,GAAG,KAAK,IAAI,UAAU,GAAG,UAAU,CAEpE"}
@@ -0,0 +1,6 @@
1
+ function isToken(token) {
2
+ return (token == null ? void 0 : token.isToken) === true;
3
+ }
4
+ export {
5
+ isToken
6
+ };
@@ -1,5 +1,4 @@
1
- import { ArrayNode, GroupNode, VariableNode } from "../ast/classes/index.js";
2
- import { type AnyToken, type ParserResults } from "../types/ast.js";
1
+ import { type AnyToken, type ArrayNode, type GroupNode, type ParserResults, type VariableNode } from "../types/ast.js";
3
2
  type Colors = {
4
3
  /** Color used to highlight the actual text content of the token nodes. */
5
4
  values: string;
@@ -1 +1 @@
1
- {"version":3,"file":"prettyAst.d.ts","sourceRoot":"","sources":["../../src/utils/prettyAst.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,SAAS,EAA6C,SAAS,EAAc,YAAY,EAAE,MAAM,yBAAyB,CAAA;AACnI,OAAO,EAAE,KAAK,QAAQ,EAAE,KAAK,aAAa,EAAc,MAAM,iBAAiB,CAAA;AAG/E,KAAK,MAAM,GAAG;IACb,0EAA0E;IAC1E,MAAM,EAAE,MAAM,CAAA;IACd,6NAA6N;IAC7N,IAAI,EAAE,MAAM,CAAA;IACZ,QAAQ,EAAE,MAAM,CAAA;IAChB,mKAAmK;IACnK,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,EAAE,MAAM,CAAA;IACb,sCAAsC;IACtC,KAAK,EAAE,MAAM,CAAA;CACb,CAAA;AAoBD;;;;;;;;;;;;;;;;;GAiBG;AAEH,wBAAgB,SAAS,CACxB,GAAG,EAAE,aAAa,GAAG,QAAQ,GAAG,YAAY,GAAG,SAAS,GAAG,SAAS,EACpE,EAAE,MAAc,EAAE,QAAe,EAAE,IAAW,EAAE,MAAY,EAAE,KAAU,EAAE,GAAE,OAAO,CAAC,MAAM,CAAC,QAAQ,GAAG,UAAU,GAAG,MAAM,GAAG,QAAQ,GAAG,OAAO,EAAE,MAAM,CAAC,CAAM,EAC7J,MAAM,GAAE,OAAO,CAAC,MAAM,CAAC,GAAG,KAAU,GAClC,MAAM,CAyFR"}
1
+ {"version":3,"file":"prettyAst.d.ts","sourceRoot":"","sources":["../../src/utils/prettyAst.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,KAAK,QAAQ,EAAE,KAAK,SAAS,EAA8B,KAAK,SAAS,EAAE,KAAK,aAAa,EAAc,KAAK,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAG9J,KAAK,MAAM,GAAG;IACb,0EAA0E;IAC1E,MAAM,EAAE,MAAM,CAAA;IACd,6NAA6N;IAC7N,IAAI,EAAE,MAAM,CAAA;IACZ,QAAQ,EAAE,MAAM,CAAA;IAChB,mKAAmK;IACnK,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,EAAE,MAAM,CAAA;IACb,sCAAsC;IACtC,KAAK,EAAE,MAAM,CAAA;CACb,CAAA;AAoBD;;;;;;;;;;;;;;;;;GAiBG;AAEH,wBAAgB,SAAS,CACxB,GAAG,EAAE,aAAa,GAAG,QAAQ,GAAG,YAAY,GAAG,SAAS,GAAG,SAAS,EACpE,EAAE,MAAc,EAAE,QAAe,EAAE,IAAW,EAAE,MAAY,EAAE,KAAU,EAAE,GAAE,OAAO,CAAC,MAAM,CAAC,QAAQ,GAAG,UAAU,GAAG,MAAM,GAAG,QAAQ,GAAG,OAAO,EAAE,MAAM,CAAC,CAAM,EAC7J,MAAM,GAAE,OAAO,CAAC,MAAM,CAAC,GAAG,KAAU,GAClC,MAAM,CA2FR"}
@@ -2,17 +2,8 @@ import * as color from "@alanscodelog/utils/colors.js";
2
2
  import { isBlank } from "@alanscodelog/utils/isBlank.js";
3
3
  import "@alanscodelog/utils/types";
4
4
  import { unreachable } from "@alanscodelog/utils/unreachable.js";
5
- import { ArrayNode } from "../ast/classes/ArrayNode.js";
6
- import { ConditionNode } from "../ast/classes/ConditionNode.js";
7
- import { ErrorToken } from "../ast/classes/ErrorToken.js";
8
- import { ExpressionNode } from "../ast/classes/ExpressionNode.js";
9
- import { GroupNode } from "../ast/classes/GroupNode.js";
10
- import "@alanscodelog/utils/crop.js";
11
- import "@alanscodelog/utils/indent.js";
12
- import "@alanscodelog/utils/pretty.js";
13
- import { ValidToken } from "../ast/classes/ValidToken.js";
14
- import { VariableNode } from "../ast/classes/VariableNode.js";
15
- import { TOKEN_TYPE } from "../types/ast.js";
5
+ import { isToken } from "./isToken.js";
6
+ import { AST_TYPE, TOKEN_TYPE } from "../types/ast.js";
16
7
  const defaultColors = {
17
8
  values: color.yellow,
18
9
  info: color.cyan,
@@ -37,28 +28,29 @@ function prettyAst(ast, { indent = " ", children = "├╴", last = "└╴",
37
28
  const _ = indent;
38
29
  const __ = arguments[3] ?? "";
39
30
  let extra = arguments[4] ?? "";
40
- if (!isBlank(extra))
41
- extra = ` ${c.hint}${extra}${c.reset}`;
31
+ if (!isBlank(extra)) extra = ` ${c.hint}${extra}${c.reset}`;
42
32
  const ___ = __ + _ + branch;
43
33
  const __L = __ + _ + indent[0];
44
34
  const prettyAst_ = prettyAst;
45
- if (ast instanceof ValidToken) {
46
- const value = `${ast.value}`;
47
- return `TOKEN ${pos} ${c.values}${quote}${value}${quote}${c.reset}${extra}`;
35
+ if (isToken(ast)) {
36
+ if (ast.valid) {
37
+ const value = `${ast.value}`;
38
+ return `TOKEN ${pos} ${c.values}${quote}${value}${quote}${c.reset}${extra}`;
39
+ }
40
+ if (!ast.valid) {
41
+ const value = `[${ast.expected.join(", ")}]`;
42
+ return `ERROR ${pos} ${c.error}${value}${c.reset}${extra}`;
43
+ }
48
44
  }
49
- if (ast instanceof ErrorToken) {
50
- const value = `[${ast.expected.join(", ")}]`;
51
- return `ERROR ${pos} ${c.error}${value}${c.reset}${extra}`;
52
- }
53
- if (ast instanceof ConditionNode) {
45
+ if (ast.type === AST_TYPE.CONDITION) {
54
46
  const header = `${c.info}${ast.operator === void 0}${c.reset}`;
55
47
  const not = ast.operator ? prettyAst_(ast.operator, opts, c, ___, `(negation)`) : "";
56
48
  const property = ast.property ? prettyAst_(ast.property, opts, c, ___, `(property)`) : "";
57
49
  const sepL = ((_a = ast.sep) == null ? void 0 : _a.left) ? prettyAst_(ast.sep.left, opts, c, ___, `(separator)`) : "";
58
50
  const op = ast.propertyOperator ? prettyAst_(ast.propertyOperator, opts, c, ___, `(property operator)`) : "";
59
51
  const sepR = ((_b = ast.sep) == null ? void 0 : _b.right) ? prettyAst_(ast.sep.right, opts, c, ___, `(separator)`) : "";
60
- const isRegex = ast.value instanceof VariableNode && ((_c = ast.value.quote) == null ? void 0 : _c.left.type) === TOKEN_TYPE.REGEX;
61
- const isArray = ast.value instanceof ArrayNode;
52
+ const isRegex = ast.value.type === AST_TYPE.VARIABLE && ((_c = ast.value.quote) == null ? void 0 : _c.left.type) === TOKEN_TYPE.REGEX;
53
+ const isArray = ast.value.type === AST_TYPE.ARRAY;
62
54
  const variable = prettyAst_(ast.value, opts, c, __L, `(${property ? "value" : "variable/alone"}${isRegex ? " - regex" : isArray ? "- array" : ""})`);
63
55
  return [
64
56
  `CONDITION ${pos} ${header}${extra}`,
@@ -66,7 +58,7 @@ function prettyAst(ast, { indent = " ", children = "├╴", last = "└╴",
66
58
  ].join(`
67
59
  ${__}`);
68
60
  }
69
- if (ast instanceof VariableNode) {
61
+ if (ast.type === AST_TYPE.VARIABLE) {
70
62
  const prefix = ast.prefix ? prettyAst_(ast.prefix, opts, c, ___, `(value prefix)`) : "";
71
63
  const left = ((_d = ast.quote) == null ? void 0 : _d.left) ? prettyAst_(ast.quote.left, opts, c, ___, "") : "";
72
64
  const value = prettyAst_(ast.value, opts, c, !ast.quote ? __L : !((_e = ast.quote) == null ? void 0 : _e.right) ? __L : ___, "");
@@ -78,7 +70,7 @@ ${__}`);
78
70
  ].join(`
79
71
  ${__}`);
80
72
  }
81
- if (ast instanceof GroupNode) {
73
+ if (ast.type === AST_TYPE.GROUP) {
82
74
  const header = `${c.info}${ast.prefix === void 0 || ast.prefix.operator === void 0}${c.reset}`;
83
75
  const prefix = ast.prefix ? prettyAst_(ast.prefix, opts, c, ___, `(group prefix)`) : "";
84
76
  const expression = prettyAst_(ast.expression, opts, c, __L, "");
@@ -88,7 +80,7 @@ ${__}`);
88
80
  ].join(`
89
81
  ${__}`);
90
82
  }
91
- if (ast instanceof ArrayNode) {
83
+ if (ast.type === AST_TYPE.ARRAY) {
92
84
  const bracketL = ast.bracket.left ? prettyAst_(ast.bracket.left, opts, c, ast.values.length === 0 && !ast.bracket.right ? __L : ___, "") : "";
93
85
  const values = ast.values.length > 0 ? ast.values.map(
94
86
  (node, i) => prettyAst_(node, opts, c, !ast.bracket.right && i === ast.values.length - 1 ? __L : ___, "")
@@ -100,11 +92,11 @@ ${__}`);
100
92
  ].join(`
101
93
  ${__}`);
102
94
  }
103
- if (ast instanceof ExpressionNode) {
95
+ if (ast.type === AST_TYPE.EXPRESSION) {
104
96
  const left = prettyAst_(ast.left, opts, c, ___, "");
105
97
  const operator = prettyAst_(ast.operator, opts, c, ___, `(boolean operator)`);
106
98
  const right = prettyAst_(ast.right, opts, c, __L, "");
107
- const header = ast.operator instanceof ErrorToken ? `${c.info}[${ast.operator.expected.join(",")}]${c.reset}` : `${c.info}"${ast.operator.value}"${c.reset}`;
99
+ const header = !ast.operator.valid ? `${c.info}[${ast.operator.expected.join(",")}]${c.reset}` : `${c.info}"${ast.operator.value}"${c.reset}`;
108
100
  return [
109
101
  `EXPRESSION ${pos} ${header}${extra}`,
110
102
  ...toRows([left, operator, right], opts)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@witchcraft/expressit",
3
3
  "description": "A blazing fast, customizable, error-tolerant expression parser that creates safe to eval expressions + a few other goodies like autocomplete.",
4
- "version": "0.1.3",
4
+ "version": "0.2.1",
5
5
  "types": "./dist/index.d.ts",
6
6
  "type": "module",
7
7
  "module": "./dist/index.js",
@@ -19,13 +19,13 @@
19
19
  "types": "./dist/ast/*",
20
20
  "import": "./dist/ast/*"
21
21
  },
22
- "./helpers": {
23
- "types": "./dist/helpers/index.d.ts",
24
- "import": "./dist/helpers/index.js"
22
+ "./internal": {
23
+ "types": "./dist/internal/index.d.ts",
24
+ "import": "./dist/internal/index.js"
25
25
  },
26
- "./helpers/*": {
27
- "types": "./dist/helpers/*",
28
- "import": "./dist/helpers/*"
26
+ "./internal/*": {
27
+ "types": "./dist/internal/*",
28
+ "import": "./dist/internal/*"
29
29
  },
30
30
  "./methods": {
31
31
  "types": "./dist/methods/index.d.ts",
package/src/Lexer.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { isBlank } from "@alanscodelog/utils/isBlank.js"
2
2
  import { pushIfNotIn } from "@alanscodelog/utils/pushIfNotIn.js"
3
3
 
4
- import { checkParserOpts } from "./helpers/parser/checkParserOpts.js"
5
- import { parseParserOptions } from "./helpers/parser/parseParserOptions.js"
4
+ import { checkParserOpts } from "./internal/checkParserOpts.js"
5
+ import { parseParserOptions } from "./internal/parseParserOptions.js"
6
6
  import type { FullParserOptions } from "./types/index.js"
7
7
 
8
8
  const regexFlags = /^[a-zA-Z]+/