@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/Lexer.ts CHANGED
@@ -1,5 +1,7 @@
1
- import { isBlank } from "@alanscodelog/utils/isBlank.js"
2
- import { pushIfNotIn } from "@alanscodelog/utils/pushIfNotIn.js"
1
+ import type { EnumLike } from "@alanscodelog/utils"
2
+ import { enumFromArray } from "@alanscodelog/utils/enumFromArray"
3
+ import { isBlank } from "@alanscodelog/utils/isBlank"
4
+ import { pushIfNotIn } from "@alanscodelog/utils/pushIfNotIn"
3
5
 
4
6
  import { checkParserOpts } from "./internal/checkParserOpts.js"
5
7
  import { parseParserOptions } from "./internal/parseParserOptions.js"
@@ -7,63 +9,68 @@ import type { FullParserOptions } from "./types/index.js"
7
9
 
8
10
  const regexFlags = /^[a-zA-Z]+/
9
11
 
10
- enum MODE {
11
- MAIN = "MAIN",
12
- MAYBE_QUOTE_ERROR = "MAYBE_QUOTE_ERROR",
13
- NOT_SINGLE = "NOT_SINGLE",
14
- NOT_DOUBLE = "NOT_DOUBLE",
15
- NOT_BACKTICK = "NOT_BACKTICK",
16
- NOT_REGEX = "NOT_REGEX",
17
- REGEX_END = "REGEX_END",
18
- BRACKET_MAIN = "BRACKET_MAIN",
19
- BRACKET_MAYBE_QUOTE_ERROR = "BRACKET_MAYBE_QUOTE_ERROR",
20
- BRACKET_NOT_SINGLE = "BRACKET_NOT_SINGLE",
21
- BRACKET_NOT_DOUBLE = "BRACKET_NOT_DOUBLE",
22
- BRACKET_NOT_BACKTICK = "BRACKET_NOT_BACKTICK",
23
-
24
- }
12
+ const MODE = enumFromArray([
13
+ "MAIN",
14
+ "MAYBE_QUOTE_ERROR",
15
+ "NOT_SINGLE",
16
+ "NOT_DOUBLE",
17
+ "NOT_BACKTICK",
18
+ "NOT_REGEX",
19
+ "REGEX_END",
20
+ "BRACKET_MAIN",
21
+ "BRACKET_MAYBE_QUOTE_ERROR",
22
+ "BRACKET_NOT_SINGLE",
23
+ "BRACKET_NOT_DOUBLE",
24
+ "BRACKET_NOT_BACKTICK",
25
+ "BRACKET_NOT_REGEX",
26
+ "BRACKET_REGEX_END",
27
+ ])
28
+
25
29
  const BRACKET_PREFIX = "BRACKET"
26
30
 
31
+ export const $T = enumFromArray([
32
+ "_",
33
+ "VALUE_UNQUOTED",
34
+ "VALUE_REGEX",
35
+ "VALUE_NOT_SINGLE",
36
+ "VALUE_NOT_DOUBLE",
37
+ "VALUE_NOT_BACKTICK",
38
+ "SYM_OR",
39
+ "SYM_AND",
40
+ "SYM_NOT",
41
+ "WORD_OR",
42
+ "WORD_AND",
43
+ "WORD_NOT",
44
+ "REGEX_START",
45
+ "REGEX_END",
46
+ "EXP_PROP_OP",
47
+ "CUSTOM_PROP_OP",
48
+ "PAREN_L",
49
+ "PAREN_R",
50
+ "BRACKET_L",
51
+ "BRACKET_R",
52
+ "QUOTE_SINGLE",
53
+ "QUOTE_DOUBLE",
54
+ "QUOTE_BACKTICK",
55
+ ])
56
+
27
57
  // eslint-disable-next-line @typescript-eslint/naming-convention
