@witchcraft/expressit 0.1.2 → 0.2.0

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 (285) hide show
  1. package/README.md +9 -5
  2. package/dist/Lexer.js +2 -2
  3. package/dist/Parser.d.ts +14 -29
  4. package/dist/Parser.d.ts.map +1 -1
  5. package/dist/Parser.js +120 -135
  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 +2 -3
  12. package/dist/ast/builders/delim.d.ts +1 -1
  13. package/dist/ast/builders/error.d.ts +3 -4
  14. package/dist/ast/builders/error.d.ts.map +1 -1
  15. package/dist/ast/builders/error.js +6 -2
  16. package/dist/ast/builders/expression.d.ts +3 -7
  17. package/dist/ast/builders/expression.d.ts.map +1 -1
  18. package/dist/ast/builders/expression.js +3 -5
  19. package/dist/ast/builders/group.d.ts +3 -8
  20. package/dist/ast/builders/group.d.ts.map +1 -1
  21. package/dist/ast/builders/group.js +3 -3
  22. package/dist/ast/builders/pos.d.ts +1 -2
  23. package/dist/ast/builders/pos.d.ts.map +1 -1
  24. package/dist/ast/builders/token.d.ts +3 -5
  25. package/dist/ast/builders/token.d.ts.map +1 -1
  26. package/dist/ast/builders/token.js +3 -4
  27. package/dist/ast/builders/type.d.ts +1 -1
  28. package/dist/ast/builders/variable.d.ts +3 -5
  29. package/dist/ast/builders/variable.d.ts.map +1 -1
  30. package/dist/ast/builders/variable.js +2 -3
  31. package/dist/ast/createArrayNode.d.ts +3 -0
  32. package/dist/ast/createArrayNode.d.ts.map +1 -0
  33. package/dist/ast/createArrayNode.js +12 -0
  34. package/dist/ast/createConditionNode.d.ts +11 -0
  35. package/dist/ast/createConditionNode.d.ts.map +1 -0
  36. package/dist/ast/createConditionNode.js +18 -0
  37. package/dist/ast/createExpressionNode.d.ts +3 -0
  38. package/dist/ast/createExpressionNode.d.ts.map +1 -0
  39. package/dist/ast/createExpressionNode.js +14 -0
  40. package/dist/ast/createGroupNode.d.ts +5 -0
  41. package/dist/ast/createGroupNode.d.ts.map +1 -0
  42. package/dist/ast/createGroupNode.js +14 -0
  43. package/dist/ast/createNormalizedCondition.d.ts +3 -0
  44. package/dist/ast/createNormalizedCondition.d.ts.map +1 -0
  45. package/dist/ast/createNormalizedCondition.js +10 -0
  46. package/dist/ast/createNormalizedExpression.d.ts +3 -0
  47. package/dist/ast/createNormalizedExpression.d.ts.map +1 -0
  48. package/dist/ast/createNormalizedExpression.js +10 -0
  49. package/dist/ast/createToken.d.ts +6 -0
  50. package/dist/ast/createToken.d.ts.map +1 -0
  51. package/dist/ast/createToken.js +15 -0
  52. package/dist/ast/createVariableNode.d.ts +3 -0
  53. package/dist/ast/createVariableNode.d.ts.map +1 -0
  54. package/dist/ast/createVariableNode.js +12 -0
  55. package/dist/ast/error.d.ts +3 -0
  56. package/dist/ast/error.d.ts.map +1 -0
  57. package/dist/ast/error.js +9 -0
  58. package/dist/ast/handlers.d.ts +5 -12
  59. package/dist/ast/handlers.d.ts.map +1 -1
  60. package/dist/ast/handlers.js +21 -29
  61. package/dist/ast/index.d.ts +9 -1
  62. package/dist/ast/index.d.ts.map +1 -1
  63. package/dist/ast/index.js +18 -2
  64. package/dist/defaults/defaultConditionNormalizer.d.ts +3 -0
  65. package/dist/defaults/defaultConditionNormalizer.d.ts.map +1 -0
  66. package/dist/defaults/defaultKeyParser.d.ts +3 -0
  67. package/dist/defaults/defaultKeyParser.d.ts.map +1 -0
  68. package/dist/defaults/defaultPrefixApplier.d.ts +3 -0
  69. package/dist/defaults/defaultPrefixApplier.d.ts.map +1 -0
  70. package/dist/defaults/defaultValueComparer.d.ts +3 -0
  71. package/dist/defaults/defaultValueComparer.d.ts.map +1 -0
  72. package/dist/{helpers/general → defaults}/index.d.ts +0 -2
  73. package/dist/defaults/index.d.ts.map +1 -0
  74. package/dist/{helpers/general → defaults}/index.js +0 -4
  75. package/dist/index.d.ts +2 -1
  76. package/dist/index.d.ts.map +1 -1
  77. package/dist/index.js +3 -1
  78. package/dist/{helpers/errors.d.ts → internal/ExpressitError.d.ts} +3 -2
  79. package/dist/internal/ExpressitError.d.ts.map +1 -0
  80. package/dist/{helpers/errors.js → internal/ExpressitError.js} +2 -2
  81. package/dist/internal/applyBoolean.d.ts.map +1 -0
  82. package/dist/{helpers/general → internal}/applyPrefix.d.ts +1 -1
  83. package/dist/internal/applyPrefix.d.ts.map +1 -0
  84. package/dist/{helpers/parser → internal}/checkParserOpts.d.ts +2 -1
  85. package/dist/internal/checkParserOpts.d.ts.map +1 -0
  86. package/dist/{helpers/parser → internal}/checkParserOpts.js +14 -14
  87. package/dist/internal/escapeVariableOrPrefix.d.ts +2 -0
  88. package/dist/internal/escapeVariableOrPrefix.d.ts.map +1 -0
  89. package/dist/internal/escapeVariableOrPrefix.js +38 -0
  90. package/dist/internal/extractPosition.d.ts +5 -0
  91. package/dist/internal/extractPosition.d.ts.map +1 -0
  92. package/dist/internal/getUnclosedRightParenCount.d.ts +4 -0
  93. package/dist/internal/getUnclosedRightParenCount.d.ts.map +1 -0
  94. package/dist/{helpers/parser → internal}/getUnclosedRightParenCount.js +1 -1
  95. package/dist/{helpers/parser → internal}/parseParserOptions.d.ts +1 -1
  96. package/dist/internal/parseParserOptions.d.ts.map +1 -0
  97. package/dist/{helpers/parser → internal}/parseParserOptions.js +4 -4
  98. package/dist/internal/unescape.d.ts.map +1 -0
  99. package/dist/package.json.js +7 -7
  100. package/dist/types/ast.d.ts +195 -6
  101. package/dist/types/ast.d.ts.map +1 -1
  102. package/dist/types/ast.js +2 -0
  103. package/dist/types/autocomplete.d.ts +5 -6
  104. package/dist/types/autocomplete.d.ts.map +1 -1
  105. package/dist/types/parser.d.ts +17 -22
  106. package/dist/types/parser.d.ts.map +1 -1
  107. package/dist/utils/extractTokens.d.ts +2 -4
  108. package/dist/utils/extractTokens.d.ts.map +1 -1
  109. package/dist/utils/extractTokens.js +14 -18
  110. package/dist/utils/generateParentsMap.d.ts +10 -0
  111. package/dist/utils/generateParentsMap.d.ts.map +1 -0
  112. package/dist/utils/generateParentsMap.js +73 -0
  113. package/dist/utils/getCursorInfo.d.ts +1 -1
  114. package/dist/utils/getCursorInfo.d.ts.map +1 -1
  115. package/dist/utils/getCursorInfo.js +3 -5
  116. package/dist/utils/getOppositeDelimiter.d.ts +2 -1
  117. package/dist/utils/getOppositeDelimiter.d.ts.map +1 -1
  118. package/dist/utils/getOppositeDelimiter.js +6 -5
  119. package/dist/utils/getParent.d.ts +5 -0
  120. package/dist/utils/getParent.d.ts.map +1 -0
  121. package/dist/utils/getParent.js +6 -0
  122. package/dist/utils/getSurroundingErrors.d.ts +3 -4
  123. package/dist/utils/getSurroundingErrors.d.ts.map +1 -1
  124. package/dist/utils/getSurroundingErrors.js +3 -3
  125. package/dist/utils/index.d.ts +4 -0
  126. package/dist/utils/index.d.ts.map +1 -1
  127. package/dist/utils/index.js +8 -0
  128. package/dist/utils/isNode.d.ts +10 -0
  129. package/dist/utils/isNode.d.ts.map +1 -0
  130. package/dist/utils/isNode.js +6 -0
  131. package/dist/utils/isToken.d.ts +8 -0
  132. package/dist/utils/isToken.d.ts.map +1 -0
  133. package/dist/utils/isToken.js +6 -0
  134. package/dist/utils/prettyAst.d.ts +1 -2
  135. package/dist/utils/prettyAst.d.ts.map +1 -1
  136. package/dist/utils/prettyAst.js +19 -26
  137. package/package.json +7 -7
  138. package/src/Lexer.ts +2 -2
  139. package/src/Parser.ts +158 -165
  140. package/src/ast/builders/array.ts +4 -9
  141. package/src/ast/builders/condition.ts +10 -15
  142. package/src/ast/builders/delim.ts +1 -1
  143. package/src/ast/builders/error.ts +10 -6
  144. package/src/ast/builders/expression.ts +17 -23
  145. package/src/ast/builders/group.ts +7 -11
  146. package/src/ast/builders/pos.ts +2 -3
  147. package/src/ast/builders/token.ts +6 -7
  148. package/src/ast/builders/type.ts +1 -1
  149. package/src/ast/builders/variable.ts +6 -8
  150. package/src/ast/createArrayNode.ts +17 -0
  151. package/src/ast/createConditionNode.ts +58 -0
  152. package/src/ast/createExpressionNode.ts +21 -0
  153. package/src/ast/createGroupNode.ts +46 -0
  154. package/src/ast/createNormalizedCondition.ts +12 -0
  155. package/src/ast/createNormalizedExpression.ts +9 -0
  156. package/src/ast/createToken.ts +26 -0
  157. package/src/ast/createVariableNode.ts +18 -0
  158. package/src/ast/error.ts +9 -0
  159. package/src/ast/handlers.ts +26 -37
  160. package/src/ast/index.ts +9 -1
  161. package/src/{helpers/general → defaults}/defaultConditionNormalizer.ts +1 -1
  162. package/src/{helpers/general → defaults}/defaultKeyParser.ts +1 -1
  163. package/src/{helpers/general → defaults}/defaultPrefixApplier.ts +1 -1
  164. package/src/{helpers/general → defaults}/defaultValueComparer.ts +1 -1
  165. package/src/{helpers/general → defaults}/index.ts +0 -2
  166. package/src/index.ts +2 -1
  167. package/src/{helpers/errors.ts → internal/ExpressitError.ts} +2 -2
  168. package/src/{helpers/general → internal}/applyPrefix.ts +1 -1
  169. package/src/{helpers/parser → internal}/checkParserOpts.ts +16 -15
  170. package/src/internal/escapeVariableOrPrefix.ts +36 -0
  171. package/src/{helpers/parser → internal}/extractPosition.ts +2 -2
  172. package/src/{helpers/parser → internal}/getUnclosedRightParenCount.ts +1 -2
  173. package/src/{helpers/parser → internal}/parseParserOptions.ts +5 -5
  174. package/src/types/ast.ts +292 -7
  175. package/src/types/autocomplete.ts +5 -7
  176. package/src/types/parser.ts +17 -22
  177. package/src/utils/extractTokens.ts +16 -20
  178. package/src/utils/generateParentsMap.ts +60 -0
  179. package/src/utils/getCursorInfo.ts +4 -6
  180. package/src/utils/getOppositeDelimiter.ts +11 -10
  181. package/src/utils/getParent.ts +16 -0
  182. package/src/utils/getSurroundingErrors.ts +10 -9
  183. package/src/utils/index.ts +4 -0
  184. package/src/utils/isNode.ts +12 -0
  185. package/src/utils/isToken.ts +10 -0
  186. package/src/utils/prettyAst.ts +20 -17
  187. package/dist/ast/classes/ArrayNode.d.ts +0 -18
  188. package/dist/ast/classes/ArrayNode.d.ts.map +0 -1
  189. package/dist/ast/classes/ArrayNode.js +0 -55
  190. package/dist/ast/classes/Condition.d.ts +0 -13
  191. package/dist/ast/classes/Condition.d.ts.map +0 -1
  192. package/dist/ast/classes/Condition.js +0 -21
  193. package/dist/ast/classes/ConditionNode.d.ts +0 -73
  194. package/dist/ast/classes/ConditionNode.d.ts.map +0 -1
  195. package/dist/ast/classes/ConditionNode.js +0 -101
  196. package/dist/ast/classes/ErrorToken.d.ts +0 -27
  197. package/dist/ast/classes/ErrorToken.d.ts.map +0 -1
  198. package/dist/ast/classes/ErrorToken.js +0 -47
  199. package/dist/ast/classes/Expression.d.ts +0 -13
  200. package/dist/ast/classes/Expression.d.ts.map +0 -1
  201. package/dist/ast/classes/Expression.js +0 -19
  202. package/dist/ast/classes/ExpressionNode.d.ts +0 -21
  203. package/dist/ast/classes/ExpressionNode.d.ts.map +0 -1
  204. package/dist/ast/classes/ExpressionNode.js +0 -57
  205. package/dist/ast/classes/GroupNode.d.ts +0 -64
  206. package/dist/ast/classes/GroupNode.d.ts.map +0 -1
  207. package/dist/ast/classes/GroupNode.js +0 -69
  208. package/dist/ast/classes/Node.d.ts +0 -22
  209. package/dist/ast/classes/Node.d.ts.map +0 -1
  210. package/dist/ast/classes/Node.js +0 -28
  211. package/dist/ast/classes/Token.d.ts +0 -27
  212. package/dist/ast/classes/Token.d.ts.map +0 -1
  213. package/dist/ast/classes/Token.js +0 -28
  214. package/dist/ast/classes/ValidToken.d.ts +0 -26
  215. package/dist/ast/classes/ValidToken.d.ts.map +0 -1
  216. package/dist/ast/classes/ValidToken.js +0 -49
  217. package/dist/ast/classes/VariableNode.d.ts +0 -33
  218. package/dist/ast/classes/VariableNode.d.ts.map +0 -1
  219. package/dist/ast/classes/VariableNode.js +0 -58
  220. package/dist/ast/classes/index.d.ts +0 -12
  221. package/dist/ast/classes/index.d.ts.map +0 -1
  222. package/dist/ast/classes/index.js +0 -24
  223. package/dist/helpers/errors.d.ts.map +0 -1
  224. package/dist/helpers/general/applyBoolean.d.ts.map +0 -1
  225. package/dist/helpers/general/applyPrefix.d.ts.map +0 -1
  226. package/dist/helpers/general/defaultConditionNormalizer.d.ts +0 -3
  227. package/dist/helpers/general/defaultConditionNormalizer.d.ts.map +0 -1
  228. package/dist/helpers/general/defaultKeyParser.d.ts +0 -3
  229. package/dist/helpers/general/defaultKeyParser.d.ts.map +0 -1
  230. package/dist/helpers/general/defaultPrefixApplier.d.ts +0 -3
  231. package/dist/helpers/general/defaultPrefixApplier.d.ts.map +0 -1
  232. package/dist/helpers/general/defaultValueComparer.d.ts +0 -3
  233. package/dist/helpers/general/defaultValueComparer.d.ts.map +0 -1
  234. package/dist/helpers/general/index.d.ts.map +0 -1
  235. package/dist/helpers/index.d.ts +0 -4
  236. package/dist/helpers/index.d.ts.map +0 -1
  237. package/dist/helpers/index.js +0 -8
  238. package/dist/helpers/parser/assignParents.d.ts +0 -4
  239. package/dist/helpers/parser/assignParents.d.ts.map +0 -1
  240. package/dist/helpers/parser/assignParents.js +0 -71
  241. package/dist/helpers/parser/checkParserOpts.d.ts.map +0 -1
  242. package/dist/helpers/parser/extractPosition.d.ts +0 -5
  243. package/dist/helpers/parser/extractPosition.d.ts.map +0 -1
  244. package/dist/helpers/parser/getUnclosedRightParenCount.d.ts +0 -4
  245. package/dist/helpers/parser/getUnclosedRightParenCount.d.ts.map +0 -1
  246. package/dist/helpers/parser/index.d.ts +0 -9
  247. package/dist/helpers/parser/index.d.ts.map +0 -1
  248. package/dist/helpers/parser/index.js +0 -18
  249. package/dist/helpers/parser/parseParserOptions.d.ts.map +0 -1
  250. package/dist/helpers/parser/seal.d.ts +0 -8
  251. package/dist/helpers/parser/seal.d.ts.map +0 -1
  252. package/dist/helpers/parser/seal.js +0 -10
  253. package/dist/helpers/parser/setParent.d.ts +0 -6
  254. package/dist/helpers/parser/setParent.d.ts.map +0 -1
  255. package/dist/helpers/parser/setParent.js +0 -4
  256. package/dist/helpers/parser/unescape.d.ts.map +0 -1
  257. package/src/ast/classes/ArrayNode.ts +0 -46
  258. package/src/ast/classes/Condition.ts +0 -22
  259. package/src/ast/classes/ConditionNode.ts +0 -141
  260. package/src/ast/classes/ErrorToken.ts +0 -49
  261. package/src/ast/classes/Expression.ts +0 -26
  262. package/src/ast/classes/ExpressionNode.ts +0 -62
  263. package/src/ast/classes/GroupNode.ts +0 -127
  264. package/src/ast/classes/Node.ts +0 -47
  265. package/src/ast/classes/Token.ts +0 -59
  266. package/src/ast/classes/ValidToken.ts +0 -56
  267. package/src/ast/classes/VariableNode.ts +0 -67
  268. package/src/ast/classes/index.ts +0 -13
  269. package/src/helpers/index.ts +0 -6
  270. package/src/helpers/parser/assignParents.ts +0 -51
  271. package/src/helpers/parser/index.ts +0 -10
  272. package/src/helpers/parser/seal.ts +0 -14
  273. package/src/helpers/parser/setParent.ts +0 -5
  274. /package/dist/{helpers/general → defaults}/defaultConditionNormalizer.js +0 -0
  275. /package/dist/{helpers/general → defaults}/defaultKeyParser.js +0 -0
  276. /package/dist/{helpers/general → defaults}/defaultPrefixApplier.js +0 -0
  277. /package/dist/{helpers/general → defaults}/defaultValueComparer.js +0 -0
  278. /package/dist/{helpers/general → internal}/applyBoolean.d.ts +0 -0
  279. /package/dist/{helpers/general → internal}/applyBoolean.js +0 -0
  280. /package/dist/{helpers/general → internal}/applyPrefix.js +0 -0
  281. /package/dist/{helpers/parser → internal}/extractPosition.js +0 -0
  282. /package/dist/{helpers/parser → internal}/unescape.d.ts +0 -0
  283. /package/dist/{helpers/parser → internal}/unescape.js +0 -0
  284. /package/src/{helpers/general → internal}/applyBoolean.ts +0 -0
  285. /package/src/{helpers/parser → internal}/unescape.ts +0 -0
