@witchcraft/expressit 0.0.2 → 0.1.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 +146 -0
- package/dist/Lexer.d.ts.map +1 -0
- package/dist/Lexer.js +960 -0
- package/dist/Parser.d.ts +140 -0
- package/dist/Parser.d.ts.map +1 -0
- package/dist/Parser.js +668 -0
- package/dist/ast/builders/token.js +1 -1
- package/dist/ast/handlers.d.ts +3 -3
- package/dist/ast/handlers.d.ts.map +1 -1
- package/dist/ast/index.d.ts.map +1 -1
- package/dist/examples/index.d.ts +2 -0
- package/dist/examples/index.d.ts.map +1 -0
- package/dist/examples/index.js +4 -0
- package/dist/examples/shortcutContextParser.d.ts +2 -1
- package/dist/examples/shortcutContextParser.d.ts.map +1 -1
- package/dist/examples/shortcutContextParser.js +9 -5
- package/dist/helpers/errors.d.ts.map +1 -1
- package/dist/helpers/errors.js +3 -1
- package/dist/helpers/index.d.ts.map +1 -1
- package/dist/helpers/parser/checkParserOpts.d.ts.map +1 -1
- package/dist/helpers/parser/checkParserOpts.js +3 -2
- package/dist/helpers/parser/extractPosition.d.ts +2 -6
- package/dist/helpers/parser/extractPosition.d.ts.map +1 -1
- package/dist/helpers/parser/extractPosition.js +3 -3
- package/dist/helpers/parser/getUnclosedRightParenCount.d.ts +2 -3
- package/dist/helpers/parser/getUnclosedRightParenCount.d.ts.map +1 -1
- package/dist/helpers/parser/getUnclosedRightParenCount.js +4 -4
- package/dist/index.d.ts +1 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -5
- package/dist/methods/autocomplete.d.ts.map +1 -1
- package/dist/methods/autocomplete.js +1 -1
- package/dist/methods/autoreplace.js +1 -1
- package/dist/methods/autosuggest.js +1 -1
- package/dist/methods/evaluate.d.ts.map +1 -1
- package/dist/methods/evaluate.js +3 -1
- package/dist/methods/getIndexes.d.ts.map +1 -1
- package/dist/methods/getIndexes.js +2 -1
- package/dist/methods/normalize.d.ts +0 -2
- package/dist/methods/normalize.d.ts.map +1 -1
- package/dist/methods/normalize.js +2 -3
- package/dist/methods/validate.d.ts.map +1 -1
- package/dist/methods/validate.js +3 -1
- package/dist/package.json.js +44 -37
- package/dist/types/ast.d.ts +2 -8
- package/dist/types/ast.d.ts.map +1 -1
- package/dist/types/errors.d.ts +5 -17
- package/dist/types/errors.d.ts.map +1 -1
- package/dist/types/errors.js +0 -1
- package/dist/types/parser.d.ts +6 -2
- package/dist/types/parser.d.ts.map +1 -1
- package/dist/utils/extractTokens.js +1 -1
- package/dist/utils/getCursorInfo.d.ts +2 -2
- package/dist/utils/getCursorInfo.d.ts.map +1 -1
- package/dist/utils/getCursorInfo.js +3 -2
- package/dist/utils/getOppositeDelimiter.d.ts.map +1 -1
- package/dist/utils/getOppositeDelimiter.js +1 -1
- package/dist/utils/prettyAst.d.ts.map +1 -1
- package/dist/utils/prettyAst.js +15 -9
- package/package.json +42 -37
- package/src/Lexer.ts +704 -0
- package/src/Parser.ts +972 -0
- package/src/ast/builders/array.ts +2 -2
- package/src/ast/builders/condition.ts +1 -1
- package/src/ast/builders/expression.ts +1 -1
- package/src/ast/builders/group.ts +1 -1
- package/src/ast/builders/index.ts +1 -1
- package/src/ast/builders/pos.ts +1 -1
- package/src/ast/builders/token.ts +2 -2
- package/src/ast/builders/type.ts +1 -1
- package/src/ast/builders/variable.ts +1 -1
- package/src/ast/classes/ConditionNode.ts +1 -1
- package/src/ast/classes/ErrorToken.ts +1 -1
- package/src/ast/classes/ValidToken.ts +2 -2
- package/src/ast/classes/index.ts +1 -1
- package/src/ast/handlers.ts +6 -6
- package/src/ast/index.ts +2 -2
- package/src/examples/index.ts +3 -0
- package/src/examples/shortcutContextParser.ts +11 -6
- package/src/helpers/errors.ts +5 -3
- package/src/helpers/general/defaultConditionNormalizer.ts +1 -1
- package/src/helpers/general/index.ts +1 -1
- package/src/helpers/index.ts +3 -2
- package/src/helpers/parser/checkParserOpts.ts +13 -12
- package/src/helpers/parser/extractPosition.ts +4 -8
- package/src/helpers/parser/getUnclosedRightParenCount.ts +6 -6
- package/src/helpers/parser/index.ts +1 -1
- package/src/helpers/parser/parseParserOptions.ts +1 -1
- package/src/index.ts +2 -2
- package/src/methods/autocomplete.ts +5 -5
- package/src/methods/autoreplace.ts +2 -2
- package/src/methods/autosuggest.ts +3 -3
- package/src/methods/evaluate.ts +4 -2
- package/src/methods/getIndexes.ts +2 -1
- package/src/methods/normalize.ts +3 -4
- package/src/methods/validate.ts +4 -2
- package/src/types/ast.ts +2 -9
- package/src/types/errors.ts +12 -22
- package/src/types/parser.ts +6 -4
- package/src/utils/extractTokens.ts +1 -1
- package/src/utils/getCursorInfo.ts +6 -4
- package/src/utils/getOppositeDelimiter.ts +5 -2
- package/src/utils/prettyAst.ts +5 -3
- package/dist/examples/advancedValueComparer.d.ts +0 -3
- package/dist/examples/advancedValueComparer.d.ts.map +0 -1
- package/dist/examples/advancedValueComparer.js +0 -28
- package/dist/grammar/ParserBase.d.ts +0 -51
- package/dist/grammar/ParserBase.d.ts.map +0 -1
- package/dist/grammar/ParserBase.js +0 -516
- package/dist/grammar/createTokens.d.ts +0 -56
- package/dist/grammar/createTokens.d.ts.map +0 -1
- package/dist/grammar/createTokens.js +0 -843
- package/dist/grammar/index.d.ts +0 -3
- package/dist/grammar/index.d.ts.map +0 -1
- package/dist/grammar/index.js +0 -6
- package/dist/parser.d.ts +0 -58
- package/dist/parser.d.ts.map +0 -1
- package/dist/parser.js +0 -136
- package/src/examples/advancedValueComparer.ts +0 -31
- package/src/grammar/ParserBase.ts +0 -715
- package/src/grammar/createTokens.ts +0 -512
- package/src/grammar/index.ts +0 -4
- package/src/parser.ts +0 -183
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Mutable } from "@alanscodelog/utils"
|
|
1
|
+
import type { Mutable } from "@alanscodelog/utils/types"
|
|
2
2
|
|
|
3
3
|
import { token } from "./token.js"
|
|
4
4
|
|
|
@@ -11,7 +11,7 @@ export function array(
|
|
|
11
11
|
values: VariableNode[],
|
|
12
12
|
bracket: { left?: boolean, right?: boolean } = { right: true, left: true },
|
|
13
13
|
parenLeftPos?: Position,
|
|
14
|
-
parenRightPos?: Position
|
|
14
|
+
parenRightPos?: Position,
|
|
15
15
|
): ArrayNode {
|
|
16
16
|
const node: Mutable<Partial<ArrayNode>> = {
|
|
17
17
|
type: AST_TYPE.ARRAY,
|
|
@@ -29,7 +29,7 @@ export function condition(
|
|
|
29
29
|
not: true | ValidToken<TOKEN_TYPE.NOT> = true,
|
|
30
30
|
property?: VariableNode | ErrorToken<TOKEN_TYPE.VALUE>,
|
|
31
31
|
propertyOperator?: AnyToken<TOKEN_TYPE.OP_CUSTOM | TOKEN_TYPE.VALUE>,
|
|
32
|
-
{ right, left }: Partial<Record<"right" | "left", ValidToken<TOKEN_TYPE.OP_EXPANDED_SEP>>> = { }
|
|
32
|
+
{ right, left }: Partial<Record<"right" | "left", ValidToken<TOKEN_TYPE.OP_EXPANDED_SEP>>> = { },
|
|
33
33
|
): ConditionNode {
|
|
34
34
|
const position = pos(value satisfies Position)
|
|
35
35
|
const notStart = not === true ? undefined : not.start
|
|
@@ -32,7 +32,7 @@ export function group(
|
|
|
32
32
|
|
|
33
33
|
paren: { left?: boolean, right?: boolean } = { right: true, left: true },
|
|
34
34
|
parenLeftPos?: Position,
|
|
35
|
-
parenRightPos?: Position
|
|
35
|
+
parenRightPos?: Position,
|
|
36
36
|
): GroupNode {
|
|
37
37
|
if (expression === undefined) {
|
|
38
38
|
expression = token(TOKEN_TYPE.VALUE, undefined, prefix?.end !== undefined ? { start: prefix.end } : undefined)
|
package/src/ast/builders/pos.ts
CHANGED
|
@@ -31,7 +31,7 @@ export function pos<TItem extends Position | Partial<Position> | EmptyObj>(
|
|
|
31
31
|
export function pos(start: number, end: number): Position
|
|
32
32
|
export function pos(
|
|
33
33
|
start: number | Position | Partial<Position> | EmptyObj | undefined,
|
|
34
|
-
end?: number | { fill: boolean }
|
|
34
|
+
end?: number | { fill: boolean },
|
|
35
35
|
): Position | EmptyObj {
|
|
36
36
|
if (typeof start === "number") {
|
|
37
37
|
return { start, end: end as number }
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isArray } from "@alanscodelog/utils"
|
|
1
|
+
import { isArray } from "@alanscodelog/utils/isArray"
|
|
2
2
|
|
|
3
3
|
import { pos } from "./pos.js"
|
|
4
4
|
|
|
@@ -21,7 +21,7 @@ export function token<
|
|
|
21
21
|
>(
|
|
22
22
|
type: TValue extends undefined ? TType | TType[] : TType,
|
|
23
23
|
value: TValue,
|
|
24
|
-
position: Position | Partial<Position> | EmptyObj = {}
|
|
24
|
+
position: Position | Partial<Position> | EmptyObj = {},
|
|
25
25
|
): TValue extends undefined
|
|
26
26
|
? ErrorToken<TType>
|
|
27
27
|
: ValidToken<TType> {
|
package/src/ast/builders/type.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { type ExtractTokenType, TOKEN_TYPE } from "../../types/ast.js"
|
|
|
3
3
|
* Given a the string value of an operator or single delimiter token, returns the corresponding @see ValidToken_TYPE .
|
|
4
4
|
*/
|
|
5
5
|
export function type<T extends string>(
|
|
6
|
-
operatorSymbol: T
|
|
6
|
+
operatorSymbol: T,
|
|
7
7
|
): ExtractTokenType<T> {
|
|
8
8
|
switch (operatorSymbol) {
|
|
9
9
|
case "`": return TOKEN_TYPE.BACKTICK as any
|
|
@@ -18,7 +18,7 @@ export function variable(
|
|
|
18
18
|
prefix: ValidToken<TOKEN_TYPE.VALUE> | undefined,
|
|
19
19
|
value: string | AnyToken<TOKEN_TYPE.VALUE>,
|
|
20
20
|
quote?: { type: TokenQuoteTypes, left?: boolean, right?: boolean, flags?: string },
|
|
21
|
-
position?: Position | EmptyObj
|
|
21
|
+
position?: Position | EmptyObj,
|
|
22
22
|
): VariableNode {
|
|
23
23
|
if (typeof value === "string") {
|
|
24
24
|
value = token(TOKEN_TYPE.VALUE, value, position)
|
|
@@ -36,7 +36,7 @@ export class ValidToken<
|
|
|
36
36
|
| GroupNode
|
|
37
37
|
| ExpressionNode
|
|
38
38
|
| ConditionNode
|
|
39
|
-
| ArrayNode
|
|
39
|
+
| ArrayNode,
|
|
40
40
|
) {
|
|
41
41
|
if (this.#parent) {throw new Error("parent property is readonly")}
|
|
42
42
|
this.#parent = value
|
|
@@ -47,7 +47,7 @@ export class ValidToken<
|
|
|
47
47
|
value: string
|
|
48
48
|
start: number
|
|
49
49
|
end: number
|
|
50
|
-
}
|
|
50
|
+
},
|
|
51
51
|
) {
|
|
52
52
|
super(start, end)
|
|
53
53
|
this.type = type
|
package/src/ast/classes/index.ts
CHANGED
package/src/ast/handlers.ts
CHANGED
|
@@ -29,11 +29,11 @@ const delimiters = <T extends TokenDelimiterTypes>
|
|
|
29
29
|
// check must be falsy, we want to return undefined when given ""
|
|
30
30
|
value ? new ValidToken({ value, type, ...pos }) : undefined
|
|
31
31
|
|
|
32
|
-
const maybeToken = <T extends TOKEN_TYPE>(type: T) =>
|
|
32
|
+
const maybeToken = <T extends TOKEN_TYPE> (type: T) => <TVal extends string | undefined> (value: TVal, pos: Position): TVal extends string ? ValidToken<T> : ErrorToken<T> => {
|
|
33
33
|
if (value === undefined) {
|
|
34
|
-
return error(pos.end, [type])
|
|
34
|
+
return error(pos.end, [type]) as any
|
|
35
35
|
} else {
|
|
36
|
-
return new ValidToken({ value, type, ...pos })
|
|
36
|
+
return new ValidToken({ value, type, ...pos }) as any
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
|
|
@@ -68,7 +68,7 @@ export function variable(
|
|
|
68
68
|
quoteL: AnyToken<TokenQuoteTypes> | null | undefined,
|
|
69
69
|
value: AnyToken<TOKEN_TYPE.VALUE> | null | undefined,
|
|
70
70
|
quoteR: AnyToken<TokenQuoteTypes> | null | undefined,
|
|
71
|
-
flags?: ValidToken<TOKEN_TYPE.VALUE
|
|
71
|
+
flags?: ValidToken<TOKEN_TYPE.VALUE>,
|
|
72
72
|
): VariableNode {
|
|
73
73
|
const node: FirstConstructorParam<typeof VariableNode> = {
|
|
74
74
|
prefix: prefix ?? undefined,
|
|
@@ -152,7 +152,7 @@ export function group(
|
|
|
152
152
|
prefix: ConditionNode | null | undefined,
|
|
153
153
|
parenL: ValidToken<TOKEN_TYPE.PARENL> | null | undefined,
|
|
154
154
|
condition: GroupNode["expression"],
|
|
155
|
-
parenR: ValidToken<TOKEN_TYPE.PARENR> | null | undefined
|
|
155
|
+
parenR: ValidToken<TOKEN_TYPE.PARENR> | null | undefined,
|
|
156
156
|
): GroupNode {
|
|
157
157
|
const node: FirstConstructorParam<typeof GroupNode> = {
|
|
158
158
|
prefix: prefix ?? operator ?? undefined,
|
|
@@ -172,7 +172,7 @@ export function group(
|
|
|
172
172
|
export function array(
|
|
173
173
|
bracketL: ValidToken<TOKEN_TYPE.BRACKETL>,
|
|
174
174
|
values: VariableNode[],
|
|
175
|
-
bracketR: ValidToken<TOKEN_TYPE.BRACKETR> | null | undefined
|
|
175
|
+
bracketR: ValidToken<TOKEN_TYPE.BRACKETR> | null | undefined,
|
|
176
176
|
): ArrayNode {
|
|
177
177
|
const node: FirstConstructorParam<typeof ArrayNode> = {
|
|
178
178
|
values,
|
package/src/ast/index.ts
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* The validate function will return a list of positions with a list of errors which includes handling invalid or duplicate regex flags.
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
import { Parser } from "../
|
|
12
|
+
import { Parser } from "../Parser.js"
|
|
13
13
|
import type { Position } from "../types/ast.js"
|
|
14
14
|
import type { ValueQuery } from "../types/parser.js"
|
|
15
15
|
|
|
@@ -24,7 +24,7 @@ export class ShortcutContextParser<T extends
|
|
|
24
24
|
|
|
25
25
|
constructor(
|
|
26
26
|
dummyContext: Record<string, any>,
|
|
27
|
-
validRegexFlags: string[] = ["i", "u", "m"]
|
|
27
|
+
validRegexFlags: string[] = ["i", "u", "m"],
|
|
28
28
|
) {
|
|
29
29
|
super({
|
|
30
30
|
arrayValues: false,
|
|
@@ -43,11 +43,11 @@ export class ShortcutContextParser<T extends
|
|
|
43
43
|
}
|
|
44
44
|
return prefix + variable
|
|
45
45
|
},
|
|
46
|
-
valueComparer: (
|
|
47
|
-
if (
|
|
48
|
-
return contextValue.match(
|
|
46
|
+
valueComparer: (condition, contextValue, _context) => {
|
|
47
|
+
if (condition.value instanceof RegExp) {
|
|
48
|
+
return contextValue.match(condition.value) !== null
|
|
49
49
|
}
|
|
50
|
-
return contextValue ===
|
|
50
|
+
return contextValue === condition.value
|
|
51
51
|
},
|
|
52
52
|
valueValidator: (_contextValue, query): T[] | void => {
|
|
53
53
|
let tokens: T[] = []
|
|
@@ -123,6 +123,11 @@ export class ShortcutContextParser<T extends
|
|
|
123
123
|
this._extractKeysFromContext(dummyContext)
|
|
124
124
|
}
|
|
125
125
|
|
|
126
|
+
setContext(context: Record<string, any>): void {
|
|
127
|
+
this.validKeys = []
|
|
128
|
+
this._extractKeysFromContext(context)
|
|
129
|
+
}
|
|
130
|
+
|
|
126
131
|
private _extractKeysFromContext(context: Record<string, any>, prev?: string): void {
|
|
127
132
|
for (const key of Object.keys(context)) {
|
|
128
133
|
if (typeof context[key] === "boolean") {
|
package/src/helpers/errors.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
1
|
+
import { crop } from "@alanscodelog/utils/crop"
|
|
2
|
+
import { indent } from "@alanscodelog/utils/indent"
|
|
3
|
+
import { pretty } from "@alanscodelog/utils/pretty"
|
|
4
|
+
import type { Keys } from "@alanscodelog/utils/types"
|
|
3
5
|
|
|
4
6
|
// @ts-expect-error todo
|
|
5
7
|
import { repository, version } from "../package.js"
|
|
@@ -38,7 +40,7 @@ function forceStringifyErrors(_key: string, value: any): any {
|
|
|
38
40
|
key === "stack"
|
|
39
41
|
? value[key]!.split(/\n/)
|
|
40
42
|
: value[key],
|
|
41
|
-
])
|
|
43
|
+
]),
|
|
42
44
|
)
|
|
43
45
|
}
|
|
44
46
|
return value
|
|
@@ -3,7 +3,7 @@ import type { ConditionNormalizer, ValueQuery } from "../../types/parser.js"
|
|
|
3
3
|
|
|
4
4
|
export const defaultConditionNormalizer: ConditionNormalizer =
|
|
5
5
|
function defaultConditionNormalizer(
|
|
6
|
-
{ value, operator, isNegated }: Pick<ValueQuery, "value" | "operator" | "isNegated"
|
|
6
|
+
{ value, operator, isNegated }: Pick<ValueQuery, "value" | "operator" | "isNegated">,
|
|
7
7
|
) {
|
|
8
8
|
return { value, operator, negate: isNegated }
|
|
9
9
|
}
|
package/src/helpers/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { isBlank
|
|
1
|
+
import { isBlank } from "@alanscodelog/utils/isBlank"
|
|
2
|
+
import { pushIfNotIn } from "@alanscodelog/utils/pushIfNotIn"
|
|
2
3
|
|
|
3
4
|
import { ERROR_CODES } from "../../types/errors.js"
|
|
4
5
|
import type { FullParserOptions, ParserOptions } from "../../types/parser.js"
|
|
@@ -19,7 +20,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
|
|
|
19
20
|
]
|
|
20
21
|
const extra: string[] = []
|
|
21
22
|
if (opts.expandedPropertySeparator) extra.push(opts.expandedPropertySeparator)
|
|
22
|
-
if (opts.customPropertyOperators) pushIfNotIn(extra,
|
|
23
|
+
if (opts.customPropertyOperators) pushIfNotIn(extra, opts.customPropertyOperators)
|
|
23
24
|
|
|
24
25
|
|
|
25
26
|
/* #region Blank Operator Checks */
|
|
@@ -27,7 +28,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
|
|
|
27
28
|
throw new BooleanParserLibraryError(
|
|
28
29
|
ERROR_CODES.PARSER_CONFLICTING_OPTIONS_ERROR,
|
|
29
30
|
{ prohibited: [""], invalid: opts.expandedPropertySeparator },
|
|
30
|
-
`expandedPropertySeparator cannot be blank
|
|
31
|
+
`expandedPropertySeparator cannot be blank`,
|
|
31
32
|
)
|
|
32
33
|
}
|
|
33
34
|
const customInvalid = opts.customPropertyOperators?.find(_ => isBlank(_))
|
|
@@ -35,7 +36,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
|
|
|
35
36
|
throw new BooleanParserLibraryError(
|
|
36
37
|
ERROR_CODES.PARSER_CONFLICTING_OPTIONS_ERROR,
|
|
37
38
|
{ prohibited: [""], invalid: customInvalid },
|
|
38
|
-
`customPropertyOperators cannot contain blank entries
|
|
39
|
+
`customPropertyOperators cannot contain blank entries`,
|
|
39
40
|
)
|
|
40
41
|
}
|
|
41
42
|
const prefixInvalid = opts.prefixableStrings?.find(_ => isBlank(_))
|
|
@@ -43,7 +44,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
|
|
|
43
44
|
throw new BooleanParserLibraryError(
|
|
44
45
|
ERROR_CODES.PARSER_CONFLICTING_OPTIONS_ERROR,
|
|
45
46
|
{ prohibited: [""], invalid: prefixInvalid },
|
|
46
|
-
`prefixableStrings cannot contain blank entries
|
|
47
|
+
`prefixableStrings cannot contain blank entries`,
|
|
47
48
|
)
|
|
48
49
|
}
|
|
49
50
|
for (const key of ["and", "or", "not"]) {
|
|
@@ -54,7 +55,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
|
|
|
54
55
|
throw new BooleanParserLibraryError(
|
|
55
56
|
ERROR_CODES.PARSER_CONFLICTING_OPTIONS_ERROR,
|
|
56
57
|
{ prohibited: [""], invalid },
|
|
57
|
-
`keywords.${key} cannot contain entries with blank values
|
|
58
|
+
`keywords.${key} cannot contain entries with blank values`,
|
|
58
59
|
)
|
|
59
60
|
}
|
|
60
61
|
}
|
|
@@ -77,7 +78,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
|
|
|
77
78
|
throw new BooleanParserLibraryError(
|
|
78
79
|
ERROR_CODES.PARSER_CONFLICTING_OPTIONS_ERROR,
|
|
79
80
|
{ prohibited: all, invalid: invalidPrefixableString },
|
|
80
|
-
`prefixableStrings ${messageInvalidAny} "${invalidPrefixableString}"
|
|
81
|
+
`prefixableStrings ${messageInvalidAny} "${invalidPrefixableString}"`,
|
|
81
82
|
)
|
|
82
83
|
}
|
|
83
84
|
|
|
@@ -87,7 +88,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
|
|
|
87
88
|
throw new BooleanParserLibraryError(
|
|
88
89
|
ERROR_CODES.PARSER_CONFLICTING_OPTIONS_ERROR,
|
|
89
90
|
{ prohibited: allKeywords, invalid: invalidExpandedPropertySeparator },
|
|
90
|
-
`expandedPropertySeparator ${messageInvalidBool} "${invalidExpandedPropertySeparator}"
|
|
91
|
+
`expandedPropertySeparator ${messageInvalidBool} "${invalidExpandedPropertySeparator}"`,
|
|
91
92
|
)
|
|
92
93
|
}
|
|
93
94
|
|
|
@@ -98,7 +99,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
|
|
|
98
99
|
throw new BooleanParserLibraryError(
|
|
99
100
|
ERROR_CODES.PARSER_CONFLICTING_OPTIONS_ERROR,
|
|
100
101
|
{ prohibited: keywords, invalid: invalidCustomPropertyOperator },
|
|
101
|
-
`customPropertyOperator ${messageInvalidBool} "${invalidCustomPropertyOperator}"
|
|
102
|
+
`customPropertyOperator ${messageInvalidBool} "${invalidCustomPropertyOperator}"`,
|
|
102
103
|
)
|
|
103
104
|
}
|
|
104
105
|
/* #regionend */
|
|
@@ -115,7 +116,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
|
|
|
115
116
|
throw new BooleanParserLibraryError(
|
|
116
117
|
ERROR_CODES.PARSER_OPTION_REQUIRED_ERROR,
|
|
117
118
|
{ options: requireCustomNormalizer, requires: "conditionNormalizer" },
|
|
118
|
-
`A custom conditionNormalizer function must be specified when using the following options: ${requireCustomNormalizer.join(", ")}
|
|
119
|
+
`A custom conditionNormalizer function must be specified when using the following options: ${requireCustomNormalizer.join(", ")}`,
|
|
119
120
|
)
|
|
120
121
|
}
|
|
121
122
|
const requireCustomComparer: (keyof ParserOptions)[] = [] as any
|
|
@@ -126,7 +127,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
|
|
|
126
127
|
throw new BooleanParserLibraryError(
|
|
127
128
|
ERROR_CODES.PARSER_OPTION_REQUIRED_ERROR,
|
|
128
129
|
{ options: requireCustomComparer, requires: "valueComparer" },
|
|
129
|
-
`A custom valueComparer function must be specified when using the following options: ${requireCustomComparer.join(", ")}
|
|
130
|
+
`A custom valueComparer function must be specified when using the following options: ${requireCustomComparer.join(", ")}`,
|
|
130
131
|
)
|
|
131
132
|
}
|
|
132
133
|
}
|
|
@@ -135,7 +136,7 @@ export function checkParserOpts<T extends {}>(opts: FullParserOptions<T>, evalua
|
|
|
135
136
|
throw new BooleanParserLibraryError(
|
|
136
137
|
ERROR_CODES.PARSER_OPTION_REQUIRED_ERROR,
|
|
137
138
|
{ requires: "valueValidator" },
|
|
138
|
-
`A custom valueValidator function must be specified when using the validate method
|
|
139
|
+
`A custom valueValidator function must be specified when using the validate method.`,
|
|
139
140
|
)
|
|
140
141
|
}
|
|
141
142
|
}
|
|
@@ -1,15 +1,11 @@
|
|
|
1
|
+
import type { Token } from "../../Lexer.js"
|
|
1
2
|
import type { Position } from "../../types/ast.js"
|
|
2
3
|
|
|
3
4
|
|
|
4
|
-
type ChevrotainLocation = {
|
|
5
|
-
startOffset?: number
|
|
6
|
-
endOffset?: number
|
|
7
|
-
}
|
|
8
|
-
|
|
9
5
|
/** @internal */
|
|
10
|
-
export function extractPosition(
|
|
6
|
+
export function extractPosition(pos: Pick<Token, "startOffset" | "endOffset">, shift: number): Position {
|
|
11
7
|
return {
|
|
12
|
-
start:
|
|
13
|
-
end:
|
|
8
|
+
start: pos.startOffset! - (shift ?? 0),
|
|
9
|
+
end: pos.endOffset! + 1 - (shift ?? 0),
|
|
14
10
|
}
|
|
15
11
|
}
|
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import { type
|
|
2
|
-
|
|
3
|
-
import type { createTokens } from "../../grammar/createTokens.js"
|
|
1
|
+
import { $T, type Token } from "../../Lexer.js"
|
|
4
2
|
|
|
5
3
|
|
|
6
4
|
/** @internal */
|
|
7
|
-
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
export function getUnclosedRightParenCount(tokens: Token[]): number {
|
|
8
8
|
let open = 0
|
|
9
9
|
let unclosed = 0
|
|
10
10
|
for (const token of tokens) {
|
|
11
|
-
if (
|
|
11
|
+
if (token.type === $T.PAREN_R) {
|
|
12
12
|
if (open > 0) {
|
|
13
13
|
open--
|
|
14
14
|
} else {
|
|
15
15
|
unclosed++
|
|
16
16
|
}
|
|
17
|
-
} else if (
|
|
17
|
+
} else if (token.type === $T.PAREN_L) {
|
|
18
18
|
open++
|
|
19
19
|
}
|
|
20
20
|
}
|
|
@@ -6,7 +6,7 @@ import { defaultValueComparer } from "../general/defaultValueComparer.js"
|
|
|
6
6
|
|
|
7
7
|
/** @internal */
|
|
8
8
|
export function parseParserOptions<T extends {} = {}>(
|
|
9
|
-
options: ParserOptions<T
|
|
9
|
+
options: ParserOptions<T>,
|
|
10
10
|
): FullParserOptions<T> {
|
|
11
11
|
const opts: ParserOptions = {
|
|
12
12
|
prefixApplier: defaultPrefixApplier,
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/* Manually Generated Index */
|
|
2
|
+
|
|
2
3
|
export * as ast from "./ast/classes/index.js"
|
|
3
|
-
export
|
|
4
|
-
export { Parser } from "./parser.js"
|
|
4
|
+
export { Parser } from "./Parser.js"
|
|
5
5
|
export * from "./types/index.js"
|
|
6
6
|
export * as utils from "./utils/index.js"
|
|
7
7
|
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { unreachable } from "@alanscodelog/utils"
|
|
1
|
+
import { unreachable } from "@alanscodelog/utils/unreachable"
|
|
2
2
|
|
|
3
3
|
import { ConditionNode } from "../ast/classes/ConditionNode.js"
|
|
4
4
|
import { VariableNode } from "../ast/classes/VariableNode.js"
|
|
5
|
-
import type { Parser } from "../
|
|
5
|
+
import type { Parser } from "../Parser.js"
|
|
6
6
|
import { type Completion, type Suggestion, SUGGESTION_TYPE } from "../types/autocomplete.js"
|
|
7
7
|
import type { FullParserOptions } from "../types/parser.js"
|
|
8
8
|
|
|
@@ -30,9 +30,9 @@ export class AutocompleteMixin<T extends {}> {
|
|
|
30
30
|
"regexFlags" |
|
|
31
31
|
"expandedPropertyOperators" |
|
|
32
32
|
"customPropertyOperators", string[]>> & {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
33
|
+
quote?: string
|
|
34
|
+
keywords?: FullParserOptions<T>["keywords"]
|
|
35
|
+
} = {},
|
|
36
36
|
): Completion[] {
|
|
37
37
|
const self = (this as any as Parser<T>)
|
|
38
38
|
return suggestions.map(suggestion => {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { insert } from "@alanscodelog/utils"
|
|
1
|
+
import { insert } from "@alanscodelog/utils/insert"
|
|
2
2
|
|
|
3
3
|
import type { Completion } from "../types/autocomplete.js"
|
|
4
4
|
|
|
@@ -11,7 +11,7 @@ export class AutoreplaceMixin {
|
|
|
11
11
|
*/
|
|
12
12
|
autoreplace(
|
|
13
13
|
input: string,
|
|
14
|
-
{ value, suggestion }: Completion
|
|
14
|
+
{ value, suggestion }: Completion,
|
|
15
15
|
): { replacement: string, cursor: number } {
|
|
16
16
|
const isQuotedLeft = ["\"", "'", "`"].includes(value[0])
|
|
17
17
|
const isQuotedRight = ["\"", "'", "`"].includes(value[value.length - 1])
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { DeepPartial } from "@alanscodelog/utils"
|
|
2
|
-
import { unreachable } from "@alanscodelog/utils"
|
|
1
|
+
import type { DeepPartial } from "@alanscodelog/utils/types"
|
|
2
|
+
import { unreachable } from "@alanscodelog/utils/unreachable"
|
|
3
3
|
|
|
4
4
|
import { pos } from "../ast/builders/pos.js"
|
|
5
5
|
import { ArrayNode } from "../ast/classes/ArrayNode.js"
|
|
@@ -8,7 +8,7 @@ import { ErrorToken } from "../ast/classes/ErrorToken.js"
|
|
|
8
8
|
import { GroupNode } from "../ast/classes/GroupNode.js"
|
|
9
9
|
import type { ValidToken } from "../ast/classes/ValidToken.js"
|
|
10
10
|
import { VariableNode } from "../ast/classes/VariableNode.js"
|
|
11
|
-
import type { Parser } from "../
|
|
11
|
+
import type { Parser } from "../Parser.js"
|
|
12
12
|
import { type ParserResults, TOKEN_TYPE } from "../types/ast.js"
|
|
13
13
|
import { type Suggestion, SUGGESTION_TYPE } from "../types/autocomplete.js"
|
|
14
14
|
import type { KeywordEntry } from "../types/parser.js"
|
package/src/methods/evaluate.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { get } from "@alanscodelog/utils/get"
|
|
2
|
+
import { type AddParameters } from "@alanscodelog/utils/types"
|
|
3
|
+
import { unreachable } from "@alanscodelog/utils/unreachable"
|
|
2
4
|
|
|
3
5
|
import { Condition } from "../ast/classes/Condition.js"
|
|
4
6
|
import { Expression } from "../ast/classes/Expression.js"
|
|
5
|
-
import type { Parser } from "../
|
|
7
|
+
import type { Parser } from "../Parser.js"
|
|
6
8
|
import { TOKEN_TYPE } from "../types/ast.js"
|
|
7
9
|
|
|
8
10
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/* eslint-disable no-labels */
|
|
2
2
|
|
|
3
|
-
import { type AddParameters
|
|
3
|
+
import { type AddParameters } from "@alanscodelog/utils/types"
|
|
4
|
+
import { unreachable } from "@alanscodelog/utils/unreachable"
|
|
4
5
|
|
|
5
6
|
import { Condition } from "../ast/classes/Condition.js"
|
|
6
7
|
import { Expression } from "../ast/classes/Expression.js"
|
package/src/methods/normalize.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { type AddParameters
|
|
1
|
+
import { type AddParameters } from "@alanscodelog/utils/types"
|
|
2
|
+
import { unreachable } from "@alanscodelog/utils/unreachable"
|
|
2
3
|
|
|
3
4
|
import { ArrayNode } from "../ast/classes/ArrayNode.js"
|
|
4
5
|
import { Condition } from "../ast/classes/Condition.js"
|
|
@@ -11,7 +12,7 @@ import { ValidToken } from "../ast/classes/ValidToken.js"
|
|
|
11
12
|
import { VariableNode } from "../ast/classes/VariableNode.js"
|
|
12
13
|
import { applyBoolean } from "../helpers/general/applyBoolean.js"
|
|
13
14
|
import { applyPrefix } from "../helpers/general/applyPrefix.js"
|
|
14
|
-
import type { Parser } from "../
|
|
15
|
+
import type { Parser } from "../Parser.js"
|
|
15
16
|
import { type ParserResults, TOKEN_TYPE, type TokenBooleanTypes } from "../types/ast.js"
|
|
16
17
|
import type { ValueQuery } from "../types/parser.js"
|
|
17
18
|
|
|
@@ -24,8 +25,6 @@ const OPPOSITE = {
|
|
|
24
25
|
export class NormalizeMixin<T extends {}> {
|
|
25
26
|
/**
|
|
26
27
|
* Normalizes the ast by applying {@link GroupNode GroupNodes} and converting {@link ConditionNode ConditionNodes} to {@link NormalizedConditionNode NormalizedConditionNodes}.
|
|
27
|
-
*
|
|
28
|
-
* To do this, a
|
|
29
28
|
*/
|
|
30
29
|
normalize<TType extends string, TValue>(ast: ParserResults): Condition<TType, TValue> | Expression<TType, TValue> {
|
|
31
30
|
// @ts-expect-error private method
|
package/src/methods/validate.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { get } from "@alanscodelog/utils/get"
|
|
2
|
+
import { isArray } from "@alanscodelog/utils/isArray"
|
|
3
|
+
import { type AddParameters } from "@alanscodelog/utils/types"
|
|
2
4
|
|
|
3
5
|
import { ArrayNode } from "../ast/classes/ArrayNode.js"
|
|
4
6
|
import { ConditionNode } from "../ast/classes/ConditionNode.js"
|
|
@@ -9,7 +11,7 @@ import { ValidToken } from "../ast/classes/ValidToken.js"
|
|
|
9
11
|
import { VariableNode } from "../ast/classes/VariableNode.js"
|
|
10
12
|
import { applyBoolean } from "../helpers/general/applyBoolean.js"
|
|
11
13
|
import { applyPrefix } from "../helpers/general/applyPrefix.js"
|
|
12
|
-
import type { Parser } from "../
|
|
14
|
+
import type { Parser } from "../Parser.js"
|
|
13
15
|
import { type ParserResults, type Position, TOKEN_TYPE } from "../types/ast.js"
|
|
14
16
|
import type { ValidationQuery } from "../types/parser.js"
|
|
15
17
|
|
package/src/types/ast.ts
CHANGED
|
@@ -1,13 +1,6 @@
|
|
|
1
|
-
import type { AnyFunction } from "@alanscodelog/utils"
|
|
2
|
-
|
|
3
|
-
import type { ArrayNode } from "../ast/classes/ArrayNode.js"
|
|
4
|
-
import type { ConditionNode } from "../ast/classes/ConditionNode.js"
|
|
5
|
-
import type { ErrorToken } from "../ast/classes/ErrorToken.js"
|
|
6
|
-
import type { ExpressionNode } from "../ast/classes/ExpressionNode.js"
|
|
7
|
-
import type { GroupNode } from "../ast/classes/GroupNode.js"
|
|
8
|
-
import type { ValidToken } from "../ast/classes/ValidToken.js"
|
|
9
|
-
import type { VariableNode } from "../ast/classes/VariableNode.js"
|
|
1
|
+
import type { AnyFunction } from "@alanscodelog/utils/types"
|
|
10
2
|
|
|
3
|
+
import type { ArrayNode, ConditionNode, ErrorToken, ExpressionNode, GroupNode, ValidToken, VariableNode } from "../ast/classes/index.js"
|
|
11
4
|
|
|
12
5
|
export type AddParameters<T extends AnyFunction, TExtra extends any[] = [boolean]> = (...args: [...Parameters<T>, ...TExtra]) => ReturnType<T>
|
|
13
6
|
|