28
- export enum $T {
29
- _ = "_", // whitespace,
30
- VALUE_UNQUOTED = "VALUE_UNQUOTED",
31
- VALUE_REGEX = "VALUE_REGEX",
32
- VALUE_NOT_SINGLE = "VALUE_NOT_SINGLE",
33
- VALUE_NOT_DOUBLE = "VALUE_NOT_DOUBLE",
34
- VALUE_NOT_BACKTICK = "VALUE_NOT_BACKTICK",
35
- SYM_OR = "SYM_OR",
36
- SYM_AND = "SYM_AND",
37
- SYM_NOT = "SYM_NOT",
38
- WORD_OR = "WORD_OR",
39
- WORD_AND = "WORD_AND",
40
- WORD_NOT = "WORD_NOT",
41
- REGEX_START = "REGEX_START",
42
- REGEX_END = "REGEX_END",
43
- EXP_PROP_OP = "EXP_PROP_OP",
44
- CUSTOM_PROP_OP = "CUSTOM_PROP_OP",
45
- PAREN_L = "PAREN_L",
46
- PAREN_R = "PAREN_R",
47
- BRACKET_L = "BRACKET_L",
48
- BRACKET_R = "BRACKET_R",
49
- QUOTE_SINGLE = "QUOTE_SINGLE",
50
- QUOTE_DOUBLE = "QUOTE_DOUBLE",
51
- QUOTE_BACKTICK = "QUOTE_BACKTICK",
52
- }
58
+ export type $TType = EnumLike<typeof $T>
53
59
 
60
+ export const $C = enumFromArray([
61
+ "ANY",
62
+ "QUOTE_ANY",
63
+ "REGEX_ANY",
64
+ "VALUE_FOR_SINGLE",
65
+ "VALUE_FOR_DOUBLE",
66
+ "VALUE_FOR_BACKTICK",
67
+ "OPERATOR_OR",
68
+ "OPERATOR_AND",
69
+ "OPERATOR_NOT",
70
+ "VALUE",
71
+ ])
54
72
  // eslint-disable-next-line @typescript-eslint/naming-convention
55
- export enum $C {
56
- ANY = "ANY",
57
- QUOTE_ANY = "QUOTE_ANY",
58
- REGEX_ANY = "REGEX_ANY",
59
- VALUE_FOR_SINGLE = "VALUE_FOR_SINGLE",
60
- VALUE_FOR_DOUBLE = "VALUE_FOR_DOUBLE",
61
- VALUE_FOR_BACKTICK = "VALUE_FOR_BACKTICK",
62
- OPERATOR_OR = "OPERATOR_OR",
63
- OPERATOR_AND = "OPERATOR_AND",
64
- OPERATOR_NOT = "OPERATOR_NOT",
65
- VALUE = "VALUE",
66
- }
73
+ export type $CType = EnumLike<typeof $C>
67
74
 
68
75
 
