@witchcraft/expressit 0.2.2 → 0.3.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 (122) hide show
  1. package/README.md +1 -2
  2. package/dist/Lexer.d.ts +99 -94
  3. package/dist/Lexer.d.ts.map +1 -1
  4. package/dist/Lexer.js +221 -574
  5. package/dist/Parser.d.ts +26 -26
  6. package/dist/Parser.d.ts.map +1 -1
  7. package/dist/Parser.js +106 -116
  8. package/dist/ast/builders/array.js +7 -8
  9. package/dist/ast/builders/condition.d.ts +1 -1
  10. package/dist/ast/builders/condition.d.ts.map +1 -1
  11. package/dist/ast/builders/condition.js +2 -1
  12. package/dist/ast/builders/delim.d.ts +2 -2
  13. package/dist/ast/builders/delim.d.ts.map +1 -1
  14. package/dist/ast/builders/error.d.ts +2 -2
  15. package/dist/ast/builders/error.d.ts.map +1 -1
  16. package/dist/ast/builders/expression.d.ts +2 -2
  17. package/dist/ast/builders/expression.d.ts.map +1 -1
  18. package/dist/ast/builders/expression.js +4 -8
  19. package/dist/ast/builders/group.d.ts +1 -1
  20. package/dist/ast/builders/group.d.ts.map +1 -1
  21. package/dist/ast/builders/group.js +8 -11
  22. package/dist/ast/builders/pos.d.ts +2 -2
  23. package/dist/ast/builders/pos.d.ts.map +1 -1
  24. package/dist/ast/builders/token.d.ts +2 -2
  25. package/dist/ast/builders/token.d.ts.map +1 -1
  26. package/dist/ast/builders/token.js +1 -1
  27. package/dist/ast/builders/type.d.ts +2 -2
  28. package/dist/ast/builders/type.d.ts.map +1 -1
  29. package/dist/ast/builders/variable.d.ts +3 -3
  30. package/dist/ast/builders/variable.d.ts.map +1 -1
  31. package/dist/ast/builders/variable.js +5 -6
  32. package/dist/ast/createConditionNode.d.ts +1 -1
  33. package/dist/ast/createConditionNode.d.ts.map +1 -1
  34. package/dist/ast/createConditionNode.js +1 -2
  35. package/dist/ast/createGroupNode.d.ts +1 -1
  36. package/dist/ast/createGroupNode.d.ts.map +1 -1
  37. package/dist/ast/createToken.d.ts +2 -2
  38. package/dist/ast/createToken.d.ts.map +1 -1
  39. package/dist/ast/createToken.js +2 -2
  40. package/dist/ast/error.d.ts +2 -2
  41. package/dist/ast/error.d.ts.map +1 -1
  42. package/dist/ast/error.js +1 -0
  43. package/dist/ast/handlers.d.ts +23 -23
  44. package/dist/ast/handlers.d.ts.map +1 -1
  45. package/dist/ast/handlers.js +23 -26
  46. package/dist/examples/ParserWithSqlSupport.d.ts +5 -5
  47. package/dist/examples/ParserWithSqlSupport.d.ts.map +1 -1
  48. package/dist/examples/ParserWithSqlSupport.js +22 -28
  49. package/dist/examples/ShortcutContextParser.js +2 -5
  50. package/dist/index.js +2 -2
  51. package/dist/internal/ExpressitError.d.ts +2 -2
  52. package/dist/internal/ExpressitError.d.ts.map +1 -1
  53. package/dist/internal/ExpressitError.js +7 -10
  54. package/dist/internal/checkParserOpts.js +21 -22
  55. package/dist/internal/escapeVariableOrPrefix.js +1 -1
  56. package/dist/internal/parseParserOptions.js +3 -4
  57. package/dist/package.json.js +2 -60
  58. package/dist/types/ast.d.ts +60 -58
  59. package/dist/types/ast.d.ts.map +1 -1
  60. package/dist/types/ast.js +26 -27
  61. package/dist/types/autocomplete.d.ts +23 -21
  62. package/dist/types/autocomplete.d.ts.map +1 -1
  63. package/dist/types/autocomplete.js +24 -21
  64. package/dist/types/errors.d.ts +12 -10
  65. package/dist/types/errors.d.ts.map +1 -1
  66. package/dist/types/errors.js +8 -7
  67. package/dist/types/index.js +2 -2
  68. package/dist/types/parser.d.ts +3 -3
  69. package/dist/types/parser.d.ts.map +1 -1
  70. package/dist/utils/extractTokens.js +4 -5
  71. package/dist/utils/generateParentsMap.js +7 -8
  72. package/dist/utils/getCursorInfo.js +5 -3
  73. package/dist/utils/getOppositeDelimiter.d.ts +2 -2
  74. package/dist/utils/getOppositeDelimiter.d.ts.map +1 -1
  75. package/dist/utils/getOppositeDelimiter.js +1 -1
  76. package/dist/utils/getSurroundingErrors.js +2 -3
  77. package/dist/utils/isBracket.js +1 -1
  78. package/dist/utils/isDelimiter.d.ts +2 -2
  79. package/dist/utils/isDelimiter.d.ts.map +1 -1
  80. package/dist/utils/isDelimiter.js +1 -1
  81. package/dist/utils/isNode.js +1 -1
  82. package/dist/utils/isParen.d.ts +2 -2
  83. package/dist/utils/isParen.d.ts.map +1 -1
  84. package/dist/utils/isParen.js +1 -1
  85. package/dist/utils/isQuote.d.ts +2 -2
  86. package/dist/utils/isQuote.d.ts.map +1 -1
  87. package/dist/utils/isQuote.js +1 -1
  88. package/dist/utils/isToken.js +1 -1
  89. package/dist/utils/prettyAst.js +10 -11
  90. package/package.json +19 -20
  91. package/src/Lexer.ts +100 -90
  92. package/src/Parser.ts +68 -68
  93. package/src/ast/builders/condition.ts +3 -3
  94. package/src/ast/builders/delim.ts +4 -4
  95. package/src/ast/builders/error.ts +3 -3
  96. package/src/ast/builders/expression.ts +4 -8
  97. package/src/ast/builders/group.ts +2 -4
  98. package/src/ast/builders/pos.ts +3 -3
  99. package/src/ast/builders/token.ts +3 -3
  100. package/src/ast/builders/type.ts +2 -2
  101. package/src/ast/builders/variable.ts +5 -5
  102. package/src/ast/createConditionNode.ts +2 -2
  103. package/src/ast/createGroupNode.ts +4 -4
  104. package/src/ast/createToken.ts +6 -6
  105. package/src/ast/error.ts +2 -2
  106. package/src/ast/handlers.ts +20 -20
  107. package/src/examples/ParserWithSqlSupport.ts +12 -6
  108. package/src/internal/ExpressitError.ts +6 -6
  109. package/src/internal/checkParserOpts.ts +13 -13
  110. package/src/internal/escapeVariableOrPrefix.ts +1 -1
  111. package/src/types/ast.ts +101 -96
  112. package/src/types/autocomplete.ts +26 -22
  113. package/src/types/errors.ts +18 -13
  114. package/src/types/parser.ts +3 -3
  115. package/src/utils/extractTokens.ts +1 -1
  116. package/src/utils/getCursorInfo.ts +2 -2
  117. package/src/utils/getOppositeDelimiter.ts +3 -3
  118. package/src/utils/getSurroundingErrors.ts +4 -4
  119. package/src/utils/isDelimiter.ts +3 -3
  120. package/src/utils/isParen.ts +2 -2
  121. package/src/utils/isQuote.ts +2 -2
  122. package/src/utils/prettyAst.ts +3 -3
