@witchcraft/expressit 0.1.2 → 0.2.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 +9 -5
- package/dist/Lexer.js +2 -2
- package/dist/Parser.d.ts +14 -29
- package/dist/Parser.d.ts.map +1 -1
- package/dist/Parser.js +120 -135
- package/dist/ast/builders/array.d.ts +1 -3
- package/dist/ast/builders/array.d.ts.map +1 -1
- package/dist/ast/builders/array.js +3 -5
- package/dist/ast/builders/condition.d.ts +5 -11
- package/dist/ast/builders/condition.d.ts.map +1 -1
- package/dist/ast/builders/condition.js +2 -3
- package/dist/ast/builders/delim.d.ts +1 -1
- package/dist/ast/builders/error.d.ts +3 -4
- package/dist/ast/builders/error.d.ts.map +1 -1
- package/dist/ast/builders/error.js +6 -2
- package/dist/ast/builders/expression.d.ts +3 -7
- package/dist/ast/builders/expression.d.ts.map +1 -1
- package/dist/ast/builders/expression.js +3 -5
- package/dist/ast/builders/group.d.ts +3 -8
- package/dist/ast/builders/group.d.ts.map +1 -1
- package/dist/ast/builders/group.js +3 -3
- package/dist/ast/builders/pos.d.ts +1 -2
- package/dist/ast/builders/pos.d.ts.map +1 -1
- package/dist/ast/builders/token.d.ts +3 -5
- package/dist/ast/builders/token.d.ts.map +1 -1
- package/dist/ast/builders/token.js +3 -4
- package/dist/ast/builders/type.d.ts +1 -1
- package/dist/ast/builders/variable.d.ts +3 -5
- package/dist/ast/builders/variable.d.ts.map +1 -1
- package/dist/ast/builders/variable.js +2 -3
- package/dist/ast/createArrayNode.d.ts +3 -0
- package/dist/ast/createArrayNode.d.ts.map +1 -0
- package/dist/ast/createArrayNode.js +12 -0
- package/dist/ast/createConditionNode.d.ts +11 -0
- package/dist/ast/createConditionNode.d.ts.map +1 -0
- package/dist/ast/createConditionNode.js +18 -0
- package/dist/ast/createExpressionNode.d.ts +3 -0
- package/dist/ast/createExpressionNode.d.ts.map +1 -0
- package/dist/ast/createExpressionNode.js +14 -0
- package/dist/ast/createGroupNode.d.ts +5 -0
- package/dist/ast/createGroupNode.d.ts.map +1 -0
- package/dist/ast/createGroupNode.js +14 -0
- package/dist/ast/createNormalizedCondition.d.ts +3 -0
- package/dist/ast/createNormalizedCondition.d.ts.map +1 -0
- package/dist/ast/createNormalizedCondition.js +10 -0
- package/dist/ast/createNormalizedExpression.d.ts +3 -0
- package/dist/ast/createNormalizedExpression.d.ts.map +1 -0
- package/dist/ast/createNormalizedExpression.js +10 -0
- package/dist/ast/createToken.d.ts +6 -0
- package/dist/ast/createToken.d.ts.map +1 -0
- package/dist/ast/createToken.js +15 -0
- package/dist/ast/createVariableNode.d.ts +3 -0
- package/dist/ast/createVariableNode.d.ts.map +1 -0
- package/dist/ast/createVariableNode.js +12 -0
- package/dist/ast/error.d.ts +3 -0
- package/dist/ast/error.d.ts.map +1 -0
- package/dist/ast/error.js +9 -0
- package/dist/ast/handlers.d.ts +5 -12
- package/dist/ast/handlers.d.ts.map +1 -1
- package/dist/ast/handlers.js +21 -29
- package/dist/ast/index.d.ts +9 -1
- package/dist/ast/index.d.ts.map +1 -1
- package/dist/ast/index.js +18 -2
- package/dist/defaults/defaultConditionNormalizer.d.ts +3 -0
- package/dist/defaults/defaultConditionNormalizer.d.ts.map +1 -0
- package/dist/defaults/defaultKeyParser.d.ts +3 -0
- package/dist/defaults/defaultKeyParser.d.ts.map +1 -0
- package/dist/defaults/defaultPrefixApplier.d.ts +3 -0
- package/dist/defaults/defaultPrefixApplier.d.ts.map +1 -0
- package/dist/defaults/defaultValueComparer.d.ts +3 -0
- package/dist/defaults/defaultValueComparer.d.ts.map +1 -0
- package/dist/{helpers/general → defaults}/index.d.ts +0 -2
- package/dist/defaults/index.d.ts.map +1 -0
- package/dist/{helpers/general → defaults}/index.js +0 -4
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/{helpers/errors.d.ts → internal/ExpressitError.d.ts} +3 -2
- package/dist/internal/ExpressitError.d.ts.map +1 -0
- package/dist/{helpers/errors.js → internal/ExpressitError.js} +2 -2
- package/dist/internal/applyBoolean.d.ts.map +1 -0
- package/dist/{helpers/general → internal}/applyPrefix.d.ts +1 -1
- package/dist/internal/applyPrefix.d.ts.map +1 -0
- package/dist/{helpers/parser → internal}/checkParserOpts.d.ts +2 -1
- package/dist/internal/checkParserOpts.d.ts.map +1 -0
- package/dist/{helpers/parser → internal}/checkParserOpts.js +14 -14
- package/dist/internal/escapeVariableOrPrefix.d.ts +2 -0
- package/dist/internal/escapeVariableOrPrefix.d.ts.map +1 -0
- package/dist/internal/escapeVariableOrPrefix.js +38 -0
- package/dist/internal/extractPosition.d.ts +5 -0
- package/dist/internal/extractPosition.d.ts.map +1 -0
- package/dist/internal/getUnclosedRightParenCount.d.ts +4 -0
- package/dist/internal/getUnclosedRightParenCount.d.ts.map +1 -0
- package/dist/{helpers/parser → internal}/getUnclosedRightParenCount.js +1 -1
- package/dist/{helpers/parser → internal}/parseParserOptions.d.ts +1 -1
- package/dist/internal/parseParserOptions.d.ts.map +1 -0
- package/dist/{helpers/parser → internal}/parseParserOptions.js +4 -4
- package/dist/internal/unescape.d.ts.map +1 -0
- package/dist/package.json.js +7 -7
- package/dist/types/ast.d.ts +195 -6
- package/dist/types/ast.d.ts.map +1 -1
- package/dist/types/ast.js +2 -0
- package/dist/types/autocomplete.d.ts +5 -6
- package/dist/types/autocomplete.d.ts.map +1 -1
- package/dist/types/parser.d.ts +17 -22
- package/dist/types/parser.d.ts.map +1 -1
- package/dist/utils/extractTokens.d.ts +2 -4
- package/dist/utils/extractTokens.d.ts.map +1 -1
- package/dist/utils/extractTokens.js +14 -18
- package/dist/utils/generateParentsMap.d.ts +10 -0
- package/dist/utils/generateParentsMap.d.ts.map +1 -0
- package/dist/utils/generateParentsMap.js +73 -0
- package/dist/utils/getCursorInfo.d.ts +1 -1
- package/dist/utils/getCursorInfo.d.ts.map +1 -1
- package/dist/utils/getCursorInfo.js +3 -5
- package/dist/utils/getOppositeDelimiter.d.ts +2 -1
- package/dist/utils/getOppositeDelimiter.d.ts.map +1 -1
- package/dist/utils/getOppositeDelimiter.js +6 -5
- package/dist/utils/getParent.d.ts +5 -0
- package/dist/utils/getParent.d.ts.map +1 -0
- package/dist/utils/getParent.js +6 -0
- package/dist/utils/getSurroundingErrors.d.ts +3 -4
- package/dist/utils/getSurroundingErrors.d.ts.map +1 -1
- package/dist/utils/getSurroundingErrors.js +3 -3
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +8 -0
- package/dist/utils/isNode.d.ts +10 -0
- package/dist/utils/isNode.d.ts.map +1 -0
- package/dist/utils/isNode.js +6 -0
- package/dist/utils/isToken.d.ts +8 -0
- package/dist/utils/isToken.d.ts.map +1 -0
- package/dist/utils/isToken.js +6 -0
- package/dist/utils/prettyAst.d.ts +1 -2
- package/dist/utils/prettyAst.d.ts.map +1 -1
- package/dist/utils/prettyAst.js +19 -26
- package/package.json +7 -7
- package/src/Lexer.ts +2 -2
- package/src/Parser.ts +158 -165
- package/src/ast/builders/array.ts +4 -9
- package/src/ast/builders/condition.ts +10 -15
- package/src/ast/builders/delim.ts +1 -1
- package/src/ast/builders/error.ts +10 -6
- package/src/ast/builders/expression.ts +17 -23
- package/src/ast/builders/group.ts +7 -11
- package/src/ast/builders/pos.ts +2 -3
- package/src/ast/builders/token.ts +6 -7
- package/src/ast/builders/type.ts +1 -1
- package/src/ast/builders/variable.ts +6 -8
- package/src/ast/createArrayNode.ts +17 -0
- package/src/ast/createConditionNode.ts +58 -0
- package/src/ast/createExpressionNode.ts +21 -0
- package/src/ast/createGroupNode.ts +46 -0
- package/src/ast/createNormalizedCondition.ts +12 -0
- package/src/ast/createNormalizedExpression.ts +9 -0
- package/src/ast/createToken.ts +26 -0
- package/src/ast/createVariableNode.ts +18 -0
- package/src/ast/error.ts +9 -0
- package/src/ast/handlers.ts +26 -37
- package/src/ast/index.ts +9 -1
- package/src/{helpers/general → defaults}/defaultConditionNormalizer.ts +1 -1
- package/src/{helpers/general → defaults}/defaultKeyParser.ts +1 -1
- package/src/{helpers/general → defaults}/defaultPrefixApplier.ts +1 -1
- package/src/{helpers/general → defaults}/defaultValueComparer.ts +1 -1
- package/src/{helpers/general → defaults}/index.ts +0 -2
- package/src/index.ts +2 -1
- package/src/{helpers/errors.ts → internal/ExpressitError.ts} +2 -2
- package/src/{helpers/general → internal}/applyPrefix.ts +1 -1
- package/src/{helpers/parser → internal}/checkParserOpts.ts +16 -15
- package/src/internal/escapeVariableOrPrefix.ts +36 -0
- package/src/{helpers/parser → internal}/extractPosition.ts +2 -2
- package/src/{helpers/parser → internal}/getUnclosedRightParenCount.ts +1 -2
- package/src/{helpers/parser → internal}/parseParserOptions.ts +5 -5
- package/src/types/ast.ts +292 -7
- package/src/types/autocomplete.ts +5 -7
- package/src/types/parser.ts +17 -22
- package/src/utils/extractTokens.ts +16 -20
- package/src/utils/generateParentsMap.ts +60 -0
- package/src/utils/getCursorInfo.ts +4 -6
- package/src/utils/getOppositeDelimiter.ts +11 -10
- package/src/utils/getParent.ts +16 -0
- package/src/utils/getSurroundingErrors.ts +10 -9
- package/src/utils/index.ts +4 -0
- package/src/utils/isNode.ts +12 -0
- package/src/utils/isToken.ts +10 -0
- package/src/utils/prettyAst.ts +20 -17
- package/dist/ast/classes/ArrayNode.d.ts +0 -18
- package/dist/ast/classes/ArrayNode.d.ts.map +0 -1
- package/dist/ast/classes/ArrayNode.js +0 -55
- package/dist/ast/classes/Condition.d.ts +0 -13
- package/dist/ast/classes/Condition.d.ts.map +0 -1
- package/dist/ast/classes/Condition.js +0 -21
- package/dist/ast/classes/ConditionNode.d.ts +0 -73
- package/dist/ast/classes/ConditionNode.d.ts.map +0 -1
- package/dist/ast/classes/ConditionNode.js +0 -101
- package/dist/ast/classes/ErrorToken.d.ts +0 -27
- package/dist/ast/classes/ErrorToken.d.ts.map +0 -1
- package/dist/ast/classes/ErrorToken.js +0 -47
- package/dist/ast/classes/Expression.d.ts +0 -13
- package/dist/ast/classes/Expression.d.ts.map +0 -1
- package/dist/ast/classes/Expression.js +0 -19
- package/dist/ast/classes/ExpressionNode.d.ts +0 -21
- package/dist/ast/classes/ExpressionNode.d.ts.map +0 -1
- package/dist/ast/classes/ExpressionNode.js +0 -57
- package/dist/ast/classes/GroupNode.d.ts +0 -64
- package/dist/ast/classes/GroupNode.d.ts.map +0 -1
- package/dist/ast/classes/GroupNode.js +0 -69
- package/dist/ast/classes/Node.d.ts +0 -22
- package/dist/ast/classes/Node.d.ts.map +0 -1
- package/dist/ast/classes/Node.js +0 -28
- package/dist/ast/classes/Token.d.ts +0 -27
- package/dist/ast/classes/Token.d.ts.map +0 -1
- package/dist/ast/classes/Token.js +0 -28
- package/dist/ast/classes/ValidToken.d.ts +0 -26
- package/dist/ast/classes/ValidToken.d.ts.map +0 -1
- package/dist/ast/classes/ValidToken.js +0 -49
- package/dist/ast/classes/VariableNode.d.ts +0 -33
- package/dist/ast/classes/VariableNode.d.ts.map +0 -1
- package/dist/ast/classes/VariableNode.js +0 -58
- package/dist/ast/classes/index.d.ts +0 -12
- package/dist/ast/classes/index.d.ts.map +0 -1
- package/dist/ast/classes/index.js +0 -24
- package/dist/helpers/errors.d.ts.map +0 -1
- package/dist/helpers/general/applyBoolean.d.ts.map +0 -1
- package/dist/helpers/general/applyPrefix.d.ts.map +0 -1
- package/dist/helpers/general/defaultConditionNormalizer.d.ts +0 -3
- package/dist/helpers/general/defaultConditionNormalizer.d.ts.map +0 -1
- package/dist/helpers/general/defaultKeyParser.d.ts +0 -3
- package/dist/helpers/general/defaultKeyParser.d.ts.map +0 -1
- package/dist/helpers/general/defaultPrefixApplier.d.ts +0 -3
- package/dist/helpers/general/defaultPrefixApplier.d.ts.map +0 -1
- package/dist/helpers/general/defaultValueComparer.d.ts +0 -3
- package/dist/helpers/general/defaultValueComparer.d.ts.map +0 -1
- package/dist/helpers/general/index.d.ts.map +0 -1
- package/dist/helpers/index.d.ts +0 -4
- package/dist/helpers/index.d.ts.map +0 -1
- package/dist/helpers/index.js +0 -8
- package/dist/helpers/parser/assignParents.d.ts +0 -4
- package/dist/helpers/parser/assignParents.d.ts.map +0 -1
- package/dist/helpers/parser/assignParents.js +0 -71
- package/dist/helpers/parser/checkParserOpts.d.ts.map +0 -1
- package/dist/helpers/parser/extractPosition.d.ts +0 -5
- package/dist/helpers/parser/extractPosition.d.ts.map +0 -1
- package/dist/helpers/parser/getUnclosedRightParenCount.d.ts +0 -4
- package/dist/helpers/parser/getUnclosedRightParenCount.d.ts.map +0 -1
- package/dist/helpers/parser/index.d.ts +0 -9
- package/dist/helpers/parser/index.d.ts.map +0 -1
- package/dist/helpers/parser/index.js +0 -18
- package/dist/helpers/parser/parseParserOptions.d.ts.map +0 -1
- package/dist/helpers/parser/seal.d.ts +0 -8
- package/dist/helpers/parser/seal.d.ts.map +0 -1
- package/dist/helpers/parser/seal.js +0 -10
- package/dist/helpers/parser/setParent.d.ts +0 -6
- package/dist/helpers/parser/setParent.d.ts.map +0 -1
- package/dist/helpers/parser/setParent.js +0 -4
- package/dist/helpers/parser/unescape.d.ts.map +0 -1
- package/src/ast/classes/ArrayNode.ts +0 -46
- package/src/ast/classes/Condition.ts +0 -22
- package/src/ast/classes/ConditionNode.ts +0 -141
- package/src/ast/classes/ErrorToken.ts +0 -49
- package/src/ast/classes/Expression.ts +0 -26
- package/src/ast/classes/ExpressionNode.ts +0 -62
- package/src/ast/classes/GroupNode.ts +0 -127
- package/src/ast/classes/Node.ts +0 -47
- package/src/ast/classes/Token.ts +0 -59
- package/src/ast/classes/ValidToken.ts +0 -56
- package/src/ast/classes/VariableNode.ts +0 -67
- package/src/ast/classes/index.ts +0 -13
- package/src/helpers/index.ts +0 -6
- package/src/helpers/parser/assignParents.ts +0 -51
- package/src/helpers/parser/index.ts +0 -10
- package/src/helpers/parser/seal.ts +0 -14
- package/src/helpers/parser/setParent.ts +0 -5
- /package/dist/{helpers/general → defaults}/defaultConditionNormalizer.js +0 -0
- /package/dist/{helpers/general → defaults}/defaultKeyParser.js +0 -0
- /package/dist/{helpers/general → defaults}/defaultPrefixApplier.js +0 -0
- /package/dist/{helpers/general → defaults}/defaultValueComparer.js +0 -0
- /package/dist/{helpers/general → internal}/applyBoolean.d.ts +0 -0
- /package/dist/{helpers/general → internal}/applyBoolean.js +0 -0
- /package/dist/{helpers/general → internal}/applyPrefix.js +0 -0
- /package/dist/{helpers/parser → internal}/extractPosition.js +0 -0
- /package/dist/{helpers/parser → internal}/unescape.d.ts +0 -0
- /package/dist/{helpers/parser → internal}/unescape.js +0 -0
- /package/src/{helpers/general → internal}/applyBoolean.ts +0 -0
- /package/src/{helpers/parser → internal}/unescape.ts +0 -0
package/src/ast/handlers.ts
CHANGED
|
@@ -1,39 +1,38 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-shadow */
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import { VariableNode } from "./classes/VariableNode.js"
|
|
3
|
+
import { createArrayNode } from "./createArrayNode.js"
|
|
4
|
+
import { createConditionNode } from "./createConditionNode.js"
|
|
5
|
+
import { createExpressionNode } from "./createExpressionNode.js"
|
|
6
|
+
import { createGroupNode } from "./createGroupNode.js"
|
|
7
|
+
import { createToken } from "./createToken.js"
|
|
8
|
+
import { createVariableNode } from "./createVariableNode.js"
|
|
10
9
|
|
|
11
|
-
import { type AnyToken, type
|
|
10
|
+
import { type AnyToken, type ArrayNode,AST_TYPE,type ConditionNode, type ErrorToken, type ExpressionNode,type FirstParam,type GroupNode, type Position, TOKEN_TYPE, type TokenBooleanTypes, type TokenDelimiterTypes, type TokenOperatorTypes, type TokenQuoteTypes, type ValidToken, type VariableNode } from "../types/ast.js"
|
|
12
11
|
|
|
13
12
|
|
|
14
13
|
/* #region HELPERS */
|
|
15
|
-
function error<T extends TOKEN_TYPE>(pos: number, expected: T[]): ErrorToken
|
|
14
|
+
function error<T extends TOKEN_TYPE>(pos: number, expected: T[]): ErrorToken {
|
|
16
15
|
if (pos === undefined) throw new Error("should never happen, passed undefined position for error token")
|
|
17
|
-
return
|
|
16
|
+
return createToken({ expected, start: pos, end: pos })
|
|
18
17
|
}
|
|
19
18
|
/* #regionend */
|
|
20
19
|
|
|
21
20
|
/* #region TOKENS */
|
|
22
21
|
const operators = <T extends TokenOperatorTypes>
|
|
23
22
|
(type: T) =>
|
|
24
|
-
(value: string, pos: Position): ValidToken<T> =>
|
|
23
|
+
(value: string, pos: Position): ValidToken<T> => createToken({ value, type, ...pos })
|
|
25
24
|
|
|
26
25
|
const delimiters = <T extends TokenDelimiterTypes>
|
|
27
26
|
(type: T) =>
|
|
28
27
|
(value: string | undefined, pos: Position): ValidToken<T> | undefined =>
|
|
29
28
|
// check must be falsy, we want to return undefined when given ""
|
|
30
|
-
value ?
|
|
29
|
+
value ? createToken({ value, type, ...pos }) : undefined
|
|
31
30
|
|
|
32
|
-
const maybeToken = <T extends TOKEN_TYPE> (type: T) => <TVal extends string | undefined> (value: TVal, pos: Position): TVal extends string ? ValidToken<T> : ErrorToken
|
|
31
|
+
const maybeToken = <T extends TOKEN_TYPE> (type: T) => <TVal extends string | undefined> (value: TVal, pos: Position): TVal extends string ? ValidToken<T> : ErrorToken => {
|
|
33
32
|
if (value === undefined) {
|
|
34
33
|
return error(pos.end, [type]) as any
|
|
35
34
|
} else {
|
|
36
|
-
return
|
|
35
|
+
return createToken({ value, type, ...pos }) as any
|
|
37
36
|
}
|
|
38
37
|
}
|
|
39
38
|
|
|
@@ -70,7 +69,7 @@ export function variable(
|
|
|
70
69
|
quoteR: AnyToken<TokenQuoteTypes> | null | undefined,
|
|
71
70
|
flags?: ValidToken<TOKEN_TYPE.VALUE>,
|
|
72
71
|
): VariableNode {
|
|
73
|
-
const node:
|
|
72
|
+
const node: FirstParam<typeof createVariableNode> = {
|
|
74
73
|
prefix: prefix ?? undefined,
|
|
75
74
|
value: value ?? error((prefix?.end ?? quoteL?.end ?? quoteR?.start)!, [TOKEN_TYPE.VALUE]),
|
|
76
75
|
start: (prefix?.start ?? quoteL?.start ?? value?.start ?? quoteR?.start)!,
|
|
@@ -79,17 +78,16 @@ export function variable(
|
|
|
79
78
|
|
|
80
79
|
if (quoteL || quoteR) {
|
|
81
80
|
node.quote = {
|
|
82
|
-
left: quoteL ?? error(node.value.start, [(quoteR!.type)]),
|
|
83
|
-
right: quoteR ?? error(node.value.end, [(quoteL!.type)]),
|
|
81
|
+
left: quoteL ?? error(node.value.start, [(quoteR!.type!)]),
|
|
82
|
+
right: quoteR ?? error(node.value.end, [(quoteL!.type!)]),
|
|
84
83
|
}
|
|
85
84
|
if (flags) {
|
|
86
85
|
node.quote.flags = flags
|
|
87
|
-
node.end = node.quote?.flags.end
|
|
86
|
+
node.end = node.quote?.flags?.end ?? node.end
|
|
88
87
|
}
|
|
89
88
|
}
|
|
90
89
|
|
|
91
|
-
|
|
92
|
-
return instance
|
|
90
|
+
return createVariableNode(node)
|
|
93
91
|
}
|
|
94
92
|
|
|
95
93
|
|
|
@@ -105,7 +103,7 @@ export function condition(
|
|
|
105
103
|
): ConditionNode {
|
|
106
104
|
const start = (not?.start ?? property?.start ?? sepL?.start ?? propertyOperator?.start ?? sepR?.start ?? value?.start)!
|
|
107
105
|
const end = (value?.end ?? sepR?.end ?? propertyOperator?.end ?? sepL?.end ?? property?.end ?? not?.end)!
|
|
108
|
-
const node:
|
|
106
|
+
const node: FirstParam<typeof createConditionNode> = {
|
|
109
107
|
value: value ? value : error(end, [TOKEN_TYPE.VALUE]),
|
|
110
108
|
start,
|
|
111
109
|
end,
|
|
@@ -121,15 +119,13 @@ export function condition(
|
|
|
121
119
|
node.propertyOperator ||= error(sepL?.end ?? sepR?.start, [TOKEN_TYPE.VALUE])
|
|
122
120
|
}
|
|
123
121
|
if (sepR) node.sep.right = sepR
|
|
124
|
-
else if (!node.value || node.value
|
|
122
|
+
else if (!node.value || node.value.type === AST_TYPE.VARIABLE) {
|
|
125
123
|
node.sep.right = error(node.value?.start ?? end, [TOKEN_TYPE.OP_EXPANDED_SEP])
|
|
126
124
|
}
|
|
127
125
|
} else if (propertyOperator) {
|
|
128
126
|
node.property ||= error(propertyOperator.start, [TOKEN_TYPE.VALUE])
|
|
129
127
|
}
|
|
130
|
-
|
|
131
|
-
const instance = new ConditionNode(node as any)
|
|
132
|
-
return instance
|
|
128
|
+
return createConditionNode(node as ConditionNode)
|
|
133
129
|
}
|
|
134
130
|
|
|
135
131
|
export function expression(
|
|
@@ -137,14 +133,13 @@ export function expression(
|
|
|
137
133
|
operator: ValidToken<TokenBooleanTypes> | null | undefined,
|
|
138
134
|
right: ConditionNode | GroupNode | null | undefined,
|
|
139
135
|
): ExpressionNode {
|
|
140
|
-
|
|
136
|
+
return createExpressionNode({
|
|
141
137
|
left: left ?? error((operator?.start ?? right?.start)!, [TOKEN_TYPE.VALUE]),
|
|
142
138
|
operator: operator ?? error((left?.end ?? right?.start)!, [TOKEN_TYPE.AND, TOKEN_TYPE.OR]),
|
|
143
139
|
right: right ?? error((operator?.end ?? left?.end)!, [TOKEN_TYPE.VALUE]),
|
|
144
140
|
start: (left?.start ?? operator?.start ?? right?.start)!,
|
|
145
141
|
end: (right?.end ?? operator?.end ?? left?.end)!,
|
|
146
142
|
})
|
|
147
|
-
return instance
|
|
148
143
|
}
|
|
149
144
|
|
|
150
145
|
export function group(
|
|
@@ -154,7 +149,7 @@ export function group(
|
|
|
154
149
|
condition: GroupNode["expression"],
|
|
155
150
|
parenR: ValidToken<TOKEN_TYPE.PARENR> | null | undefined,
|
|
156
151
|
): GroupNode {
|
|
157
|
-
|
|
152
|
+
return createGroupNode({
|
|
158
153
|
prefix: prefix ?? operator ?? undefined,
|
|
159
154
|
expression: condition ?? error((parenL?.end ?? parenR?.start)!, [TOKEN_TYPE.VALUE]),
|
|
160
155
|
paren: {
|
|
@@ -163,10 +158,7 @@ export function group(
|
|
|
163
158
|
},
|
|
164
159
|
start: (prefix?.start ?? operator?.start ?? parenL?.start ?? condition?.start ?? parenR?.start)!,
|
|
165
160
|
end: (parenR?.end ?? condition?.end ?? parenL?.end ?? operator?.end ?? prefix?.end)!,
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
const instance = new GroupNode(node as any)
|
|
169
|
-
return instance
|
|
161
|
+
})
|
|
170
162
|
}
|
|
171
163
|
|
|
172
164
|
export function array(
|
|
@@ -174,7 +166,7 @@ export function array(
|
|
|
174
166
|
values: VariableNode[],
|
|
175
167
|
bracketR: ValidToken<TOKEN_TYPE.BRACKETR> | null | undefined,
|
|
176
168
|
): ArrayNode {
|
|
177
|
-
|
|
169
|
+
return createArrayNode({
|
|
178
170
|
values,
|
|
179
171
|
bracket: {
|
|
180
172
|
left: bracketL, // always valid for now
|
|
@@ -182,9 +174,6 @@ export function array(
|
|
|
182
174
|
},
|
|
183
175
|
start: bracketL.start,
|
|
184
176
|
end: (bracketR?.end ?? values[values.length - 1]?.end ?? bracketL.end)!,
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
const instance = new ArrayNode(node as any)
|
|
188
|
-
return instance
|
|
177
|
+
})
|
|
189
178
|
}
|
|
190
179
|
/* #regionend */
|
package/src/ast/index.ts
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
export * as builders from "./builders/index.js"
|
|
2
|
-
export * as classes from "./classes/index.js"
|
|
3
2
|
/* Autogenerated Index */
|
|
4
3
|
|
|
4
|
+
export { createArrayNode } from "./createArrayNode.js"
|
|
5
|
+
export { createCondition } from "./createNormalizedCondition.js"
|
|
6
|
+
export { createConditionNode } from "./createConditionNode.js"
|
|
7
|
+
export { createExpression } from "./createNormalizedExpression.js"
|
|
8
|
+
export { createExpressionNode } from "./createExpressionNode.js"
|
|
9
|
+
export { createGroupNode } from "./createGroupNode.js"
|
|
10
|
+
export { createToken } from "./createToken.js"
|
|
11
|
+
export { createVariableNode } from "./createVariableNode.js"
|
|
12
|
+
export { error } from "./error.js"
|
|
5
13
|
export { token } from "./handlers.js"
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
/* Autogenerated Index [Ignore] */
|
|
2
2
|
|
|
3
|
-
export { applyBoolean } from "./applyBoolean.js"
|
|
4
|
-
export { applyPrefix } from "./applyPrefix.js"
|
|
5
3
|
export { defaultConditionNormalizer } from "./defaultConditionNormalizer.js"
|
|
6
4
|
export { defaultKeyParser } from "./defaultKeyParser.js"
|
|
7
5
|
export { defaultPrefixApplier } from "./defaultPrefixApplier.js"
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/* Manually Generated Index */
|
|
2
2
|
|
|
3
|
-
export * as ast from "./ast/
|
|
3
|
+
export * as ast from "./ast/index.js"
|
|
4
4
|
export { Parser } from "./Parser.js"
|
|
5
5
|
export * from "./types/index.js"
|
|
6
6
|
export * as utils from "./utils/index.js"
|
|
7
|
+
export * as defaults from "./defaults/index.js"
|
|
7
8
|
|
|
@@ -7,8 +7,8 @@ import type { Keys } from "@alanscodelog/utils/types"
|
|
|
7
7
|
import { repository, version } from "../package.js"
|
|
8
8
|
import type { ERROR_CODES, ErrorInfo } from "../types/errors.js"
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
export class
|
|
10
|
+
/** @internal */
|
|
11
|
+
export class ExpressitError<T extends ERROR_CODES> extends Error {
|
|
12
12
|
version: string = version
|
|
13
13
|
|
|
14
14
|
repo: string = repository
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { isBlank } from "@alanscodelog/utils/isBlank.js"
|
|
2
2
|
import { pushIfNotIn } from "@alanscodelog/utils/pushIfNotIn.js"
|
|
3
3
|
|
|
4
|
-
import {
|
|
5
|
-
import type { FullParserOptions, ParserOptions } from "../../types/parser.js"
|
|
6
|
-
import { BooleanParserLibraryError } from "../errors.js"
|
|
7
|
-
import { defaultConditionNormalizer } from "../general/defaultConditionNormalizer.js"
|
|
8
|
-
import { defaultValueComparer } from "../general/defaultValueComparer.js"
|
|
4
|
+
import { ExpressitError } from "./ExpressitError.js"
|
|
9
5
|
|
|
6
|
+
import { defaultConditionNormalizer } from "../defaults/defaultConditionNormalizer.js"
|
|
7
|
+
import { defaultValueComparer } from "../defaults/defaultValueComparer.js"
|
|
8
|
+
import { ERROR_CODES } from "../types/errors.js"
|
|
9
|
+
import type { FullParserOptions, ParserOptions } from "../types/parser.js"
|
|
10
10
|
|
|
11
|
+
/** @internal */
|
|
11
12
|
export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evaluatorChecks: boolean = false, validatorChecks: boolean = false): void {
|
|
12
13
|
if (!evaluatorChecks) {
|
|
13
14
|
const keywordsList = [...opts.keywords.and, ...opts.keywords.or, ...opts.keywords.not].map(keyword => keyword.value)
|
|
@@ -25,7 +26,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
|
|
|
25
26
|
|
|
26
27
|
/* #region Blank Operator Checks */
|
|
27
28
|
if (opts.expandedPropertySeparator && isBlank(opts.expandedPropertySeparator)) {
|
|
28
|
-
throw new
|
|
29
|
+
throw new ExpressitError(
|
|
29
30
|
ERROR_CODES.PARSER_CONFLICTING_OPTIONS_ERROR,
|
|
30
31
|
{ prohibited: [""], invalid: opts.expandedPropertySeparator },
|
|
31
32
|
`expandedPropertySeparator cannot be blank`,
|
|
@@ -33,7 +34,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
|
|
|
33
34
|
}
|
|
34
35
|
const customInvalid = opts.customPropertyOperators?.find(_ => isBlank(_))
|
|
35
36
|
if (customInvalid !== undefined) {
|
|
36
|
-
throw new
|
|
37
|
+
throw new ExpressitError(
|
|
37
38
|
ERROR_CODES.PARSER_CONFLICTING_OPTIONS_ERROR,
|
|
38
39
|
{ prohibited: [""], invalid: customInvalid },
|
|
39
40
|
`customPropertyOperators cannot contain blank entries`,
|
|
@@ -41,7 +42,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
|
|
|
41
42
|
}
|
|
42
43
|
const prefixInvalid = opts.prefixableStrings?.find(_ => isBlank(_))
|
|
43
44
|
if (prefixInvalid !== undefined) {
|
|
44
|
-
throw new
|
|
45
|
+
throw new ExpressitError(
|
|
45
46
|
ERROR_CODES.PARSER_CONFLICTING_OPTIONS_ERROR,
|
|
46
47
|
{ prohibited: [""], invalid: prefixInvalid },
|
|
47
48
|
`prefixableStrings cannot contain blank entries`,
|
|
@@ -52,7 +53,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
|
|
|
52
53
|
?.find(_ => isBlank(_.value))
|
|
53
54
|
?.value
|
|
54
55
|
if (invalid !== undefined) {
|
|
55
|
-
throw new
|
|
56
|
+
throw new ExpressitError(
|
|
56
57
|
ERROR_CODES.PARSER_CONFLICTING_OPTIONS_ERROR,
|
|
57
58
|
{ prohibited: [""], invalid },
|
|
58
59
|
`keywords.${key} cannot contain entries with blank values`,
|
|
@@ -75,7 +76,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
|
|
|
75
76
|
|
|
76
77
|
const invalidPrefixableString = opts.prefixableStrings?.find(val => all.includes(val))
|
|
77
78
|
if (invalidPrefixableString) {
|
|
78
|
-
throw new
|
|
79
|
+
throw new ExpressitError(
|
|
79
80
|
ERROR_CODES.PARSER_CONFLICTING_OPTIONS_ERROR,
|
|
80
81
|
{ prohibited: all, invalid: invalidPrefixableString },
|
|
81
82
|
`prefixableStrings ${messageInvalidAny} "${invalidPrefixableString}"`,
|
|
@@ -85,7 +86,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
|
|
|
85
86
|
const invalidExpandedPropertySeparator = allKeywords
|
|
86
87
|
.find(_ => _ === opts.expandedPropertySeparator as any)
|
|
87
88
|
if (invalidExpandedPropertySeparator) {
|
|
88
|
-
throw new
|
|
89
|
+
throw new ExpressitError(
|
|
89
90
|
ERROR_CODES.PARSER_CONFLICTING_OPTIONS_ERROR,
|
|
90
91
|
{ prohibited: allKeywords, invalid: invalidExpandedPropertySeparator },
|
|
91
92
|
`expandedPropertySeparator ${messageInvalidBool} "${invalidExpandedPropertySeparator}"`,
|
|
@@ -96,7 +97,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
|
|
|
96
97
|
? opts.expandedPropertySeparator
|
|
97
98
|
: undefined
|
|
98
99
|
if (invalidCustomPropertyOperator) {
|
|
99
|
-
throw new
|
|
100
|
+
throw new ExpressitError(
|
|
100
101
|
ERROR_CODES.PARSER_CONFLICTING_OPTIONS_ERROR,
|
|
101
102
|
{ prohibited: keywords, invalid: invalidCustomPropertyOperator },
|
|
102
103
|
`customPropertyOperator ${messageInvalidBool} "${invalidCustomPropertyOperator}"`,
|
|
@@ -113,7 +114,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
|
|
|
113
114
|
if (opts.regexValues) requireCustomNormalizer.push("regexValues")
|
|
114
115
|
|
|
115
116
|
if (requireCustomNormalizer.length > 0 && opts.conditionNormalizer === defaultConditionNormalizer) {
|
|
116
|
-
throw new
|
|
117
|
+
throw new ExpressitError(
|
|
117
118
|
ERROR_CODES.PARSER_OPTION_REQUIRED_ERROR,
|
|
118
119
|
{ options: requireCustomNormalizer, requires: "conditionNormalizer" },
|
|
119
120
|
`A custom conditionNormalizer function must be specified when using the following options: ${requireCustomNormalizer.join(", ")}`,
|
|
@@ -124,7 +125,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
|
|
|
124
125
|
if (opts.regexValues) requireCustomComparer.push("arrayValues")
|
|
125
126
|
|
|
126
127
|
if (requireCustomComparer.length > 0 && opts.valueComparer === defaultValueComparer) {
|
|
127
|
-
throw new
|
|
128
|
+
throw new ExpressitError(
|
|
128
129
|
ERROR_CODES.PARSER_OPTION_REQUIRED_ERROR,
|
|
129
130
|
{ options: requireCustomComparer, requires: "valueComparer" },
|
|
130
131
|
`A custom valueComparer function must be specified when using the following options: ${requireCustomComparer.join(", ")}`,
|
|
@@ -133,7 +134,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
|
|
|
133
134
|
}
|
|
134
135
|
if (validatorChecks) {
|
|
135
136
|
if (opts.valueValidator === undefined) {
|
|
136
|
-
throw new
|
|
137
|
+
throw new ExpressitError(
|
|
137
138
|
ERROR_CODES.PARSER_OPTION_REQUIRED_ERROR,
|
|
138
139
|
{ requires: "valueValidator" },
|
|
139
140
|
`A custom valueValidator function must be specified when using the validate method.`,
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { multisplice } from "@alanscodelog/utils/multisplice.js"
|
|
2
|
+
|
|
3
|
+
export function escapeVariableOrPrefix(variable: string, preferredQuote: string): string {
|
|
4
|
+
let doQuote = false
|
|
5
|
+
for (const quoteType of ["\"", "'", "`"]) {
|
|
6
|
+
if (!variable.includes(quoteType) && !variable.includes(" ")) continue
|
|
7
|
+
if (variable.startsWith(quoteType)
|
|
8
|
+
&& variable.endsWith(quoteType)) {
|
|
9
|
+
break
|
|
10
|
+
}
|
|
11
|
+
const indexes: number[] = []
|
|
12
|
+
for (let i = 0; i < variable.length; i++) {
|
|
13
|
+
const char = variable[i]
|
|
14
|
+
if (char === undefined) break
|
|
15
|
+
if (char === "\\") {
|
|
16
|
+
i += 2
|
|
17
|
+
continue
|
|
18
|
+
}
|
|
19
|
+
if (char === " ") {
|
|
20
|
+
doQuote = true
|
|
21
|
+
}
|
|
22
|
+
if (char === quoteType) indexes.push(i)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (indexes.length === 0) break
|
|
26
|
+
|
|
27
|
+
const newVal = multisplice(variable.split(""), indexes, 0, "\\").array.join("")
|
|
28
|
+
variable = newVal
|
|
29
|
+
|
|
30
|
+
break
|
|
31
|
+
}
|
|
32
|
+
if (doQuote) {
|
|
33
|
+
variable = `${preferredQuote}${variable}${preferredQuote}`
|
|
34
|
+
}
|
|
35
|
+
return variable
|
|
36
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
1
|
+
import { defaultConditionNormalizer } from "../defaults/defaultConditionNormalizer.js"
|
|
2
|
+
import { defaultKeyParser } from "../defaults/defaultKeyParser.js"
|
|
3
|
+
import { defaultPrefixApplier } from "../defaults/defaultPrefixApplier.js"
|
|
4
|
+
import { defaultValueComparer } from "../defaults/defaultValueComparer.js"
|
|
5
|
+
import type { FullParserOptions, ParserOptions } from "../types/parser.js"
|
|
6
6
|
|
|
7
7
|
/** @internal */
|
|
8
8
|
export function parseParserOptions<T extends {} = {}>(
|