@witchcraft/expressit 0.0.2
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.
- package/README.md +86 -0
- package/dist/ast/builders/array.d.ts +8 -0
- package/dist/ast/builders/array.d.ts.map +1 -0
- package/dist/ast/builders/array.js +31 -0
- package/dist/ast/builders/condition.d.ts +20 -0
- package/dist/ast/builders/condition.d.ts.map +1 -0
- package/dist/ast/builders/condition.js +28 -0
- package/dist/ast/builders/delim.d.ts +11 -0
- package/dist/ast/builders/delim.d.ts.map +1 -0
- package/dist/ast/builders/delim.js +20 -0
- package/dist/ast/builders/error.d.ts +9 -0
- package/dist/ast/builders/error.d.ts.map +1 -0
- package/dist/ast/builders/error.js +16 -0
- package/dist/ast/builders/expression.d.ts +12 -0
- package/dist/ast/builders/expression.d.ts.map +1 -0
- package/dist/ast/builders/expression.js +31 -0
- package/dist/ast/builders/group.d.ts +20 -0
- package/dist/ast/builders/group.d.ts.map +1 -0
- package/dist/ast/builders/group.js +47 -0
- package/dist/ast/builders/index.d.ts +12 -0
- package/dist/ast/builders/index.d.ts.map +1 -0
- package/dist/ast/builders/index.js +24 -0
- package/dist/ast/builders/isFullPos.d.ts +6 -0
- package/dist/ast/builders/isFullPos.d.ts.map +1 -0
- package/dist/ast/builders/isFullPos.js +6 -0
- package/dist/ast/builders/pos.d.ts +21 -0
- package/dist/ast/builders/pos.d.ts.map +1 -0
- package/dist/ast/builders/pos.js +28 -0
- package/dist/ast/builders/token.d.ts +12 -0
- package/dist/ast/builders/token.d.ts.map +1 -0
- package/dist/ast/builders/token.js +26 -0
- package/dist/ast/builders/type.d.ts +6 -0
- package/dist/ast/builders/type.d.ts.map +1 -0
- package/dist/ast/builders/type.js +37 -0
- package/dist/ast/builders/variable.d.ts +17 -0
- package/dist/ast/builders/variable.d.ts.map +1 -0
- package/dist/ast/builders/variable.js +62 -0
- package/dist/ast/classes/ArrayNode.d.ts +18 -0
- package/dist/ast/classes/ArrayNode.d.ts.map +1 -0
- package/dist/ast/classes/ArrayNode.js +55 -0
- package/dist/ast/classes/Condition.d.ts +13 -0
- package/dist/ast/classes/Condition.d.ts.map +1 -0
- package/dist/ast/classes/Condition.js +21 -0
- package/dist/ast/classes/ConditionNode.d.ts +73 -0
- package/dist/ast/classes/ConditionNode.d.ts.map +1 -0
- package/dist/ast/classes/ConditionNode.js +101 -0
- package/dist/ast/classes/ErrorToken.d.ts +27 -0
- package/dist/ast/classes/ErrorToken.d.ts.map +1 -0
- package/dist/ast/classes/ErrorToken.js +47 -0
- package/dist/ast/classes/Expression.d.ts +13 -0
- package/dist/ast/classes/Expression.d.ts.map +1 -0
- package/dist/ast/classes/Expression.js +19 -0
- package/dist/ast/classes/ExpressionNode.d.ts +21 -0
- package/dist/ast/classes/ExpressionNode.d.ts.map +1 -0
- package/dist/ast/classes/ExpressionNode.js +57 -0
- package/dist/ast/classes/GroupNode.d.ts +64 -0
- package/dist/ast/classes/GroupNode.d.ts.map +1 -0
- package/dist/ast/classes/GroupNode.js +69 -0
- package/dist/ast/classes/Node.d.ts +22 -0
- package/dist/ast/classes/Node.d.ts.map +1 -0
- package/dist/ast/classes/Node.js +28 -0
- package/dist/ast/classes/Token.d.ts +27 -0
- package/dist/ast/classes/Token.d.ts.map +1 -0
- package/dist/ast/classes/Token.js +28 -0
- package/dist/ast/classes/ValidToken.d.ts +26 -0
- package/dist/ast/classes/ValidToken.d.ts.map +1 -0
- package/dist/ast/classes/ValidToken.js +49 -0
- package/dist/ast/classes/VariableNode.d.ts +33 -0
- package/dist/ast/classes/VariableNode.d.ts.map +1 -0
- package/dist/ast/classes/VariableNode.js +58 -0
- package/dist/ast/classes/index.d.ts +12 -0
- package/dist/ast/classes/index.d.ts.map +1 -0
- package/dist/ast/classes/index.js +24 -0
- package/dist/ast/handlers.d.ts +42 -0
- package/dist/ast/handlers.d.ts.map +1 -0
- package/dist/ast/handlers.js +150 -0
- package/dist/ast/index.d.ts +4 -0
- package/dist/ast/index.d.ts.map +1 -0
- package/dist/ast/index.js +8 -0
- package/dist/examples/advancedValueComparer.d.ts +3 -0
- package/dist/examples/advancedValueComparer.d.ts.map +1 -0
- package/dist/examples/advancedValueComparer.js +28 -0
- package/dist/examples/shortcutContextParser.d.ts +22 -0
- package/dist/examples/shortcutContextParser.d.ts.map +1 -0
- package/dist/examples/shortcutContextParser.js +126 -0
- package/dist/global.d.js +1 -0
- package/dist/grammar/ParserBase.d.ts +51 -0
- package/dist/grammar/ParserBase.d.ts.map +1 -0
- package/dist/grammar/ParserBase.js +516 -0
- package/dist/grammar/createTokens.d.ts +56 -0
- package/dist/grammar/createTokens.d.ts.map +1 -0
- package/dist/grammar/createTokens.js +843 -0
- package/dist/grammar/index.d.ts +3 -0
- package/dist/grammar/index.d.ts.map +1 -0
- package/dist/grammar/index.js +6 -0
- package/dist/helpers/errors.d.ts +9 -0
- package/dist/helpers/errors.d.ts.map +1 -0
- package/dist/helpers/errors.js +41 -0
- package/dist/helpers/general/applyBoolean.d.ts +3 -0
- package/dist/helpers/general/applyBoolean.d.ts.map +1 -0
- package/dist/helpers/general/applyBoolean.js +17 -0
- package/dist/helpers/general/applyPrefix.d.ts +4 -0
- package/dist/helpers/general/applyPrefix.d.ts.map +1 -0
- package/dist/helpers/general/applyPrefix.js +9 -0
- package/dist/helpers/general/defaultConditionNormalizer.d.ts +3 -0
- package/dist/helpers/general/defaultConditionNormalizer.d.ts.map +1 -0
- package/dist/helpers/general/defaultConditionNormalizer.js +6 -0
- package/dist/helpers/general/defaultKeyParser.d.ts +3 -0
- package/dist/helpers/general/defaultKeyParser.d.ts.map +1 -0
- package/dist/helpers/general/defaultKeyParser.js +8 -0
- package/dist/helpers/general/defaultPrefixApplier.d.ts +3 -0
- package/dist/helpers/general/defaultPrefixApplier.d.ts.map +1 -0
- package/dist/helpers/general/defaultPrefixApplier.js +6 -0
- package/dist/helpers/general/defaultValueComparer.d.ts +3 -0
- package/dist/helpers/general/defaultValueComparer.d.ts.map +1 -0
- package/dist/helpers/general/defaultValueComparer.js +6 -0
- package/dist/helpers/general/index.d.ts +7 -0
- package/dist/helpers/general/index.d.ts.map +1 -0
- package/dist/helpers/general/index.js +14 -0
- package/dist/helpers/index.d.ts +4 -0
- package/dist/helpers/index.d.ts.map +1 -0
- package/dist/helpers/index.js +8 -0
- package/dist/helpers/parser/assignParents.d.ts +4 -0
- package/dist/helpers/parser/assignParents.d.ts.map +1 -0
- package/dist/helpers/parser/assignParents.js +71 -0
- package/dist/helpers/parser/checkParserOpts.d.ts +3 -0
- package/dist/helpers/parser/checkParserOpts.d.ts.map +1 -0
- package/dist/helpers/parser/checkParserOpts.js +126 -0
- package/dist/helpers/parser/extractPosition.d.ts +9 -0
- package/dist/helpers/parser/extractPosition.d.ts.map +1 -0
- package/dist/helpers/parser/extractPosition.js +9 -0
- package/dist/helpers/parser/getUnclosedRightParenCount.d.ts +5 -0
- package/dist/helpers/parser/getUnclosedRightParenCount.d.ts.map +1 -0
- package/dist/helpers/parser/getUnclosedRightParenCount.js +20 -0
- package/dist/helpers/parser/index.d.ts +9 -0
- package/dist/helpers/parser/index.d.ts.map +1 -0
- package/dist/helpers/parser/index.js +18 -0
- package/dist/helpers/parser/parseParserOptions.d.ts +4 -0
- package/dist/helpers/parser/parseParserOptions.d.ts.map +1 -0
- package/dist/helpers/parser/parseParserOptions.js +45 -0
- package/dist/helpers/parser/seal.d.ts +8 -0
- package/dist/helpers/parser/seal.d.ts.map +1 -0
- package/dist/helpers/parser/seal.js +10 -0
- package/dist/helpers/parser/setParent.d.ts +6 -0
- package/dist/helpers/parser/setParent.d.ts.map +1 -0
- package/dist/helpers/parser/setParent.js +4 -0
- package/dist/helpers/parser/unescape.d.ts +3 -0
- package/dist/helpers/parser/unescape.d.ts.map +1 -0
- package/dist/helpers/parser/unescape.js +6 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +17 -0
- package/dist/methods/autocomplete.d.ts +18 -0
- package/dist/methods/autocomplete.d.ts.map +1 -0
- package/dist/methods/autocomplete.js +109 -0
- package/dist/methods/autoreplace.d.ts +13 -0
- package/dist/methods/autoreplace.d.ts.map +1 -0
- package/dist/methods/autoreplace.js +36 -0
- package/dist/methods/autosuggest.d.ts +28 -0
- package/dist/methods/autosuggest.d.ts.map +1 -0
- package/dist/methods/autosuggest.js +371 -0
- package/dist/methods/evaluate.d.ts +11 -0
- package/dist/methods/evaluate.d.ts.map +1 -0
- package/dist/methods/evaluate.js +30 -0
- package/dist/methods/getBestIndex.d.ts +19 -0
- package/dist/methods/getBestIndex.d.ts.map +1 -0
- package/dist/methods/getBestIndex.js +53 -0
- package/dist/methods/getIndexes.d.ts +17 -0
- package/dist/methods/getIndexes.d.ts.map +1 -0
- package/dist/methods/getIndexes.js +97 -0
- package/dist/methods/index.d.ts +9 -0
- package/dist/methods/index.d.ts.map +1 -0
- package/dist/methods/index.js +18 -0
- package/dist/methods/normalize.d.ts +12 -0
- package/dist/methods/normalize.d.ts.map +1 -0
- package/dist/methods/normalize.js +99 -0
- package/dist/methods/validate.d.ts +11 -0
- package/dist/methods/validate.d.ts.map +1 -0
- package/dist/methods/validate.js +111 -0
- package/dist/package.js +7 -0
- package/dist/package.json.js +193 -0
- package/dist/parser.d.ts +58 -0
- package/dist/parser.d.ts.map +1 -0
- package/dist/parser.js +136 -0
- package/dist/types/ast.d.ts +70 -0
- package/dist/types/ast.d.ts.map +1 -0
- package/dist/types/ast.js +29 -0
- package/dist/types/autocomplete.d.ts +143 -0
- package/dist/types/autocomplete.d.ts.map +1 -0
- package/dist/types/autocomplete.js +24 -0
- package/dist/types/errors.d.ts +34 -0
- package/dist/types/errors.d.ts.map +1 -0
- package/dist/types/errors.js +10 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +9 -0
- package/dist/types/parser.d.ts +451 -0
- package/dist/types/parser.d.ts.map +1 -0
- package/dist/types/parser.js +1 -0
- package/dist/utils/extractTokens.d.ts +8 -0
- package/dist/utils/extractTokens.d.ts.map +1 -0
- package/dist/utils/extractTokens.js +50 -0
- package/dist/utils/getCursorInfo.d.ts +7 -0
- package/dist/utils/getCursorInfo.d.ts.map +1 -0
- package/dist/utils/getCursorInfo.js +86 -0
- package/dist/utils/getOppositeDelimiter.d.ts +6 -0
- package/dist/utils/getOppositeDelimiter.d.ts.map +1 -0
- package/dist/utils/getOppositeDelimiter.js +35 -0
- package/dist/utils/getSurroundingErrors.d.ts +25 -0
- package/dist/utils/getSurroundingErrors.d.ts.map +1 -0
- package/dist/utils/getSurroundingErrors.js +37 -0
- package/dist/utils/index.d.ts +10 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +20 -0
- package/dist/utils/isBracket.d.ts +3 -0
- package/dist/utils/isBracket.d.ts.map +1 -0
- package/dist/utils/isBracket.js +7 -0
- package/dist/utils/isDelimiter.d.ts +6 -0
- package/dist/utils/isDelimiter.d.ts.map +1 -0
- package/dist/utils/isDelimiter.js +17 -0
- package/dist/utils/isParen.d.ts +3 -0
- package/dist/utils/isParen.d.ts.map +1 -0
- package/dist/utils/isParen.js +7 -0
- package/dist/utils/isQuote.d.ts +4 -0
- package/dist/utils/isQuote.d.ts.map +1 -0
- package/dist/utils/isQuote.js +7 -0
- package/dist/utils/prettyAst.d.ts +35 -0
- package/dist/utils/prettyAst.d.ts.map +1 -0
- package/dist/utils/prettyAst.js +112 -0
- package/package.json +152 -0
- package/src/ast/builders/array.ts +45 -0
- package/src/ast/builders/condition.ts +56 -0
- package/src/ast/builders/delim.ts +39 -0
- package/src/ast/builders/error.ts +22 -0
- package/src/ast/builders/expression.ts +66 -0
- package/src/ast/builders/group.ts +79 -0
- package/src/ast/builders/index.ts +13 -0
- package/src/ast/builders/isFullPos.ts +10 -0
- package/src/ast/builders/pos.ts +57 -0
- package/src/ast/builders/token.ts +46 -0
- package/src/ast/builders/type.ts +32 -0
- package/src/ast/builders/variable.ts +89 -0
- package/src/ast/classes/ArrayNode.ts +46 -0
- package/src/ast/classes/Condition.ts +22 -0
- package/src/ast/classes/ConditionNode.ts +141 -0
- package/src/ast/classes/ErrorToken.ts +49 -0
- package/src/ast/classes/Expression.ts +26 -0
- package/src/ast/classes/ExpressionNode.ts +62 -0
- package/src/ast/classes/GroupNode.ts +127 -0
- package/src/ast/classes/Node.ts +47 -0
- package/src/ast/classes/Token.ts +59 -0
- package/src/ast/classes/ValidToken.ts +56 -0
- package/src/ast/classes/VariableNode.ts +67 -0
- package/src/ast/classes/index.ts +13 -0
- package/src/ast/handlers.ts +190 -0
- package/src/ast/index.ts +5 -0
- package/src/examples/advancedValueComparer.ts +31 -0
- package/src/examples/shortcutContextParser.ts +140 -0
- package/src/global.d.ts +4 -0
- package/src/grammar/ParserBase.ts +715 -0
- package/src/grammar/createTokens.ts +512 -0
- package/src/grammar/index.ts +4 -0
- package/src/helpers/errors.ts +45 -0
- package/src/helpers/general/applyBoolean.ts +9 -0
- package/src/helpers/general/applyPrefix.ts +7 -0
- package/src/helpers/general/defaultConditionNormalizer.ts +9 -0
- package/src/helpers/general/defaultKeyParser.ts +8 -0
- package/src/helpers/general/defaultPrefixApplier.ts +7 -0
- package/src/helpers/general/defaultValueComparer.ts +7 -0
- package/src/helpers/general/index.ts +8 -0
- package/src/helpers/index.ts +5 -0
- package/src/helpers/parser/assignParents.ts +51 -0
- package/src/helpers/parser/checkParserOpts.ts +143 -0
- package/src/helpers/parser/extractPosition.ts +15 -0
- package/src/helpers/parser/getUnclosedRightParenCount.ts +22 -0
- package/src/helpers/parser/index.ts +10 -0
- package/src/helpers/parser/parseParserOptions.ts +54 -0
- package/src/helpers/parser/seal.ts +14 -0
- package/src/helpers/parser/setParent.ts +5 -0
- package/src/helpers/parser/unescape.ts +4 -0
- package/src/index.ts +7 -0
- package/src/methods/autocomplete.ts +128 -0
- package/src/methods/autoreplace.ts +46 -0
- package/src/methods/autosuggest.ts +543 -0
- package/src/methods/evaluate.ts +37 -0
- package/src/methods/getBestIndex.ts +53 -0
- package/src/methods/getIndexes.ts +99 -0
- package/src/methods/index.ts +10 -0
- package/src/methods/normalize.ts +138 -0
- package/src/methods/validate.ts +141 -0
- package/src/package.js +11 -0
- package/src/parser.ts +183 -0
- package/src/types/ast.ts +148 -0
- package/src/types/autocomplete.ts +152 -0
- package/src/types/errors.ts +40 -0
- package/src/types/index.ts +6 -0
- package/src/types/parser.ts +479 -0
- package/src/utils/extractTokens.ts +67 -0
- package/src/utils/getCursorInfo.ts +106 -0
- package/src/utils/getOppositeDelimiter.ts +36 -0
- package/src/utils/getSurroundingErrors.ts +57 -0
- package/src/utils/index.ts +11 -0
- package/src/utils/isBracket.ts +6 -0
- package/src/utils/isDelimiter.ts +18 -0
- package/src/utils/isParen.ts +6 -0
- package/src/utils/isQuote.ts +6 -0
- package/src/utils/prettyAst.ts +152 -0
package/src/types/ast.ts
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import type { AnyFunction } from "@alanscodelog/utils"
|
|
2
|
+
|
|
3
|
+
import type { ArrayNode } from "../ast/classes/ArrayNode.js"
|
|
4
|
+
import type { ConditionNode } from "../ast/classes/ConditionNode.js"
|
|
5
|
+
import type { ErrorToken } from "../ast/classes/ErrorToken.js"
|
|
6
|
+
import type { ExpressionNode } from "../ast/classes/ExpressionNode.js"
|
|
7
|
+
import type { GroupNode } from "../ast/classes/GroupNode.js"
|
|
8
|
+
import type { ValidToken } from "../ast/classes/ValidToken.js"
|
|
9
|
+
import type { VariableNode } from "../ast/classes/VariableNode.js"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
export type AddParameters<T extends AnyFunction, TExtra extends any[] = [boolean]> = (...args: [...Parameters<T>, ...TExtra]) => ReturnType<T>
|
|
13
|
+
|
|
14
|
+
export enum TOKEN_TYPE {
|
|
15
|
+
VALUE = "VALUE",
|
|
16
|
+
AND = "AND",
|
|
17
|
+
OR = "OR",
|
|
18
|
+
NOT = "NOT",
|
|
19
|
+
BACKTICK = "BACKTICK",
|
|
20
|
+
SINGLEQUOTE = "SINGLEQUOTE",
|
|
21
|
+
DOUBLEQUOTE = "DOUBLEQUOTE",
|
|
22
|
+
PARENL = "PARENL",
|
|
23
|
+
PARENR = "PARENR",
|
|
24
|
+
BRACKETL = "BRACKETL",
|
|
25
|
+
BRACKETR = "BRACKETR",
|
|
26
|
+
OP_EXPANDED_SEP = "OP_EXPANDED",
|
|
27
|
+
OP_CUSTOM = "OP_CUSTOM",
|
|
28
|
+
REGEX = "REGEX",
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @internal
|
|
33
|
+
* Note if the negation operator, `!`, is used as a propertyOperator, this will return the wrong type.
|
|
34
|
+
*/
|
|
35
|
+
export type ExtractTokenType<T extends string> =
|
|
36
|
+
T extends "`"
|
|
37
|
+
? TOKEN_TYPE.BACKTICK
|
|
38
|
+
: T extends `'`
|
|
39
|
+
? TOKEN_TYPE.SINGLEQUOTE
|
|
40
|
+
: T extends `"`
|
|
41
|
+
? TOKEN_TYPE.DOUBLEQUOTE
|
|
42
|
+
: T extends `/`
|
|
43
|
+
? TOKEN_TYPE.REGEX
|
|
44
|
+
: T extends `(`
|
|
45
|
+
? TOKEN_TYPE.PARENL
|
|
46
|
+
: T extends `)`
|
|
47
|
+
? TOKEN_TYPE.PARENR
|
|
48
|
+
: T extends `[`
|
|
49
|
+
? TOKEN_TYPE.BRACKETL
|
|
50
|
+
: T extends `]`
|
|
51
|
+
? TOKEN_TYPE.BRACKETR
|
|
52
|
+
: T extends `and`
|
|
53
|
+
? TOKEN_TYPE.AND
|
|
54
|
+
: T extends `&&`
|
|
55
|
+
? TOKEN_TYPE.AND
|
|
56
|
+
: T extends `&`
|
|
57
|
+
? TOKEN_TYPE.AND
|
|
58
|
+
: T extends `or`
|
|
59
|
+
? TOKEN_TYPE.OR
|
|
60
|
+
: T extends `||`
|
|
61
|
+
? TOKEN_TYPE.OR
|
|
62
|
+
: T extends `|`
|
|
63
|
+
? TOKEN_TYPE.OR
|
|
64
|
+
: T extends `not`
|
|
65
|
+
? TOKEN_TYPE.NOT
|
|
66
|
+
: T extends `!`
|
|
67
|
+
? TOKEN_TYPE.NOT
|
|
68
|
+
: TOKEN_TYPE.VALUE
|
|
69
|
+
|
|
70
|
+
export type TokenParenTypes =
|
|
71
|
+
| TOKEN_TYPE.PARENL
|
|
72
|
+
| TOKEN_TYPE.PARENR
|
|
73
|
+
export type TokenBracketTypes =
|
|
74
|
+
| TOKEN_TYPE.BRACKETL
|
|
75
|
+
| TOKEN_TYPE.BRACKETR
|
|
76
|
+
|
|
77
|
+
export type TokenDelimiterTypes =
|
|
78
|
+
| TokenParenTypes
|
|
79
|
+
| TokenQuoteTypes
|
|
80
|
+
| TokenBracketTypes
|
|
81
|
+
| TOKEN_TYPE.OP_EXPANDED_SEP
|
|
82
|
+
|
|
83
|
+
export type TokenQuoteTypes =
|
|
84
|
+
| TOKEN_TYPE.BACKTICK
|
|
85
|
+
| TOKEN_TYPE.SINGLEQUOTE
|
|
86
|
+
| TOKEN_TYPE.DOUBLEQUOTE
|
|
87
|
+
| TOKEN_TYPE.REGEX
|
|
88
|
+
|
|
89
|
+
export type TokenBooleanTypes =
|
|
90
|
+
| TOKEN_TYPE.AND
|
|
91
|
+
| TOKEN_TYPE.OR
|
|
92
|
+
|
|
93
|
+
export type TokenOperatorTypes =
|
|
94
|
+
| TokenBooleanTypes
|
|
95
|
+
| TOKEN_TYPE.NOT
|
|
96
|
+
|
|
97
|
+
export type TokenPropertyOperatorTypes =
|
|
98
|
+
| TOKEN_TYPE.OP_CUSTOM
|
|
99
|
+
| TOKEN_TYPE.OP_EXPANDED_SEP
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
// export type EmptyObj = Record<"start"|"end", undefined>
|
|
103
|
+
export type EmptyObj = Record<any, never>
|
|
104
|
+
export type FirstConstructorParam<T extends new (...args: any) => any> = ConstructorParameters<T>["0"]
|
|
105
|
+
|
|
106
|
+
export type Position = {
|
|
107
|
+
start: number
|
|
108
|
+
end: number
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export enum AST_TYPE {
|
|
112
|
+
EXPRESSION = "EXPRESSION",
|
|
113
|
+
GROUP = "GROUP",
|
|
114
|
+
ARRAY = "ARRAY",
|
|
115
|
+
CONDITION = "CONDITION",
|
|
116
|
+
VARIABLE = "VARIABLE",
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// #region AST nodes
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* For more easily typing tokens that might or might not be valid.
|
|
123
|
+
*
|
|
124
|
+
* Using @see Token does not work well in certain situations and is also more complex because it has so many generics.
|
|
125
|
+
*/
|
|
126
|
+
export type AnyToken<
|
|
127
|
+
TType extends TOKEN_TYPE = TOKEN_TYPE,
|
|
128
|
+
> =
|
|
129
|
+
| ValidToken<TType>
|
|
130
|
+
| ErrorToken<TType>
|
|
131
|
+
|
|
132
|
+
export type ParserResults = ExpressionNode | ConditionNode | GroupNode | ErrorToken<TOKEN_TYPE.VALUE>
|
|
133
|
+
/* eslint-disable @typescript-eslint/naming-convention */
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
export type Nodes = ExpressionNode | ConditionNode | GroupNode | VariableNode | ArrayNode
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Contains any delimiter tokens some AST nodes ( @see GroupNode and @see VariableNode ) can have.
|
|
140
|
+
*
|
|
141
|
+
* These are usually not important for evaluating an expression but are useful for syntax highlighting.
|
|
142
|
+
*/
|
|
143
|
+
export type NodeDelimiters<TLEFT extends TOKEN_TYPE, TRIGHT extends TOKEN_TYPE = TLEFT> = {
|
|
144
|
+
left: AnyToken<TLEFT>
|
|
145
|
+
right: AnyToken<TRIGHT>
|
|
146
|
+
/** Only exists if regexes are enabled and this is a regex value. */
|
|
147
|
+
flags?: ValidToken<TOKEN_TYPE.VALUE>
|
|
148
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import type { AnyToken, Position } from "./ast.js"
|
|
2
|
+
|
|
3
|
+
import type { ValidToken } from "../ast/classes/ValidToken.js"
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Contains information regarding the tokens around a cursor position. Mostly for internally use by @see autosuggest.
|
|
8
|
+
*
|
|
9
|
+
* Notes:
|
|
10
|
+
*
|
|
11
|
+
* - There are no whitespace tokens because whitespace is not tokenized. `prev`, `at`, `next` properties that contain tokens are set looking only at the list of tokens extracted from the ast by @see extractTokens. This is why there is an extra `whitespace` property to tell us whether there is whitespace (i.e. a hole) between the cursor and the next/prev **valid** tokens or if it can't find any, the start/end of the input.
|
|
12
|
+
* - If next/prev are invalid tokens, note that there are cases where more invalid tokens might follow them. To get them we can use @see getSurroundingErrors or we can just find their index in the tokens list and go forward/backward as needed:
|
|
13
|
+
* ```ts
|
|
14
|
+
* let i = tokens.findIndex(t => t === info.next)
|
|
15
|
+
* while (tokens[i] instanceof ErrorToken) {...}
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* Examples:
|
|
19
|
+
*
|
|
20
|
+
* ```js
|
|
21
|
+
* aaaa| bbbb // tokens: ["aaaa", TokenError(operator), "bbbb"]
|
|
22
|
+
* ^
|
|
23
|
+
* {
|
|
24
|
+
* index: 4, // cursor position
|
|
25
|
+
* at: undefined, // it's not inside a token
|
|
26
|
+
* prev: "aaaa",
|
|
27
|
+
* next: TokenError, // operator is missing to the right
|
|
28
|
+
* // closest valid tokens to either side
|
|
29
|
+
* valid: {
|
|
30
|
+
* prev: "aaaa",
|
|
31
|
+
* next: "bbbb",
|
|
32
|
+
* },
|
|
33
|
+
* // whether there is whitespace between the cursor and the next/prev valid tokens or start/end of input
|
|
34
|
+
* whitespace: {
|
|
35
|
+
* prev: false,
|
|
36
|
+
* next: true,
|
|
37
|
+
* }
|
|
38
|
+
* }
|
|
39
|
+
* ```
|
|
40
|
+
* ```js
|
|
41
|
+
* aaaa || bb|bb && cccc
|
|
42
|
+
* ^
|
|
43
|
+
* {
|
|
44
|
+
* index: 10,
|
|
45
|
+
* at: "bbbb", // it's inside a token
|
|
46
|
+
* prev: "||",
|
|
47
|
+
* next: "&&",
|
|
48
|
+
* valid: {
|
|
49
|
+
* prev: "||",
|
|
50
|
+
* next: "&&",
|
|
51
|
+
* },
|
|
52
|
+
* whitespace: {
|
|
53
|
+
* prev: true,
|
|
54
|
+
* next: true,
|
|
55
|
+
* }
|
|
56
|
+
* }
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
export type CursorInfo = {
|
|
60
|
+
index: number
|
|
61
|
+
/**
|
|
62
|
+
* The token the cursor is inside of. By "inside", we mean the ends of the token are before/after the cursor respectively (e.g. `a|a`, but NOT `|aa` or `aa|`). This token, if defined, is always a valid token, since error tokens have no length.
|
|
63
|
+
*
|
|
64
|
+
* Note though that there are cases where one might be inside a variable but not inside a token because quotes are their own tokens (e.g. `"|var"` or `"var|"`).
|
|
65
|
+
*/
|
|
66
|
+
at?: ValidToken
|
|
67
|
+
/** The first token, valid or invalid, that starts at or after the index position. */
|
|
68
|
+
next?: AnyToken
|
|
69
|
+
/** The first token (going backwards), valid or invalid, that ends at or before the index position. */
|
|
70
|
+
prev?: AnyToken
|
|
71
|
+
/** Closest valid tokens. */
|
|
72
|
+
valid: {
|
|
73
|
+
/** Closest prev valid token. */
|
|
74
|
+
next?: ValidToken
|
|
75
|
+
/** Closest next valid token. */
|
|
76
|
+
prev?: ValidToken
|
|
77
|
+
}
|
|
78
|
+
/** Whether there is whitespace between the cursor and the next/prev valid tokens or start/end of the input. */
|
|
79
|
+
whitespace: {
|
|
80
|
+
/** Whether there is whitespace between the cursor and the next valid token or the end of the input. */
|
|
81
|
+
next: boolean
|
|
82
|
+
/** Whether there is whitespace between the cursor and the prev valid token or the start of the input. */
|
|
83
|
+
prev: boolean
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export enum SUGGESTION_TYPE {
|
|
88
|
+
// can ignore whitespace requirement if replacing with quoted
|
|
89
|
+
VARIABLE = "VARIABLE",
|
|
90
|
+
ARRAY_VALUE = "ARRAY_VALUE",
|
|
91
|
+
VALUE = "VALUE",
|
|
92
|
+
PREFIX = "PREFIX",
|
|
93
|
+
BOOLEAN_WORD_OP = "BOOLEAN_WORD_OP",
|
|
94
|
+
BOOLEAN_SYMBOL_OP = "BOOLEAN_SYMBOL_OP",
|
|
95
|
+
BACKTICK = "BACKTICK",
|
|
96
|
+
DOUBLEQUOTE = "DOUBLEQUOTE",
|
|
97
|
+
SINGLEQUOTE = "SINGLEQUOTE",
|
|
98
|
+
PARENL = "PARENL",
|
|
99
|
+
PARENR = "PARENR",
|
|
100
|
+
PROPERTY = "PROPERTY",
|
|
101
|
+
EXPANDED_PROPERTY_OPERATOR = "EXPANDED_PROPERTY_OPERATOR",
|
|
102
|
+
CUSTOM_PROPERTY_OPERATOR = "CUSTOM_PROPERTY_OPERATOR",
|
|
103
|
+
PROPERTY_SEP = "PROPERTY_SEP",
|
|
104
|
+
BRAKCETR = "BRAKCETR",
|
|
105
|
+
REGEX = "REGEX",
|
|
106
|
+
REGEX_FLAGS = "REGEX_FLAGS",
|
|
107
|
+
/** This is not an oversight, I haven't figured out a fast way to detect left bracket errors. */
|
|
108
|
+
// BRAKCETL = "BRAKCETL",
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* A suggestion entry that describes a type of suggestion.
|
|
113
|
+
*/
|
|
114
|
+
export type Suggestion = {
|
|
115
|
+
type: SUGGESTION_TYPE
|
|
116
|
+
/** The range the suggestion should replace / be inserted at. */
|
|
117
|
+
range: Position
|
|
118
|
+
/** @see CursorInfo */
|
|
119
|
+
cursorInfo: CursorInfo
|
|
120
|
+
/** Tells us any additional requirements for inserting the suggestion. */
|
|
121
|
+
requires: {
|
|
122
|
+
/**
|
|
123
|
+
* Whether the suggestions requires inserting whitespace before/after to keep the intent of the expression consistent. For example, an unquoted variable needs a space between it and a non-symbol operator.
|
|
124
|
+
*
|
|
125
|
+
* In the case of variables this requirement can be ignored though if you insert a quoted value.
|
|
126
|
+
*/
|
|
127
|
+
whitespace: {
|
|
128
|
+
after: boolean
|
|
129
|
+
before: boolean
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* If prefixableGroups is enabled, a suggestion can be a prefix suggestion and this tells us whether `()` needs to be inserted at the end in the case it does not already exist.
|
|
133
|
+
*
|
|
134
|
+
* For example, a user might choose to insert a prefix in this situation `a && |` and we want the result to be `a && prefix()` because without the `()` the prefix will probably be an invalid variable name on insertion.
|
|
135
|
+
*
|
|
136
|
+
* The opposite happens with an existing prefix where the user might want to replace it. `a && existing|(...)`. We want the result to be `a && replacement(...)` not `a && replacement()(...)`.
|
|
137
|
+
*/
|
|
138
|
+
group: boolean
|
|
139
|
+
/** If `prefixableValues` is enabled and a value is prefixed, contains the prefix it was prefixed with. Note that the prefix is included in the range. This way you can choose to suggest/replace just the prefix, just the value or both. */
|
|
140
|
+
prefix: string | false
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Whether the suggestion was created because there was an error token there and it would fix it.
|
|
144
|
+
*/
|
|
145
|
+
isError: boolean
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
export type Completion = {
|
|
150
|
+
suggestion: Suggestion
|
|
151
|
+
value: string
|
|
152
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { DeepPartial } from "@alanscodelog/utils"
|
|
2
|
+
import type { ILexingError, IRecognitionException, IToken } from "chevrotain"
|
|
3
|
+
|
|
4
|
+
import type { ParserOptions } from "./parser.js"
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
export enum ERROR_CODES {
|
|
8
|
+
"PARSER_ERROR" = "PARSER.ERROR",
|
|
9
|
+
"PARSER_POSITION_ERROR" = "PARSER.POSITION",
|
|
10
|
+
"PARSER_CONFLICTING_OPTIONS_ERROR" = "PARSER.OPTIONS.CONFLICTING",
|
|
11
|
+
"PARSER_OPTION_REQUIRED_ERROR" = "PARSER.OPTIONS.CUSTOM_REQUIRED",
|
|
12
|
+
}
|
|
13
|
+
export type ErrorInfo<T extends keyof ErrorInfos> = ErrorInfos[T]
|
|
14
|
+
export type ErrorInfos = {
|
|
15
|
+
[ERROR_CODES.PARSER_ERROR]: {
|
|
16
|
+
input: string
|
|
17
|
+
options: DeepPartial<ParserOptions> | undefined
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
19
|
+
"parsed options": ParserOptions
|
|
20
|
+
error: Error
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
22
|
+
"lexer errors": ILexingError[]
|
|
23
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
24
|
+
"lexed tokens": IToken[]
|
|
25
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
26
|
+
"parser errors": IRecognitionException[]
|
|
27
|
+
}
|
|
28
|
+
[ERROR_CODES.PARSER_POSITION_ERROR]: {
|
|
29
|
+
start?: number
|
|
30
|
+
end?: number
|
|
31
|
+
}
|
|
32
|
+
[ERROR_CODES.PARSER_CONFLICTING_OPTIONS_ERROR]: {
|
|
33
|
+
prohibited: string[]
|
|
34
|
+
invalid: string
|
|
35
|
+
}
|
|
36
|
+
[ERROR_CODES.PARSER_OPTION_REQUIRED_ERROR]: {
|
|
37
|
+
options?: (keyof ParserOptions)[]
|
|
38
|
+
requires: keyof ParserOptions
|
|
39
|
+
}
|
|
40
|
+
}
|