package/src/types/ast.ts CHANGED
@@ -1,94 +1,97 @@
1
- import type { AnyFunction } from "@alanscodelog/utils/types"
1
+ import { enumFromArray } from "@alanscodelog/utils/enumFromArray"
2
+ import type { AnyFunction, EnumLike } from "@alanscodelog/utils/types"
2
3
 
3
4
 
4
5
  export type AddParameters<T extends AnyFunction, TExtra extends any[] = [boolean]> = (...args: [...Parameters<T>, ...TExtra]) => ReturnType<T>
5
6
 
6
- export enum TOKEN_TYPE {
7
- VALUE = "VALUE",
8
- AND = "AND",
9
- OR = "OR",
10
- NOT = "NOT",
11
- BACKTICK = "BACKTICK",
12
- SINGLEQUOTE = "SINGLEQUOTE",
13
- DOUBLEQUOTE = "DOUBLEQUOTE",
14
- PARENL = "PARENL",
15
- PARENR = "PARENR",
16
- BRACKETL = "BRACKETL",
17
- BRACKETR = "BRACKETR",
18
- OP_EXPANDED_SEP = "OP_EXPANDED",
19
- OP_CUSTOM = "OP_CUSTOM",
20
- REGEX = "REGEX",
21
- }
7
+ export const TOKEN_TYPE = enumFromArray([
8
+ "VALUE",
9
+ "AND",
10
+ "OR",
11
+ "NOT",
12
+ "BACKTICK",
13
+ "SINGLEQUOTE",
14
+ "DOUBLEQUOTE",
15
+ "PARENL",
16
+ "PARENR",
17
+ "BRACKETL",
18
+ "BRACKETR",
19
+ "OP_EXPANDED_SEP",
20
+ "OP_CUSTOM",
21
+ "REGEX",
22
+ ])
23
+
24
+ export type TokenType = EnumLike<typeof TOKEN_TYPE>
22
25
 
23
26
  /**
24
27
  * @internal
25
28
  * Note if the negation operator, `!`, is used as a propertyOperator, this will return the wrong type.
26
29
  */
27
- export type ExtractTokenType<T extends string> =
30
+ export type ExtractToken<T extends string> =
28
31
  T extends "`"
29
- ? TOKEN_TYPE.BACKTICK
32
+ ? typeof TOKEN_TYPE.BACKTICK
30
33
  : T extends `'`
