@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
|
@@ -0,0 +1,512 @@
|
|
|
1
|
+
/* eslint-disable camelcase */
|
|
2
|
+
import { escapeRegex, isBlank } from "@alanscodelog/utils"
|
|
3
|
+
import { createToken, Lexer, type TokenType } from "chevrotain"
|
|
4
|
+
|
|
5
|
+
import type { FullParserOptions } from "../types/parser.js"
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
/** Makes it easier to rename the tokens while still returning a properly typed record of them.*/
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
10
|
+
enum $T {
|
|
11
|
+
_ = "_", // whitespace,
|
|
12
|
+
ANY = "ANY",
|
|
13
|
+
QUOTE_ANY = "QUOTE_ANY",
|
|
14
|
+
QUOTE_SINGLE = "QUOTE_SINGLE",
|
|
15
|
+
QUOTE_DOUBLE = "QUOTE_DOUBLE",
|
|
16
|
+
QUOTE_BACKTICK = "QUOTE_BACKTICK",
|
|
17
|
+
VALUE = "VALUE",
|
|
18
|
+
REGEX_ANY = "REGEX_ANY",
|
|
19
|
+
VALUE_UNQUOTED = "VALUE_UNQUOTED",
|
|
20
|
+
BRACKET_VALUE_UNQUOTED = "BRACKET_VALUE_UNQUOTED",
|
|
21
|
+
VALUE_REGEX = "VALUE_REGEX",
|
|
22
|
+
VALUE_NOT_SINGLE = "VALUE_NOT_SINGLE",
|
|
23
|
+
VALUE_NOT_DOUBLE = "VALUE_NOT_DOUBLE",
|
|
24
|
+
VALUE_NOT_BACKTICK = "VALUE_NOT_BACKTICK",
|
|
25
|
+
VALUE_FOR_SINGLE = "VALUE_FOR_SINGLE",
|
|
26
|
+
VALUE_FOR_DOUBLE = "VALUE_FOR_DOUBLE",
|
|
27
|
+
VALUE_FOR_BACKTICK = "VALUE_FOR_BACKTICK",
|
|
28
|
+
OPERATOR_OR = "OPERATOR_OR",
|
|
29
|
+
OPERATOR_AND = "OPERATOR_AND",
|
|
30
|
+
OPERATOR_NOT = "OPERATOR_NOT",
|
|
31
|
+
SYM_OR = "SYM_OR",
|
|
32
|
+
SYM_AND = "SYM_AND",
|
|
33
|
+
SYM_NOT = "SYM_NOT",
|
|
34
|
+
WORD_OR = "WORD_OR",
|
|
35
|
+
WORD_AND = "WORD_AND",
|
|
36
|
+
WORD_NOT = "WORD_NOT",
|
|
37
|
+
REGEX_START = "REGEX_START",
|
|
38
|
+
REGEX_END = "REGEX_END",
|
|
39
|
+
QUOTE_SINGLE_START = "QUOTE_SINGLE_START",
|
|
40
|
+
QUOTE_DOUBLE_START = "QUOTE_DOUBLE_START",
|
|
41
|
+
QUOTE_BACKTICK_START = "QUOTE_BACKTICK_START",
|
|
42
|
+
QUOTE_SINGLE_END = "QUOTE_SINGLE_END",
|
|
43
|
+
QUOTE_DOUBLE_END = "QUOTE_DOUBLE_END",
|
|
44
|
+
QUOTE_BACKTICK_END = "QUOTE_BACKTICK_END",
|
|
45
|
+
EXP_PROP_OP = "EXP_PROP_OP",
|
|
46
|
+
CUSTOM_PROP_OP = "CUSTOM_PROP_OP",
|
|
47
|
+
PAREN_L = "PAREN_L",
|
|
48
|
+
PAREN_R = "PAREN_R",
|
|
49
|
+
BRACKET_L = "BRACKET_L",
|
|
50
|
+
BRACKET_R = "BRACKET_R",
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Should only be used internally by the lexer.
|
|
54
|
+
type LexerOnly =
|
|
55
|
+
// | $T.ANY - special case, needed when we want to consume any token
|
|
56
|
+
| $T.VALUE_NOT_BACKTICK
|
|
57
|
+
| $T.VALUE_NOT_DOUBLE
|
|
58
|
+
| $T.VALUE_NOT_SINGLE
|
|
59
|
+
| $T.SYM_OR
|
|
60
|
+
| $T.SYM_AND
|
|
61
|
+
| $T.WORD_OR
|
|
62
|
+
| $T.WORD_AND
|
|
63
|
+
// | $T.REGEX_START - special case, needed for recovering
|
|
64
|
+
| $T.REGEX_END
|
|
65
|
+
| $T.QUOTE_SINGLE_START
|
|
66
|
+
| $T.QUOTE_DOUBLE_START
|
|
67
|
+
| $T.QUOTE_BACKTICK_START
|
|
68
|
+
| $T.QUOTE_SINGLE_END
|
|
69
|
+
| $T.QUOTE_DOUBLE_END
|
|
70
|
+
| $T.QUOTE_BACKTICK_END
|
|
71
|
+
| $T.WORD_NOT
|
|
72
|
+
// | $T.SYM_NOT - special case, allowed because custom prop operators can be the "symbol" negation tokens
|
|
73
|
+
|
|
74
|
+
function clone(token: TokenType): TokenType {
|
|
75
|
+
const t: any = {}
|
|
76
|
+
if (token.name !== undefined) t.name = token.name
|
|
77
|
+
if (token.CATEGORIES !== undefined) t.categories = token.CATEGORIES
|
|
78
|
+
if (token.GROUP !== undefined) t.group = token.GROUP
|
|
79
|
+
if (token.LINE_BREAKS !== undefined) t.line_breaks = token.LINE_BREAKS
|
|
80
|
+
if (token.LONGER_ALT !== undefined) t.longet_alt = token.LONGER_ALT
|
|
81
|
+
if (token.PATTERN !== undefined) t.pattern = token.PATTERN
|
|
82
|
+
if (token.PUSH_MODE !== undefined) t.push_mode = token.PUSH_MODE
|
|
83
|
+
if (token.POP_MODE !== undefined) t.pop_mode = token.POP_MODE
|
|
84
|
+
return createToken(t)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function changePushMode(tokens: TokenType[], mode: string | undefined | ((str?: string) => string)): TokenType[] {
|
|
88
|
+
return tokens.map(_ => clone({
|
|
89
|
+
..._,
|
|
90
|
+
...(mode ? { PUSH_MODE: typeof mode === "function" ? mode(_.PUSH_MODE) : mode } : {}),
|
|
91
|
+
}))
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function createTokens<T extends {} = {}>(opts: FullParserOptions<T>): {
|
|
95
|
+
tokens: Record<Exclude<$T, LexerOnly>, TokenType>
|
|
96
|
+
lexer: Lexer
|
|
97
|
+
info: {
|
|
98
|
+
expandedSepAlsoCustom: boolean
|
|
99
|
+
customOpAlsoNegation: boolean
|
|
100
|
+
}
|
|
101
|
+
} {
|
|
102
|
+
const $: Record<$T, TokenType> = {} as any
|
|
103
|
+
|
|
104
|
+
/* #region TOKEN CATEGORIES */
|
|
105
|
+
|
|
106
|
+
$[$T.ANY] = createToken({
|
|
107
|
+
name: $T.ANY,
|
|
108
|
+
pattern: Lexer.NA,
|
|
109
|
+
})
|
|
110
|
+
$[$T.QUOTE_ANY] = createToken({
|
|
111
|
+
name: $T.QUOTE_ANY,
|
|
112
|
+
pattern: Lexer.NA,
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
// so we can easily match start/end tokens in wrong positions
|
|
116
|
+
$[$T.QUOTE_SINGLE] = createToken({
|
|
117
|
+
name: $T.QUOTE_SINGLE,
|
|
118
|
+
pattern: Lexer.NA,
|
|
119
|
+
})
|
|
120
|
+
$[$T.QUOTE_DOUBLE] = createToken({
|
|
121
|
+
name: $T.QUOTE_DOUBLE,
|
|
122
|
+
pattern: Lexer.NA,
|
|
123
|
+
})
|
|
124
|
+
$[$T.QUOTE_BACKTICK] = createToken({
|
|
125
|
+
name: $T.QUOTE_BACKTICK,
|
|
126
|
+
pattern: Lexer.NA,
|
|
127
|
+
})
|
|
128
|
+
$[$T.REGEX_ANY] = createToken({
|
|
129
|
+
name: $T.REGEX_ANY,
|
|
130
|
+
pattern: Lexer.NA,
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
// required to make bracket values work
|
|
134
|
+
// see the bracket modes at the end for an explanation
|
|
135
|
+
$[$T.VALUE_FOR_SINGLE] = createToken({
|
|
136
|
+
name: $T.VALUE_FOR_SINGLE,
|
|
137
|
+
pattern: Lexer.NA,
|
|
138
|
+
})
|
|
139
|
+
$[$T.VALUE_FOR_DOUBLE] = createToken({
|
|
140
|
+
name: $T.VALUE_FOR_DOUBLE,
|
|
141
|
+
pattern: Lexer.NA,
|
|
142
|
+
})
|
|
143
|
+
$[$T.VALUE_FOR_BACKTICK] = createToken({
|
|
144
|
+
name: $T.VALUE_FOR_BACKTICK,
|
|
145
|
+
pattern: Lexer.NA,
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
$[$T.OPERATOR_OR] = createToken({
|
|
149
|
+
name: $T.OPERATOR_OR,
|
|
150
|
+
pattern: Lexer.NA,
|
|
151
|
+
})
|
|
152
|
+
$[$T.OPERATOR_AND] = createToken({
|
|
153
|
+
name: $T.OPERATOR_AND,
|
|
154
|
+
pattern: Lexer.NA,
|
|
155
|
+
})
|
|
156
|
+
$[$T.OPERATOR_NOT] = createToken({
|
|
157
|
+
name: $T.OPERATOR_NOT,
|
|
158
|
+
pattern: Lexer.NA,
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
$[$T.VALUE] = createToken({
|
|
162
|
+
name: $T.VALUE,
|
|
163
|
+
pattern: Lexer.NA,
|
|
164
|
+
})
|
|
165
|
+
/* #regionend */
|
|
166
|
+
/* #region ACTUAL TOKENS */
|
|
167
|
+
|
|
168
|
+
$[$T._] = createToken({
|
|
169
|
+
name: $T._,
|
|
170
|
+
pattern: /\s+/,
|
|
171
|
+
group: Lexer.SKIPPED,
|
|
172
|
+
line_breaks: true,
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
$[$T.REGEX_START] = createToken({
|
|
177
|
+
name: $T.REGEX_START,
|
|
178
|
+
push_mode: "notRegex",
|
|
179
|
+
pattern: /\//,
|
|
180
|
+
categories: [$[$T.REGEX_ANY], $[$T.ANY]],
|
|
181
|
+
})
|
|
182
|
+
$[$T.REGEX_END] = createToken({
|
|
183
|
+
name: $T.REGEX_END,
|
|
184
|
+
push_mode: "main",
|
|
185
|
+
pattern: /\/[a-z]*/,
|
|
186
|
+
categories: [$[$T.REGEX_ANY], $[$T.ANY]],
|
|
187
|
+
})
|
|
188
|
+
$[$T.VALUE_REGEX] = createToken({
|
|
189
|
+
name: $T.VALUE_REGEX,
|
|
190
|
+
push_mode: "regexEnd",
|
|
191
|
+
line_breaks: true,
|
|
192
|
+
categories: [$[$T.ANY]],
|
|
193
|
+
pattern: {
|
|
194
|
+
exec: (text, start) => {
|
|
195
|
+
let end = start
|
|
196
|
+
let inGroup = 0
|
|
197
|
+
let char = text[end]
|
|
198
|
+
let prevEscaped = false
|
|
199
|
+
while (char !== undefined && (char !== "/" || inGroup > 0 || prevEscaped)) {
|
|
200
|
+
if (char === "[") inGroup++
|
|
201
|
+
// normally something like /][/ will error, but we pretend the initial "negative" ] are ignored so things like /][]/ won't
|
|
202
|
+
if (char === "]" && inGroup > 0) inGroup--
|
|
203
|
+
if (char === "\\") {
|
|
204
|
+
if (!prevEscaped) {
|
|
205
|
+
prevEscaped = true
|
|
206
|
+
} else {
|
|
207
|
+
prevEscaped = false
|
|
208
|
+
}
|
|
209
|
+
} else {
|
|
210
|
+
prevEscaped &&= false
|
|
211
|
+
}
|
|
212
|
+
end++
|
|
213
|
+
char = text[end]
|
|
214
|
+
}
|
|
215
|
+
if (start === end) return null
|
|
216
|
+
return [text.substring(start, end)]
|
|
217
|
+
},
|
|
218
|
+
},
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
$[$T.QUOTE_SINGLE_START] = createToken({
|
|
223
|
+
name: $T.QUOTE_SINGLE_START,
|
|
224
|
+
pattern: /'/,
|
|
225
|
+
push_mode: "notSingle",
|
|
226
|
+
categories: [$[$T.QUOTE_SINGLE], $[$T.QUOTE_ANY], $[$T.ANY]],
|
|
227
|
+
})
|
|
228
|
+
$[$T.QUOTE_DOUBLE_START] = createToken({
|
|
229
|
+
name: $T.QUOTE_DOUBLE_START,
|
|
230
|
+
pattern: /"/,
|
|
231
|
+
push_mode: "notDouble",
|
|
232
|
+
categories: [$[$T.QUOTE_DOUBLE], $[$T.QUOTE_ANY], $[$T.ANY]],
|
|
233
|
+
})
|
|
234
|
+
$[$T.QUOTE_BACKTICK_START] = createToken({
|
|
235
|
+
name: $T.QUOTE_BACKTICK_START,
|
|
236
|
+
pattern: /`/,
|
|
237
|
+
push_mode: "notBacktick",
|
|
238
|
+
categories: [$[$T.QUOTE_BACKTICK], $[$T.QUOTE_ANY], $[$T.ANY]],
|
|
239
|
+
})
|
|
240
|
+
$[$T.QUOTE_SINGLE_END] = createToken({
|
|
241
|
+
name: $T.QUOTE_SINGLE_END,
|
|
242
|
+
pattern: /'/,
|
|
243
|
+
push_mode: "main",
|
|
244
|
+
categories: [$[$T.QUOTE_SINGLE], $[$T.QUOTE_ANY], $[$T.ANY]],
|
|
245
|
+
})
|
|
246
|
+
$[$T.QUOTE_DOUBLE_END] = createToken({
|
|
247
|
+
name: $T.QUOTE_DOUBLE_END,
|
|
248
|
+
pattern: /"/,
|
|
249
|
+
push_mode: "main",
|
|
250
|
+
categories: [$[$T.QUOTE_DOUBLE], $[$T.QUOTE_ANY], $[$T.ANY]],
|
|
251
|
+
})
|
|
252
|
+
$[$T.QUOTE_BACKTICK_END] = createToken({
|
|
253
|
+
name: $T.QUOTE_BACKTICK_END,
|
|
254
|
+
pattern: /`/,
|
|
255
|
+
push_mode: "main",
|
|
256
|
+
categories: [$[$T.QUOTE_BACKTICK], $[$T.QUOTE_ANY], $[$T.ANY]],
|
|
257
|
+
})
|
|
258
|
+
$[$T.VALUE_NOT_SINGLE] = createToken({
|
|
259
|
+
name: $T.VALUE_NOT_SINGLE,
|
|
260
|
+
pattern: /(\\[\s\S]|[^'])+/,
|
|
261
|
+
push_mode: "endQuotes",
|
|
262
|
+
categories: [$[$T.VALUE], $[$T.VALUE_FOR_SINGLE], $[$T.ANY]],
|
|
263
|
+
line_breaks: true,
|
|
264
|
+
})
|
|
265
|
+
$[$T.VALUE_NOT_DOUBLE] = createToken({
|
|
266
|
+
name: $T.VALUE_NOT_DOUBLE,
|
|
267
|
+
pattern: /(\\[\s\S]|[^"])+/,
|
|
268
|
+
push_mode: "endQuotes",
|
|
269
|
+
categories: [$[$T.VALUE], $[$T.VALUE_FOR_DOUBLE], $[$T.ANY]],
|
|
270
|
+
line_breaks: true,
|
|
271
|
+
})
|
|
272
|
+
$[$T.VALUE_NOT_BACKTICK] = createToken({
|
|
273
|
+
name: $T.VALUE_NOT_BACKTICK,
|
|
274
|
+
pattern: /(\\[\s\S]|[^`])+/,
|
|
275
|
+
push_mode: "endQuotes",
|
|
276
|
+
categories: [$[$T.VALUE], $[$T.VALUE_FOR_BACKTICK], $[$T.ANY]],
|
|
277
|
+
line_breaks: true,
|
|
278
|
+
})
|
|
279
|
+
|
|
280
|
+
const symOrs = opts.keywords.or.filter(_ => _.isSymbol).map(_ => escapeRegex(_.value))
|
|
281
|
+
const symAnds = opts.keywords.and.filter(_ => _.isSymbol).map(_ => escapeRegex(_.value))
|
|
282
|
+
const symNots = opts.keywords.not.filter(_ => _.isSymbol).map(_ => escapeRegex(_.value))
|
|
283
|
+
const wordOrs = opts.keywords.or.filter(_ => !_.isSymbol).map(_ => escapeRegex(_.value))
|
|
284
|
+
const wordAnds = opts.keywords.and.filter(_ => !_.isSymbol).map(_ => escapeRegex(_.value))
|
|
285
|
+
const wordNots = opts.keywords.not.filter(_ => !_.isSymbol).map(_ => escapeRegex(_.value))
|
|
286
|
+
let syms = [...symOrs, ...symAnds, ...symNots]
|
|
287
|
+
|
|
288
|
+
const customPropertyOperators = (opts.customPropertyOperators ?? []).map(_ => escapeRegex(_))
|
|
289
|
+
|
|
290
|
+
const expandedPropertySeparator = escapeRegex(opts.expandedPropertySeparator ?? "")
|
|
291
|
+
|
|
292
|
+
if (expandedPropertySeparator) syms.push(expandedPropertySeparator)
|
|
293
|
+
if (customPropertyOperators) syms = syms.concat(customPropertyOperators)
|
|
294
|
+
if (opts.regexValues) syms.push("\\/")
|
|
295
|
+
if (opts.arrayValues) {
|
|
296
|
+
syms.push("\\[")
|
|
297
|
+
// [ makes the lexer enter a bracket value, but ] should not be ignored by VALUE_UNQUOTED in case we get input like just `]` or `...]` which should be parsed as values
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// future change to custom pattern, should be faster
|
|
301
|
+
$[$T.VALUE_UNQUOTED] = createToken({
|
|
302
|
+
name: $T.VALUE_UNQUOTED,
|
|
303
|
+
pattern: new RegExp(`(\\\\[\\s\\S]|(${syms.length > 0 ? `(?!(${syms.join("|")}))` : ``}[^ \\t()'"\`\\\\]))+`),
|
|
304
|
+
push_mode: "endQuotes",
|
|
305
|
+
categories: [$[$T.VALUE], $[$T.ANY]],
|
|
306
|
+
})
|
|
307
|
+
|
|
308
|
+
$[$T.BRACKET_VALUE_UNQUOTED] = createToken({
|
|
309
|
+
name: $T.BRACKET_VALUE_UNQUOTED,
|
|
310
|
+
pattern: /(\\[\s\S]|([^ \]\\t'"`\\]))+/,
|
|
311
|
+
push_mode: "bracket_endQuotes",
|
|
312
|
+
categories: [$[$T.VALUE], $[$T.ANY]],
|
|
313
|
+
})
|
|
314
|
+
|
|
315
|
+
// operators are only added if they're enabled, otherwise cheverotain will complain about empty regex expressions
|
|
316
|
+
const operators = []
|
|
317
|
+
|
|
318
|
+
$[$T.SYM_OR] = createToken({
|
|
319
|
+
name: $T.SYM_OR,
|
|
320
|
+
pattern: new RegExp(`(${symOrs.join("|")})`),
|
|
321
|
+
categories: [$[$T.OPERATOR_OR], $[$T.ANY]],
|
|
322
|
+
})
|
|
323
|
+
if (symOrs.length > 0) operators.push($[$T.SYM_OR])
|
|
324
|
+
|
|
325
|
+
$[$T.SYM_AND] = createToken({
|
|
326
|
+
name: $T.SYM_AND,
|
|
327
|
+
pattern: new RegExp(`(${symAnds.join("|")})`),
|
|
328
|
+
categories: [$[$T.OPERATOR_AND], $[$T.ANY]],
|
|
329
|
+
})
|
|
330
|
+
if (symAnds.length > 0) operators.push($[$T.SYM_AND])
|
|
331
|
+
|
|
332
|
+
$[$T.SYM_NOT] = createToken({
|
|
333
|
+
name: $T.SYM_NOT,
|
|
334
|
+
pattern: new RegExp(`(${symNots.join("|")})`),
|
|
335
|
+
categories: [$[$T.OPERATOR_NOT], $[$T.ANY]],
|
|
336
|
+
})
|
|
337
|
+
if (symNots.length > 0) operators.push($[$T.SYM_NOT])
|
|
338
|
+
|
|
339
|
+
$[$T.WORD_OR] = createToken({
|
|
340
|
+
name: $T.WORD_OR,
|
|
341
|
+
pattern: new RegExp(`(${wordOrs.join("|")})`),
|
|
342
|
+
longer_alt: $[$T.VALUE_UNQUOTED],
|
|
343
|
+
categories: [$[$T.OPERATOR_OR], $[$T.ANY]],
|
|
344
|
+
})
|
|
345
|
+
if (wordOrs.length > 0) operators.push($[$T.WORD_OR])
|
|
346
|
+
|
|
347
|
+
$[$T.WORD_AND] = createToken({
|
|
348
|
+
name: $T.WORD_AND,
|
|
349
|
+
pattern: new RegExp(`(${wordAnds.join("|")})`),
|
|
350
|
+
longer_alt: $[$T.VALUE_UNQUOTED],
|
|
351
|
+
categories: [$[$T.OPERATOR_AND], $[$T.ANY]],
|
|
352
|
+
})
|
|
353
|
+
if (wordAnds.length > 0) operators.push($[$T.WORD_AND])
|
|
354
|
+
|
|
355
|
+
$[$T.WORD_NOT] = createToken({
|
|
356
|
+
name: $T.WORD_NOT,
|
|
357
|
+
pattern: new RegExp(`(${wordNots.join("|")})`),
|
|
358
|
+
longer_alt: $[$T.VALUE_UNQUOTED],
|
|
359
|
+
categories: [$[$T.OPERATOR_NOT], $[$T.ANY]],
|
|
360
|
+
})
|
|
361
|
+
if (wordNots.length > 0) operators.push($[$T.WORD_NOT])
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
/* region Operators */
|
|
365
|
+
$[$T.EXP_PROP_OP] = createToken({
|
|
366
|
+
name: $T.EXP_PROP_OP,
|
|
367
|
+
pattern: new RegExp(`${expandedPropertySeparator}`),
|
|
368
|
+
categories: [$[$T.ANY]],
|
|
369
|
+
})
|
|
370
|
+
|
|
371
|
+
if (!isBlank(expandedPropertySeparator)) operators.splice(0, 0, $[$T.EXP_PROP_OP])
|
|
372
|
+
|
|
373
|
+
$[$T.CUSTOM_PROP_OP] = createToken({
|
|
374
|
+
name: $T.CUSTOM_PROP_OP,
|
|
375
|
+
pattern: new RegExp(`(${customPropertyOperators.join("|")})`),
|
|
376
|
+
categories: [$[$T.ANY]],
|
|
377
|
+
})
|
|
378
|
+
|
|
379
|
+
// only add custom operator if it's pattern doesn't match the not or expanded operator separator patterns (which can be the same)
|
|
380
|
+
// otherwise chevrotain will complain when the regex patterns match exactly
|
|
381
|
+
const customOpEqualsExpandedOrNegationToken = [$[$T.SYM_NOT].PATTERN, $[$T.EXP_PROP_OP].PATTERN]
|
|
382
|
+
.find(_ => _?.toString() === $[$T.CUSTOM_PROP_OP].PATTERN?.toString()) !== undefined
|
|
383
|
+
|
|
384
|
+
if (
|
|
385
|
+
(customPropertyOperators?.length ?? 0) > 0 &&
|
|
386
|
+
!customOpEqualsExpandedOrNegationToken
|
|
387
|
+
) operators.splice(1, 0, $[$T.CUSTOM_PROP_OP])
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
// for parser
|
|
391
|
+
const expandedSepAlsoCustom = customPropertyOperators.includes(expandedPropertySeparator)
|
|
392
|
+
const customOpAlsoNegation = symNots.length > 0 &&
|
|
393
|
+
customPropertyOperators?.find(_ => symNots.includes(_)) !== undefined
|
|
394
|
+
/* regionend */
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
$[$T.PAREN_L] = createToken({
|
|
398
|
+
name: $T.PAREN_L,
|
|
399
|
+
pattern: /\(/,
|
|
400
|
+
categories: [$[$T.ANY]],
|
|
401
|
+
})
|
|
402
|
+
$[$T.PAREN_R] = createToken({
|
|
403
|
+
name: $T.PAREN_R,
|
|
404
|
+
pattern: /\)/,
|
|
405
|
+
categories: [$[$T.ANY]],
|
|
406
|
+
})
|
|
407
|
+
const parens = [$[$T.PAREN_L], $[$T.PAREN_R]]
|
|
408
|
+
// they still need to be defined for the parser, but if opts.arrayValues is false, they're never in any of the lexer modes
|
|
409
|
+
$[$T.BRACKET_L] = createToken({
|
|
410
|
+
name: $T.BRACKET_L,
|
|
411
|
+
pattern: /\[/,
|
|
412
|
+
push_mode: "bracket_main",
|
|
413
|
+
categories: [$[$T.ANY]],
|
|
414
|
+
})
|
|
415
|
+
$[$T.BRACKET_R] = createToken({
|
|
416
|
+
name: $T.BRACKET_R,
|
|
417
|
+
pattern: /\]/,
|
|
418
|
+
push_mode: "main",
|
|
419
|
+
categories: [$[$T.ANY]],
|
|
420
|
+
})
|
|
421
|
+
|
|
422
|
+
const quotes = [
|
|
423
|
+
$[$T.QUOTE_SINGLE_START],
|
|
424
|
+
$[$T.QUOTE_DOUBLE_START],
|
|
425
|
+
$[$T.QUOTE_BACKTICK_START],
|
|
426
|
+
]
|
|
427
|
+
|
|
428
|
+
const quotesEnds = [
|
|
429
|
+
$[$T.QUOTE_SINGLE_END],
|
|
430
|
+
$[$T.QUOTE_DOUBLE_END],
|
|
431
|
+
$[$T.QUOTE_BACKTICK_END],
|
|
432
|
+
]
|
|
433
|
+
|
|
434
|
+
/* #regionend */
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
const toBracket = (mode?: string): string => `bracket_${mode!}`
|
|
438
|
+
const lexerOptions = {
|
|
439
|
+
modes: {
|
|
440
|
+
main: [
|
|
441
|
+
$[$T._],
|
|
442
|
+
...parens,
|
|
443
|
+
...(opts.arrayValues ? [$[$T.BRACKET_L]] : []), // moves to bracket_main until a bracket pops it back out
|
|
444
|
+
...operators,
|
|
445
|
+
...quotes, // moves to not*
|
|
446
|
+
...(opts.regexValues ? [$[$T.REGEX_START]] : []), // moves to notRegex
|
|
447
|
+
$[$T.VALUE_UNQUOTED], // moves to maybeQuoteError
|
|
448
|
+
],
|
|
449
|
+
endQuotes: [
|
|
450
|
+
$[$T._],
|
|
451
|
+
...parens,
|
|
452
|
+
...(opts.arrayValues ? [$[$T.BRACKET_L]] : []), // moves to bracket_main until a bracket pops it back out to main
|
|
453
|
+
...operators,
|
|
454
|
+
...quotesEnds,
|
|
455
|
+
$[$T.VALUE_UNQUOTED], // moves to maybeQuoteError
|
|
456
|
+
...(opts.regexValues ? [$[$T.REGEX_START]] : []), // error
|
|
457
|
+
],
|
|
458
|
+
// we can have situations like `a"` where the left quote is missing
|
|
459
|
+
// we want the quote to match a quote end so that it pushes the state to main again, instead of shifting how everything is parsed
|
|
460
|
+
maybeQuoteError: [
|
|
461
|
+
...quotesEnds,
|
|
462
|
+
...(opts.regexValues ? [$[$T.REGEX_END]] : []),
|
|
463
|
+
],
|
|
464
|
+
// all move to endQuotes
|
|
465
|
+
notSingle: [$[$T.VALUE_NOT_SINGLE], $[$T.QUOTE_SINGLE_END]],
|
|
466
|
+
notDouble: [$[$T.VALUE_NOT_DOUBLE], $[$T.QUOTE_DOUBLE_END]],
|
|
467
|
+
notBacktick: [$[$T.VALUE_NOT_BACKTICK], $[$T.QUOTE_BACKTICK_END]],
|
|
468
|
+
...(opts.regexValues
|
|
469
|
+
? {
|
|
470
|
+
notRegex: [
|
|
471
|
+
$[$T.VALUE_REGEX],
|
|
472
|
+
$[$T.REGEX_END], // regex is empty
|
|
473
|
+
], // moves to regexEnd
|
|
474
|
+
regexEnd: [$[$T.REGEX_END]], // moves to main
|
|
475
|
+
} : {}),
|
|
476
|
+
...(opts.arrayValues
|
|
477
|
+
? {
|
|
478
|
+
bracket_main: [
|
|
479
|
+
...changePushMode(quotes, toBracket),
|
|
480
|
+
$[$T.BRACKET_R], // move back to main
|
|
481
|
+
$[$T.BRACKET_VALUE_UNQUOTED],
|
|
482
|
+
],
|
|
483
|
+
// all the following follow the same logic as the non-bracket modes, except operators and parens and regexes are not supported and are just parsed as values with BRACKET_VALUE_UNQUOTED
|
|
484
|
+
// the following tokens are also cloned to push differently: quotes (above), quote values, and quote ends
|
|
485
|
+
// they can still be matched because their parent categories are also cloned and it's those we match against
|
|
486
|
+
bracket_endQuotes: [
|
|
487
|
+
$[$T._],
|
|
488
|
+
...changePushMode(quotesEnds, toBracket),
|
|
489
|
+
$[$T.BRACKET_R], // move back to main
|
|
490
|
+
$[$T.BRACKET_VALUE_UNQUOTED],
|
|
491
|
+
],
|
|
492
|
+
bracket_maybeQuoteError: changePushMode(quotesEnds, toBracket),
|
|
493
|
+
bracket_notSingle: changePushMode([
|
|
494
|
+
$[$T.VALUE_NOT_SINGLE], $[$T.QUOTE_SINGLE_END],
|
|
495
|
+
], toBracket),
|
|
496
|
+
bracket_notDouble: changePushMode([
|
|
497
|
+
$[$T.VALUE_NOT_DOUBLE], $[$T.QUOTE_DOUBLE_END],
|
|
498
|
+
], toBracket),
|
|
499
|
+
bracket_notBacktick: changePushMode([
|
|
500
|
+
$[$T.VALUE_NOT_BACKTICK], $[$T.QUOTE_BACKTICK_END],
|
|
501
|
+
], toBracket),
|
|
502
|
+
} : {}
|
|
503
|
+
),
|
|
504
|
+
|
|
505
|
+
},
|
|
506
|
+
defaultMode: "main",
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
const lexer = new Lexer(lexerOptions) // only because we don't care about newlines
|
|
510
|
+
return { tokens: $, lexer, info: { expandedSepAlsoCustom, customOpAlsoNegation } }
|
|
511
|
+
}
|
|
512
|
+
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { Keys } from "@alanscodelog/utils"
|
|
2
|
+
import { crop, indent, pretty } from "@alanscodelog/utils"
|
|
3
|
+
|
|
4
|
+
// @ts-expect-error todo
|
|
5
|
+
import { repository, version } from "../package.js"
|
|
6
|
+
import type { ERROR_CODES, ErrorInfo } from "../types/errors.js"
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
export class BooleanParserLibraryError<T extends ERROR_CODES> extends Error {
|
|
10
|
+
version: string = version
|
|
11
|
+
|
|
12
|
+
repo: string = repository
|
|
13
|
+
|
|
14
|
+
type: T
|
|
15
|
+
|
|
16
|
+
info: ErrorInfo<T>
|
|
17
|
+
|
|
18
|
+
constructor(type: T, info: ErrorInfo<T>, message?: string) {
|
|
19
|
+
super(
|
|
20
|
+
message
|
|
21
|
+
? `${message}\n${pretty(info)}`
|
|
22
|
+
: `This error should never happen, please file a bug report at ${repository}/issues with the following information: \n${crop`
|
|
23
|
+
version: ${version}
|
|
24
|
+
type: ${type}
|
|
25
|
+
info: ${indent(JSON.stringify(info, forceStringifyErrors, "\t"), 5)}
|
|
26
|
+
`}`)
|
|
27
|
+
this.type = type
|
|
28
|
+
this.info = info
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function forceStringifyErrors(_key: string, value: any): any {
|
|
33
|
+
if (value instanceof Error) {
|
|
34
|
+
return Object.fromEntries(
|
|
35
|
+
(Object.getOwnPropertyNames(value) as Keys<Error>)
|
|
36
|
+
.map(key => [
|
|
37
|
+
key,
|
|
38
|
+
key === "stack"
|
|
39
|
+
? value[key]!.split(/\n/)
|
|
40
|
+
: value[key],
|
|
41
|
+
])
|
|
42
|
+
)
|
|
43
|
+
}
|
|
44
|
+
return value
|
|
45
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { PrefixApplier } from "../../types/parser.js"
|
|
2
|
+
|
|
3
|
+
/** @internal */
|
|
4
|
+
export function applyPrefix(left: string | undefined, right: string, prefixApplier: PrefixApplier): string {
|
|
5
|
+
if (left === undefined) {return right}
|
|
6
|
+
return prefixApplier(left, right)
|
|
7
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ConditionNormalizer, ValueQuery } from "../../types/parser.js"
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export const defaultConditionNormalizer: ConditionNormalizer =
|
|
5
|
+
function defaultConditionNormalizer(
|
|
6
|
+
{ value, operator, isNegated }: Pick<ValueQuery, "value" | "operator" | "isNegated">
|
|
7
|
+
) {
|
|
8
|
+
return { value, operator, negate: isNegated }
|
|
9
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/* Autogenerated Index */
|
|
2
|
+
|
|
3
|
+
export { applyBoolean } from "./applyBoolean.js"
|
|
4
|
+
export { applyPrefix } from "./applyPrefix.js"
|
|
5
|
+
export { defaultConditionNormalizer } from "./defaultConditionNormalizer.js"
|
|
6
|
+
export { defaultKeyParser } from "./defaultKeyParser.js"
|
|
7
|
+
export { defaultPrefixApplier } from "./defaultPrefixApplier.js"
|
|
8
|
+
export { defaultValueComparer } from "./defaultValueComparer.js"
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { ArrayNode } from "../../ast/classes/ArrayNode.js"
|
|
2
|
+
import { ConditionNode } from "../../ast/classes/ConditionNode.js"
|
|
3
|
+
import { ExpressionNode } from "../../ast/classes/ExpressionNode.js"
|
|
4
|
+
import { GroupNode } from "../../ast/classes/GroupNode.js"
|
|
5
|
+
import { Node } from "../../ast/classes/Node.js"
|
|
6
|
+
import { VariableNode } from "../../ast/classes/VariableNode.js"
|
|
7
|
+
import type { Nodes } from "../../types/ast.js"
|
|
8
|
+
|
|
9
|
+
/** @internal */
|
|
10
|
+
export function assignParents(ast: Nodes): void {
|
|
11
|
+
if (ast instanceof VariableNode) {
|
|
12
|
+
if (ast.prefix) ast.prefix.parent = ast
|
|
13
|
+
if (ast.quote?.left) ast.quote.left.parent = ast
|
|
14
|
+
if (ast.quote?.right) ast.quote.right.parent = ast
|
|
15
|
+
if (ast.quote?.flags) ast.quote.flags.parent = ast
|
|
16
|
+
ast.value.parent = ast
|
|
17
|
+
} else if (ast instanceof ConditionNode) {
|
|
18
|
+
if (ast.operator) ast.operator.parent = ast
|
|
19
|
+
if (ast.property) {
|
|
20
|
+
ast.property.parent = ast
|
|
21
|
+
if (ast.property instanceof Node) assignParents(ast.property)
|
|
22
|
+
}
|
|
23
|
+
if (ast.propertyOperator) ast.propertyOperator.parent = ast
|
|
24
|
+
if (ast.sep?.left) ast.sep.left.parent = ast
|
|
25
|
+
if (ast.sep?.right) ast.sep.right.parent = ast
|
|
26
|
+
ast.value.parent = ast
|
|
27
|
+
if (ast.value instanceof Node) assignParents(ast.value)
|
|
28
|
+
} else if (ast instanceof ExpressionNode) {
|
|
29
|
+
if (ast.operator) ast.operator.parent = ast
|
|
30
|
+
ast.right.parent = ast
|
|
31
|
+
if (ast.right instanceof Node) assignParents(ast.right)
|
|
32
|
+
ast.left.parent = ast
|
|
33
|
+
if (ast.left instanceof Node) assignParents(ast.left)
|
|
34
|
+
} else if (ast instanceof GroupNode) {
|
|
35
|
+
if (ast.prefix) {
|
|
36
|
+
ast.prefix.parent = ast
|
|
37
|
+
if (ast.prefix instanceof Node) assignParents(ast.prefix)
|
|
38
|
+
}
|
|
39
|
+
if (ast.paren?.left) ast.paren.left.parent = ast
|
|
40
|
+
if (ast.paren?.right) ast.paren.right.parent = ast
|
|
41
|
+
ast.expression.parent = ast
|
|
42
|
+
if (ast.expression instanceof Node) assignParents(ast.expression)
|
|
43
|
+
} else if (ast instanceof ArrayNode) {
|
|
44
|
+
if (ast.bracket.left) ast.bracket.left.parent = ast
|
|
45
|
+
if (ast.bracket.right) ast.bracket.right.parent = ast
|
|
46
|
+
for (const val of ast.values) {
|
|
47
|
+
val.parent = ast
|
|
48
|
+
assignParents(val)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|