@witchcraft/expressit 0.2.2 → 0.3.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 (99) 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 +211 -559
  5. package/dist/Parser.d.ts +26 -26
  6. package/dist/Parser.d.ts.map +1 -1
  7. package/dist/Parser.js +2 -1
  8. package/dist/ast/builders/condition.d.ts +1 -1
  9. package/dist/ast/builders/condition.d.ts.map +1 -1
  10. package/dist/ast/builders/condition.js +1 -0
  11. package/dist/ast/builders/delim.d.ts +2 -2
  12. package/dist/ast/builders/delim.d.ts.map +1 -1
  13. package/dist/ast/builders/error.d.ts +2 -2
  14. package/dist/ast/builders/error.d.ts.map +1 -1
  15. package/dist/ast/builders/expression.d.ts +2 -2
  16. package/dist/ast/builders/expression.d.ts.map +1 -1
  17. package/dist/ast/builders/expression.js +2 -6
  18. package/dist/ast/builders/group.d.ts +1 -1
  19. package/dist/ast/builders/group.d.ts.map +1 -1
  20. package/dist/ast/builders/group.js +1 -3
  21. package/dist/ast/builders/pos.d.ts +2 -2
  22. package/dist/ast/builders/pos.d.ts.map +1 -1
  23. package/dist/ast/builders/token.d.ts +2 -2
  24. package/dist/ast/builders/token.d.ts.map +1 -1
  25. package/dist/ast/builders/type.d.ts +2 -2
  26. package/dist/ast/builders/type.d.ts.map +1 -1
  27. package/dist/ast/builders/variable.d.ts +3 -3
  28. package/dist/ast/builders/variable.d.ts.map +1 -1
  29. package/dist/ast/createConditionNode.d.ts +1 -1
  30. package/dist/ast/createConditionNode.d.ts.map +1 -1
  31. package/dist/ast/createGroupNode.d.ts +1 -1
  32. package/dist/ast/createGroupNode.d.ts.map +1 -1
  33. package/dist/ast/createToken.d.ts +2 -2
  34. package/dist/ast/createToken.d.ts.map +1 -1
  35. package/dist/ast/createToken.js +2 -2
  36. package/dist/ast/error.d.ts +2 -2
  37. package/dist/ast/error.d.ts.map +1 -1
  38. package/dist/ast/error.js +1 -0
  39. package/dist/ast/handlers.d.ts +23 -23
  40. package/dist/ast/handlers.d.ts.map +1 -1
  41. package/dist/ast/handlers.js +1 -1
  42. package/dist/examples/ParserWithSqlSupport.d.ts +5 -5
  43. package/dist/examples/ParserWithSqlSupport.d.ts.map +1 -1
  44. package/dist/examples/ParserWithSqlSupport.js +2 -0
  45. package/dist/index.js +2 -2
  46. package/dist/internal/ExpressitError.d.ts +2 -2
  47. package/dist/internal/ExpressitError.d.ts.map +1 -1
  48. package/dist/internal/checkParserOpts.js +11 -11
  49. package/dist/package.json.js +2 -60
  50. package/dist/types/ast.d.ts +60 -58
  51. package/dist/types/ast.d.ts.map +1 -1
  52. package/dist/types/ast.js +26 -27
  53. package/dist/types/autocomplete.d.ts +23 -21
  54. package/dist/types/autocomplete.d.ts.map +1 -1
  55. package/dist/types/autocomplete.js +24 -21
  56. package/dist/types/errors.d.ts +12 -10
  57. package/dist/types/errors.d.ts.map +1 -1
  58. package/dist/types/errors.js +8 -7
  59. package/dist/types/index.js +2 -2
  60. package/dist/types/parser.d.ts +3 -3
  61. package/dist/types/parser.d.ts.map +1 -1
  62. package/dist/utils/getCursorInfo.js +2 -0
  63. package/dist/utils/getOppositeDelimiter.d.ts +2 -2
  64. package/dist/utils/getOppositeDelimiter.d.ts.map +1 -1
  65. package/dist/utils/isDelimiter.d.ts +2 -2
  66. package/dist/utils/isDelimiter.d.ts.map +1 -1
  67. package/dist/utils/isParen.d.ts +2 -2
  68. package/dist/utils/isParen.d.ts.map +1 -1
  69. package/dist/utils/isQuote.d.ts +2 -2
  70. package/dist/utils/isQuote.d.ts.map +1 -1
  71. package/package.json +13 -13
  72. package/src/Lexer.ts +98 -88
  73. package/src/Parser.ts +61 -61
  74. package/src/ast/builders/condition.ts +3 -3
  75. package/src/ast/builders/delim.ts +4 -4
  76. package/src/ast/builders/error.ts +3 -3
  77. package/src/ast/builders/expression.ts +4 -8
  78. package/src/ast/builders/group.ts +2 -4
  79. package/src/ast/builders/pos.ts +3 -3
  80. package/src/ast/builders/token.ts +2 -2
  81. package/src/ast/builders/type.ts +2 -2
  82. package/src/ast/builders/variable.ts +5 -5
  83. package/src/ast/createConditionNode.ts +2 -2
  84. package/src/ast/createGroupNode.ts +4 -4
  85. package/src/ast/createToken.ts +6 -6
  86. package/src/ast/error.ts +2 -2
  87. package/src/ast/handlers.ts +20 -20
  88. package/src/examples/ParserWithSqlSupport.ts +11 -5
  89. package/src/internal/ExpressitError.ts +3 -3
  90. package/src/internal/checkParserOpts.ts +11 -11
  91. package/src/types/ast.ts +101 -96
  92. package/src/types/autocomplete.ts +26 -22
  93. package/src/types/errors.ts +18 -13
  94. package/src/types/parser.ts +3 -3
  95. package/src/utils/getOppositeDelimiter.ts +2 -2
  96. package/src/utils/getSurroundingErrors.ts +4 -4
  97. package/src/utils/isDelimiter.ts +3 -3
  98. package/src/utils/isParen.ts +2 -2
  99. package/src/utils/isQuote.ts +2 -2
@@ -1,3 +1,6 @@
1
+ import { type EnumLike } from "@alanscodelog/utils"
2
+ import { enumFromArray } from "@alanscodelog/utils/enumFromArray.js"
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.js"
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
@@ -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
  }