31
- ? TOKEN_TYPE.SINGLEQUOTE
34
+ ? typeof TOKEN_TYPE.SINGLEQUOTE
32
35
  : T extends `"`
33
- ? TOKEN_TYPE.DOUBLEQUOTE
36
+ ? typeof TOKEN_TYPE.DOUBLEQUOTE
34
37
  : T extends `/`
35
- ? TOKEN_TYPE.REGEX
38
+ ? typeof TOKEN_TYPE.REGEX
36
39
  : T extends `(`
37
- ? TOKEN_TYPE.PARENL
40
+ ? typeof TOKEN_TYPE.PARENL
38
41
  : T extends `)`
39
- ? TOKEN_TYPE.PARENR
42
+ ? typeof TOKEN_TYPE.PARENR
40
43
  : T extends `[`
41
- ? TOKEN_TYPE.BRACKETL
44
+ ? typeof TOKEN_TYPE.BRACKETL
42
45
  : T extends `]`
43
- ? TOKEN_TYPE.BRACKETR
46
+ ? typeof TOKEN_TYPE.BRACKETR
44
47
  : T extends `and`
45
- ? TOKEN_TYPE.AND
48
+ ? typeof TOKEN_TYPE.AND
46
49
  : T extends `&&`
47
- ? TOKEN_TYPE.AND
50
+ ? typeof TOKEN_TYPE.AND
48
51
  : T extends `&`
49
- ? TOKEN_TYPE.AND
52
+ ? typeof TOKEN_TYPE.AND
50
53
  : T extends `or`
51
- ? TOKEN_TYPE.OR
54
+ ? typeof TOKEN_TYPE.OR
52
55
  : T extends `||`
53
- ? TOKEN_TYPE.OR
56
+ ? typeof TOKEN_TYPE.OR
54
57
  : T extends `|`
55
- ? TOKEN_TYPE.OR
58
+ ? typeof TOKEN_TYPE.OR
56
59
  : T extends `not`
57
- ? TOKEN_TYPE.NOT
60
+ ? typeof TOKEN_TYPE.NOT
58
61
  : T extends `!`
59
- ? TOKEN_TYPE.NOT
60
- : TOKEN_TYPE.VALUE
62
+ ? typeof TOKEN_TYPE.NOT
63
+ : typeof TOKEN_TYPE.VALUE
61
64
 
62
- export type TokenParenTypes =
63
- | TOKEN_TYPE.PARENL
64
- | TOKEN_TYPE.PARENR
65
+ export type TokenParen =
66
+ | typeof TOKEN_TYPE.PARENL
67
+ | typeof TOKEN_TYPE.PARENR
65
68
  export type TokenBracketTypes =
66
- | TOKEN_TYPE.BRACKETL
67
- | TOKEN_TYPE.BRACKETR
69
+ | typeof TOKEN_TYPE.BRACKETL
70
+ | typeof TOKEN_TYPE.BRACKETR
68
71
 
69
- export type TokenDelimiterTypes =
70
- | TokenParenTypes
71
- | TokenQuoteTypes
72
+ export type TokenDelimiter =
73
+ | TokenParen
74
+ | TokenQuote
72
75
  | TokenBracketTypes
73
- | TOKEN_TYPE.OP_EXPANDED_SEP
76
+ | typeof TOKEN_TYPE.OP_EXPANDED_SEP
74
77
 
75
- export type TokenQuoteTypes =
76
- | TOKEN_TYPE.BACKTICK
77
- | TOKEN_TYPE.SINGLEQUOTE
78
- | TOKEN_TYPE.DOUBLEQUOTE
79
- | TOKEN_TYPE.REGEX
78
+ export type TokenQuote =
79
+ | typeof TOKEN_TYPE.BACKTICK
80
+ | typeof TOKEN_TYPE.SINGLEQUOTE
81
+ | typeof TOKEN_TYPE.DOUBLEQUOTE
82
+ | typeof TOKEN_TYPE.REGEX
80
83
 
81
- export type TokenBooleanTypes =
82
- | TOKEN_TYPE.AND
83
- | TOKEN_TYPE.OR
84
+ export type TokenBoolean =
85
+ | typeof TOKEN_TYPE.AND
86
+ | typeof TOKEN_TYPE.OR
84
87
 
85
- export type TokenOperatorTypes =
86
- | TokenBooleanTypes
87
- | TOKEN_TYPE.NOT
88
+ export type TokenOperator =
89
+ | TokenBoolean
90
+ | typeof TOKEN_TYPE.NOT
88
91
 
89
- export type TokenPropertyOperatorTypes =
90
- | TOKEN_TYPE.OP_CUSTOM
91
- | TOKEN_TYPE.OP_EXPANDED_SEP
92
+ export type TokenPropertyOperator =
93
+ | typeof TOKEN_TYPE.OP_CUSTOM
94
+ | typeof TOKEN_TYPE.OP_EXPANDED_SEP
92
95
 
93
96
 
94
97
  // export type EmptyObj = Record<"start"|"end", undefined>
@@ -101,16 +104,18 @@ export type Position = {
101
104
  end: number
102
105
  }
103
106
 