69
76
  type SymbolInfo = {
@@ -80,40 +87,42 @@ type SymbolInfo = {
80
87
 
81
88
  type TokenMatchFunc = (c: string, input: string, start: number, mode: string) => string | boolean
82
89
 
83
- interface BaseTokenType<T extends $T | $C> {
90
+ interface LexerBaseToken<T extends $TType | $CType> {
84
91
  type: T
85
92
  skip?: boolean
86
93
  }
87
- export interface RealTokenType<
88
- T extends $T = $T,
94
+
95
+ export interface LexerRealToken<
96
+ T extends $TType = $TType,
89
97
  TPush extends string | undefined = undefined,
90
98
  // TCategories extends $C[] | undefined = undefined,
91
- > extends BaseTokenType<T> {
99
+ > extends LexerBaseToken<T> {
92
100
  matches: TokenMatchFunc
93
101
  push?: TPush | ((mode: string, tokens: Token[]) => TPush)
94
102
  // categories?: TCategories
95
- longerAlt?: $T
103
+ longerAlt?: $TType
96
104
  skip?: boolean
97
105
  }
98
- export interface TokenCategoryType<
99
- TC extends $C,
100
- TTokens extends RealTokenType<$T, any>[] = RealTokenType<$T, any>[],
101
- > extends BaseTokenType<TC> {
106
+
107
+ export interface LexerCategoryToken<
108
+ TC extends $CType,
109
+ TTokens extends LexerRealToken<$TType, any>[] = LexerRealToken<$TType, any>[],
110
+ > extends LexerBaseToken<TC> {
102
111
  isCategory: true
103
112
  entries: Partial<Record<TTokens[number]["type"], TTokens[number]>>
104
113
  // entries: Partial<Record<TTokens[number]["type"], TTokens[number]>>
105
114
  }
106
115
 
107
- export type TokenType<TC extends $C | $T> = TC extends $T
108
- ? RealTokenType<TC, any>
109
- : TC extends $C
110
- ? TokenCategoryType<TC>
116
+ export type LexerToken<TC extends $CType | $TType> = TC extends $TType
117
+ ? LexerRealToken<TC, any>
118
+ : TC extends $CType
119
+ ? LexerCategoryToken<TC>
111
120
  : never
112
121
 
113
- function createTokenCategoryType<T extends $C, TTokens extends RealTokenType<$T, any>>(
122
+ function createLexerCategoryToken<T extends $CType, TTokens extends LexerRealToken<$TType, any>>(
114
123
  type: T,
115
124
  entries: (TTokens | undefined)[],
116
- ): TokenCategoryType<T, TTokens[]> {
125
+ ): LexerCategoryToken<T, TTokens[]> {
117
126
  return {
118
127
  type,
119
128
  isCategory: true,
@@ -125,12 +134,12 @@ function createTokenCategoryType<T extends $C, TTokens extends RealTokenType<$T,
125
134
  }
126
135
 
127
136
  function createTokenType<
128
- T extends $T,
137
+ T extends $TType,
129
138
  TPush extends string | undefined = undefined,
130
139
  >(
131
140
  type: T,
132
- opts: Omit<RealTokenType<T, TPush>, "type">,
133
- ): RealTokenType<T, TPush > {
141
+ opts: Omit<LexerRealToken<T, TPush>, "type">,
142
+ ): LexerRealToken<T, TPush > {
134
143
  return {
135
144
  type,
136
145
  ...opts,
@@ -164,7 +173,7 @@ function matchSymbol(symbols: string[]): TokenMatchFunc {
164
173
  }
165
174
  }
166
175
 
167
- export interface Token<T extends $T | $C = $T | $C> {
176
+ export interface Token<T extends $TType | $CType = $TType | $CType> {
168
177
  type: T
169
178
  value: string
170
179
  startOffset: number
@@ -175,11 +184,11 @@ export interface Token<T extends $T | $C = $T | $C> {
175
184
  export class Lexer {
176
185
  symbols: SymbolInfo
177
186
 
178
- $: {[key in $T]: RealTokenType<key, any> }
187
+ $: {[key in $TType]: LexerRealToken<key, any> }
179
188
 
180
189
  $categories: ReturnType<Lexer["createTokens"]>["$categories"]
181
190
 
182
- branches: {[key in keyof typeof MODE]?: TokenType<$T>[] }
191
+ branches: {[key in keyof typeof MODE]?: LexerToken<$TType>[] }
183
192
 
184
193
  opts: FullParserOptions<any>
185
194
 
@@ -509,7 +518,7 @@ export class Lexer {
509
518
  }),
510
519
  }
511
520
  const $categories = {
512
- [$C.ANY]: createTokenCategoryType($C.ANY, [
521
+ [$C.ANY]: createLexerCategoryToken($C.ANY, [
513
522
  $[$T.REGEX_START],
514
523
  $[$T.REGEX_END],
515
524
  $[$T.QUOTE_SINGLE],
@@ -532,39 +541,39 @@ export class Lexer {
532
541
  $[$T.BRACKET_L],
533
542
  $[$T.BRACKET_R],
534
543
  ] as const),
535
- [$C.VALUE]: createTokenCategoryType($C.VALUE, [
544
+ [$C.VALUE]: createLexerCategoryToken($C.VALUE, [
536
545
  $[$T.VALUE_UNQUOTED],
537
546
  $[$T.VALUE_NOT_SINGLE],
538
547
  $[$T.VALUE_NOT_DOUBLE],
539
548
  $[$T.VALUE_NOT_BACKTICK],
540
549
  ] as const),
541
- [$C.VALUE_FOR_SINGLE]: createTokenCategoryType($C.VALUE_FOR_SINGLE, [
550
+ [$C.VALUE_FOR_SINGLE]: createLexerCategoryToken($C.VALUE_FOR_SINGLE, [
542
551
  $[$T.VALUE_NOT_SINGLE],
543
552
  ] as const),
544
- [$C.VALUE_FOR_DOUBLE]: createTokenCategoryType($C.VALUE_FOR_DOUBLE, [
553
+ [$C.VALUE_FOR_DOUBLE]: createLexerCategoryToken($C.VALUE_FOR_DOUBLE, [
545
554
  $[$T.VALUE_NOT_DOUBLE],
546
555
  ] as const),
547
- [$C.VALUE_FOR_BACKTICK]: createTokenCategoryType($C.VALUE_FOR_BACKTICK, [
556
+ [$C.VALUE_FOR_BACKTICK]: createLexerCategoryToken($C.VALUE_FOR_BACKTICK, [
548
557
  $[$T.VALUE_NOT_BACKTICK],
549
558
  ] as const),
550
- [$C.REGEX_ANY]: createTokenCategoryType($C.REGEX_ANY, [
559
+ [$C.REGEX_ANY]: createLexerCategoryToken($C.REGEX_ANY, [
551
560
  $[$T.REGEX_START],
552
561
  $[$T.REGEX_END],
553
562
  ] as const),
554
- [$C.QUOTE_ANY]: createTokenCategoryType($C.QUOTE_ANY, [
563
+ [$C.QUOTE_ANY]: createLexerCategoryToken($C.QUOTE_ANY, [
555
564
  $[$T.QUOTE_SINGLE],
556
565
  $[$T.QUOTE_DOUBLE],
557
566
  $[$T.QUOTE_BACKTICK],
558
567
  ] as const),
559
- [$C.OPERATOR_OR]: createTokenCategoryType($C.OPERATOR_OR, [
568
+ [$C.OPERATOR_OR]: createLexerCategoryToken($C.OPERATOR_OR, [
560
569
  $[$T.SYM_OR],
561
570
  $[$T.WORD_OR],
562
571
  ] as const),
563
- [$C.OPERATOR_AND]: createTokenCategoryType($C.OPERATOR_AND, [
572
+ [$C.OPERATOR_AND]: createLexerCategoryToken($C.OPERATOR_AND, [
564
573
  $[$T.SYM_AND],
565
574
  $[$T.WORD_AND],
566
575
  ] as const),
567
- [$C.OPERATOR_NOT]: createTokenCategoryType($C.OPERATOR_NOT, [
576
+ [$C.OPERATOR_NOT]: createLexerCategoryToken($C.OPERATOR_NOT, [
568
577
  $[$T.SYM_NOT],
569
578
  $[$T.WORD_NOT],
570
579
  ] as const),
@@ -572,7 +581,7 @@ export class Lexer {
572
581
  return { $, $categories }
573
582
  }
574
583
 
575
- createModeBranches(): {[key in keyof typeof MODE]?: TokenType<$T>[] } {
584
+ createModeBranches(): {[key in keyof typeof MODE]?: LexerToken<$TType>[] } {
576
585
  const opts = this.opts
577
586
  const $ = this.$
578
587
  const quotes = [
@@ -652,13 +661,13 @@ export class Lexer {
652
661
  }
653
662
 
654
663
 
655
- tokenize(input: string): Token<$T>[] {
664
+ tokenize(input: string): Token<$TType>[] {
656
665
  const branches = this.createModeBranches()
657
- const tokens: Token<$T>[] = []
666
+ const tokens: Token<$TType>[] = []
658
667
  let mode = MODE.MAIN
659
668
  let index = 0
660
669
  let c = input[index]
661
- let branch = branches[mode] as any as TokenType<$T>[]
670
+ let branch = branches[mode] as any as LexerToken<$TType>[]
662
671
  while (index < input.length) {
663
672
  for (const t of branch) {
664
673
  let match = t.matches(c, input, index, mode)
@@ -694,7 +703,8 @@ export class Lexer {
694
703
  return tokens
695
704
  }
696
705
  }
697
- function createToken<T extends $T>(type: T, value: string, startOffset: number, endOffset: number): Token<T> {
706
+
707
+ function createToken<T extends $TType>(type: T, value: string, startOffset: number, endOffset: number): Token<T> {
698
708
  return {
699
709
  type,
700
710
  value,