@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,39 +1,38 @@
1
1
  /* eslint-disable @typescript-eslint/no-shadow */
2
2
 
3
- import { ArrayNode } from "./classes/ArrayNode.js"
4
- import { ConditionNode } from "./classes/ConditionNode.js"
5
- import { ErrorToken } from "./classes/ErrorToken.js"
6
- import { ExpressionNode } from "./classes/ExpressionNode.js"
7
- import { GroupNode } from "./classes/GroupNode.js"
8
- import { ValidToken } from "./classes/ValidToken.js"
9
- import { VariableNode } from "./classes/VariableNode.js"
3
+ import { createArrayNode } from "./createArrayNode.js"
4
+ import { createConditionNode } from "./createConditionNode.js"
5
+ import { createExpressionNode } from "./createExpressionNode.js"
6
+ import { createGroupNode } from "./createGroupNode.js"
7
+ import { createToken } from "./createToken.js"
8
+ import { createVariableNode } from "./createVariableNode.js"
10
9
 
11
- import { type AnyToken, type FirstConstructorParam, type Position, TOKEN_TYPE, type TokenBooleanTypes, type TokenDelimiterTypes, type TokenOperatorTypes, type TokenQuoteTypes } from "../types/ast.js"
10
+ import { type AnyToken, type ArrayNode,AST_TYPE,type ConditionNode, type ErrorToken, type ExpressionNode,type FirstParam,type GroupNode, type Position, TOKEN_TYPE, type TokenBooleanTypes, type TokenDelimiterTypes, type TokenOperatorTypes, type TokenQuoteTypes, type ValidToken, type VariableNode } from "../types/ast.js"
12
11
 
13
12
 
14
13
  /* #region HELPERS */