@@ -1,10 +1,7 @@
1
- import type { Mutable } from "@alanscodelog/utils/types"
2
-
3
1
  import { token } from "./token.js"
4
2
 
5
- import { AST_TYPE, type Position, TOKEN_TYPE } from "../../types/ast.js"
6
- import { ArrayNode } from "../classes/ArrayNode.js"
7
- import type { VariableNode } from "../classes/VariableNode.js"
3
+ import { type ArrayNode, type FirstParam,type Position, TOKEN_TYPE, type VariableNode } from "../../types/ast.js"
4
+ import { createArrayNode } from "../createArrayNode.js"
8
5
 
9
6
 
10
7
  export function array(
@@ -13,8 +10,7 @@ export function array(
13
10
  parenLeftPos?: Position,
14
11
  parenRightPos?: Position,
15
12
  ): ArrayNode {
16
- const node: Mutable<Partial<ArrayNode>> = {
17
- type: AST_TYPE.ARRAY,
13
+ const node: Partial<FirstParam<typeof createArrayNode>> = {
18
14
  values,
19
15
  }
20
16
 
@@ -40,6 +36,5 @@ export function array(
40
36
  node.start = node.bracket!.left?.start ?? start ?? node.bracket?.right.start
41
37
  node.end = node.bracket?.right?.end ?? end ?? node.bracket!.left.end
42
38
 
43
- const instance = new ArrayNode(node as any)
44
- return instance
39
+ return createArrayNode(node as any)
45
40
  }
@@ -1,19 +1,14 @@
1
1
  import { pos } from "./pos.js"
2
2
 
3
- import { type AnyToken, type Position, type TOKEN_TYPE } from "../../types/ast.js"
4
- import type { ArrayNode } from "../classes/ArrayNode.js"
5
- import { ConditionNode } from "../classes/ConditionNode.js"
6
- import type { ErrorToken } from "../classes/ErrorToken.js"
7
- import type { GroupNode } from "../classes/GroupNode.js"
8
- import type { ValidToken } from "../classes/ValidToken.js"
9
- import type { VariableNode } from "../classes/VariableNode.js"
3
+ import { type AnyToken, type ArrayNode, type ConditionNode, type ErrorToken, type FirstParam, type GroupNode, type Position, type TOKEN_TYPE, type ValidToken, type VariableNode } from "../../types/ast.js"
4
+ import { createConditionNode } from "../createConditionNode.js"
10
5
 
11
6
  /**
12
- * Creates a @see ConditionNode
7
+ * Creates a {@link ConditionNode}
13
8
  *
14
- * @param variable An existing @see VariableNode
9
+ * @param variable An existing {@link VariableNode}
15
10
  *
16
- * @param not Defaults to plain true condition. To negate it you must pass an existing "not" operator @see ValidToken which will be set as the node's operator and set the node's value to false.
11
+ * @param not Defaults to plain true condition. To negate it you must pass an existing "not" operator {@link ValidToken} which will be set as the node's operator and set the node's value to false.
17
12
  *
18
13
  * @param property Set the property for a property operator. A property operator must be passed if this is passed.
19
14
  * @param propertyOperator Set the operator for a property condition. A property must have been passed. The property operator must be a valid token. There is no case where it would not be. If using an operator that is the same as a separator, if used as an operator, should be set as an operator.
@@ -25,9 +20,9 @@ export function condition(
25
20
  | VariableNode
26
21
  | GroupNode
27
22
  | ArrayNode
28
- | ErrorToken<TOKEN_TYPE.VALUE>,
23
+ | ErrorToken,
29
24
  not: true | ValidToken<TOKEN_TYPE.NOT> = true,
30
- property?: VariableNode | ErrorToken<TOKEN_TYPE.VALUE>,
25
+ property?: VariableNode | ErrorToken,
31
26
  propertyOperator?: AnyToken<TOKEN_TYPE.OP_CUSTOM | TOKEN_TYPE.VALUE>,
32
27
  { right, left }: Partial<Record<"right" | "left", ValidToken<TOKEN_TYPE.OP_EXPANDED_SEP>>> = { },
33
28
  ): ConditionNode {
@@ -35,7 +30,7 @@ export function condition(
35
30
  const notStart = not === true ? undefined : not.start
36
31
  position.start = notStart ?? property?.start ?? propertyOperator?.start ?? position.start
37
32
 
38
- const node: Partial<ConstructorParameters<typeof ConditionNode>[0]> = {
33
+ const node: FirstParam<typeof createConditionNode> = {
39
34
  property,
40
35
  value,
41
36
  propertyOperator,
@@ -50,7 +45,7 @@ export function condition(
50
45
  if (right) node.sep.right = right
51
46
  if (left) node.sep.left = left
52
47
  }
53
- const instance = new ConditionNode(node as any)
54
- return instance
48
+
49
+ return createConditionNode(node)
55
50
  }
56
51
 
@@ -3,7 +3,7 @@ import { type } from "./type.js"
3
3
  import { type ExtractTokenType, TOKEN_TYPE } from "../../types/ast.js"
4
4
 
5
5
  /**
6
- * Faster way, more intuitive way to generate the options for matching delimiters (e.g. quotes and parens) for functions like @see variable and @see group .
6
+ * Faster way, more intuitive way to generate the options for matching delimiters (e.g. quotes and parens) for functions like {@link variable} and {@link group} .
7
7
  */
8
8
 
9
9
  export function delim<
@@ -1,16 +1,16 @@
1
1
  import { type } from "./type.js"
2
2
 
3
- import { TOKEN_TYPE } from "../../types/ast.js"
4
- import { ErrorToken } from "../classes/ErrorToken.js"
3
+ import { type ErrorToken,TOKEN_TYPE } from "../../types/ast.js"
4
+ import { createToken } from "../createToken.js"
5
5
 
6
6
 
7
7
  /**
8
8
  * Creates an error token.
9
9
  *
10
- * @param expected Can be passed an array of @see ValidToken_TYPE or strings, in which case it will use @see type internally. So instead of `[TOKEN_TYPE.VALUE]` we can do `[""]` and instead of `[TOKEN_TYPE.PARENR]` we can do `[")"]`.
10
+ * @param expected Can be passed an array of {@link ValidToken_TYPE} or strings, in which case it will use {@link type} internally. So instead of `[TOKEN_TYPE.VALUE]` we can do `[""]` and instead of `[TOKEN_TYPE.PARENR]` we can do `[")"]`.
11
11
  */
12
- export function error<T extends TOKEN_TYPE>(pos: number, expected: T[]): ErrorToken<T>
13
- export function error<T extends TOKEN_TYPE>(pos: number, expected_: T[]): ErrorToken<T> {
12
+ export function error<T extends TOKEN_TYPE>(pos: number, expected: T[]): ErrorToken
13
+ export function error<T extends TOKEN_TYPE>(pos: number, expected_: T[]): ErrorToken {
14
14
  const expected = expected_.map(item => {
15
15
  if (TOKEN_TYPE[item as keyof typeof TOKEN_TYPE] === undefined) {
16
16
  return type(item)
@@ -18,5 +18,9 @@ export function error<T extends TOKEN_TYPE>(pos: number, expected_: T[]): ErrorT
18
18
  return item
19
19
  }
20
20
  }) as T []
21
- return new ErrorToken({ expected, start: pos, end: pos })
21
+ return createToken({
22
+ expected,
23
+ start: pos,
24
+ end: pos
25
+ })
22
26
  }
@@ -1,34 +1,31 @@
1
1
  import { pos } from "./pos.js"
2
2
  import { token } from "./token.js"
3
3
 
4
- import { type AnyToken, type Position, TOKEN_TYPE, type TokenBooleanTypes } from "../../types/ast.js"
5
- import type { ConditionNode } from "../classes/ConditionNode.js"
6
- import type { ErrorToken } from "../classes/ErrorToken.js"
7
- import { ExpressionNode } from "../classes/ExpressionNode.js"
8
- import type { GroupNode } from "../classes/GroupNode.js"
4
+ import { type AnyToken, type ConditionNode, type ErrorToken, type ExpressionNode,type GroupNode, type Position, TOKEN_TYPE, type TokenBooleanTypes } from "../../types/ast.js"
5
+ import { createExpressionNode } from "../createExpressionNode.js"
9
6
 
10
7
 
11
8
  /**
12
- * Creates an @see ExpressionNode, can be passed nothing for any of the tokens to automatically create error tokens.
9
+ * Creates an {@link ExpressionNode}, can be passed nothing for any of the tokens to automatically create error tokens.
13
10
  *
14
11
  * Also automatically sets the correct start/end positions from valid tokens (e.g. for start, searching left to right for a valid token and vice versa) assuming at least one is defined.
15
12
  */
16
13
  export function expression(
17
14
  left:
18
- ConditionNode |
19
- ExpressionNode |
20
- GroupNode |
21
- ErrorToken<TOKEN_TYPE.VALUE> |
22
- undefined,
15
+ ConditionNode |
16
+ ExpressionNode |
17
+ GroupNode |
18
+ ErrorToken |
19
+ undefined,
23
20
  operator:
24
- AnyToken<TokenBooleanTypes> |
25
- undefined,
21
+ AnyToken<TokenBooleanTypes> |
22
+ undefined,
26
23
  right:
27
- ConditionNode |
28
- ExpressionNode |
29
- GroupNode |
30
- ErrorToken<TOKEN_TYPE.VALUE> |
31
- undefined,
24
+ ConditionNode |
25
+ ExpressionNode |
26
+ GroupNode |
27
+ ErrorToken |
28
+ undefined,
32
29
  ): ExpressionNode {
33
30
  const start =
34
31
  left?.start ??
@@ -54,13 +51,10 @@ export function expression(
54
51
  right = token(TOKEN_TYPE.VALUE, undefined, { start: op.end })
55
52
  }
56
53
 
57
- const node: ConstructorParameters<typeof ExpressionNode>[0] = {
54
+ return createExpressionNode({
58
55
  left,
59
56
  operator: op,
60
57
  right,
61
58
  ...position,
62
- }
63
-
64
- const instance = new ExpressionNode(node)
65
- return instance
59
+ })
66
60
  }
@@ -1,18 +1,14 @@
1
1
  import { token } from "./token.js"
2
2
 
3
- import { type Position, TOKEN_TYPE } from "../../types/ast.js"
4
- import type { ConditionNode } from "../classes/ConditionNode.js"
5
- import type { ErrorToken } from "../classes/ErrorToken.js"
6
- import type { ExpressionNode } from "../classes/ExpressionNode.js"
7
- import { GroupNode } from "../classes/GroupNode.js"
8
- import type { ValidToken } from "../classes/ValidToken.js"
3
+ import { type ConditionNode, type ErrorToken, type ExpressionNode, type FirstParam,type GroupNode, type Position, TOKEN_TYPE, type ValidToken } from "../../types/ast.js"
4
+ import { createGroupNode } from "../createGroupNode.js"
9
5
 
10
6
  /**
11
7
  * Creates a group.
12
8
  *
13
9
  * Automatically creates error tokens if missing condition or parens (not prefix because it is optional). Will look at the position of prefix if available.
14
10
  *
15
- * @param paren Use to add parens to group, usually by using @see delim . If either `right` or `left` are false or undefined, error tokens will be created. Unlike @see variable, if undefined is passed for parens, parens are created by default.
11
+ * @param paren Use to add parens to group, usually by using {@link delim} . If either `right` or `left` are false or undefined, error tokens will be created. Unlike {@link variable}, if undefined is passed for parens, parens are created by default.
16
12
  *
17
13
  * The positions of the parens can be adjusted with the last two parameters by passing positions.
18
14
  */
@@ -27,7 +23,7 @@ export function group(
27
23
  | ConditionNode
28
24
  | ExpressionNode
29
25
  | GroupNode
30
- | ErrorToken<TOKEN_TYPE.VALUE>
26
+ | ErrorToken
31
27
  | undefined,
32
28
 
33
29
  paren: { left?: boolean, right?: boolean } = { right: true, left: true },
@@ -38,7 +34,8 @@ export function group(
38
34
  expression = token(TOKEN_TYPE.VALUE, undefined, prefix?.end !== undefined ? { start: prefix.end } : undefined)
39
35
  }
40
36
 
41
- const node: Partial<ConstructorParameters<typeof GroupNode>[0]> = {
37
+ const node: Partial<FirstParam<typeof createGroupNode>> = {
38
+ prefix: undefined,
42
39
  expression,
43
40
  }
44
41
  if (prefix) {
@@ -74,6 +71,5 @@ export function group(
74
71
  node.start = node.prefix?.start ?? node.paren?.left?.start ?? node.expression?.start ?? node.paren?.right.start
75
72
  node.end = node.paren?.right?.end ?? node.expression?.end ?? node.paren?.left.end ?? node.prefix?.end
76
73
 
77
- const instance = new GroupNode(node as any)
78
- return instance
74
+ return createGroupNode(node as any)
79
75
  }
@@ -1,7 +1,6 @@
1
1
  import { isFullPos } from "./isFullPos.js"
2
2
 
3
- import type { AnyToken, AST_TYPE, EmptyObj, Position, TOKEN_TYPE } from "../../types/ast.js"
4
- import type { Node } from "../classes/Node.js"
3
+ import type { AnyToken, AST_TYPE, EmptyObj, Node, Position, TOKEN_TYPE } from "../../types/ast.js"
5
4
 
6
5
  /**
7
6
  * Can either:
@@ -20,7 +19,7 @@ export function pos<TItem extends Position | Partial<Position> | EmptyObj>(
20
19
  item: TItem,
21
20
  opts?: undefined | { fill: false }
22
21
  ):
23
- // not sure why this needs the AnyToken check, prob something to do with using classes
22
+ // not sure why this needs the AnyToken check
24
23
  TItem extends Position
25
24
  ? Position
26
25
  : TItem extends AnyToken<TOKEN_TYPE> | Position | EmptyObj
@@ -2,13 +2,12 @@ import { isArray } from "@alanscodelog/utils/isArray.js"
2
2
 
3
3
  import { pos } from "./pos.js"
4
4
 
5
- import type { AnyToken, EmptyObj, Position, TOKEN_TYPE } from "../../types/ast.js"
6
- import { ErrorToken } from "../classes/ErrorToken.js"
7
- import { ValidToken } from "../classes/ValidToken.js"
5
+ import type { AnyToken, EmptyObj, ErrorToken, Position, TOKEN_TYPE, ValidToken } from "../../types/ast.js"
6
+ import { createToken } from "../createToken.js"
8
7
 
9
8
 
10
9
  /**
11
- * Creates a @see ValidToken or of the given type with the given value. If no value is given, creates an @see ErrorToken instead.
10
+ * Creates a {@link ValidToken} or of the given type with the given value. If no value is given, creates an {@link ErrorToken} instead.
12
11
  *
13
12
  * Can be passed multiple types when creating an error token to set the expected field.
14
13
  *
@@ -23,20 +22,20 @@ export function token<
23
22
  value: TValue,
24
23
  position: Position | Partial<Position> | EmptyObj = {},
25
24
  ): TValue extends undefined
26
- ? ErrorToken<TType>
25
+ ? ErrorToken
27
26
  : ValidToken<TType> {
28
27
  position = pos(position, { fill: true })
29
28
  // eslint-disable-next-line @typescript-eslint/no-shadow
30
29
  let token: AnyToken<TType>
31
30
  if (value !== undefined) {
32
- token = new ValidToken<TType>({
31
+ token = createToken({
33
32
  type: type as TType,
34
33
  value,
35
34
  start: position.start!,
36
35
  end: position.end!,
37
36
  })
38
37
  } else {
39
- token = new ErrorToken<TType>({
38
+ token = createToken({
40
39
  expected: (isArray(type) ? type : [type]) as TType[],
41
40
  start: position.start!,
42
41
  end: position.end!,
@@ -1,6 +1,6 @@
1
1
  import { type ExtractTokenType, TOKEN_TYPE } from "../../types/ast.js"
2
2
  /**
3
- * Given a the string value of an operator or single delimiter token, returns the corresponding @see ValidToken_TYPE .
3
+ * Given a the string value of an operator or single delimiter token, returns the corresponding {@link ValidToken_TYPE} .
4
4
  */
5
5
  export function type<T extends string>(
6
6
  operatorSymbol: T,
@@ -1,15 +1,14 @@
1
1
  import { pos } from "./pos.js"
2
2
  import { token } from "./token.js"
3
3
 
4
- import { type AnyToken, type EmptyObj, type Position, TOKEN_TYPE, type TokenQuoteTypes } from "../../types/ast.js"
5
- import type { ValidToken } from "../classes/ValidToken.js"
6
- import { VariableNode } from "../classes/VariableNode.js"
4
+ import { type AnyToken, type EmptyObj,type FirstParam, type Position, TOKEN_TYPE, type TokenQuoteTypes, type ValidToken, type VariableNode } from "../../types/ast.js"
5
+ import { createVariableNode } from "../createVariableNode.js"
7
6
 
8
7
 
9
8
  /**
10
- * Creates an @see VariableNode .
9
+ * Creates an {@link VariableNode} .
11
10
  *
12
- * @param quote Use to add quotes to the variable, usually by using @see delim . If false or undefined, no quotes are added. Otherwise at least `type` must be passed. If one side is defined but not the other, error tokens will be created with the given type.
11
+ * @param quote Use to add quotes to the variable, usually by using {@link delim} . If false or undefined, no quotes are added. Otherwise at least `type` must be passed. If one side is defined but not the other, error tokens will be created with the given type.
13
12
  *
14
13
  * @param position Refers to position of the value (in case it needs to be created) not the node. Quote positions are automatically calculated from this if used. If value is already a token, there's no need to pass a position, it can be extracted from the token.
15
14
  */
@@ -26,7 +25,7 @@ export function variable(
26
25
 
27
26
  position = pos(value)
28
27
 
29
- const node: Partial<ConstructorParameters<typeof VariableNode>[0]> = {
28
+ const node: Partial<FirstParam<typeof createVariableNode>> = {
30
29
  value,
31
30
  prefix,
32
31
  }
@@ -74,8 +73,7 @@ export function variable(
74
73
  node.value?.end ??
75
74
  node.quote?.left.end
76
75
 
77
- const instance = new VariableNode(node as any)
78
- return instance
76
+ return createVariableNode(node as any)
79
77
  }
80
78
 
81
79
  function quoteFromType(type: TokenQuoteTypes | undefined): string {
@@ -0,0 +1,17 @@
1
+ import { type ArrayNode, AST_TYPE, type RawNode } from "../types/ast.js"
2
+
3
+
4
+ export function createArrayNode<TValid extends boolean = boolean>(
5
+ raw: RawNode<ArrayNode<TValid>>
6
+ ): ArrayNode<TValid> {
7
+ return {
8
+ type: AST_TYPE.ARRAY,
9
+ isNode: true,
10
+ valid: (
11
+ raw.values.every(val => val.valid) &&
12
+ raw.bracket.left.valid &&
13
+ raw.bracket.right.valid
14
+ ) as TValid,
15
+ ...raw
16
+ }
17
+ }
@@ -0,0 +1,58 @@
1
+ import { AST_TYPE, type ConditionNode, type TOKEN_TYPE, type ValidToken } from "../types/ast.js"
2
+ import { isNode } from "../utils/isNode.js"
3
+
4
+
5
+ export function createConditionNode<
6
+ TValid extends boolean = boolean,
7
+ TOperator extends
8
+ ValidToken<TOKEN_TYPE.NOT> | undefined =
9
+ ValidToken<TOKEN_TYPE.NOT> | undefined,
10
+ >(raw: {
11
+ operator?: TOperator
12
+ property?: ConditionNode<TValid>["property"]
13
+ propertyOperator?: ConditionNode<TValid>["propertyOperator"]
14
+ sep?: ConditionNode<TValid>["sep"]
15
+ value: ConditionNode<TValid>["value"]
16
+ start: number
17
+ end: number
18
+ }
19
+ ): ConditionNode<TValid> {
20
+ const valid = (
21
+ (
22
+ raw.operator === undefined
23
+ || raw.operator?.valid
24
+ )
25
+ && isNode(raw.value)
26
+ && raw.value.valid
27
+ )
28
+ && (
29
+ (
30
+ raw.property === undefined &&
31
+ raw.propertyOperator === undefined &&
32
+ raw.sep === undefined
33
+ ) || (
34
+ raw.property !== undefined
35
+ && "type" in raw.property
36
+ && raw.property.type === AST_TYPE.VARIABLE
37
+ && (
38
+ raw.propertyOperator?.valid === true
39
+ || (
40
+ raw.sep?.left?.valid === true
41
+ && (
42
+ raw.sep.right === undefined
43
+ || raw.sep?.right.valid
44
+ )
45
+ )
46
+ )
47
+ )
48
+ )
49
+ return {
50
+ property: undefined,
51
+ propertyOperator: undefined,
52
+ sep: undefined,
53
+ ...raw,
54
+ isNode: true,
55
+ type: AST_TYPE.CONDITION,
56
+ valid
57
+ }
58
+ }
@@ -0,0 +1,21 @@
1
+ import { AST_TYPE, type ExpressionNode,type RawNode } from "../types/ast.js"
2
+ import { isNode } from "../utils/isNode.js"
3
+
4
+
5
+ export function createExpressionNode<TValid extends boolean = boolean>(
6
+ raw: RawNode< ExpressionNode<TValid>>
7
+ ): ExpressionNode<TValid> {
8
+ const valid = (
9
+ raw.operator.valid &&
10
+ isNode(raw.left) &&
11
+ raw.left.valid &&
12
+ isNode(raw.right) &&
13
+ raw.right.valid
14
+ )
15
+ return {
16
+ ...raw,
17
+ isNode: true,
18
+ valid,
19
+ type: AST_TYPE.EXPRESSION,
20
+ }
21
+ }
@@ -0,0 +1,46 @@
1
+ import { AST_TYPE, type ConditionNode, type GroupNode, type RawNode, type TOKEN_TYPE, type ValidToken } from "../types/ast.js"
2
+ import { isNode } from "../utils/isNode.js"
3
+ import { isToken } from "../utils/isToken.js"
4
+
5
+
6
+ export function createGroupNode<
7
+ TValid extends boolean = boolean,
8
+ TPrefixable extends boolean = true,
9
+ TPrefix extends
10
+ TPrefixable extends true
11
+ ? ConditionNode<TValid> |
12
+ ValidToken<TOKEN_TYPE.NOT> |
13
+ undefined
14
+ : ValidToken<TOKEN_TYPE.NOT> |
15
+ undefined =
16
+ TPrefixable extends true
17
+ ? ConditionNode<TValid> |
18
+ ValidToken<TOKEN_TYPE.NOT> |
19
+ undefined
20
+ : ValidToken<TOKEN_TYPE.NOT>,
21
+ >(raw: {
22
+ prefix: TPrefix
23
+ } & RawNode<GroupNode<TValid>>): GroupNode<TValid, TPrefixable, TPrefix> {
24
+ return {
25
+ ...raw,
26
+ isNode: true,
27
+ valid: (
28
+ (
29
+ raw.prefix === undefined
30
+ || (isToken(raw.prefix) && raw.prefix.valid)
31
+ || (isNode(raw.prefix) && raw.prefix.valid)
32
+ )
33
+ && isNode(raw.expression)
34
+ && raw.expression.valid
35
+ && (
36
+ raw.paren === undefined
37
+ || (
38
+ raw.paren.left.valid
39
+ && raw.paren.right.valid
40
+ )
41
+ )
42
+ ),
43
+ type: AST_TYPE.GROUP,
44
+ }
45
+ }
46
+
@@ -0,0 +1,12 @@
1
+ import { AST_TYPE, type NormalizedCondition } from "../types/ast.js"
2
+
3
+
4
+ export function createCondition<
5
+ TOp extends string = string,
6
+ TValue = any,
7
+ >(raw: Omit<NormalizedCondition<TOp, TValue>, "type">): NormalizedCondition<TOp, TValue> {
8
+ return {
9
+ ...raw,
10
+ type: AST_TYPE.NORMALIZED_CONDITION,
11
+ }
12
+ }
@@ -0,0 +1,9 @@
1
+ import { AST_TYPE, type NormalizedExpression } from "../types/ast.js"
2
+
3
+
4
+ export function createExpression<TType extends string = string, TValue = any>(raw: Omit<NormalizedExpression<TType, TValue>, "type">): NormalizedExpression<TType, TValue> {
5
+ return {
6
+ ...raw,
7
+ type: AST_TYPE.NORMALIZED_EXPRESSION
8
+ }
9
+ }
@@ -0,0 +1,26 @@
1
+ import { ExpressitError } from "../internal/ExpressitError.js"
2
+ import type { ErrorToken, RawToken, TOKEN_TYPE, ValidToken } from "../types/ast.js"
3
+ import { ERROR_CODES } from "../types/errors.js"
4
+
5
+ export function createToken<
6
+ TType extends TOKEN_TYPE,
7
+ >(raw: { type: TType } & RawToken<ValidToken>): ValidToken<TType>
8
+ export function createToken(raw: RawToken<ErrorToken>): ErrorToken
9
+ export function createToken<
10
+ TValid extends boolean = boolean,
11
+ TType extends
12
+ TValid extends true ? TOKEN_TYPE : never =
13
+ TValid extends true ? TOKEN_TYPE : never,
14
+ >(raw: RawToken<ValidToken> | RawToken<ErrorToken>): TValid extends true
15
+ ? ValidToken<TType>
16
+ : ErrorToken {
17
+ if (raw.start === undefined || raw.end === undefined) {
18
+ throw new ExpressitError(ERROR_CODES.PARSER_POSITION_ERROR, raw)
19
+ }
20
+ return {
21
+ ...raw,
22
+ valid: typeof (raw as any).expected === "undefined",
23
+ isToken: true,
24
+ } as any
25
+ }
26
+
@@ -0,0 +1,18 @@
1
+ import { AST_TYPE, type RawNode, type VariableNode } from "../types/ast.js"
2
+
3
+ export function createVariableNode<
4
+ TValid extends boolean = boolean,
5
+ >(raw: RawNode<VariableNode<TValid>>): VariableNode<TValid> {
6
+ return {
7
+ ...raw,
8
+ isNode: true,
9
+ valid: (raw.value.valid && (
10
+ raw.quote === undefined ||
11
+ (
12
+ raw.quote.left.valid &&
13
+ raw.quote.right.valid
14
+ )
15
+ )) as TValid,
16
+ type: AST_TYPE.VARIABLE,
17
+ }
18
+ }
@@ -0,0 +1,9 @@
1
+ import { createToken } from "./createToken.js"
2
+
3
+ import { type ErrorToken,type TOKEN_TYPE } from "../types/ast.js"
4
+
5
+ export function error<T extends TOKEN_TYPE>(pos: number, expected: T[]): ErrorToken {
6
+ if (pos === undefined) throw new Error("should never happen, passed undefined position for error token")
7
+ return createToken({ expected, start: pos, end: pos })
8
+ }
9
+