@witchcraft/expressit 0.2.1 → 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.
- package/README.md +6 -4
- package/dist/Lexer.d.ts +102 -99
- package/dist/Lexer.d.ts.map +1 -1
- package/dist/Lexer.js +211 -557
- package/dist/Parser.d.ts +27 -27
- package/dist/Parser.d.ts.map +1 -1
- package/dist/Parser.js +5 -2
- package/dist/ast/builders/condition.d.ts +1 -1
- package/dist/ast/builders/condition.d.ts.map +1 -1
- package/dist/ast/builders/condition.js +1 -0
- package/dist/ast/builders/delim.d.ts +3 -3
- package/dist/ast/builders/delim.d.ts.map +1 -1
- package/dist/ast/builders/error.d.ts +2 -2
- package/dist/ast/builders/error.d.ts.map +1 -1
- package/dist/ast/builders/expression.d.ts +2 -2
- package/dist/ast/builders/expression.d.ts.map +1 -1
- package/dist/ast/builders/expression.js +2 -6
- package/dist/ast/builders/group.d.ts +1 -1
- package/dist/ast/builders/group.d.ts.map +1 -1
- package/dist/ast/builders/group.js +1 -3
- package/dist/ast/builders/pos.d.ts +2 -2
- package/dist/ast/builders/pos.d.ts.map +1 -1
- package/dist/ast/builders/token.d.ts +2 -2
- package/dist/ast/builders/token.d.ts.map +1 -1
- package/dist/ast/builders/type.d.ts +2 -2
- package/dist/ast/builders/type.d.ts.map +1 -1
- package/dist/ast/builders/variable.d.ts +3 -3
- package/dist/ast/builders/variable.d.ts.map +1 -1
- package/dist/ast/createConditionNode.d.ts +1 -1
- package/dist/ast/createConditionNode.d.ts.map +1 -1
- package/dist/ast/createGroupNode.d.ts +1 -1
- package/dist/ast/createGroupNode.d.ts.map +1 -1
- package/dist/ast/createToken.d.ts +2 -2
- package/dist/ast/createToken.d.ts.map +1 -1
- package/dist/ast/createToken.js +2 -2
- package/dist/ast/error.d.ts +2 -2
- package/dist/ast/error.d.ts.map +1 -1
- package/dist/ast/error.js +1 -0
- package/dist/ast/handlers.d.ts +23 -23
- package/dist/ast/handlers.d.ts.map +1 -1
- package/dist/ast/handlers.js +4 -4
- package/dist/examples/ParserWithSqlSupport.d.ts +62 -0
- package/dist/examples/ParserWithSqlSupport.d.ts.map +1 -0
- package/dist/examples/ParserWithSqlSupport.js +271 -0
- package/dist/examples/{shortcutContextParser.d.ts → ShortcutContextParser.d.ts} +5 -5
- package/dist/examples/ShortcutContextParser.d.ts.map +1 -0
- package/dist/examples/{shortcutContextParser.js → ShortcutContextParser.js} +2 -2
- package/dist/examples/index.d.ts +2 -1
- package/dist/examples/index.d.ts.map +1 -1
- package/dist/examples/index.js +3 -1
- package/dist/index.js +2 -2
- package/dist/internal/ExpressitError.d.ts +2 -2
- package/dist/internal/ExpressitError.d.ts.map +1 -1
- package/dist/internal/ExpressitError.js +2 -1
- package/dist/internal/checkParserOpts.d.ts +1 -1
- package/dist/internal/checkParserOpts.d.ts.map +1 -1
- package/dist/internal/checkParserOpts.js +11 -11
- package/dist/internal/parseParserOptions.d.ts +1 -1
- package/dist/internal/parseParserOptions.d.ts.map +1 -1
- package/dist/package.json.js +4 -195
- package/dist/types/ast.d.ts +60 -58
- package/dist/types/ast.d.ts.map +1 -1
- package/dist/types/ast.js +26 -27
- package/dist/types/autocomplete.d.ts +23 -21
- package/dist/types/autocomplete.d.ts.map +1 -1
- package/dist/types/autocomplete.js +24 -21
- package/dist/types/errors.d.ts +12 -10
- package/dist/types/errors.d.ts.map +1 -1
- package/dist/types/errors.js +8 -7
- package/dist/types/index.js +2 -2
- package/dist/types/parser.d.ts +9 -9
- package/dist/types/parser.d.ts.map +1 -1
- package/dist/utils/getCursorInfo.js +3 -1
- package/dist/utils/getOppositeDelimiter.d.ts +2 -2
- package/dist/utils/getOppositeDelimiter.d.ts.map +1 -1
- package/dist/utils/isDelimiter.d.ts +2 -2
- package/dist/utils/isDelimiter.d.ts.map +1 -1
- package/dist/utils/isParen.d.ts +2 -2
- package/dist/utils/isParen.d.ts.map +1 -1
- package/dist/utils/isQuote.d.ts +2 -2
- package/dist/utils/isQuote.d.ts.map +1 -1
- package/package.json +29 -27
- package/src/Lexer.ts +103 -92
- package/src/Parser.ts +70 -64
- package/src/ast/builders/condition.ts +3 -3
- package/src/ast/builders/delim.ts +5 -5
- package/src/ast/builders/error.ts +3 -3
- package/src/ast/builders/expression.ts +4 -8
- package/src/ast/builders/group.ts +2 -4
- package/src/ast/builders/pos.ts +3 -3
- package/src/ast/builders/token.ts +2 -2
- package/src/ast/builders/type.ts +2 -2
- package/src/ast/builders/variable.ts +5 -5
- package/src/ast/createConditionNode.ts +2 -2
- package/src/ast/createGroupNode.ts +4 -4
- package/src/ast/createToken.ts +6 -6
- package/src/ast/error.ts +2 -2
- package/src/ast/handlers.ts +23 -23
- package/src/examples/ParserWithSqlSupport.ts +371 -0
- package/src/examples/{shortcutContextParser.ts → ShortcutContextParser.ts} +14 -14
- package/src/examples/index.ts +2 -1
- package/src/internal/ExpressitError.ts +4 -4
- package/src/internal/checkParserOpts.ts +14 -14
- package/src/internal/parseParserOptions.ts +2 -2
- package/src/types/ast.ts +101 -96
- package/src/types/autocomplete.ts +26 -22
- package/src/types/errors.ts +18 -13
- package/src/types/parser.ts +9 -9
- package/src/utils/getCursorInfo.ts +1 -1
- package/src/utils/getOppositeDelimiter.ts +2 -2
- package/src/utils/getSurroundingErrors.ts +4 -4
- package/src/utils/isDelimiter.ts +3 -3
- package/src/utils/isParen.ts +2 -2
- package/src/utils/isQuote.ts +2 -2
- package/dist/examples/shortcutContextParser.d.ts.map +0 -1
- package/dist/global.d.js +0 -1
- package/dist/package.js +0 -7
- package/src/global.d.ts +0 -4
- package/src/package.js +0 -11
|
@@ -5,11 +5,11 @@ import { ExpressitError } from "./ExpressitError.js"
|
|
|
5
5
|
|
|
6
6
|
import { defaultConditionNormalizer } from "../defaults/defaultConditionNormalizer.js"
|
|
7
7
|
import { defaultValueComparer } from "../defaults/defaultValueComparer.js"
|
|
8
|
-
import {
|
|
8
|
+
import { PARSER_ERROR } from "../types/errors.js"
|
|
9
9
|
import type { FullParserOptions, ParserOptions } from "../types/parser.js"
|
|
10
10
|
|
|
11
11
|
/** @internal */
|
|
12
|
-
export function checkParserOpts<T
|
|
12
|
+
export function checkParserOpts<T>(opts: FullParserOptions<T>, evaluatorChecks: boolean = false, validatorChecks: boolean = false): void {
|
|
13
13
|
if (!evaluatorChecks) {
|
|
14
14
|
const keywordsList = [...opts.keywords.and, ...opts.keywords.or, ...opts.keywords.not].map(keyword => keyword.value)
|
|
15
15
|
const symNots = opts.keywords.not.filter(op => op.isSymbol).map(op => op.value)
|
|
@@ -27,7 +27,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
|
|
|
27
27
|
/* #region Blank Operator Checks */
|
|
28
28
|
if (opts.expandedPropertySeparator && isBlank(opts.expandedPropertySeparator)) {
|
|
29
29
|
throw new ExpressitError(
|
|
30
|
-
|
|
30
|
+
PARSER_ERROR.CONFLICTING_OPTIONS_ERROR,
|
|
31
31
|
{ prohibited: [""], invalid: opts.expandedPropertySeparator },
|
|
32
32
|
`expandedPropertySeparator cannot be blank`,
|
|
33
33
|
)
|
|
@@ -35,7 +35,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
|
|
|
35
35
|
const customInvalid = opts.customPropertyOperators?.find(_ => isBlank(_))
|
|
36
36
|
if (customInvalid !== undefined) {
|
|
37
37
|
throw new ExpressitError(
|
|
38
|
-
|
|
38
|
+
PARSER_ERROR.CONFLICTING_OPTIONS_ERROR,
|
|
39
39
|
{ prohibited: [""], invalid: customInvalid },
|
|
40
40
|
`customPropertyOperators cannot contain blank entries`,
|
|
41
41
|
)
|
|
@@ -43,18 +43,18 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
|
|
|
43
43
|
const prefixInvalid = opts.prefixableStrings?.find(_ => isBlank(_))
|
|
44
44
|
if (prefixInvalid !== undefined) {
|
|
45
45
|
throw new ExpressitError(
|
|
46
|
-
|
|
46
|
+
PARSER_ERROR.CONFLICTING_OPTIONS_ERROR,
|
|
47
47
|
{ prohibited: [""], invalid: prefixInvalid },
|
|
48
48
|
`prefixableStrings cannot contain blank entries`,
|
|
49
49
|
)
|
|
50
50
|
}
|
|
51
|
-
for (const key of ["and", "or", "not"]) {
|
|
52
|
-
const invalid = opts.keywords[key
|
|
51
|
+
for (const key of ["and", "or", "not"] as const) {
|
|
52
|
+
const invalid = opts.keywords[key]
|
|
53
53
|
?.find(_ => isBlank(_.value))
|
|
54
54
|
?.value
|
|
55
55
|
if (invalid !== undefined) {
|
|
56
56
|
throw new ExpressitError(
|
|
57
|
-
|
|
57
|
+
PARSER_ERROR.CONFLICTING_OPTIONS_ERROR,
|
|
58
58
|
{ prohibited: [""], invalid },
|
|
59
59
|
`keywords.${key} cannot contain entries with blank values`,
|
|
60
60
|
)
|
|
@@ -77,7 +77,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
|
|
|
77
77
|
const invalidPrefixableString = opts.prefixableStrings?.find(val => all.includes(val))
|
|
78
78
|
if (invalidPrefixableString) {
|
|
79
79
|
throw new ExpressitError(
|
|
80
|
-
|
|
80
|
+
PARSER_ERROR.CONFLICTING_OPTIONS_ERROR,
|
|
81
81
|
{ prohibited: all, invalid: invalidPrefixableString },
|
|
82
82
|
`prefixableStrings ${messageInvalidAny} "${invalidPrefixableString}"`,
|
|
83
83
|
)
|
|
@@ -87,7 +87,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
|
|
|
87
87
|
.find(_ => _ === opts.expandedPropertySeparator as any)
|
|
88
88
|
if (invalidExpandedPropertySeparator) {
|
|
89
89
|
throw new ExpressitError(
|
|
90
|
-
|
|
90
|
+
PARSER_ERROR.CONFLICTING_OPTIONS_ERROR,
|
|
91
91
|
{ prohibited: allKeywords, invalid: invalidExpandedPropertySeparator },
|
|
92
92
|
`expandedPropertySeparator ${messageInvalidBool} "${invalidExpandedPropertySeparator}"`,
|
|
93
93
|
)
|
|
@@ -98,7 +98,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
|
|
|
98
98
|
: undefined
|
|
99
99
|
if (invalidCustomPropertyOperator) {
|
|
100
100
|
throw new ExpressitError(
|
|
101
|
-
|
|
101
|
+
PARSER_ERROR.CONFLICTING_OPTIONS_ERROR,
|
|
102
102
|
{ prohibited: keywords, invalid: invalidCustomPropertyOperator },
|
|
103
103
|
`customPropertyOperator ${messageInvalidBool} "${invalidCustomPropertyOperator}"`,
|
|
104
104
|
)
|
|
@@ -115,7 +115,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
|
|
|
115
115
|
|
|
116
116
|
if (requireCustomNormalizer.length > 0 && opts.conditionNormalizer === defaultConditionNormalizer) {
|
|
117
117
|
throw new ExpressitError(
|
|
118
|
-
|
|
118
|
+
PARSER_ERROR.OPTION_REQUIRED_ERROR,
|
|
119
119
|
{ options: requireCustomNormalizer, requires: "conditionNormalizer" },
|
|
120
120
|
`A custom conditionNormalizer function must be specified when using the following options: ${requireCustomNormalizer.join(", ")}`,
|
|
121
121
|
)
|
|
@@ -126,7 +126,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
|
|
|
126
126
|
|
|
127
127
|
if (requireCustomComparer.length > 0 && opts.valueComparer === defaultValueComparer) {
|
|
128
128
|
throw new ExpressitError(
|
|
129
|
-
|
|
129
|
+
PARSER_ERROR.OPTION_REQUIRED_ERROR,
|
|
130
130
|
{ options: requireCustomComparer, requires: "valueComparer" },
|
|
131
131
|
`A custom valueComparer function must be specified when using the following options: ${requireCustomComparer.join(", ")}`,
|
|
132
132
|
)
|
|
@@ -135,7 +135,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
|
|
|
135
135
|
if (validatorChecks) {
|
|
136
136
|
if (opts.valueValidator === undefined) {
|
|
137
137
|
throw new ExpressitError(
|
|
138
|
-
|
|
138
|
+
PARSER_ERROR.OPTION_REQUIRED_ERROR,
|
|
139
139
|
{ requires: "valueValidator" },
|
|
140
140
|
`A custom valueValidator function must be specified when using the validate method.`,
|
|
141
141
|
)
|
|
@@ -5,10 +5,10 @@ import { defaultValueComparer } from "../defaults/defaultValueComparer.js"
|
|
|
5
5
|
import type { FullParserOptions, ParserOptions } from "../types/parser.js"
|
|
6
6
|
|
|
7
7
|
/** @internal */
|
|
8
|
-
export function parseParserOptions<T
|
|
8
|
+
export function parseParserOptions<T>(
|
|
9
9
|
options: ParserOptions<T>,
|
|
10
10
|
): FullParserOptions<T> {
|
|
11
|
-
const opts: ParserOptions = {
|
|
11
|
+
const opts: ParserOptions<T> = {
|
|
12
12
|
prefixApplier: defaultPrefixApplier,
|
|
13
13
|
keyParser: defaultKeyParser,
|
|
14
14
|
valueComparer: defaultValueComparer,
|
package/src/types/ast.ts
CHANGED
|
@@ -1,94 +1,97 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { enumFromArray } from "@alanscodelog/utils/enumFromArray.js"
|
|
2
|
+
import type { AnyFunction, EnumLike } from "@alanscodelog/utils/types"
|
|
2
3
|
|
|
3
4
|
|
|
4
5
|
export type AddParameters<T extends AnyFunction, TExtra extends any[] = [boolean]> = (...args: [...Parameters<T>, ...TExtra]) => ReturnType<T>
|
|
5
6
|
|
|
6
|
-
export
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
OP_EXPANDED_SEP
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
7
|
+
export const TOKEN_TYPE = enumFromArray([
|
|
8
|
+
"VALUE",
|
|
9
|
+
"AND",
|
|
10
|
+
"OR",
|
|
11
|
+
"NOT",
|
|
12
|
+
"BACKTICK",
|
|
13
|
+
"SINGLEQUOTE",
|
|
14
|
+
"DOUBLEQUOTE",
|
|
15
|
+
"PARENL",
|
|
16
|
+
"PARENR",
|
|
17
|
+
"BRACKETL",
|
|
18
|
+
"BRACKETR",
|
|
19
|
+
"OP_EXPANDED_SEP",
|
|
20
|
+
"OP_CUSTOM",
|
|
21
|
+
"REGEX",
|
|
22
|
+
])
|
|
23
|
+
|
|
24
|
+
export type TokenType = EnumLike<typeof TOKEN_TYPE>
|
|
22
25
|
|
|
23
26
|
/**
|
|
24
27
|
* @internal
|
|
25
28
|
* Note if the negation operator, `!`, is used as a propertyOperator, this will return the wrong type.
|
|
26
29
|
*/
|
|
27
|
-
export type
|
|
30
|
+
export type ExtractToken<T extends string> =
|
|
28
31
|
T extends "`"
|
|
29
|
-
? TOKEN_TYPE.BACKTICK
|
|
32
|
+
? typeof TOKEN_TYPE.BACKTICK
|
|
30
33
|
: T extends `'`
|
|
31
|
-
? TOKEN_TYPE.SINGLEQUOTE
|
|
34
|
+
? typeof TOKEN_TYPE.SINGLEQUOTE
|
|
32
35
|
: T extends `"`
|
|
33
|
-
? TOKEN_TYPE.DOUBLEQUOTE
|
|
36
|
+
? typeof TOKEN_TYPE.DOUBLEQUOTE
|
|
34
37
|
: T extends `/`
|
|
35
|
-
? TOKEN_TYPE.REGEX
|
|
38
|
+
? typeof TOKEN_TYPE.REGEX
|
|
36
39
|
: T extends `(`
|
|
37
|
-
? TOKEN_TYPE.PARENL
|
|
40
|
+
? typeof TOKEN_TYPE.PARENL
|
|
38
41
|
: T extends `)`
|
|
39
|
-
? TOKEN_TYPE.PARENR
|
|
42
|
+
? typeof TOKEN_TYPE.PARENR
|
|
40
43
|
: T extends `[`
|
|
41
|
-
? TOKEN_TYPE.BRACKETL
|
|
44
|
+
? typeof TOKEN_TYPE.BRACKETL
|
|
42
45
|
: T extends `]`
|
|
43
|
-
? TOKEN_TYPE.BRACKETR
|
|
46
|
+
? typeof TOKEN_TYPE.BRACKETR
|
|
44
47
|
: T extends `and`
|
|
45
|
-
? TOKEN_TYPE.AND
|
|
48
|
+
? typeof TOKEN_TYPE.AND
|
|
46
49
|
: T extends `&&`
|
|
47
|
-
? TOKEN_TYPE.AND
|
|
50
|
+
? typeof TOKEN_TYPE.AND
|
|
48
51
|
: T extends `&`
|
|
49
|
-
? TOKEN_TYPE.AND
|
|
52
|
+
? typeof TOKEN_TYPE.AND
|
|
50
53
|
: T extends `or`
|
|
51
|
-
? TOKEN_TYPE.OR
|
|
54
|
+
? typeof TOKEN_TYPE.OR
|
|
52
55
|
: T extends `||`
|
|
53
|
-
? TOKEN_TYPE.OR
|
|
56
|
+
? typeof TOKEN_TYPE.OR
|
|
54
57
|
: T extends `|`
|
|
55
|
-
? TOKEN_TYPE.OR
|
|
58
|
+
? typeof TOKEN_TYPE.OR
|
|
56
59
|
: T extends `not`
|
|
57
|
-
? TOKEN_TYPE.NOT
|
|
60
|
+
? typeof TOKEN_TYPE.NOT
|
|
58
61
|
: T extends `!`
|
|
59
|
-
? TOKEN_TYPE.NOT
|
|
60
|
-
: TOKEN_TYPE.VALUE
|
|
62
|
+
? typeof TOKEN_TYPE.NOT
|
|
63
|
+
: typeof TOKEN_TYPE.VALUE
|
|
61
64
|
|
|
62
|
-
export type
|
|
63
|
-
| TOKEN_TYPE.PARENL
|
|
64
|
-
| TOKEN_TYPE.PARENR
|
|
65
|
+
export type TokenParen =
|
|
66
|
+
| typeof TOKEN_TYPE.PARENL
|
|
67
|
+
| typeof TOKEN_TYPE.PARENR
|
|
65
68
|
export type TokenBracketTypes =
|
|
66
|
-
| TOKEN_TYPE.BRACKETL
|
|
67
|
-
| TOKEN_TYPE.BRACKETR
|
|
69
|
+
| typeof TOKEN_TYPE.BRACKETL
|
|
70
|
+
| typeof TOKEN_TYPE.BRACKETR
|
|
68
71
|
|
|
69
|
-
export type
|
|
70
|
-
|
|
|
71
|
-
|
|
|
72
|
+
export type TokenDelimiter =
|
|
73
|
+
| TokenParen
|
|
74
|
+
| TokenQuote
|
|
72
75
|
| TokenBracketTypes
|
|
73
|
-
| TOKEN_TYPE.OP_EXPANDED_SEP
|
|
76
|
+
| typeof TOKEN_TYPE.OP_EXPANDED_SEP
|
|
74
77
|
|
|
75
|
-
export type
|
|
76
|
-
| TOKEN_TYPE.BACKTICK
|
|
77
|
-
| TOKEN_TYPE.SINGLEQUOTE
|
|
78
|
-
| TOKEN_TYPE.DOUBLEQUOTE
|
|
79
|
-
| TOKEN_TYPE.REGEX
|
|
78
|
+
export type TokenQuote =
|
|
79
|
+
| typeof TOKEN_TYPE.BACKTICK
|
|
80
|
+
| typeof TOKEN_TYPE.SINGLEQUOTE
|
|
81
|
+
| typeof TOKEN_TYPE.DOUBLEQUOTE
|
|
82
|
+
| typeof TOKEN_TYPE.REGEX
|
|
80
83
|
|
|
81
|
-
export type
|
|
82
|
-
| TOKEN_TYPE.AND
|
|
83
|
-
| TOKEN_TYPE.OR
|
|
84
|
+
export type TokenBoolean =
|
|
85
|
+
| typeof TOKEN_TYPE.AND
|
|
86
|
+
| typeof TOKEN_TYPE.OR
|
|
84
87
|
|
|
85
|
-
export type
|
|
86
|
-
|
|
|
87
|
-
| TOKEN_TYPE.NOT
|
|
88
|
+
export type TokenOperator =
|
|
89
|
+
| TokenBoolean
|
|
90
|
+
| typeof TOKEN_TYPE.NOT
|
|
88
91
|
|
|
89
|
-
export type
|
|
90
|
-
| TOKEN_TYPE.OP_CUSTOM
|
|
91
|
-
| TOKEN_TYPE.OP_EXPANDED_SEP
|
|
92
|
+
export type TokenPropertyOperator =
|
|
93
|
+
| typeof TOKEN_TYPE.OP_CUSTOM
|
|
94
|
+
| typeof TOKEN_TYPE.OP_EXPANDED_SEP
|
|
92
95
|
|
|
93
96
|
|
|
94
97
|
// export type EmptyObj = Record<"start"|"end", undefined>
|
|
@@ -101,16 +104,18 @@ export type Position = {
|
|
|
101
104
|
end: number
|
|
102
105
|
}
|
|
103
106
|
|
|
104
|
-
export
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
107
|
+
export const AST_TYPE = enumFromArray([
|
|
108
|
+
"EXPRESSION",
|
|
109
|
+
"NORMALIZED_EXPRESSION",
|
|
110
|
+
"GROUP",
|
|
111
|
+
"ARRAY",
|
|
112
|
+
"CONDITION",
|
|
113
|
+
"NORMALIZED_CONDITION",
|
|
114
|
+
"VARIABLE",
|
|
115
|
+
])
|
|
116
|
+
|
|
113
117
|
// #region AST nodes
|
|
118
|
+
export type AstType = EnumLike<typeof AST_TYPE>
|
|
114
119
|
|
|
115
120
|
|
|
116
121
|
export type RawNode<T extends Node> = Omit<T, "valid" | "type" | "isNode">
|
|
@@ -130,7 +135,7 @@ export interface BaseToken {
|
|
|
130
135
|
/**
|
|
131
136
|
* Valid tokens always have a value, even if it might be an empty string.
|
|
132
137
|
*/
|
|
133
|
-
export interface ValidToken<TType extends
|
|
138
|
+
export interface ValidToken<TType extends TokenType = TokenType> extends BaseToken {
|
|
134
139
|
valid: true
|
|
135
140
|
type: TType
|
|
136
141
|
value: string
|
|
@@ -150,7 +155,7 @@ export interface ErrorToken extends BaseToken {
|
|
|
150
155
|
type?: undefined
|
|
151
156
|
value?: undefined
|
|
152
157
|
valid: false
|
|
153
|
-
expected:
|
|
158
|
+
expected: TokenType[]
|
|
154
159
|
}
|
|
155
160
|
|
|
156
161
|
/**
|
|
@@ -159,7 +164,7 @@ export interface ErrorToken extends BaseToken {
|
|
|
159
164
|
* Using {@link Token} does not work well in certain situations and is also more complex because it has so many generics.
|
|
160
165
|
*/
|
|
161
166
|
export type AnyToken<
|
|
162
|
-
TType extends
|
|
167
|
+
TType extends TokenType = TokenType,
|
|
163
168
|
> =
|
|
164
169
|
| ValidToken<TType>
|
|
165
170
|
| ErrorToken
|
|
@@ -175,7 +180,7 @@ export type ParserResults = ExpressionNode | ConditionNode | GroupNode | ErrorTo
|
|
|
175
180
|
*/
|
|
176
181
|
|
|
177
182
|
export interface Node<
|
|
178
|
-
TType extends
|
|
183
|
+
TType extends AstType = AstType,
|
|
179
184
|
TValid extends boolean = boolean,
|
|
180
185
|
> {
|
|
181
186
|
isNode: true
|
|
@@ -222,16 +227,16 @@ export interface GroupNode<
|
|
|
222
227
|
TPrefix extends
|
|
223
228
|
TPrefixable extends true
|
|
224
229
|
? ConditionNode<TValid> |
|
|
225
|
-
ValidToken<TOKEN_TYPE.NOT> |
|
|
230
|
+
ValidToken<typeof TOKEN_TYPE.NOT> |
|
|
226
231
|
undefined
|
|
227
|
-
: ValidToken<TOKEN_TYPE.NOT> |
|
|
232
|
+
: ValidToken<typeof TOKEN_TYPE.NOT> |
|
|
228
233
|
undefined =
|
|
229
234
|
TPrefixable extends true
|
|
230
235
|
? ConditionNode<TValid> |
|
|
231
|
-
ValidToken<TOKEN_TYPE.NOT> |
|
|
236
|
+
ValidToken<typeof TOKEN_TYPE.NOT> |
|
|
232
237
|
undefined
|
|
233
|
-
: ValidToken<TOKEN_TYPE.NOT>,
|
|
234
|
-
> extends Node<AST_TYPE.GROUP> {
|
|
238
|
+
: ValidToken<typeof TOKEN_TYPE.NOT>,
|
|
239
|
+
> extends Node<typeof AST_TYPE.GROUP> {
|
|
235
240
|
/**
|
|
236
241
|
* If the condition is negated this will contain a "not" **token**, {@link ValidToken} .
|
|
237
242
|
*
|
|
@@ -250,7 +255,7 @@ export interface GroupNode<
|
|
|
250
255
|
/**
|
|
251
256
|
* The parenthesis tokens, {@link ValidToken} . These will always be defined (although not necessarily with valid tokens).
|
|
252
257
|
*/
|
|
253
|
-
paren: NodeDelimiters<TOKEN_TYPE.PARENL, TOKEN_TYPE.PARENR>
|
|
258
|
+
paren: NodeDelimiters<typeof TOKEN_TYPE.PARENL, typeof TOKEN_TYPE.PARENR>
|
|
254
259
|
}
|
|
255
260
|
|
|
256
261
|
|
|
@@ -266,18 +271,18 @@ export interface GroupNode<
|
|
|
266
271
|
* If `prefixableStrings` is true, the `prefix` property might contain a value token.
|
|
267
272
|
*/
|
|
268
273
|
|
|
269
|
-
export interface VariableNode<TValid extends boolean = boolean> extends Node<AST_TYPE.VARIABLE, TValid> {
|
|
274
|
+
export interface VariableNode<TValid extends boolean = boolean> extends Node<typeof AST_TYPE.VARIABLE, TValid> {
|
|
270
275
|
value: TValid extends boolean
|
|
271
|
-
? AnyToken<TOKEN_TYPE.VALUE>
|
|
276
|
+
? AnyToken<typeof TOKEN_TYPE.VALUE>
|
|
272
277
|
: TValid extends true
|
|
273
|
-
? ValidToken<TOKEN_TYPE.VALUE>
|
|
278
|
+
? ValidToken<typeof TOKEN_TYPE.VALUE>
|
|
274
279
|
: ErrorToken
|
|
275
|
-
prefix?: ValidToken<TOKEN_TYPE.VALUE> // todo
|
|
276
|
-
quote?: NodeDelimiters<
|
|
280
|
+
prefix?: ValidToken<typeof TOKEN_TYPE.VALUE> // todo
|
|
281
|
+
quote?: NodeDelimiters<TokenQuote, TokenQuote>
|
|
277
282
|
}
|
|
278
283
|
|
|
279
|
-
export interface ExpressionNode<TValid extends boolean = boolean> extends Node<AST_TYPE.EXPRESSION> {
|
|
280
|
-
operator: AnyToken<
|
|
284
|
+
export interface ExpressionNode<TValid extends boolean = boolean> extends Node<typeof AST_TYPE.EXPRESSION> {
|
|
285
|
+
operator: AnyToken<TokenBoolean>
|
|
281
286
|
left:
|
|
282
287
|
| ExpressionNode<TValid>
|
|
283
288
|
| ConditionNode<TValid>
|
|
@@ -298,17 +303,17 @@ export type Nodes = ExpressionNode | ConditionNode | GroupNode | VariableNode |
|
|
|
298
303
|
*
|
|
299
304
|
* These are usually not important for evaluating an expression but are useful for syntax highlighting.
|
|
300
305
|
*/
|
|
301
|
-
export type NodeDelimiters<TLEFT extends
|
|
306
|
+
export type NodeDelimiters<TLEFT extends TokenType, TRIGHT extends TokenType = TLEFT> = {
|
|
302
307
|
left: AnyToken<TLEFT>
|
|
303
308
|
right: AnyToken<TRIGHT>
|
|
304
309
|
/** Only exists if regexes are enabled and this is a regex value. */
|
|
305
|
-
flags?: ValidToken<TOKEN_TYPE.VALUE>
|
|
310
|
+
flags?: ValidToken<typeof TOKEN_TYPE.VALUE>
|
|
306
311
|
}
|
|
307
312
|
|
|
308
313
|
|
|
309
|
-
export interface ArrayNode<TValid extends boolean = boolean> extends Node<AST_TYPE.ARRAY> {
|
|
314
|
+
export interface ArrayNode<TValid extends boolean = boolean> extends Node<typeof AST_TYPE.ARRAY> {
|
|
310
315
|
values: VariableNode[]
|
|
311
|
-
bracket: NodeDelimiters<TOKEN_TYPE.BRACKETL, TOKEN_TYPE.BRACKETR>
|
|
316
|
+
bracket: NodeDelimiters<typeof TOKEN_TYPE.BRACKETL, typeof TOKEN_TYPE.BRACKETR>
|
|
312
317
|
valid: TValid
|
|
313
318
|
}
|
|
314
319
|
|
|
@@ -321,7 +326,7 @@ export interface ArrayNode<TValid extends boolean = boolean> extends Node<AST_TY
|
|
|
321
326
|
*/
|
|
322
327
|
export interface ConditionNode<
|
|
323
328
|
TValid extends boolean = boolean,
|
|
324
|
-
> extends Node<AST_TYPE.CONDITION> {
|
|
329
|
+
> extends Node<typeof AST_TYPE.CONDITION> {
|
|
325
330
|
/**
|
|
326
331
|
* Contains a value node which could be a variable, an array node (if enabled), or a group.
|
|
327
332
|
*
|
|
@@ -337,7 +342,7 @@ export interface ConditionNode<
|
|
|
337
342
|
/**
|
|
338
343
|
* If the condition was negated, contains the "not" token, {@link ValidToken} , the condition was negated with.
|
|
339
344
|
*/
|
|
340
|
-
operator?: ValidToken<TOKEN_TYPE.NOT>
|
|
345
|
+
operator?: ValidToken<typeof TOKEN_TYPE.NOT>
|
|
341
346
|
/**
|
|
342
347
|
* If condition property operators are used, this will contain the property (as a variable), or an error token if it was missing (but some separator or operator was passed).
|
|
343
348
|
*
|
|
@@ -355,7 +360,7 @@ export interface ConditionNode<
|
|
|
355
360
|
*
|
|
356
361
|
* See the corresponding {@link ParserOptions} for more details.
|
|
357
362
|
*/
|
|
358
|
-
propertyOperator?: AnyToken<TOKEN_TYPE.OP_CUSTOM | TOKEN_TYPE.VALUE>
|
|
363
|
+
propertyOperator?: AnyToken<typeof TOKEN_TYPE.OP_CUSTOM | typeof TOKEN_TYPE.VALUE>
|
|
359
364
|
/**
|
|
360
365
|
* If "long/expanded" form condition property operators are used, this will contain the separators, otherwise it is undefined.
|
|
361
366
|
*
|
|
@@ -368,8 +373,8 @@ export interface ConditionNode<
|
|
|
368
373
|
* See the corresponding {@link ParserOptions} for more details.
|
|
369
374
|
*/
|
|
370
375
|
sep?: {
|
|
371
|
-
left?: AnyToken<TOKEN_TYPE.OP_EXPANDED_SEP>
|
|
372
|
-
right?: AnyToken<TOKEN_TYPE.OP_EXPANDED_SEP>
|
|
376
|
+
left?: AnyToken<typeof TOKEN_TYPE.OP_EXPANDED_SEP>
|
|
377
|
+
right?: AnyToken<typeof TOKEN_TYPE.OP_EXPANDED_SEP>
|
|
373
378
|
}
|
|
374
379
|
}
|
|
375
380
|
|
|
@@ -377,7 +382,7 @@ export interface NormalizedCondition<
|
|
|
377
382
|
TOp extends string = string,
|
|
378
383
|
TValue = any,
|
|
379
384
|
> {
|
|
380
|
-
type: AST_TYPE.NORMALIZED_CONDITION
|
|
385
|
+
type: typeof AST_TYPE.NORMALIZED_CONDITION
|
|
381
386
|
value: TValue
|
|
382
387
|
operator?: TOp
|
|
383
388
|
property: string[]
|
|
@@ -386,14 +391,14 @@ export interface NormalizedCondition<
|
|
|
386
391
|
|
|
387
392
|
|
|
388
393
|
export interface NormalizedExpression<TType extends string = string, TValue = any> {
|
|
389
|
-
type: AST_TYPE.NORMALIZED_EXPRESSION
|
|
394
|
+
type: typeof AST_TYPE.NORMALIZED_EXPRESSION
|
|
390
395
|
left:
|
|
391
396
|
| NormalizedExpression<TType, TValue>
|
|
392
397
|
| NormalizedCondition<TType, TValue>
|
|
393
398
|
right:
|
|
394
399
|
| NormalizedExpression<TType, TValue>
|
|
395
400
|
| NormalizedCondition<TType, TValue>
|
|
396
|
-
operator:
|
|
401
|
+
operator: TokenBoolean
|
|
397
402
|
}
|
|
398
403
|
|
|
399
404
|
export type ParentTypes<T extends Node | BaseToken | undefined> =
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { type EnumLike } from "@alanscodelog/utils"
|
|
2
|
+
import { enumFromArray } from "@alanscodelog/utils/enumFromArray.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
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
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:
|
|
117
|
+
type: SuggestionType
|
|
114
118
|
/** The range the suggestion should replace / be inserted at. */
|
|
115
119
|
range: Position
|
|
116
120
|
/** {@link CursorInfo} */
|
package/src/types/errors.ts
CHANGED
|
@@ -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
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
|
12
|
-
T extends
|
|
13
|
-
?
|
|
16
|
+
export type ErrorInfo<T extends ParserError> =
|
|
17
|
+
T extends ParserError
|
|
18
|
+
? ErrorCodesInfo[T]
|
|
14
19
|
: never
|
|
15
20
|
|
|
16
|
-
|
|
17
|
-
export type
|
|
18
|
-
[
|
|
21
|
+
|
|
22
|
+
export type ErrorCodesInfo = {
|
|
23
|
+
[PARSER_ERROR.POSITION_ERROR]: {
|
|
19
24
|
start?: number
|
|
20
25
|
end?: number
|
|
21
26
|
}
|
|
22
|
-
[
|
|
27
|
+
[PARSER_ERROR.CONFLICTING_OPTIONS_ERROR]: {
|
|
23
28
|
prohibited: string[]
|
|
24
29
|
invalid: string
|
|
25
30
|
}
|
|
26
|
-
[
|
|
31
|
+
[PARSER_ERROR.OPTION_REQUIRED_ERROR]: {
|
|
27
32
|
options?: (keyof ParserOptions)[]
|
|
28
33
|
requires: keyof ParserOptions
|
|
29
34
|
}
|
package/src/types/parser.ts
CHANGED
|
@@ -4,7 +4,7 @@ import type { ArrayNode, ConditionNode, NormalizedCondition, Position, TOKEN_TYP
|
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
// #partially-synced
|
|
7
|
-
export type FullParserOptions<T
|
|
7
|
+
export type FullParserOptions<T = any> = MakeRequired<
|
|
8
8
|
ParserOptions<T>,
|
|
9
9
|
// makes required all except:
|
|
10
10
|
Exclude<keyof ParserOptions<T>,
|
|
@@ -18,7 +18,7 @@ export type FullParserOptions<T extends {} = {}> = MakeRequired<
|
|
|
18
18
|
// overrides
|
|
19
19
|
keywords: DeepRequired<KeywordOptions>
|
|
20
20
|
}
|
|
21
|
-
export type ParserOptions<T
|
|
21
|
+
export type ParserOptions<T = any> = {
|
|
22
22
|
/**
|
|
23
23
|
* Allows any conditions (i.e. a variable or negated variable) to precede groups and append themselves to all variables inside them. Regular use of groups for changing precedence (e.g. `(a || b) && c` ) or negating expressions `!(a || b)` is still supported even if `prefixableGroups` is false.
|
|
24
24
|
*
|
|
@@ -116,7 +116,7 @@ export type ParserOptions<T extends {} = {}> = {
|
|
|
116
116
|
/**
|
|
117
117
|
* Enables regex strings as values. The value is treated as if it was quoted by forward slashes. Any repetition of lowercase characters (even if there are multiple) attached to the end are assumed to be flags and added as a single token to the value's `quotes.mode` property.
|
|
118
118
|
*
|
|
119
|
-
* Can be passed a custom function to determine when to use the regex value or not (it is converted to a regular value). The function is passed the property, the operator, and whether it's an expanded operator. If
|
|
119
|
+
* Can be passed a custom function to determine when to use the regex value or not (it is converted to a regular value). The function is passed the property, the operator, and whether it's an expanded operator. If there is an error token for the property or operator, an empty string is passed.
|
|
120
120
|
*
|
|
121
121
|
* ```ts
|
|
122
122
|
* // allow anything (`prop=/val/`, `prop:op:/val`, `prop=(/val/)`, `prop:op(/val/)`) but the value alone (`/regex/`)
|
|
@@ -272,7 +272,7 @@ export type ParserOptions<T extends {} = {}> = {
|
|
|
272
272
|
* ```ts
|
|
273
273
|
* type Operators = "contains"
|
|
274
274
|
* function valueComparer(condition: Omit<Condition, "negate">, contextValue: any, context: any): boolean {
|
|
275
|
-
* switch (operator as Operators) {
|
|
275
|
+
* switch (condition.operator as Operators) {
|
|
276
276
|
* case "contains": return (contextValue as string[]).includes(condition.value as string)
|
|
277
277
|
* // ...
|
|
278
278
|
* }
|
|
@@ -307,13 +307,13 @@ export type ParserOptions<T extends {} = {}> = {
|
|
|
307
307
|
* if (prefix) {
|
|
308
308
|
* const val = value as string // it's always a string if prefixed
|
|
309
309
|
* switch (prefix as RawPrefixes) {
|
|
310
|
-
* case "num": finalValue = parseInt(val,
|
|
310
|
+
* case "num": finalValue = parseInt(val, 10); break
|
|
311
311
|
* // ...
|
|
312
312
|
* }
|
|
313
313
|
* }
|
|
314
314
|
* // another way to allow special unquoted value types is something like this:
|
|
315
315
|
* if (typeof value === "string" && !isQuoted) {
|
|
316
|
-
* const asNum = parseInt(value,
|
|
316
|
+
* const asNum = parseInt(value, 10)
|
|
317
317
|
* if (!isNaN(asNum)) finalValue = asNum
|
|
318
318
|
* if (["true","false"].includes(value)) {
|
|
319
319
|
* finalValue = value === "true" ? true : false
|
|
@@ -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
|