15
- function error<T extends TOKEN_TYPE>(pos: number, expected: T[]): ErrorToken<T> {
14
+ function error<T extends TOKEN_TYPE>(pos: number, expected: T[]): ErrorToken {
16
15
  if (pos === undefined) throw new Error("should never happen, passed undefined position for error token")
17
- return new ErrorToken({ expected, start: pos, end: pos })
16
+ return createToken({ expected, start: pos, end: pos })
18
17
  }
19
18
  /* #regionend */
20
19
 
21
20
  /* #region TOKENS */
22
21
  const operators = <T extends TokenOperatorTypes>
23
22
  (type: T) =>
24
- (value: string, pos: Position): ValidToken<T> => new ValidToken({ value, type, ...pos })
23
+ (value: string, pos: Position): ValidToken<T> => createToken({ value, type, ...pos })
25
24
 
26
25
  const delimiters = <T extends TokenDelimiterTypes>
27
26
  (type: T) =>
28
27
  (value: string | undefined, pos: Position): ValidToken<T> | undefined =>
29
28
  // check must be falsy, we want to return undefined when given ""
30
- value ? new ValidToken({ value, type, ...pos }) : undefined
29
+ value ? createToken({ value, type, ...pos }) : undefined
31
30
 
32
- const maybeToken = <T extends TOKEN_TYPE> (type: T) => <TVal extends string | undefined> (value: TVal, pos: Position): TVal extends string ? ValidToken<T> : ErrorToken<T> => {
31
+ const maybeToken = <T extends TOKEN_TYPE> (type: T) => <TVal extends string | undefined> (value: TVal, pos: Position): TVal extends string ? ValidToken<T> : ErrorToken => {
33
32
  if (value === undefined) {
34
33
  return error(pos.end, [type]) as any
35
34
  } else {
36
- return new ValidToken({ value, type, ...pos }) as any
35
+ return createToken({ value, type, ...pos }) as any
37
36
  }
38
37
  }
39
38
 
@@ -70,7 +69,7 @@ export function variable(
70
69
  quoteR: AnyToken<TokenQuoteTypes> | null | undefined,
71
70
  flags?: ValidToken<TOKEN_TYPE.VALUE>,
72
71
  ): VariableNode {
73
- const node: FirstConstructorParam<typeof VariableNode> = {
72
+ const node: FirstParam<typeof createVariableNode> = {
74
73
  prefix: prefix ?? undefined,
75
74
  value: value ?? error((prefix?.end ?? quoteL?.end ?? quoteR?.start)!, [TOKEN_TYPE.VALUE]),
76
75
  start: (prefix?.start ?? quoteL?.start ?? value?.start ?? quoteR?.start)!,
@@ -79,17 +78,16 @@ export function variable(
79
78
 
80
79
  if (quoteL || quoteR) {
81
80
  node.quote = {
82
- left: quoteL ?? error(node.value.start, [(quoteR!.type)]),
83
- right: quoteR ?? error(node.value.end, [(quoteL!.type)]),
81
+ left: quoteL ?? error(node.value.start, [(quoteR!.type!)]),
82
+ right: quoteR ?? error(node.value.end, [(quoteL!.type!)]),
84
83
  }
85
84
  if (flags) {
86
85
  node.quote.flags = flags
87
- node.end = node.quote?.flags.end
86
+ node.end = node.quote?.flags?.end ?? node.end
88
87
  }
89
88
  }
90
89
 
91
- const instance = new VariableNode(node as any)
92
- return instance
90
+ return createVariableNode(node)
93
91
  }
94
92
 
95
93
 
@@ -105,7 +103,7 @@ export function condition(
105
103
  ): ConditionNode {
106
104
  const start = (not?.start ?? property?.start ?? sepL?.start ?? propertyOperator?.start ?? sepR?.start ?? value?.start)!
107
105
  const end = (value?.end ?? sepR?.end ?? propertyOperator?.end ?? sepL?.end ?? property?.end ?? not?.end)!
108
- const node: FirstConstructorParam<typeof ConditionNode> = {
106
+ const node: FirstParam<typeof createConditionNode> = {
109
107
  value: value ? value : error(end, [TOKEN_TYPE.VALUE]),
110
108
  start,
111
109
  end,
@@ -121,15 +119,13 @@ export function condition(
121
119
  node.propertyOperator ||= error(sepL?.end ?? sepR?.start, [TOKEN_TYPE.VALUE])
122
120
  }
123
121
  if (sepR) node.sep.right = sepR
124
- else if (!node.value || node.value instanceof VariableNode) {
122
+ else if (!node.value || node.value.type === AST_TYPE.VARIABLE) {
125
123
  node.sep.right = error(node.value?.start ?? end, [TOKEN_TYPE.OP_EXPANDED_SEP])
126
124
  }
127
125
  } else if (propertyOperator) {
128
126
  node.property ||= error(propertyOperator.start, [TOKEN_TYPE.VALUE])
129
127
  }
130
-
131
- const instance = new ConditionNode(node as any)
132
- return instance
128
+ return createConditionNode(node as ConditionNode)
133
129
  }
134
130
 
135
131
  export function expression(
@@ -137,14 +133,13 @@ export function expression(
137
133
  operator: ValidToken<TokenBooleanTypes> | null | undefined,
138
134
  right: ConditionNode | GroupNode | null | undefined,
139
135
  ): ExpressionNode {
140
- const instance = new ExpressionNode({
136
+ return createExpressionNode({
141
137
  left: left ?? error((operator?.start ?? right?.start)!, [TOKEN_TYPE.VALUE]),
142
138
  operator: operator ?? error((left?.end ?? right?.start)!, [TOKEN_TYPE.AND, TOKEN_TYPE.OR]),
143
139
  right: right ?? error((operator?.end ?? left?.end)!, [TOKEN_TYPE.VALUE]),
144
140
  start: (left?.start ?? operator?.start ?? right?.start)!,
145
141
  end: (right?.end ?? operator?.end ?? left?.end)!,
146
142
  })
147
- return instance
148
143
  }
149
144
 
150
145
  export function group(
@@ -154,7 +149,7 @@ export function group(
154
149
  condition: GroupNode["expression"],
155
150
  parenR: ValidToken<TOKEN_TYPE.PARENR> | null | undefined,
156
151
  ): GroupNode {
157
- const node: FirstConstructorParam<typeof GroupNode> = {
152
+ return createGroupNode({
158
153
  prefix: prefix ?? operator ?? undefined,
159
154
  expression: condition ?? error((parenL?.end ?? parenR?.start)!, [TOKEN_TYPE.VALUE]),
160
155
  paren: {
@@ -163,10 +158,7 @@ export function group(
163
158
  },
164
159
  start: (prefix?.start ?? operator?.start ?? parenL?.start ?? condition?.start ?? parenR?.start)!,
165
160
  end: (parenR?.end ?? condition?.end ?? parenL?.end ?? operator?.end ?? prefix?.end)!,
166
- }
167
-
168
- const instance = new GroupNode(node as any)
169
- return instance
161
+ })
170
162
  }
171
163
 
172
164
  export function array(
@@ -174,7 +166,7 @@ export function array(
174
166
  values: VariableNode[],
175
167
  bracketR: ValidToken<TOKEN_TYPE.BRACKETR> | null | undefined,
176
168
  ): ArrayNode {
177
- const node: FirstConstructorParam<typeof ArrayNode> = {
169
+ return createArrayNode({
178
170
  values,
179
171
  bracket: {
180
172
  left: bracketL, // always valid for now
@@ -182,9 +174,6 @@ export function array(
182
174
  },
183
175
  start: bracketL.start,
184
176
  end: (bracketR?.end ?? values[values.length - 1]?.end ?? bracketL.end)!,
185
- }
186
-
187
- const instance = new ArrayNode(node as any)
188
- return instance
177
+ })
189
178
  }
190
179
  /* #regionend */
package/src/ast/index.ts CHANGED
@@ -1,5 +1,13 @@
1
1
  export * as builders from "./builders/index.js"
2
- export * as classes from "./classes/index.js"
3
2
  /* Autogenerated Index */
4
3
 
4
+ export { createArrayNode } from "./createArrayNode.js"
5
+ export { createCondition } from "./createNormalizedCondition.js"
6
+ export { createConditionNode } from "./createConditionNode.js"
7
+ export { createExpression } from "./createNormalizedExpression.js"
8
+ export { createExpressionNode } from "./createExpressionNode.js"
9
+ export { createGroupNode } from "./createGroupNode.js"
10
+ export { createToken } from "./createToken.js"
11
+ export { createVariableNode } from "./createVariableNode.js"
12
+ export { error } from "./error.js"
5
13
  export { token } from "./handlers.js"
@@ -1,4 +1,4 @@
1
- import type { ConditionNormalizer, ValueQuery } from "../../types/parser.js"
1
+ import type { ConditionNormalizer, ValueQuery } from "../types/parser.js"
2
2
 
3
3
 
4
4
  export const defaultConditionNormalizer: ConditionNormalizer =
@@ -1,4 +1,4 @@
1
- import type { KeyParser } from "../../types/parser.js"
1
+ import type { KeyParser } from "../types/parser.js"
2
2
 
3
3
 
4
4
  export const defaultKeyParser: KeyParser =
@@ -1,4 +1,4 @@
1
- import type { PrefixApplier } from "../../types/parser.js"
1
+ import type { PrefixApplier } from "../types/parser.js"
2
2
 
3
3
 
4
4
  export const defaultPrefixApplier: PrefixApplier =
@@ -1,4 +1,4 @@
1
- import type { ValueComparer } from "../../types/parser.js"
1
+ import type { ValueComparer } from "../types/parser.js"
2
2
 
3
3
 
4
4
  export const defaultValueComparer: ValueComparer =
@@ -1,7 +1,5 @@
1
1
  /* Autogenerated Index [Ignore] */
2
2
 
3
- export { applyBoolean } from "./applyBoolean.js"
4
- export { applyPrefix } from "./applyPrefix.js"
5
3
  export { defaultConditionNormalizer } from "./defaultConditionNormalizer.js"
6
4
  export { defaultKeyParser } from "./defaultKeyParser.js"
7
5
  export { defaultPrefixApplier } from "./defaultPrefixApplier.js"
package/src/index.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  /* Manually Generated Index */
2
2
 
3
- export * as ast from "./ast/classes/index.js"
3
+ export * as ast from "./ast/index.js"
4
4
  export { Parser } from "./Parser.js"
5
5
  export * from "./types/index.js"
6
6
  export * as utils from "./utils/index.js"
7
+ export * as defaults from "./defaults/index.js"
7
8
 
@@ -7,8 +7,8 @@ import type { Keys } from "@alanscodelog/utils/types"
7
7
  import { repository, version } from "../package.js"
8
8
  import type { ERROR_CODES, ErrorInfo } from "../types/errors.js"
9
9
 
10
-
11
- export class BooleanParserLibraryError<T extends ERROR_CODES> extends Error {
10
+ /** @internal */
11
+ export class ExpressitError<T extends ERROR_CODES> extends Error {
12
12
  version: string = version
13
13
 
14
14
  repo: string = repository
@@ -1,4 +1,4 @@
1
- import type { PrefixApplier } from "../../types/parser.js"
1
+ import type { PrefixApplier } from "../types/parser.js"
2
2
 
3
3
  /** @internal */
4
4
  export function applyPrefix(left: string | undefined, right: string, prefixApplier: PrefixApplier): string {
@@ -1,13 +1,14 @@
1
1
  import { isBlank } from "@alanscodelog/utils/isBlank.js"
2
2
  import { pushIfNotIn } from "@alanscodelog/utils/pushIfNotIn.js"
3
3
 
4
- import { ERROR_CODES } from "../../types/errors.js"
5
- import type { FullParserOptions, ParserOptions } from "../../types/parser.js"
6
- import { BooleanParserLibraryError } from "../errors.js"
7
- import { defaultConditionNormalizer } from "../general/defaultConditionNormalizer.js"
8
- import { defaultValueComparer } from "../general/defaultValueComparer.js"
4
+ import { ExpressitError } from "./ExpressitError.js"
9
5
 
6
+ import { defaultConditionNormalizer } from "../defaults/defaultConditionNormalizer.js"
7
+ import { defaultValueComparer } from "../defaults/defaultValueComparer.js"
8
+ import { ERROR_CODES } from "../types/errors.js"
9
+ import type { FullParserOptions, ParserOptions } from "../types/parser.js"
10
10
 
11
+ /** @internal */
11
12
  export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evaluatorChecks: boolean = false, validatorChecks: boolean = false): void {
12
13
  if (!evaluatorChecks) {
13
14
  const keywordsList = [...opts.keywords.and, ...opts.keywords.or, ...opts.keywords.not].map(keyword => keyword.value)
@@ -25,7 +26,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
25
26
 
26
27
  /* #region Blank Operator Checks */
27
28
  if (opts.expandedPropertySeparator && isBlank(opts.expandedPropertySeparator)) {
28
- throw new BooleanParserLibraryError(
29
+ throw new ExpressitError(
29
30
  ERROR_CODES.PARSER_CONFLICTING_OPTIONS_ERROR,
30
31
  { prohibited: [""], invalid: opts.expandedPropertySeparator },
31
32
  `expandedPropertySeparator cannot be blank`,
@@ -33,7 +34,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
33
34
  }
34
35
  const customInvalid = opts.customPropertyOperators?.find(_ => isBlank(_))
35
36
  if (customInvalid !== undefined) {
36
- throw new BooleanParserLibraryError(
37
+ throw new ExpressitError(
37
38
  ERROR_CODES.PARSER_CONFLICTING_OPTIONS_ERROR,
38
39
  { prohibited: [""], invalid: customInvalid },
39
40
  `customPropertyOperators cannot contain blank entries`,
@@ -41,7 +42,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
41
42
  }
42
43
  const prefixInvalid = opts.prefixableStrings?.find(_ => isBlank(_))
43
44
  if (prefixInvalid !== undefined) {
44
- throw new BooleanParserLibraryError(
45
+ throw new ExpressitError(
45
46
  ERROR_CODES.PARSER_CONFLICTING_OPTIONS_ERROR,
46
47
  { prohibited: [""], invalid: prefixInvalid },
47
48
  `prefixableStrings cannot contain blank entries`,
@@ -52,7 +53,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
52
53
  ?.find(_ => isBlank(_.value))
53
54
  ?.value
54
55
  if (invalid !== undefined) {
55
- throw new BooleanParserLibraryError(
56
+ throw new ExpressitError(
56
57
  ERROR_CODES.PARSER_CONFLICTING_OPTIONS_ERROR,
57
58
  { prohibited: [""], invalid },
58
59
  `keywords.${key} cannot contain entries with blank values`,
@@ -75,7 +76,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
75
76
 
76
77
  const invalidPrefixableString = opts.prefixableStrings?.find(val => all.includes(val))
77
78
  if (invalidPrefixableString) {
78
- throw new BooleanParserLibraryError(
79
+ throw new ExpressitError(
79
80
  ERROR_CODES.PARSER_CONFLICTING_OPTIONS_ERROR,
80
81
  { prohibited: all, invalid: invalidPrefixableString },
81
82
  `prefixableStrings ${messageInvalidAny} "${invalidPrefixableString}"`,
@@ -85,7 +86,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
85
86
  const invalidExpandedPropertySeparator = allKeywords
86
87
  .find(_ => _ === opts.expandedPropertySeparator as any)
87
88
  if (invalidExpandedPropertySeparator) {
88
- throw new BooleanParserLibraryError(
89
+ throw new ExpressitError(
89
90
  ERROR_CODES.PARSER_CONFLICTING_OPTIONS_ERROR,
90
91
  { prohibited: allKeywords, invalid: invalidExpandedPropertySeparator },
91
92
  `expandedPropertySeparator ${messageInvalidBool} "${invalidExpandedPropertySeparator}"`,
@@ -96,7 +97,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
96
97
  ? opts.expandedPropertySeparator
97
98
  : undefined
98
99
  if (invalidCustomPropertyOperator) {
99
- throw new BooleanParserLibraryError(
100
+ throw new ExpressitError(
100
101
  ERROR_CODES.PARSER_CONFLICTING_OPTIONS_ERROR,
101
102
  { prohibited: keywords, invalid: invalidCustomPropertyOperator },
102
103
  `customPropertyOperator ${messageInvalidBool} "${invalidCustomPropertyOperator}"`,
@@ -113,7 +114,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
113
114
  if (opts.regexValues) requireCustomNormalizer.push("regexValues")
114
115
 
115
116
  if (requireCustomNormalizer.length > 0 && opts.conditionNormalizer === defaultConditionNormalizer) {
116
- throw new BooleanParserLibraryError(
117
+ throw new ExpressitError(
117
118
  ERROR_CODES.PARSER_OPTION_REQUIRED_ERROR,
118
119
  { options: requireCustomNormalizer, requires: "conditionNormalizer" },
119
120
  `A custom conditionNormalizer function must be specified when using the following options: ${requireCustomNormalizer.join(", ")}`,
@@ -124,7 +125,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
124
125
  if (opts.regexValues) requireCustomComparer.push("arrayValues")
125
126
 
126
127
  if (requireCustomComparer.length > 0 && opts.valueComparer === defaultValueComparer) {
127
- throw new BooleanParserLibraryError(
128
+ throw new ExpressitError(
128
129
  ERROR_CODES.PARSER_OPTION_REQUIRED_ERROR,
129
130
  { options: requireCustomComparer, requires: "valueComparer" },
130
131
  `A custom valueComparer function must be specified when using the following options: ${requireCustomComparer.join(", ")}`,
@@ -133,7 +134,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
133
134
  }
134
135
  if (validatorChecks) {
135
136
  if (opts.valueValidator === undefined) {
136
- throw new BooleanParserLibraryError(
137
+ throw new ExpressitError(
137
138
  ERROR_CODES.PARSER_OPTION_REQUIRED_ERROR,
138
139
  { requires: "valueValidator" },
139
140
  `A custom valueValidator function must be specified when using the validate method.`,
@@ -0,0 +1,36 @@
1
+ import { multisplice } from "@alanscodelog/utils/multisplice.js"
2
+
3
+ export function escapeVariableOrPrefix(variable: string, preferredQuote: string): string {
4
+ let doQuote = false
5
+ for (const quoteType of ["\"", "'", "`"]) {
6
+ if (!variable.includes(quoteType) && !variable.includes(" ")) continue
7
+ if (variable.startsWith(quoteType)
8
+ && variable.endsWith(quoteType)) {
9
+ break
10
+ }
11
+ const indexes: number[] = []
12
+ for (let i = 0; i < variable.length; i++) {
13
+ const char = variable[i]
14
+ if (char === undefined) break
15
+ if (char === "\\") {
16
+ i += 2
17
+ continue
18
+ }
19
+ if (char === " ") {
20
+ doQuote = true
21
+ }
22
+ if (char === quoteType) indexes.push(i)
23
+ }
24
+
25
+ if (indexes.length === 0) break
26
+
27
+ const newVal = multisplice(variable.split(""), indexes, 0, "\\").array.join("")
28
+ variable = newVal
29
+
30
+ break
31
+ }
32
+ if (doQuote) {
33
+ variable = `${preferredQuote}${variable}${preferredQuote}`
34
+ }
35
+ return variable
36
+ }
@@ -1,5 +1,5 @@
1
- import type { Token } from "../../Lexer.js"
2
- import type { Position } from "../../types/ast.js"
1
+ import type { Token } from "../Lexer.js"
2
+ import type { Position } from "../types/ast.js"
3
3
 
4
4
 
5
5
  /** @internal */
@@ -1,9 +1,8 @@
1
- import { $T, type Token } from "../../Lexer.js"
1
+ import { $T, type Token } from "../Lexer.js"
2
2
 
3
3
 
4
4
  /** @internal */
5
5
 
6
-
7
6
  export function getUnclosedRightParenCount(tokens: Token[]): number {
8
7
  let open = 0
9
8
  let unclosed = 0
@@ -1,8 +1,8 @@
1
- import type { FullParserOptions, ParserOptions } from "../../types/parser.js"
2
- import { defaultConditionNormalizer } from "../general/defaultConditionNormalizer.js"
3
- import { defaultKeyParser } from "../general/defaultKeyParser.js"
4
- import { defaultPrefixApplier } from "../general/defaultPrefixApplier.js"
5
- import { defaultValueComparer } from "../general/defaultValueComparer.js"
1
+ import { defaultConditionNormalizer } from "../defaults/defaultConditionNormalizer.js"
2
+ import { defaultKeyParser } from "../defaults/defaultKeyParser.js"
3
+ import { defaultPrefixApplier } from "../defaults/defaultPrefixApplier.js"
4
+ import { defaultValueComparer } from "../defaults/defaultValueComparer.js"
5
+ import type { FullParserOptions, ParserOptions } from "../types/parser.js"
6
6
 
7
7
  /** @internal */
8
8
  export function parseParserOptions<T extends {} = {}>(