@witchcraft/expressit 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +86 -0
- package/dist/ast/builders/array.d.ts +8 -0
- package/dist/ast/builders/array.d.ts.map +1 -0
- package/dist/ast/builders/array.js +31 -0
- package/dist/ast/builders/condition.d.ts +20 -0
- package/dist/ast/builders/condition.d.ts.map +1 -0
- package/dist/ast/builders/condition.js +28 -0
- package/dist/ast/builders/delim.d.ts +11 -0
- package/dist/ast/builders/delim.d.ts.map +1 -0
- package/dist/ast/builders/delim.js +20 -0
- package/dist/ast/builders/error.d.ts +9 -0
- package/dist/ast/builders/error.d.ts.map +1 -0
- package/dist/ast/builders/error.js +16 -0
- package/dist/ast/builders/expression.d.ts +12 -0
- package/dist/ast/builders/expression.d.ts.map +1 -0
- package/dist/ast/builders/expression.js +31 -0
- package/dist/ast/builders/group.d.ts +20 -0
- package/dist/ast/builders/group.d.ts.map +1 -0
- package/dist/ast/builders/group.js +47 -0
- package/dist/ast/builders/index.d.ts +12 -0
- package/dist/ast/builders/index.d.ts.map +1 -0
- package/dist/ast/builders/index.js +24 -0
- package/dist/ast/builders/isFullPos.d.ts +6 -0
- package/dist/ast/builders/isFullPos.d.ts.map +1 -0
- package/dist/ast/builders/isFullPos.js +6 -0
- package/dist/ast/builders/pos.d.ts +21 -0
- package/dist/ast/builders/pos.d.ts.map +1 -0
- package/dist/ast/builders/pos.js +28 -0
- package/dist/ast/builders/token.d.ts +12 -0
- package/dist/ast/builders/token.d.ts.map +1 -0
- package/dist/ast/builders/token.js +26 -0
- package/dist/ast/builders/type.d.ts +6 -0
- package/dist/ast/builders/type.d.ts.map +1 -0
- package/dist/ast/builders/type.js +37 -0
- package/dist/ast/builders/variable.d.ts +17 -0
- package/dist/ast/builders/variable.d.ts.map +1 -0
- package/dist/ast/builders/variable.js +62 -0
- package/dist/ast/classes/ArrayNode.d.ts +18 -0
- package/dist/ast/classes/ArrayNode.d.ts.map +1 -0
- package/dist/ast/classes/ArrayNode.js +55 -0
- package/dist/ast/classes/Condition.d.ts +13 -0
- package/dist/ast/classes/Condition.d.ts.map +1 -0
- package/dist/ast/classes/Condition.js +21 -0
- package/dist/ast/classes/ConditionNode.d.ts +73 -0
- package/dist/ast/classes/ConditionNode.d.ts.map +1 -0
- package/dist/ast/classes/ConditionNode.js +101 -0
- package/dist/ast/classes/ErrorToken.d.ts +27 -0
- package/dist/ast/classes/ErrorToken.d.ts.map +1 -0
- package/dist/ast/classes/ErrorToken.js +47 -0
- package/dist/ast/classes/Expression.d.ts +13 -0
- package/dist/ast/classes/Expression.d.ts.map +1 -0
- package/dist/ast/classes/Expression.js +19 -0
- package/dist/ast/classes/ExpressionNode.d.ts +21 -0
- package/dist/ast/classes/ExpressionNode.d.ts.map +1 -0
- package/dist/ast/classes/ExpressionNode.js +57 -0
- package/dist/ast/classes/GroupNode.d.ts +64 -0
- package/dist/ast/classes/GroupNode.d.ts.map +1 -0
- package/dist/ast/classes/GroupNode.js +69 -0
- package/dist/ast/classes/Node.d.ts +22 -0
- package/dist/ast/classes/Node.d.ts.map +1 -0
- package/dist/ast/classes/Node.js +28 -0
- package/dist/ast/classes/Token.d.ts +27 -0
- package/dist/ast/classes/Token.d.ts.map +1 -0
- package/dist/ast/classes/Token.js +28 -0
- package/dist/ast/classes/ValidToken.d.ts +26 -0
- package/dist/ast/classes/ValidToken.d.ts.map +1 -0
- package/dist/ast/classes/ValidToken.js +49 -0
- package/dist/ast/classes/VariableNode.d.ts +33 -0
- package/dist/ast/classes/VariableNode.d.ts.map +1 -0
- package/dist/ast/classes/VariableNode.js +58 -0
- package/dist/ast/classes/index.d.ts +12 -0
- package/dist/ast/classes/index.d.ts.map +1 -0
- package/dist/ast/classes/index.js +24 -0
- package/dist/ast/handlers.d.ts +42 -0
- package/dist/ast/handlers.d.ts.map +1 -0
- package/dist/ast/handlers.js +150 -0
- package/dist/ast/index.d.ts +4 -0
- package/dist/ast/index.d.ts.map +1 -0
- package/dist/ast/index.js +8 -0
- package/dist/examples/advancedValueComparer.d.ts +3 -0
- package/dist/examples/advancedValueComparer.d.ts.map +1 -0
- package/dist/examples/advancedValueComparer.js +28 -0
- package/dist/examples/shortcutContextParser.d.ts +22 -0
- package/dist/examples/shortcutContextParser.d.ts.map +1 -0
- package/dist/examples/shortcutContextParser.js +126 -0
- package/dist/global.d.js +1 -0
- package/dist/grammar/ParserBase.d.ts +51 -0
- package/dist/grammar/ParserBase.d.ts.map +1 -0
- package/dist/grammar/ParserBase.js +516 -0
- package/dist/grammar/createTokens.d.ts +56 -0
- package/dist/grammar/createTokens.d.ts.map +1 -0
- package/dist/grammar/createTokens.js +843 -0
- package/dist/grammar/index.d.ts +3 -0
- package/dist/grammar/index.d.ts.map +1 -0
- package/dist/grammar/index.js +6 -0
- package/dist/helpers/errors.d.ts +9 -0
- package/dist/helpers/errors.d.ts.map +1 -0
- package/dist/helpers/errors.js +41 -0
- package/dist/helpers/general/applyBoolean.d.ts +3 -0
- package/dist/helpers/general/applyBoolean.d.ts.map +1 -0
- package/dist/helpers/general/applyBoolean.js +17 -0
- package/dist/helpers/general/applyPrefix.d.ts +4 -0
- package/dist/helpers/general/applyPrefix.d.ts.map +1 -0
- package/dist/helpers/general/applyPrefix.js +9 -0
- package/dist/helpers/general/defaultConditionNormalizer.d.ts +3 -0
- package/dist/helpers/general/defaultConditionNormalizer.d.ts.map +1 -0
- package/dist/helpers/general/defaultConditionNormalizer.js +6 -0
- package/dist/helpers/general/defaultKeyParser.d.ts +3 -0
- package/dist/helpers/general/defaultKeyParser.d.ts.map +1 -0
- package/dist/helpers/general/defaultKeyParser.js +8 -0
- package/dist/helpers/general/defaultPrefixApplier.d.ts +3 -0
- package/dist/helpers/general/defaultPrefixApplier.d.ts.map +1 -0
- package/dist/helpers/general/defaultPrefixApplier.js +6 -0
- package/dist/helpers/general/defaultValueComparer.d.ts +3 -0
- package/dist/helpers/general/defaultValueComparer.d.ts.map +1 -0
- package/dist/helpers/general/defaultValueComparer.js +6 -0
- package/dist/helpers/general/index.d.ts +7 -0
- package/dist/helpers/general/index.d.ts.map +1 -0
- package/dist/helpers/general/index.js +14 -0
- package/dist/helpers/index.d.ts +4 -0
- package/dist/helpers/index.d.ts.map +1 -0
- package/dist/helpers/index.js +8 -0
- package/dist/helpers/parser/assignParents.d.ts +4 -0
- package/dist/helpers/parser/assignParents.d.ts.map +1 -0
- package/dist/helpers/parser/assignParents.js +71 -0
- package/dist/helpers/parser/checkParserOpts.d.ts +3 -0
- package/dist/helpers/parser/checkParserOpts.d.ts.map +1 -0
- package/dist/helpers/parser/checkParserOpts.js +126 -0
- package/dist/helpers/parser/extractPosition.d.ts +9 -0
- package/dist/helpers/parser/extractPosition.d.ts.map +1 -0
- package/dist/helpers/parser/extractPosition.js +9 -0
- package/dist/helpers/parser/getUnclosedRightParenCount.d.ts +5 -0
- package/dist/helpers/parser/getUnclosedRightParenCount.d.ts.map +1 -0
- package/dist/helpers/parser/getUnclosedRightParenCount.js +20 -0
- package/dist/helpers/parser/index.d.ts +9 -0
- package/dist/helpers/parser/index.d.ts.map +1 -0
- package/dist/helpers/parser/index.js +18 -0
- package/dist/helpers/parser/parseParserOptions.d.ts +4 -0
- package/dist/helpers/parser/parseParserOptions.d.ts.map +1 -0
- package/dist/helpers/parser/parseParserOptions.js +45 -0
- package/dist/helpers/parser/seal.d.ts +8 -0
- package/dist/helpers/parser/seal.d.ts.map +1 -0
- package/dist/helpers/parser/seal.js +10 -0
- package/dist/helpers/parser/setParent.d.ts +6 -0
- package/dist/helpers/parser/setParent.d.ts.map +1 -0
- package/dist/helpers/parser/setParent.js +4 -0
- package/dist/helpers/parser/unescape.d.ts +3 -0
- package/dist/helpers/parser/unescape.d.ts.map +1 -0
- package/dist/helpers/parser/unescape.js +6 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +17 -0
- package/dist/methods/autocomplete.d.ts +18 -0
- package/dist/methods/autocomplete.d.ts.map +1 -0
- package/dist/methods/autocomplete.js +109 -0
- package/dist/methods/autoreplace.d.ts +13 -0
- package/dist/methods/autoreplace.d.ts.map +1 -0
- package/dist/methods/autoreplace.js +36 -0
- package/dist/methods/autosuggest.d.ts +28 -0
- package/dist/methods/autosuggest.d.ts.map +1 -0
- package/dist/methods/autosuggest.js +371 -0
- package/dist/methods/evaluate.d.ts +11 -0
- package/dist/methods/evaluate.d.ts.map +1 -0
- package/dist/methods/evaluate.js +30 -0
- package/dist/methods/getBestIndex.d.ts +19 -0
- package/dist/methods/getBestIndex.d.ts.map +1 -0
- package/dist/methods/getBestIndex.js +53 -0
- package/dist/methods/getIndexes.d.ts +17 -0
- package/dist/methods/getIndexes.d.ts.map +1 -0
- package/dist/methods/getIndexes.js +97 -0
- package/dist/methods/index.d.ts +9 -0
- package/dist/methods/index.d.ts.map +1 -0
- package/dist/methods/index.js +18 -0
- package/dist/methods/normalize.d.ts +12 -0
- package/dist/methods/normalize.d.ts.map +1 -0
- package/dist/methods/normalize.js +99 -0
- package/dist/methods/validate.d.ts +11 -0
- package/dist/methods/validate.d.ts.map +1 -0
- package/dist/methods/validate.js +111 -0
- package/dist/package.js +7 -0
- package/dist/package.json.js +193 -0
- package/dist/parser.d.ts +58 -0
- package/dist/parser.d.ts.map +1 -0
- package/dist/parser.js +136 -0
- package/dist/types/ast.d.ts +70 -0
- package/dist/types/ast.d.ts.map +1 -0
- package/dist/types/ast.js +29 -0
- package/dist/types/autocomplete.d.ts +143 -0
- package/dist/types/autocomplete.d.ts.map +1 -0
- package/dist/types/autocomplete.js +24 -0
- package/dist/types/errors.d.ts +34 -0
- package/dist/types/errors.d.ts.map +1 -0
- package/dist/types/errors.js +10 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +9 -0
- package/dist/types/parser.d.ts +451 -0
- package/dist/types/parser.d.ts.map +1 -0
- package/dist/types/parser.js +1 -0
- package/dist/utils/extractTokens.d.ts +8 -0
- package/dist/utils/extractTokens.d.ts.map +1 -0
- package/dist/utils/extractTokens.js +50 -0
- package/dist/utils/getCursorInfo.d.ts +7 -0
- package/dist/utils/getCursorInfo.d.ts.map +1 -0
- package/dist/utils/getCursorInfo.js +86 -0
- package/dist/utils/getOppositeDelimiter.d.ts +6 -0
- package/dist/utils/getOppositeDelimiter.d.ts.map +1 -0
- package/dist/utils/getOppositeDelimiter.js +35 -0
- package/dist/utils/getSurroundingErrors.d.ts +25 -0
- package/dist/utils/getSurroundingErrors.d.ts.map +1 -0
- package/dist/utils/getSurroundingErrors.js +37 -0
- package/dist/utils/index.d.ts +10 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +20 -0
- package/dist/utils/isBracket.d.ts +3 -0
- package/dist/utils/isBracket.d.ts.map +1 -0
- package/dist/utils/isBracket.js +7 -0
- package/dist/utils/isDelimiter.d.ts +6 -0
- package/dist/utils/isDelimiter.d.ts.map +1 -0
- package/dist/utils/isDelimiter.js +17 -0
- package/dist/utils/isParen.d.ts +3 -0
- package/dist/utils/isParen.d.ts.map +1 -0
- package/dist/utils/isParen.js +7 -0
- package/dist/utils/isQuote.d.ts +4 -0
- package/dist/utils/isQuote.d.ts.map +1 -0
- package/dist/utils/isQuote.js +7 -0
- package/dist/utils/prettyAst.d.ts +35 -0
- package/dist/utils/prettyAst.d.ts.map +1 -0
- package/dist/utils/prettyAst.js +112 -0
- package/package.json +152 -0
- package/src/ast/builders/array.ts +45 -0
- package/src/ast/builders/condition.ts +56 -0
- package/src/ast/builders/delim.ts +39 -0
- package/src/ast/builders/error.ts +22 -0
- package/src/ast/builders/expression.ts +66 -0
- package/src/ast/builders/group.ts +79 -0
- package/src/ast/builders/index.ts +13 -0
- package/src/ast/builders/isFullPos.ts +10 -0
- package/src/ast/builders/pos.ts +57 -0
- package/src/ast/builders/token.ts +46 -0
- package/src/ast/builders/type.ts +32 -0
- package/src/ast/builders/variable.ts +89 -0
- package/src/ast/classes/ArrayNode.ts +46 -0
- package/src/ast/classes/Condition.ts +22 -0
- package/src/ast/classes/ConditionNode.ts +141 -0
- package/src/ast/classes/ErrorToken.ts +49 -0
- package/src/ast/classes/Expression.ts +26 -0
- package/src/ast/classes/ExpressionNode.ts +62 -0
- package/src/ast/classes/GroupNode.ts +127 -0
- package/src/ast/classes/Node.ts +47 -0
- package/src/ast/classes/Token.ts +59 -0
- package/src/ast/classes/ValidToken.ts +56 -0
- package/src/ast/classes/VariableNode.ts +67 -0
- package/src/ast/classes/index.ts +13 -0
- package/src/ast/handlers.ts +190 -0
- package/src/ast/index.ts +5 -0
- package/src/examples/advancedValueComparer.ts +31 -0
- package/src/examples/shortcutContextParser.ts +140 -0
- package/src/global.d.ts +4 -0
- package/src/grammar/ParserBase.ts +715 -0
- package/src/grammar/createTokens.ts +512 -0
- package/src/grammar/index.ts +4 -0
- package/src/helpers/errors.ts +45 -0
- package/src/helpers/general/applyBoolean.ts +9 -0
- package/src/helpers/general/applyPrefix.ts +7 -0
- package/src/helpers/general/defaultConditionNormalizer.ts +9 -0
- package/src/helpers/general/defaultKeyParser.ts +8 -0
- package/src/helpers/general/defaultPrefixApplier.ts +7 -0
- package/src/helpers/general/defaultValueComparer.ts +7 -0
- package/src/helpers/general/index.ts +8 -0
- package/src/helpers/index.ts +5 -0
- package/src/helpers/parser/assignParents.ts +51 -0
- package/src/helpers/parser/checkParserOpts.ts +143 -0
- package/src/helpers/parser/extractPosition.ts +15 -0
- package/src/helpers/parser/getUnclosedRightParenCount.ts +22 -0
- package/src/helpers/parser/index.ts +10 -0
- package/src/helpers/parser/parseParserOptions.ts +54 -0
- package/src/helpers/parser/seal.ts +14 -0
- package/src/helpers/parser/setParent.ts +5 -0
- package/src/helpers/parser/unescape.ts +4 -0
- package/src/index.ts +7 -0
- package/src/methods/autocomplete.ts +128 -0
- package/src/methods/autoreplace.ts +46 -0
- package/src/methods/autosuggest.ts +543 -0
- package/src/methods/evaluate.ts +37 -0
- package/src/methods/getBestIndex.ts +53 -0
- package/src/methods/getIndexes.ts +99 -0
- package/src/methods/index.ts +10 -0
- package/src/methods/normalize.ts +138 -0
- package/src/methods/validate.ts +141 -0
- package/src/package.js +11 -0
- package/src/parser.ts +183 -0
- package/src/types/ast.ts +148 -0
- package/src/types/autocomplete.ts +152 -0
- package/src/types/errors.ts +40 -0
- package/src/types/index.ts +6 -0
- package/src/types/parser.ts +479 -0
- package/src/utils/extractTokens.ts +67 -0
- package/src/utils/getCursorInfo.ts +106 -0
- package/src/utils/getOppositeDelimiter.ts +36 -0
- package/src/utils/getSurroundingErrors.ts +57 -0
- package/src/utils/index.ts +11 -0
- package/src/utils/isBracket.ts +6 -0
- package/src/utils/isDelimiter.ts +18 -0
- package/src/utils/isParen.ts +6 -0
- package/src/utils/isQuote.ts +6 -0
- package/src/utils/prettyAst.ts +152 -0
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { isArray, unreachable } from "@alanscodelog/utils";
|
|
2
|
+
import { extractTokens } from "./extractTokens.js";
|
|
3
|
+
import { ValidToken } from "../ast/classes/ValidToken.js";
|
|
4
|
+
import { ErrorToken } from "../ast/classes/ErrorToken.js";
|
|
5
|
+
function getCursorInfo(input, ast, index) {
|
|
6
|
+
const tokens = isArray(ast) ? ast : extractTokens(ast);
|
|
7
|
+
if (input.length < index || index < 0) {
|
|
8
|
+
throw new Error(`Index ${index} out of bounds. Input length is ${input.length}.`);
|
|
9
|
+
}
|
|
10
|
+
const info = {
|
|
11
|
+
index,
|
|
12
|
+
at: void 0,
|
|
13
|
+
prev: void 0,
|
|
14
|
+
next: void 0,
|
|
15
|
+
valid: {
|
|
16
|
+
prev: void 0,
|
|
17
|
+
next: void 0
|
|
18
|
+
},
|
|
19
|
+
whitespace: {
|
|
20
|
+
prev: false,
|
|
21
|
+
next: false
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
for (const token of tokens) {
|
|
25
|
+
if (token.end <= index) {
|
|
26
|
+
if (token instanceof ValidToken) {
|
|
27
|
+
info.prev = token;
|
|
28
|
+
info.valid.prev = token;
|
|
29
|
+
} else if (token.end !== index) {
|
|
30
|
+
info.prev = token;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if (token.start < index && token.end > index) {
|
|
34
|
+
if (token instanceof ErrorToken) {
|
|
35
|
+
unreachable();
|
|
36
|
+
} else {
|
|
37
|
+
info.at = token;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (token.start >= index) {
|
|
41
|
+
info.next || (info.next = token);
|
|
42
|
+
if (token instanceof ValidToken && !info.valid.next) {
|
|
43
|
+
info.valid.next = token;
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
setWhitespaceBetween(input, index, "prev", info);
|
|
49
|
+
setWhitespaceBetween(input, index, "next", info);
|
|
50
|
+
return info;
|
|
51
|
+
}
|
|
52
|
+
function setWhitespaceBetween(input, index, side, info) {
|
|
53
|
+
const oppositePos = side === "next" ? "start" : "end";
|
|
54
|
+
const pos = side === "next" ? "end" : "start";
|
|
55
|
+
const limit = side === "next" ? input.length : 0;
|
|
56
|
+
if (info.at) {
|
|
57
|
+
if (info.valid[side]) {
|
|
58
|
+
const nextTokStart = info.valid[side][oppositePos];
|
|
59
|
+
if (info.at[pos] !== nextTokStart)
|
|
60
|
+
info.whitespace[side] = true;
|
|
61
|
+
else
|
|
62
|
+
return;
|
|
63
|
+
} else {
|
|
64
|
+
if (info.at[pos] !== limit)
|
|
65
|
+
info.whitespace[side] = true;
|
|
66
|
+
else
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
} else {
|
|
70
|
+
if (info.valid[side]) {
|
|
71
|
+
const nextTokStart = info.valid[side][oppositePos];
|
|
72
|
+
if (index !== nextTokStart)
|
|
73
|
+
info.whitespace[side] = true;
|
|
74
|
+
else
|
|
75
|
+
return;
|
|
76
|
+
} else {
|
|
77
|
+
if (index !== limit)
|
|
78
|
+
info.whitespace[side] = true;
|
|
79
|
+
else
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
export {
|
|
85
|
+
getCursorInfo
|
|
86
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type AnyToken, type TokenDelimiterTypes } from "../types/ast.js";
|
|
2
|
+
/**
|
|
3
|
+
* Given a delimiter token, returns it's opposite pair, or undefined if the type passed was not a delimiter token (so you can pass any type without checking).
|
|
4
|
+
*/
|
|
5
|
+
export declare function getOppositeDelimiter(token: AnyToken): AnyToken<TokenDelimiterTypes> | undefined;
|
|
6
|
+
//# sourceMappingURL=getOppositeDelimiter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getOppositeDelimiter.d.ts","sourceRoot":"","sources":["../../src/utils/getOppositeDelimiter.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,KAAK,QAAQ,EAAc,KAAK,mBAAmB,EAAE,MAAM,iBAAiB,CAAA;AAErF;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,QAAQ,GAAG,QAAQ,CAAC,mBAAmB,CAAC,GAAG,SAAS,CAsB/F"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { unreachable } from "@alanscodelog/utils";
|
|
2
|
+
import { isBracket } from "./isBracket.js";
|
|
3
|
+
import { isDelimiter } from "./isDelimiter.js";
|
|
4
|
+
import { isParen } from "./isParen.js";
|
|
5
|
+
import { isQuote } from "./isQuote.js";
|
|
6
|
+
import { TOKEN_TYPE } from "../types/ast.js";
|
|
7
|
+
function getOppositeDelimiter(token) {
|
|
8
|
+
if (!isDelimiter(token))
|
|
9
|
+
throw new Error("Token is not a delimiter type.");
|
|
10
|
+
if (isParen(token)) {
|
|
11
|
+
const paren = token.parent.paren;
|
|
12
|
+
const opposite = paren.left === token ? "right" : "left";
|
|
13
|
+
return paren[opposite];
|
|
14
|
+
} else if (isBracket(token)) {
|
|
15
|
+
const bracket = token.parent.bracket;
|
|
16
|
+
const opposite = bracket.left === token ? "right" : "left";
|
|
17
|
+
return bracket[opposite];
|
|
18
|
+
} else if (isQuote(token)) {
|
|
19
|
+
const quotes = token.parent.quote;
|
|
20
|
+
if (quotes === void 0)
|
|
21
|
+
unreachable();
|
|
22
|
+
const opposite = quotes.left === token ? "right" : "left";
|
|
23
|
+
return quotes[opposite];
|
|
24
|
+
} else if (token.type === TOKEN_TYPE.OP_EXPANDED_SEP) {
|
|
25
|
+
const sep = token.parent.sep;
|
|
26
|
+
if (sep === void 0)
|
|
27
|
+
unreachable();
|
|
28
|
+
const opposite = sep.left === token ? "right" : "left";
|
|
29
|
+
return sep[opposite];
|
|
30
|
+
}
|
|
31
|
+
unreachable();
|
|
32
|
+
}
|
|
33
|
+
export {
|
|
34
|
+
getOppositeDelimiter
|
|
35
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mostly for internal use by @see autosuggest.
|
|
3
|
+
*
|
|
4
|
+
* Returns all error tokens immediately before/after cursor (since there might be multiple error tokens one after the other).
|
|
5
|
+
*
|
|
6
|
+
* The errors are sorted by closeness to the given cursor (inside @see CursorInfo), with quote errors having priority, then paren errors, then any other errors. They can be sorted by closeness because although two errors might follow each other, their positions might be different because of whitespace, but they can still be fixed from any cursor position between their ends.
|
|
7
|
+
*
|
|
8
|
+
* For example:
|
|
9
|
+
* ```
|
|
10
|
+
* a a"
|
|
11
|
+
* ^ operator missing at 1
|
|
12
|
+
* ^ doublequote missing at 2
|
|
13
|
+
* if the cursor is at 1 (`a| a"`):
|
|
14
|
+
* errors = [operator, doublequote]
|
|
15
|
+
* if the cursor is at 2 (`a |a"`):
|
|
16
|
+
* errors = [doublequote, operator]
|
|
17
|
+
*
|
|
18
|
+
* either insertion would fix the issue for either position would fix the issue, albeit with different results
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
import { ErrorToken } from "../ast/classes/ErrorToken.js";
|
|
22
|
+
import { type AnyToken } from "../types/ast.js";
|
|
23
|
+
import type { CursorInfo } from "../types/autocomplete.js";
|
|
24
|
+
export declare function getSurroundingErrors(tokens: AnyToken[], token: CursorInfo): ErrorToken[];
|
|
25
|
+
//# sourceMappingURL=getSurroundingErrors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getSurroundingErrors.d.ts","sourceRoot":"","sources":["../../src/utils/getSurroundingErrors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,8BAA8B,CAAA;AACzD,OAAO,EAAE,KAAK,QAAQ,EAAc,MAAM,iBAAiB,CAAA;AAC3D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAA;AAG1D,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,QAAQ,EAAE,EAAE,KAAK,EAAE,UAAU,GAAG,UAAU,EAAE,CA8BxF"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { ErrorToken } from "../ast/classes/ErrorToken.js";
|
|
2
|
+
import { TOKEN_TYPE } from "../types/ast.js";
|
|
3
|
+
function getSurroundingErrors(tokens, token) {
|
|
4
|
+
if (token.at) {
|
|
5
|
+
return [];
|
|
6
|
+
}
|
|
7
|
+
const i = tokens.indexOf(token.next ?? token.prev);
|
|
8
|
+
let iNext = tokens[i] === token.next ? i : i + 1;
|
|
9
|
+
let iPrev = tokens[i] === token.next ? i - 1 : i;
|
|
10
|
+
const errors = [];
|
|
11
|
+
while (tokens[iNext] instanceof ErrorToken) {
|
|
12
|
+
errors.push(tokens[iNext]);
|
|
13
|
+
iNext++;
|
|
14
|
+
}
|
|
15
|
+
while (tokens[iPrev] instanceof ErrorToken) {
|
|
16
|
+
errors.push(tokens[iPrev]);
|
|
17
|
+
iPrev--;
|
|
18
|
+
}
|
|
19
|
+
return errors.sort((a, b) => {
|
|
20
|
+
const aIsQuote = [TOKEN_TYPE.DOUBLEQUOTE, TOKEN_TYPE.BACKTICK, TOKEN_TYPE.SINGLEQUOTE].includes(a.expected[0]);
|
|
21
|
+
const bIsQuote = [TOKEN_TYPE.DOUBLEQUOTE, TOKEN_TYPE.BACKTICK, TOKEN_TYPE.SINGLEQUOTE].includes(b.expected[0]);
|
|
22
|
+
const aIsParen = [TOKEN_TYPE.PARENR, TOKEN_TYPE.PARENL].includes(a.expected[0]);
|
|
23
|
+
const bIsParen = [TOKEN_TYPE.PARENR, TOKEN_TYPE.PARENL].includes(b.expected[0]);
|
|
24
|
+
const aCloseness = Math.abs(i - tokens.indexOf(a));
|
|
25
|
+
const bCloseness = Math.abs(i - tokens.indexOf(b));
|
|
26
|
+
const closenessComparison = aCloseness - bCloseness;
|
|
27
|
+
if (aIsQuote === bIsQuote) {
|
|
28
|
+
if (aIsParen !== bIsParen) {
|
|
29
|
+
return aIsParen ? -1 : 1;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return closenessComparison;
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
export {
|
|
36
|
+
getSurroundingErrors
|
|
37
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { extractTokens } from "./extractTokens.js";
|
|
2
|
+
export { getCursorInfo } from "./getCursorInfo.js";
|
|
3
|
+
export { getOppositeDelimiter } from "./getOppositeDelimiter.js";
|
|
4
|
+
export { getSurroundingErrors } from "./getSurroundingErrors.js";
|
|
5
|
+
export { isBracket } from "./isBracket.js";
|
|
6
|
+
export { isDelimiter } from "./isDelimiter.js";
|
|
7
|
+
export { isParen } from "./isParen.js";
|
|
8
|
+
export { isQuote } from "./isQuote.js";
|
|
9
|
+
export { prettyAst } from "./prettyAst.js";
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAClD,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAClD,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAA;AAChE,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAA;AAChE,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1C,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAC9C,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AACtC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AACtC,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { extractTokens } from "./extractTokens.js";
|
|
2
|
+
import { getCursorInfo } from "./getCursorInfo.js";
|
|
3
|
+
import { getOppositeDelimiter } from "./getOppositeDelimiter.js";
|
|
4
|
+
import { getSurroundingErrors } from "./getSurroundingErrors.js";
|
|
5
|
+
import { isBracket } from "./isBracket.js";
|
|
6
|
+
import { isDelimiter } from "./isDelimiter.js";
|
|
7
|
+
import { isParen } from "./isParen.js";
|
|
8
|
+
import { isQuote } from "./isQuote.js";
|
|
9
|
+
import { prettyAst } from "./prettyAst.js";
|
|
10
|
+
export {
|
|
11
|
+
extractTokens,
|
|
12
|
+
getCursorInfo,
|
|
13
|
+
getOppositeDelimiter,
|
|
14
|
+
getSurroundingErrors,
|
|
15
|
+
isBracket,
|
|
16
|
+
isDelimiter,
|
|
17
|
+
isParen,
|
|
18
|
+
isQuote,
|
|
19
|
+
prettyAst
|
|
20
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isBracket.d.ts","sourceRoot":"","sources":["../../src/utils/isBracket.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,QAAQ,EAAc,KAAK,iBAAiB,EAAE,MAAM,iBAAiB,CAAA;AAGnF,wBAAgB,SAAS,CAAC,KAAK,CAAC,EAAE,QAAQ,GAAG,KAAK,IAAI,QAAQ,CAAC,iBAAiB,CAAC,CAEhF"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type AnyToken, type TokenDelimiterTypes } from "../types/ast.js";
|
|
2
|
+
/**
|
|
3
|
+
* Returns whether token is a delimiter type (including if it's an expanded operator separator).
|
|
4
|
+
*/
|
|
5
|
+
export declare function isDelimiter(token?: AnyToken): token is AnyToken<TokenDelimiterTypes>;
|
|
6
|
+
//# sourceMappingURL=isDelimiter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isDelimiter.d.ts","sourceRoot":"","sources":["../../src/utils/isDelimiter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,QAAQ,EAAc,KAAK,mBAAmB,EAAE,MAAM,iBAAiB,CAAA;AAErF;;GAEG;AACH,wBAAgB,WAAW,CAAC,KAAK,CAAC,EAAE,QAAQ,GAAG,KAAK,IAAI,QAAQ,CAAC,mBAAmB,CAAC,CAYpF"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { TOKEN_TYPE } from "../types/ast.js";
|
|
2
|
+
function isDelimiter(token) {
|
|
3
|
+
return [
|
|
4
|
+
TOKEN_TYPE.BACKTICK,
|
|
5
|
+
TOKEN_TYPE.DOUBLEQUOTE,
|
|
6
|
+
TOKEN_TYPE.SINGLEQUOTE,
|
|
7
|
+
TOKEN_TYPE.PARENL,
|
|
8
|
+
TOKEN_TYPE.PARENR,
|
|
9
|
+
TOKEN_TYPE.BRACKETL,
|
|
10
|
+
TOKEN_TYPE.BRACKETR,
|
|
11
|
+
TOKEN_TYPE.OP_EXPANDED_SEP,
|
|
12
|
+
TOKEN_TYPE.REGEX
|
|
13
|
+
].includes(token == null ? void 0 : token.type);
|
|
14
|
+
}
|
|
15
|
+
export {
|
|
16
|
+
isDelimiter
|
|
17
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isParen.d.ts","sourceRoot":"","sources":["../../src/utils/isParen.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,QAAQ,EAAc,KAAK,eAAe,EAAE,MAAM,iBAAiB,CAAA;AAGjF,wBAAgB,OAAO,CAAC,KAAK,CAAC,EAAE,QAAQ,GAAG,KAAK,IAAI,QAAQ,CAAC,eAAe,CAAC,CAE5E"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { type AnyToken, type TokenQuoteTypes } from "../types/ast.js";
|
|
2
|
+
/** Returns if the token is a quote token. This includes regex delimiters. */
|
|
3
|
+
export declare function isQuote(token?: AnyToken): token is AnyToken<TokenQuoteTypes>;
|
|
4
|
+
//# sourceMappingURL=isQuote.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isQuote.d.ts","sourceRoot":"","sources":["../../src/utils/isQuote.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,QAAQ,EAAc,KAAK,eAAe,EAAE,MAAM,iBAAiB,CAAA;AAEjF,6EAA6E;AAC7E,wBAAgB,OAAO,CAAC,KAAK,CAAC,EAAE,QAAQ,GAAG,KAAK,IAAI,QAAQ,CAAC,eAAe,CAAC,CAE5E"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { ArrayNode, GroupNode, VariableNode } from "../ast/classes/index.js";
|
|
2
|
+
import { type AnyToken, type ParserResults } from "../types/ast.js";
|
|
3
|
+
type Colors = {
|
|
4
|
+
/** Color used to highlight the actual text content of the token nodes. */
|
|
5
|
+
values: string;
|
|
6
|
+
/** Color used to highlight the extra information some nodes contain in their headers for a quick overview (e.g. which operator for expression nodes, if a condition/group value is true, how long an array value is etc). */
|
|
7
|
+
info: string;
|
|
8
|
+
position: string;
|
|
9
|
+
/** Color used to highlight the hints in parens that indicate how the node is being used (e.g. a variable node might be a property, or alone as a variable, etc) */
|
|
10
|
+
hint: string;
|
|
11
|
+
error: string;
|
|
12
|
+
/** Color used to reset highlights. */
|
|
13
|
+
reset: string;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Returns a more compressed, color coded, string representation of the ast for debugging.
|
|
17
|
+
*
|
|
18
|
+
* There are options to change which symbols are used for tree and if the variables are surrounded by quotes (default false).
|
|
19
|
+
*
|
|
20
|
+
* Colors can changed by passing ansi codes (or whatever you want\*) to the third parameter. Or you can pass false instead of an object to disable them. Default colors are:
|
|
21
|
+
* ```ts
|
|
22
|
+
* {
|
|
23
|
+
* values: // yellow,
|
|
24
|
+
* position: // green,
|
|
25
|
+
* info: // cyan,
|
|
26
|
+
* hint: // blue,
|
|
27
|
+
* error: // red,
|
|
28
|
+
* reset: // ansi reset code
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
31
|
+
* \* For example, you could pass html tags to show this in the browser instead (this is how the demo works). This is why the reset color is exposed. For example, a color might be `<span class="error">` and reset can be `</span>`.
|
|
32
|
+
*/
|
|
33
|
+
export declare function prettyAst(ast: ParserResults | AnyToken | VariableNode | ArrayNode | GroupNode, { indent, children, last, branch, quote }?: Partial<Record<"indent" | "children" | "last" | "branch" | "quote", string>>, colors?: Partial<Colors> | false): string;
|
|
34
|
+
export {};
|
|
35
|
+
//# sourceMappingURL=prettyAst.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prettyAst.d.ts","sourceRoot":"","sources":["../../src/utils/prettyAst.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,SAAS,EAA6C,SAAS,EAAc,YAAY,EAAE,MAAM,yBAAyB,CAAA;AACnI,OAAO,EAAE,KAAK,QAAQ,EAAE,KAAK,aAAa,EAAc,MAAM,iBAAiB,CAAA;AAG/E,KAAK,MAAM,GAAG;IACb,0EAA0E;IAC1E,MAAM,EAAE,MAAM,CAAA;IACd,6NAA6N;IAC7N,IAAI,EAAE,MAAM,CAAA;IACZ,QAAQ,EAAE,MAAM,CAAA;IAChB,mKAAmK;IACnK,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,EAAE,MAAM,CAAA;IACb,sCAAsC;IACtC,KAAK,EAAE,MAAM,CAAA;CACb,CAAA;AAoBD;;;;;;;;;;;;;;;;;GAiBG;AAEH,wBAAgB,SAAS,CACxB,GAAG,EAAE,aAAa,GAAG,QAAQ,GAAG,YAAY,GAAG,SAAS,GAAG,SAAS,EACpE,EAAE,MAAc,EAAE,QAAe,EAAE,IAAW,EAAE,MAAY,EAAE,KAAU,EAAE,GAAE,OAAO,CAAC,MAAM,CAAC,QAAQ,GAAG,UAAU,GAAG,MAAM,GAAG,QAAQ,GAAG,OAAO,EAAE,MAAM,CAAC,CAAM,EAC7J,MAAM,GAAE,OAAO,CAAC,MAAM,CAAC,GAAG,KAAU,GAClC,MAAM,CAyFR"}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { colors, isBlank, unreachable } from "@alanscodelog/utils";
|
|
2
|
+
import { ArrayNode } from "../ast/classes/ArrayNode.js";
|
|
3
|
+
import { ConditionNode } from "../ast/classes/ConditionNode.js";
|
|
4
|
+
import { ErrorToken } from "../ast/classes/ErrorToken.js";
|
|
5
|
+
import { ExpressionNode } from "../ast/classes/ExpressionNode.js";
|
|
6
|
+
import { GroupNode } from "../ast/classes/GroupNode.js";
|
|
7
|
+
import { ValidToken } from "../ast/classes/ValidToken.js";
|
|
8
|
+
import { VariableNode } from "../ast/classes/VariableNode.js";
|
|
9
|
+
import { TOKEN_TYPE } from "../types/ast.js";
|
|
10
|
+
const defaultColors = {
|
|
11
|
+
values: colors.yellow,
|
|
12
|
+
info: colors.cyan,
|
|
13
|
+
position: colors.green,
|
|
14
|
+
hint: colors.blue,
|
|
15
|
+
error: colors.red,
|
|
16
|
+
reset: colors.reset
|
|
17
|
+
};
|
|
18
|
+
const disableColors = Object.fromEntries(Object.keys(defaultColors).map((key) => [key, ""]));
|
|
19
|
+
const toRows = (rows, opts) => {
|
|
20
|
+
rows = rows.filter((child) => child !== "");
|
|
21
|
+
return [
|
|
22
|
+
...rows.slice(0, rows.length - 1).map((child) => `${opts.indent}${opts.children}${child}`),
|
|
23
|
+
`${opts.indent}${opts.last}${rows[rows.length - 1]}`
|
|
24
|
+
];
|
|
25
|
+
};
|
|
26
|
+
function prettyAst(ast, { indent = " ", children = "├╴", last = "└╴", branch = "│", quote = "" } = {}, colors2 = {}) {
|
|
27
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
28
|
+
const opts = { indent, children, last, branch, quote };
|
|
29
|
+
const c = colors2 ? { ...defaultColors, ...colors2 } : disableColors;
|
|
30
|
+
const pos = `${c.position}(${ast.start}, ${ast.end})${c.reset}`;
|
|
31
|
+
const _ = indent;
|
|
32
|
+
const __ = arguments[3] ?? "";
|
|
33
|
+
let extra = arguments[4] ?? "";
|
|
34
|
+
if (!isBlank(extra))
|
|
35
|
+
extra = ` ${c.hint}${extra}${c.reset}`;
|
|
36
|
+
const ___ = __ + _ + branch;
|
|
37
|
+
const __L = __ + _ + indent[0];
|
|
38
|
+
const prettyAst_ = prettyAst;
|
|
39
|
+
if (ast instanceof ValidToken) {
|
|
40
|
+
const value = `${ast.value}`;
|
|
41
|
+
return `TOKEN ${pos} ${c.values}${quote}${value}${quote}${c.reset}${extra}`;
|
|
42
|
+
}
|
|
43
|
+
if (ast instanceof ErrorToken) {
|
|
44
|
+
const value = `[${ast.expected.join(", ")}]`;
|
|
45
|
+
return `ERROR ${pos} ${c.error}${value}${c.reset}${extra}`;
|
|
46
|
+
}
|
|
47
|
+
if (ast instanceof ConditionNode) {
|
|
48
|
+
const header = `${c.info}${ast.operator === void 0}${c.reset}`;
|
|
49
|
+
const not = ast.operator ? prettyAst_(ast.operator, opts, c, ___, `(negation)`) : "";
|
|
50
|
+
const property = ast.property ? prettyAst_(ast.property, opts, c, ___, `(property)`) : "";
|
|
51
|
+
const sepL = ((_a = ast.sep) == null ? void 0 : _a.left) ? prettyAst_(ast.sep.left, opts, c, ___, `(separator)`) : "";
|
|
52
|
+
const op = ast.propertyOperator ? prettyAst_(ast.propertyOperator, opts, c, ___, `(property operator)`) : "";
|
|
53
|
+
const sepR = ((_b = ast.sep) == null ? void 0 : _b.right) ? prettyAst_(ast.sep.right, opts, c, ___, `(separator)`) : "";
|
|
54
|
+
const isRegex = ast.value instanceof VariableNode && ((_c = ast.value.quote) == null ? void 0 : _c.left.type) === TOKEN_TYPE.REGEX;
|
|
55
|
+
const isArray = ast.value instanceof ArrayNode;
|
|
56
|
+
const variable = prettyAst_(ast.value, opts, c, __L, `(${property ? "value" : "variable/alone"}${isRegex ? " - regex" : isArray ? "- array" : ""})`);
|
|
57
|
+
return [
|
|
58
|
+
`CONDITION ${pos} ${header}${extra}`,
|
|
59
|
+
...toRows([not, property, sepL, op, sepR, variable], opts)
|
|
60
|
+
].join(`
|
|
61
|
+
${__}`);
|
|
62
|
+
}
|
|
63
|
+
if (ast instanceof VariableNode) {
|
|
64
|
+
const prefix = ast.prefix ? prettyAst_(ast.prefix, opts, c, ___, `(value prefix)`) : "";
|
|
65
|
+
const left = ((_d = ast.quote) == null ? void 0 : _d.left) ? prettyAst_(ast.quote.left, opts, c, ___, "") : "";
|
|
66
|
+
const value = prettyAst_(ast.value, opts, c, !ast.quote ? __L : !((_e = ast.quote) == null ? void 0 : _e.right) ? __L : ___, "");
|
|
67
|
+
const right = ((_f = ast.quote) == null ? void 0 : _f.right) ? prettyAst_(ast.quote.right, opts, c, !((_g = ast.quote) == null ? void 0 : _g.flags) ? __L : ___, "") : "";
|
|
68
|
+
const flags = ((_h = ast.quote) == null ? void 0 : _h.flags) ? prettyAst_(ast.quote.flags, opts, c, __L, "(flags)") : "";
|
|
69
|
+
return [
|
|
70
|
+
`VARIABLE ${pos}${extra}`,
|
|
71
|
+
...toRows([prefix, left, value, right, flags], opts)
|
|
72
|
+
].join(`
|
|
73
|
+
${__}`);
|
|
74
|
+
}
|
|
75
|
+
if (ast instanceof GroupNode) {
|
|
76
|
+
const header = `${c.info}${ast.prefix === void 0 || ast.prefix.operator === void 0}${c.reset}`;
|
|
77
|
+
const prefix = ast.prefix ? prettyAst_(ast.prefix, opts, c, ___, `(group prefix)`) : "";
|
|
78
|
+
const expression = prettyAst_(ast.expression, opts, c, __L, "");
|
|
79
|
+
return [
|
|
80
|
+
`GROUP ${pos}${!extra.includes("value") ? ` ${header}` : ""}${extra}`,
|
|
81
|
+
...toRows([prefix, expression], opts)
|
|
82
|
+
].join(`
|
|
83
|
+
${__}`);
|
|
84
|
+
}
|
|
85
|
+
if (ast instanceof ArrayNode) {
|
|
86
|
+
const bracketL = ast.bracket.left ? prettyAst_(ast.bracket.left, opts, c, ast.values.length === 0 && !ast.bracket.right ? __L : ___, "") : "";
|
|
87
|
+
const values = ast.values.length > 0 ? ast.values.map(
|
|
88
|
+
(node, i) => prettyAst_(node, opts, c, !ast.bracket.right && i === ast.values.length - 1 ? __L : ___, "")
|
|
89
|
+
) : [];
|
|
90
|
+
const bracketR = ast.bracket.right ? prettyAst_(ast.bracket.right, opts, c, __L, "") : "";
|
|
91
|
+
return [
|
|
92
|
+
`ARRAY ${pos} ${c.info}[${ast.values.length}]${c.reset}${extra}`,
|
|
93
|
+
...toRows([bracketL, ...values, bracketR], opts)
|
|
94
|
+
].join(`
|
|
95
|
+
${__}`);
|
|
96
|
+
}
|
|
97
|
+
if (ast instanceof ExpressionNode) {
|
|
98
|
+
const left = prettyAst_(ast.left, opts, c, ___, "");
|
|
99
|
+
const operator = prettyAst_(ast.operator, opts, c, ___, `(boolean operator)`);
|
|
100
|
+
const right = prettyAst_(ast.right, opts, c, __L, "");
|
|
101
|
+
const header = ast.operator instanceof ErrorToken ? `${c.info}[${ast.operator.expected.join(",")}]${c.reset}` : `${c.info}"${ast.operator.value}"${c.reset}`;
|
|
102
|
+
return [
|
|
103
|
+
`EXPRESSION ${pos} ${header}${extra}`,
|
|
104
|
+
...toRows([left, operator, right], opts)
|
|
105
|
+
].join(`
|
|
106
|
+
${__}`);
|
|
107
|
+
}
|
|
108
|
+
unreachable();
|
|
109
|
+
}
|
|
110
|
+
export {
|
|
111
|
+
prettyAst
|
|
112
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@witchcraft/expressit",
|
|
3
|
+
"description": "A blazing fast, customizable, error-tolerant expression parser that creates safe to eval expressions + a few other goodies like autocomplete.",
|
|
4
|
+
"version": "0.0.2",
|
|
5
|
+
"types": "./dist/index.d.ts",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js"
|
|
11
|
+
},
|
|
12
|
+
"./ast": {
|
|
13
|
+
"types": "./dist/ast/index.d.ts",
|
|
14
|
+
"import": "./dist/ast/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./grammar": {
|
|
17
|
+
"types": "./dist/grammar/index.d.ts",
|
|
18
|
+
"import": "./dist/grammar/index.js"
|
|
19
|
+
},
|
|
20
|
+
"./helpers": {
|
|
21
|
+
"types": "./dist/helpers/index.d.ts",
|
|
22
|
+
"import": "./dist/helpers/index.js"
|
|
23
|
+
},
|
|
24
|
+
"./methods": {
|
|
25
|
+
"types": "./dist/methods/index.d.ts",
|
|
26
|
+
"import": "./dist/methods/index.js"
|
|
27
|
+
},
|
|
28
|
+
"./types": {
|
|
29
|
+
"types": "./dist/types/index.d.ts",
|
|
30
|
+
"import": "./dist/types/index.js"
|
|
31
|
+
},
|
|
32
|
+
"./utils": {
|
|
33
|
+
"types": "./dist/utils/index.d.ts",
|
|
34
|
+
"import": "./dist/utils/index.js"
|
|
35
|
+
},
|
|
36
|
+
"./examples": {
|
|
37
|
+
"types": "./dist/examples/index.d.ts",
|
|
38
|
+
"import": "./dist/examples/index.js"
|
|
39
|
+
},
|
|
40
|
+
"./*": {
|
|
41
|
+
"types": "./dist/*",
|
|
42
|
+
"import": "./dist/*"
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"debug": "ts-node -r tsconfig-paths/register -T --esm",
|
|
47
|
+
"build": "vite build",
|
|
48
|
+
"build:dev": "vite build --mode development",
|
|
49
|
+
"build:watch": "vite build --watch --mode production",
|
|
50
|
+
"build:types": "tsc --emitDeclarationOnly -p tsconfig.types.json && npm run build:types:fix",
|
|
51
|
+
"build:types:fix": "tsc-alias -p tsconfig.types.json --debug",
|
|
52
|
+
"lint:eslint": "eslint \"{src,tests,bin}/**/*.{cjs,js,ts}\" \"*.{cjs,js,ts}\" --max-warnings=0 --report-unused-disable-directives",
|
|
53
|
+
"lint:types": "tsc --noEmit --pretty",
|
|
54
|
+
"lint:commits": "commitlint --from $(git rev-list --max-parents=0 HEAD) --to HEAD --verbose",
|
|
55
|
+
"lint:imports": "madge --circular --extensions ts ./src",
|
|
56
|
+
"lint": "npm run lint:types && npm run lint:eslint",
|
|
57
|
+
"coverage": "vitest --coverage",
|
|
58
|
+
"coverage:dev": "vitest --watch --coverage",
|
|
59
|
+
"test": "npm run lint:types && vitest run",
|
|
60
|
+
"test:watch": "vitest --watch",
|
|
61
|
+
"test:inspect-errors": "cross-env INSPECT_ERRORS=true npm run test",
|
|
62
|
+
"doc": "typedoc --options typedoc.config.cjs",
|
|
63
|
+
"doc:watch": "onchange -i \"src/**/*.ts\" \"typedoc.config.cjs\" -- npm run doc",
|
|
64
|
+
"doc:serve": "http-server docs --port=5001",
|
|
65
|
+
"doc:dev": "concurrently \"npm run doc:watch\" \"npm run doc:serve\"",
|
|
66
|
+
"doc:check-invalid": "typedoc --options typedoc.config.cjs --listInvalidSymbolLinks",
|
|
67
|
+
"demo:dev": "cd demo && concurrently \"vite\" \"npm run build:watch\"",
|
|
68
|
+
"demo:build": "cd demo && npm run build",
|
|
69
|
+
"actions:debug": "act -r -v -j build",
|
|
70
|
+
"gen:exports": "indexit update -o '${path}.js' -i **/*.d.ts",
|
|
71
|
+
"prepare": "husky install && npm run build"
|
|
72
|
+
},
|
|
73
|
+
"dependencies": {
|
|
74
|
+
"@alanscodelog/utils": "^4.0.0-beta.11",
|
|
75
|
+
"chevrotain": "^11.0.3"
|
|
76
|
+
},
|
|
77
|
+
"devDependencies": {
|
|
78
|
+
"@alanscodelog/commitlint-config": "^2.0.0",
|
|
79
|
+
"@alanscodelog/eslint-config": "^4.0.3",
|
|
80
|
+
"@alanscodelog/semantic-release-config": "^3.0.0",
|
|
81
|
+
"@alanscodelog/tsconfigs": "^3.2.0",
|
|
82
|
+
"@knodes/typedoc-plugin-pages": "^0.23.4",
|
|
83
|
+
"@types/node": "^20.4.1",
|
|
84
|
+
"@typescript-eslint/eslint-plugin": "^6.7.4",
|
|
85
|
+
"@typescript-eslint/parser": "^6.7.4",
|
|
86
|
+
"@vitest/coverage-c8": "^0.33.0",
|
|
87
|
+
"commitlint": "^17.6.6",
|
|
88
|
+
"concurrently": "^8.2.0",
|
|
89
|
+
"cross-env": "^7.0.3",
|
|
90
|
+
"eslint": "^8.44.0",
|
|
91
|
+
"eslint-import-resolver-typescript": "^3.5.5",
|
|
92
|
+
"eslint-plugin-import": "^2.27.5",
|
|
93
|
+
"eslint-plugin-jsdoc": "^46.4.3",
|
|
94
|
+
"eslint-plugin-simple-import-sort": "^10.0.0",
|
|
95
|
+
"fast-glob": "^3.3.1",
|
|
96
|
+
"http-server": "^14.1.1",
|
|
97
|
+
"husky": "^8.0.3",
|
|
98
|
+
"indexit": "2.1.0-beta.3",
|
|
99
|
+
"madge": "^6.1.0",
|
|
100
|
+
"onchange": "^7.1.0",
|
|
101
|
+
"semantic-release": "^22.0.5",
|
|
102
|
+
"ts-node": "^10.9.1",
|
|
103
|
+
"tsc-alias": "^1.8.7",
|
|
104
|
+
"typedoc": "~0.23.1",
|
|
105
|
+
"typescript": "~5.0.0",
|
|
106
|
+
"vite": "^4.4.2",
|
|
107
|
+
"vite-plugin-externalize-deps": "^0.7.0",
|
|
108
|
+
"vite-tsconfig-paths": "^4.2.0",
|
|
109
|
+
"vitest": "^0.34.6"
|
|
110
|
+
},
|
|
111
|
+
"author": "Alan <alanscodelog@gmail.com>",
|
|
112
|
+
"repository": "https://github.com/witchcraftjs/expressit",
|
|
113
|
+
"keywords": [
|
|
114
|
+
"boolean",
|
|
115
|
+
"parser",
|
|
116
|
+
"query",
|
|
117
|
+
"evaluator",
|
|
118
|
+
"expression",
|
|
119
|
+
"condition",
|
|
120
|
+
"search"
|
|
121
|
+
],
|
|
122
|
+
"license": "MIT",
|
|
123
|
+
"files": [
|
|
124
|
+
"src",
|
|
125
|
+
"dist"
|
|
126
|
+
],
|
|
127
|
+
"release": {
|
|
128
|
+
"extends": [
|
|
129
|
+
"@alanscodelog/semantic-release-config"
|
|
130
|
+
]
|
|
131
|
+
},
|
|
132
|
+
"commitlint": {
|
|
133
|
+
"extends": [
|
|
134
|
+
"@alanscodelog"
|
|
135
|
+
]
|
|
136
|
+
},
|
|
137
|
+
"browserslist": "defaults and supports es6-module,maintained node versions",
|
|
138
|
+
"engines": {
|
|
139
|
+
"node": ">=18.0.0"
|
|
140
|
+
},
|
|
141
|
+
"@comments": {
|
|
142
|
+
"scripts": {
|
|
143
|
+
"test": "Runs `lint:types` before (so that flags can be passed to the test command) so that we can test type assertions. See expect_type function in @alanscodelog/utils.",
|
|
144
|
+
"lint:commits": "Lints all unpushed commits in the active branch.",
|
|
145
|
+
"prepare": "Needed so that if we pull the package from git it will get built and installed properly.",
|
|
146
|
+
"actions:debug": "For debugging github build action locally with nektos/act. Requires act and docker. Note: Cache will never work locally because of https://github.com/nektos/act/issues/285"
|
|
147
|
+
}
|
|
148
|
+
},
|
|
149
|
+
"publishConfig": {
|
|
150
|
+
"access": "public"
|
|
151
|
+
}
|
|
152
|
+
}
|