104
- export enum AST_TYPE {
105
- EXPRESSION = "EXPRESSION",
106
- NORMALIZED_EXPRESSION = "NORMALIZED_EXPRESSION",
107
- GROUP = "GROUP",
108
- ARRAY = "ARRAY",
109
- CONDITION = "CONDITION",
110
- NORMALIZED_CONDITION = "NORMALIZED_CONDITION",
111
- VARIABLE = "VARIABLE",
112
- }
107
+ export const AST_TYPE = enumFromArray([
108
+ "EXPRESSION",
109
+ "NORMALIZED_EXPRESSION",
110
+ "GROUP",
111
+ "ARRAY",
112
+ "CONDITION",
113
+ "NORMALIZED_CONDITION",
114
+ "VARIABLE",
115
+ ])
116
+
113
117
  // #region AST nodes
118
+ export type AstType = EnumLike<typeof AST_TYPE>
114
119
 
115
120
 
116
121
  export type RawNode<T extends Node> = Omit<T, "valid" | "type" | "isNode">
@@ -130,7 +135,7 @@ export interface BaseToken {
130
135
  /**
131
136
  * Valid tokens always have a value, even if it might be an empty string.
132
137
  */
133
- export interface ValidToken<TType extends TOKEN_TYPE = TOKEN_TYPE> extends BaseToken {
138
+ export interface ValidToken<TType extends TokenType = TokenType> extends BaseToken {
134
139
  valid: true
135
140
  type: TType
136
141
  value: string
@@ -150,7 +155,7 @@ export interface ErrorToken extends BaseToken {
150
155
  type?: undefined
151
156
  value?: undefined
152
157
  valid: false
153
- expected: TOKEN_TYPE[]
158
+ expected: TokenType[]
154
159
  }
155
160
 
156
161
  /**
@@ -159,7 +164,7 @@ export interface ErrorToken extends BaseToken {
159
164
  * Using {@link Token} does not work well in certain situations and is also more complex because it has so many generics.
160
165
  */
161
166
  export type AnyToken<
162
- TType extends TOKEN_TYPE = TOKEN_TYPE,
167
+ TType extends TokenType = TokenType,
163
168
  > =
164
169
  | ValidToken<TType>
165
170
  | ErrorToken
@@ -175,7 +180,7 @@ export type ParserResults = ExpressionNode | ConditionNode | GroupNode | ErrorTo
175
180
  */
176
181
 
177
182
  export interface Node<
178
- TType extends AST_TYPE = AST_TYPE,
183
+ TType extends AstType = AstType,
179
184
  TValid extends boolean = boolean,
180
185
  > {
181
186
  isNode: true
@@ -222,16 +227,16 @@ export interface GroupNode<
222
227
  TPrefix extends
223
228
  TPrefixable extends true
224
229
  ? ConditionNode<TValid> |
225
- ValidToken<TOKEN_TYPE.NOT> |
230
+ ValidToken<typeof TOKEN_TYPE.NOT> |
226
231
  undefined
227
- : ValidToken<TOKEN_TYPE.NOT> |
232
+ : ValidToken<typeof TOKEN_TYPE.NOT> |
228
233
  undefined =
229
234
  TPrefixable extends true
230
235
  ? ConditionNode<TValid> |
231
- ValidToken<TOKEN_TYPE.NOT> |
236
+ ValidToken<typeof TOKEN_TYPE.NOT> |
232
237
  undefined
233
- : ValidToken<TOKEN_TYPE.NOT>,
234
- > extends Node<AST_TYPE.GROUP> {
238
+ : ValidToken<typeof TOKEN_TYPE.NOT>,
239
+ > extends Node<typeof AST_TYPE.GROUP> {
235
240
  /**
236
241
  * If the condition is negated this will contain a "not" **token**, {@link ValidToken} .
237
242
  *
@@ -250,7 +255,7 @@ export interface GroupNode<
250
255
  /**
251
256
  * The parenthesis tokens, {@link ValidToken} . These will always be defined (although not necessarily with valid tokens).
252
257
  */
253
- paren: NodeDelimiters<TOKEN_TYPE.PARENL, TOKEN_TYPE.PARENR>
258
+ paren: NodeDelimiters<typeof TOKEN_TYPE.PARENL, typeof TOKEN_TYPE.PARENR>
254
259
  }
255
260
 
256
261
 
@@ -266,18 +271,18 @@ export interface GroupNode<
266
271
  * If `prefixableStrings` is true, the `prefix` property might contain a value token.
267
272
  */
268
273
 
269
- export interface VariableNode<TValid extends boolean = boolean> extends Node<AST_TYPE.VARIABLE, TValid> {
274
+ export interface VariableNode<TValid extends boolean = boolean> extends Node<typeof AST_TYPE.VARIABLE, TValid> {
270
275
  value: TValid extends boolean
271
- ? AnyToken<TOKEN_TYPE.VALUE>
276
+ ? AnyToken<typeof TOKEN_TYPE.VALUE>
272
277
  : TValid extends true
273
- ? ValidToken<TOKEN_TYPE.VALUE>
278
+ ? ValidToken<typeof TOKEN_TYPE.VALUE>
274
279
  : ErrorToken
275
- prefix?: ValidToken<TOKEN_TYPE.VALUE> // todo
276
- quote?: NodeDelimiters<TokenQuoteTypes, TokenQuoteTypes>
280
+ prefix?: ValidToken<typeof TOKEN_TYPE.VALUE> // todo
281
+ quote?: NodeDelimiters<TokenQuote, TokenQuote>
277
282
  }
278
283
 
279
- export interface ExpressionNode<TValid extends boolean = boolean> extends Node<AST_TYPE.EXPRESSION> {
280
- operator: AnyToken<TokenBooleanTypes>
284
+ export interface ExpressionNode<TValid extends boolean = boolean> extends Node<typeof AST_TYPE.EXPRESSION> {
285
+ operator: AnyToken<TokenBoolean>
281
286
  left:
282
287
  | ExpressionNode<TValid>
283
288
  | ConditionNode<TValid>
@@ -298,17 +303,17 @@ export type Nodes = ExpressionNode | ConditionNode | GroupNode | VariableNode |
298
303
  *
299
304
  * These are usually not important for evaluating an expression but are useful for syntax highlighting.
300
305
  */
301
- export type NodeDelimiters<TLEFT extends TOKEN_TYPE, TRIGHT extends TOKEN_TYPE = TLEFT> = {
306
+ export type NodeDelimiters<TLEFT extends TokenType, TRIGHT extends TokenType = TLEFT> = {
302
307
  left: AnyToken<TLEFT>
303
308
  right: AnyToken<TRIGHT>
304
309
  /** Only exists if regexes are enabled and this is a regex value. */
305
- flags?: ValidToken<TOKEN_TYPE.VALUE>
310
+ flags?: ValidToken<typeof TOKEN_TYPE.VALUE>
306
311
  }
307
312
 
308
313
 
309
- export interface ArrayNode<TValid extends boolean = boolean> extends Node<AST_TYPE.ARRAY> {
314
+ export interface ArrayNode<TValid extends boolean = boolean> extends Node<typeof AST_TYPE.ARRAY> {
310
315
  values: VariableNode[]
311
- bracket: NodeDelimiters<TOKEN_TYPE.BRACKETL, TOKEN_TYPE.BRACKETR>
316
+ bracket: NodeDelimiters<typeof TOKEN_TYPE.BRACKETL, typeof TOKEN_TYPE.BRACKETR>
312
317
  valid: TValid
313
318
  }
314
319
 
@@ -321,7 +326,7 @@ export interface ArrayNode<TValid extends boolean = boolean> extends Node<AST_TY
321
326
  */
322
327
  export interface ConditionNode<
323
328
  TValid extends boolean = boolean,
324
- > extends Node<AST_TYPE.CONDITION> {
329
+ > extends Node<typeof AST_TYPE.CONDITION> {
325
330
  /**
326
331
  * Contains a value node which could be a variable, an array node (if enabled), or a group.
327
332
  *
@@ -337,7 +342,7 @@ export interface ConditionNode<
337
342
  /**
338
343
  * If the condition was negated, contains the "not" token, {@link ValidToken} , the condition was negated with.
339
344
  */
340
- operator?: ValidToken<TOKEN_TYPE.NOT>
345
+ operator?: ValidToken<typeof TOKEN_TYPE.NOT>
341
346
  /**
342
347
  * If condition property operators are used, this will contain the property (as a variable), or an error token if it was missing (but some separator or operator was passed).
343
348
  *
@@ -355,7 +360,7 @@ export interface ConditionNode<
355
360
  *
356
361
  * See the corresponding {@link ParserOptions} for more details.
357
362
  */
358
- propertyOperator?: AnyToken<TOKEN_TYPE.OP_CUSTOM | TOKEN_TYPE.VALUE>
363
+ propertyOperator?: AnyToken<typeof TOKEN_TYPE.OP_CUSTOM | typeof TOKEN_TYPE.VALUE>
359
364
  /**
360
365
  * If "long/expanded" form condition property operators are used, this will contain the separators, otherwise it is undefined.
361
366
  *
@@ -368,8 +373,8 @@ export interface ConditionNode<
368
373
  * See the corresponding {@link ParserOptions} for more details.
369
374
  */
370
375
  sep?: {
371
- left?: AnyToken<TOKEN_TYPE.OP_EXPANDED_SEP>
372
- right?: AnyToken<TOKEN_TYPE.OP_EXPANDED_SEP>
376
+ left?: AnyToken<typeof TOKEN_TYPE.OP_EXPANDED_SEP>
377
+ right?: AnyToken<typeof TOKEN_TYPE.OP_EXPANDED_SEP>
373
378
  }
374
379
  }
375
380
 
@@ -377,7 +382,7 @@ export interface NormalizedCondition<
377
382
  TOp extends string = string,
378
383
  TValue = any,
379
384
  > {
380
- type: AST_TYPE.NORMALIZED_CONDITION
385
+ type: typeof AST_TYPE.NORMALIZED_CONDITION
381
386
  value: TValue
382
387
  operator?: TOp
383
388
  property: string[]
@@ -386,14 +391,14 @@ export interface NormalizedCondition<
386
391
 
387
392
 
388
393
  export interface NormalizedExpression<TType extends string = string, TValue = any> {
389
- type: AST_TYPE.NORMALIZED_EXPRESSION
394
+ type: typeof AST_TYPE.NORMALIZED_EXPRESSION
390
395
  left:
391
396
  | NormalizedExpression<TType, TValue>
392
397
  | NormalizedCondition<TType, TValue>
393
398
  right:
394
399
  | NormalizedExpression<TType, TValue>
395
400
  | NormalizedCondition<TType, TValue>
396
- operator: TokenBooleanTypes
401
+ operator: TokenBoolean
397
402
  }
398
403
 
399
404
  export type ParentTypes<T extends Node | BaseToken | undefined> =
@@ -1,3 +1,6 @@
1
+ import { type EnumLike } from "@alanscodelog/utils"
2
+ import { enumFromArray } from "@alanscodelog/utils/enumFromArray"
3
+
1
4
  import type { AnyToken, Position, ValidToken } from "./ast.js"
2
5
 
3
6
 
@@ -82,35 +85,36 @@ export type CursorInfo = {
82
85
  }
83
86
  }
84
87
 
85
- export enum SUGGESTION_TYPE {
86
- // can ignore whitespace requirement if replacing with quoted
87
- VARIABLE = "VARIABLE",
88
- ARRAY_VALUE = "ARRAY_VALUE",
89
- VALUE = "VALUE",
90
- PREFIX = "PREFIX",
91
- BOOLEAN_WORD_OP = "BOOLEAN_WORD_OP",
92
- BOOLEAN_SYMBOL_OP = "BOOLEAN_SYMBOL_OP",
93
- BACKTICK = "BACKTICK",
94
- DOUBLEQUOTE = "DOUBLEQUOTE",
95
- SINGLEQUOTE = "SINGLEQUOTE",
96
- PARENL = "PARENL",
97
- PARENR = "PARENR",
98
- PROPERTY = "PROPERTY",
99
- EXPANDED_PROPERTY_OPERATOR = "EXPANDED_PROPERTY_OPERATOR",
100
- CUSTOM_PROPERTY_OPERATOR = "CUSTOM_PROPERTY_OPERATOR",
101
- PROPERTY_SEP = "PROPERTY_SEP",
102
- BRAKCETR = "BRAKCETR",
103
- REGEX = "REGEX",
104
- REGEX_FLAGS = "REGEX_FLAGS",
88
+ export const SUGGESTION_TYPE = enumFromArray([
89
+ "VARIABLE",
90
+ "ARRAY_VALUE",
91
+ "VALUE",
92
+ "PREFIX",
93
+ "BOOLEAN_WORD_OP",
94
+ "BOOLEAN_SYMBOL_OP",
95
+ "BACKTICK",
96
+ "DOUBLEQUOTE",
97
+ "SINGLEQUOTE",
98
+ "PARENL",
99
+ "PARENR",
100
+ "PROPERTY",
101
+ "EXPANDED_PROPERTY_OPERATOR",
102
+ "CUSTOM_PROPERTY_OPERATOR",
103
+ "PROPERTY_SEP",
104
+ "BRAKCETR",
105
+ "REGEX",
106
+ "REGEX_FLAGS",
105
107
  /** This is not an oversight, I haven't figured out a fast way to detect left bracket errors. */
106
108
  // BRAKCETL = "BRAKCETL",
107
- }
109
+ ])
110
+
111
+ export type SuggestionType = EnumLike<typeof SUGGESTION_TYPE>
108
112
 
109
113
  /**
110
114
  * A suggestion entry that describes a type of suggestion.
111
115
  */
112
116
  export type Suggestion = {
113
- type: SUGGESTION_TYPE
117
+ type: SuggestionType
114
118
  /** The range the suggestion should replace / be inserted at. */
115
119
  range: Position
116
120
  /** {@link CursorInfo} */
@@ -1,29 +1,34 @@
1
+ import { type EnumLike } from "@alanscodelog/utils"
2
+ import { enumFromArray } from "@alanscodelog/utils/enumFromArray"
3
+
1
4
  import type { ParserOptions } from "./parser.js"
2
5
 
3
6
 
4
- export enum ERROR_CODES {
5
- PARSER_POSITION_ERROR = "PARSER.POSITION",
6
- PARSER_CONFLICTING_OPTIONS_ERROR = "PARSER.OPTIONS.CONFLICTING",
7
- PARSER_OPTION_REQUIRED_ERROR = "PARSER.OPTIONS.CUSTOM_REQUIRED",
8
- }
7
+ export const PARSER_ERROR = enumFromArray([
8
+ "POSITION_ERROR",
9
+ "CONFLICTING_OPTIONS_ERROR",
10
+ "OPTION_REQUIRED_ERROR",
11
+ ], "PARSER.")
12
+
13
+ export type ParserError = EnumLike<typeof PARSER_ERROR>
9
14
 
10
15
 
11
- export type ErrorInfo<T extends ERROR_CODES> =
12
- T extends ERROR_CODES
13
- ? ERROR_Info[T]
16
+ export type ErrorInfo<T extends ParserError> =
17
+ T extends ParserError
18
+ ? ErrorCodesInfo[T]
14
19
  : never
15
20
 
16
- // eslint-disable-next-line @typescript-eslint/naming-convention
17
- export type ERROR_Info = {
18
- [ERROR_CODES.PARSER_POSITION_ERROR]: {
21
+
22
+ export type ErrorCodesInfo = {
23
+ [PARSER_ERROR.POSITION_ERROR]: {
19
24
  start?: number
20
25
  end?: number
21
26
  }
22
- [ERROR_CODES.PARSER_CONFLICTING_OPTIONS_ERROR]: {
27
+ [PARSER_ERROR.CONFLICTING_OPTIONS_ERROR]: {
23
28
  prohibited: string[]
24
29
  invalid: string
25
30
  }
26
- [ERROR_CODES.PARSER_OPTION_REQUIRED_ERROR]: {
31
+ [PARSER_ERROR.OPTION_REQUIRED_ERROR]: {
27
32
  options?: (keyof ParserOptions)[]
28
33
  requires: keyof ParserOptions
29
34
  }
@@ -443,9 +443,9 @@ export type ValueQuery
443
443
  /** {@link ParserOptions}["valueValidator"] */
444
444
  export type ValidationQuery = Omit<ValueQuery, "value" | "operator" | "prefix" | "regexFlags" | "property"> & {
445
445
  value?: VariableNode | ArrayNode
446
- operator?: ValidToken<TOKEN_TYPE.VALUE | TOKEN_TYPE.OP_CUSTOM>
447
- prefix?: ValidToken<TOKEN_TYPE.VALUE>
448
- regexFlags?: ValidToken<TOKEN_TYPE.VALUE>
446
+ operator?: ValidToken<typeof TOKEN_TYPE.VALUE | typeof TOKEN_TYPE.OP_CUSTOM>
447
+ prefix?: ValidToken<typeof TOKEN_TYPE.VALUE>
448
+ regexFlags?: ValidToken<typeof TOKEN_TYPE.VALUE>
449
449
  property: VariableNode[]
450
450
  /** The property as would be passed to the {@link ParserOptions}["valueComparer"] function (i.e. joined as a string using the {@link ParseOptions}["prefixApplier"] ) */
451
451
  propertyName?: string
@@ -1,4 +1,4 @@
1
- import { unreachable } from "@alanscodelog/utils/unreachable.js"
1
+ import { unreachable } from "@alanscodelog/utils/unreachable"
2
2
 
3
3
  import { isNode } from "./isNode.js"
4
4
  import { isToken } from "./isToken.js"
@@ -1,5 +1,5 @@
1
- import { isArray } from "@alanscodelog/utils/isArray.js"
2
- import { unreachable } from "@alanscodelog/utils/unreachable.js"
1
+ import { isArray } from "@alanscodelog/utils/isArray"
2
+ import { unreachable } from "@alanscodelog/utils/unreachable"
3
3
 
4
4
  import { extractTokens } from "./extractTokens.js"
5
5
 
@@ -1,4 +1,4 @@
1
- import { unreachable } from "@alanscodelog/utils/unreachable.js"
1
+ import { unreachable } from "@alanscodelog/utils/unreachable"
2
2
 
3
3
  import type { generateParentsMap } from "./generateParentsMap.js"
4
4
  import { isBracket } from "./isBracket.js"
@@ -6,7 +6,7 @@ import { isDelimiter } from "./isDelimiter.js"
6
6
  import { isParen } from "./isParen.js"
7
7
  import { isQuote } from "./isQuote.js"
8
8
 
9
- import { type AnyToken, type ArrayNode, type ConditionNode,type GroupNode, TOKEN_TYPE, type TokenDelimiterTypes, type VariableNode } from "../types/ast.js"
9
+ import { type AnyToken, type ArrayNode, type ConditionNode,type GroupNode, TOKEN_TYPE, type TokenDelimiter, type VariableNode } from "../types/ast.js"
10
10
 
11
11
  /**
12
12
  * 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).
@@ -14,7 +14,7 @@ import { type AnyToken, type ArrayNode, type ConditionNode,type GroupNode, TOKEN
14
14
  export function getOppositeDelimiter(
15
15
  token: AnyToken,
16
16
  parentsMap: ReturnType<typeof generateParentsMap>
17
- ): AnyToken<TokenDelimiterTypes> | undefined {
17
+ ): AnyToken<TokenDelimiter> | undefined {
18
18
  const parent = parentsMap.get(token)
19
19
  if (!isDelimiter(token)) throw new Error("Token is not a delimiter type.")
20
20
  if (isParen(token)) {
@@ -43,10 +43,10 @@ export function getSurroundingErrors(tokens: AnyToken[], token: CursorInfo): Err
43
43
  }
44
44
 
45
45
  return errors.sort((a, b) => {
46
- const aIsQuote = [TOKEN_TYPE.DOUBLEQUOTE, TOKEN_TYPE.BACKTICK, TOKEN_TYPE.SINGLEQUOTE].includes(a.expected[0])
47
- const bIsQuote = [TOKEN_TYPE.DOUBLEQUOTE, TOKEN_TYPE.BACKTICK, TOKEN_TYPE.SINGLEQUOTE].includes(b.expected[0])
48
- const aIsParen = [TOKEN_TYPE.PARENR, TOKEN_TYPE.PARENL].includes(a.expected[0])
49
- const bIsParen = [TOKEN_TYPE.PARENR, TOKEN_TYPE.PARENL].includes(b.expected[0])
46
+ const aIsQuote = ([TOKEN_TYPE.DOUBLEQUOTE, TOKEN_TYPE.BACKTICK, TOKEN_TYPE.SINGLEQUOTE] as string[]).includes(a.expected[0])
47
+ const bIsQuote = ([TOKEN_TYPE.DOUBLEQUOTE, TOKEN_TYPE.BACKTICK, TOKEN_TYPE.SINGLEQUOTE]as string[]).includes(b.expected[0])
48
+ const aIsParen = ([TOKEN_TYPE.PARENR, TOKEN_TYPE.PARENL] as string[]).includes(a.expected[0])
49
+ const bIsParen = ([TOKEN_TYPE.PARENR, TOKEN_TYPE.PARENL] as string[]).includes(b.expected[0])
50
50
  const aCloseness = Math.abs(i - tokens.indexOf(a))
51
51
  const bCloseness = Math.abs(i - tokens.indexOf(b))
52
52
  const closenessComparison = aCloseness - bCloseness
@@ -1,9 +1,9 @@
1
- import { type AnyToken, TOKEN_TYPE, type TokenDelimiterTypes } from "../types/ast.js"
1
+ import { type AnyToken, TOKEN_TYPE, type TokenDelimiter } from "../types/ast.js"
2
2
 
3
3
  /**
4
4
  * Returns whether token is a delimiter type (including if it's an expanded operator separator).
5
5
  */
6
- export function isDelimiter(token?: AnyToken): token is AnyToken<TokenDelimiterTypes> {
6
+ export function isDelimiter(token?: AnyToken): token is AnyToken<TokenDelimiter> {
7
7
  return ([
8
8
  TOKEN_TYPE.BACKTICK,
9
9
  TOKEN_TYPE.DOUBLEQUOTE,
@@ -14,5 +14,5 @@ export function isDelimiter(token?: AnyToken): token is AnyToken<TokenDelimiterT
14
14
  TOKEN_TYPE.BRACKETR,
15
15
  TOKEN_TYPE.OP_EXPANDED_SEP,
16
16
  TOKEN_TYPE.REGEX,
17
- ] as TokenDelimiterTypes[]).includes(token?.type as TokenDelimiterTypes)
17
+ ] as TokenDelimiter[]).includes(token?.type as TokenDelimiter)
18
18
  }
@@ -1,6 +1,6 @@
1
- import { type AnyToken, TOKEN_TYPE, type TokenParenTypes } from "../types/ast.js"
1
+ import { type AnyToken, TOKEN_TYPE, type TokenParen } from "../types/ast.js"
2
2
 
3
3
 
4
- export function isParen(token?: AnyToken): token is AnyToken<TokenParenTypes> {
4
+ export function isParen(token?: AnyToken): token is AnyToken<TokenParen> {
5
5
  return ([TOKEN_TYPE.PARENL, TOKEN_TYPE.PARENR] as any[]).includes(token?.type)
6
6
  }
@@ -1,6 +1,6 @@
1
- import { type AnyToken, TOKEN_TYPE, type TokenQuoteTypes } from "../types/ast.js"
1
+ import { type AnyToken, TOKEN_TYPE, type TokenQuote } from "../types/ast.js"
2
2
 
3
3
  /** Returns if the token is a quote token. This includes regex delimiters. */
4
- export function isQuote(token?: AnyToken): token is AnyToken<TokenQuoteTypes> {
4
+ export function isQuote(token?: AnyToken): token is AnyToken<TokenQuote> {
5
5
  return ([TOKEN_TYPE.BACKTICK, TOKEN_TYPE.DOUBLEQUOTE, TOKEN_TYPE.SINGLEQUOTE, TOKEN_TYPE.REGEX] as any[]).includes(token?.type)
6
6
  }
@@ -1,8 +1,8 @@
1
1
  /* eslint-disable prefer-rest-params */
2
- import * as color from "@alanscodelog/utils/colors.js"
3
- import { isBlank } from "@alanscodelog/utils/isBlank.js"
2
+ import * as color from "@alanscodelog/utils/colors"
3
+ import { isBlank } from "@alanscodelog/utils/isBlank"
4
4
  import { type AddParameters } from "@alanscodelog/utils/types"
5
- import { unreachable } from "@alanscodelog/utils/unreachable.js"
5
+ import { unreachable } from "@alanscodelog/utils/unreachable"
6
6
 
7
7
  import { isToken } from "./isToken.js"
8
8