@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.
Files changed (124) hide show
  1. package/README.md +6 -4
  2. package/dist/Lexer.d.ts +146 -0
  3. package/dist/Lexer.d.ts.map +1 -0
  4. package/dist/Lexer.js +960 -0
  5. package/dist/Parser.d.ts +140 -0
  6. package/dist/Parser.d.ts.map +1 -0
  7. package/dist/Parser.js +668 -0
  8. package/dist/ast/builders/token.js +1 -1
  9. package/dist/ast/handlers.d.ts +3 -3
  10. package/dist/ast/handlers.d.ts.map +1 -1
  11. package/dist/ast/index.d.ts.map +1 -1
  12. package/dist/examples/index.d.ts +2 -0
  13. package/dist/examples/index.d.ts.map +1 -0
  14. package/dist/examples/index.js +4 -0
  15. package/dist/examples/shortcutContextParser.d.ts +2 -1
  16. package/dist/examples/shortcutContextParser.d.ts.map +1 -1
  17. package/dist/examples/shortcutContextParser.js +9 -5
  18. package/dist/helpers/errors.d.ts.map +1 -1
  19. package/dist/helpers/errors.js +3 -1
  20. package/dist/helpers/index.d.ts.map +1 -1
  21. package/dist/helpers/parser/checkParserOpts.d.ts.map +1 -1
  22. package/dist/helpers/parser/checkParserOpts.js +3 -2
  23. package/dist/helpers/parser/extractPosition.d.ts +2 -6
  24. package/dist/helpers/parser/extractPosition.d.ts.map +1 -1
  25. package/dist/helpers/parser/extractPosition.js +3 -3
  26. package/dist/helpers/parser/getUnclosedRightParenCount.d.ts +2 -3
  27. package/dist/helpers/parser/getUnclosedRightParenCount.d.ts.map +1 -1
  28. package/dist/helpers/parser/getUnclosedRightParenCount.js +4 -4
  29. package/dist/index.d.ts +1 -2
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.js +3 -5
  32. package/dist/methods/autocomplete.d.ts.map +1 -1
  33. package/dist/methods/autocomplete.js +1 -1
  34. package/dist/methods/autoreplace.js +1 -1
  35. package/dist/methods/autosuggest.js +1 -1
  36. package/dist/methods/evaluate.d.ts.map +1 -1
  37. package/dist/methods/evaluate.js +3 -1
  38. package/dist/methods/getIndexes.d.ts.map +1 -1
  39. package/dist/methods/getIndexes.js +2 -1
  40. package/dist/methods/normalize.d.ts +0 -2
  41. package/dist/methods/normalize.d.ts.map +1 -1
  42. package/dist/methods/normalize.js +2 -3
  43. package/dist/methods/validate.d.ts.map +1 -1
  44. package/dist/methods/validate.js +3 -1
  45. package/dist/package.json.js +44 -37
  46. package/dist/types/ast.d.ts +2 -8
  47. package/dist/types/ast.d.ts.map +1 -1
  48. package/dist/types/errors.d.ts +5 -17
  49. package/dist/types/errors.d.ts.map +1 -1
  50. package/dist/types/errors.js +0 -1
  51. package/dist/types/parser.d.ts +6 -2
  52. package/dist/types/parser.d.ts.map +1 -1
  53. package/dist/utils/extractTokens.js +1 -1
  54. package/dist/utils/getCursorInfo.d.ts +2 -2
  55. package/dist/utils/getCursorInfo.d.ts.map +1 -1
  56. package/dist/utils/getCursorInfo.js +3 -2
  57. package/dist/utils/getOppositeDelimiter.d.ts.map +1 -1
  58. package/dist/utils/getOppositeDelimiter.js +1 -1
  59. package/dist/utils/prettyAst.d.ts.map +1 -1
  60. package/dist/utils/prettyAst.js +15 -9
  61. package/package.json +42 -37
  62. package/src/Lexer.ts +704 -0
  63. package/src/Parser.ts +972 -0
  64. package/src/ast/builders/array.ts +2 -2
  65. package/src/ast/builders/condition.ts +1 -1
  66. package/src/ast/builders/expression.ts +1 -1
  67. package/src/ast/builders/group.ts +1 -1
  68. package/src/ast/builders/index.ts +1 -1
  69. package/src/ast/builders/pos.ts +1 -1
  70. package/src/ast/builders/token.ts +2 -2
  71. package/src/ast/builders/type.ts +1 -1
  72. package/src/ast/builders/variable.ts +1 -1
  73. package/src/ast/classes/ConditionNode.ts +1 -1
  74. package/src/ast/classes/ErrorToken.ts +1 -1
  75. package/src/ast/classes/ValidToken.ts +2 -2
  76. package/src/ast/classes/index.ts +1 -1
  77. package/src/ast/handlers.ts +6 -6
  78. package/src/ast/index.ts +2 -2
  79. package/src/examples/index.ts +3 -0
  80. package/src/examples/shortcutContextParser.ts +11 -6
  81. package/src/helpers/errors.ts +5 -3
  82. package/src/helpers/general/defaultConditionNormalizer.ts +1 -1
  83. package/src/helpers/general/index.ts +1 -1
  84. package/src/helpers/index.ts +3 -2
  85. package/src/helpers/parser/checkParserOpts.ts +13 -12
  86. package/src/helpers/parser/extractPosition.ts +4 -8
  87. package/src/helpers/parser/getUnclosedRightParenCount.ts +6 -6
  88. package/src/helpers/parser/index.ts +1 -1
  89. package/src/helpers/parser/parseParserOptions.ts +1 -1
  90. package/src/index.ts +2 -2
  91. package/src/methods/autocomplete.ts +5 -5
  92. package/src/methods/autoreplace.ts +2 -2
  93. package/src/methods/autosuggest.ts +3 -3
  94. package/src/methods/evaluate.ts +4 -2
  95. package/src/methods/getIndexes.ts +2 -1
  96. package/src/methods/normalize.ts +3 -4
  97. package/src/methods/validate.ts +4 -2
  98. package/src/types/ast.ts +2 -9
  99. package/src/types/errors.ts +12 -22
  100. package/src/types/parser.ts +6 -4
  101. package/src/utils/extractTokens.ts +1 -1
  102. package/src/utils/getCursorInfo.ts +6 -4
  103. package/src/utils/getOppositeDelimiter.ts +5 -2
  104. package/src/utils/prettyAst.ts +5 -3
  105. package/dist/examples/advancedValueComparer.d.ts +0 -3
  106. package/dist/examples/advancedValueComparer.d.ts.map +0 -1
  107. package/dist/examples/advancedValueComparer.js +0 -28
  108. package/dist/grammar/ParserBase.d.ts +0 -51
  109. package/dist/grammar/ParserBase.d.ts.map +0 -1
  110. package/dist/grammar/ParserBase.js +0 -516
  111. package/dist/grammar/createTokens.d.ts +0 -56
  112. package/dist/grammar/createTokens.d.ts.map +0 -1
  113. package/dist/grammar/createTokens.js +0 -843
  114. package/dist/grammar/index.d.ts +0 -3
  115. package/dist/grammar/index.d.ts.map +0 -1
  116. package/dist/grammar/index.js +0 -6
  117. package/dist/parser.d.ts +0 -58
  118. package/dist/parser.d.ts.map +0 -1
  119. package/dist/parser.js +0 -136
  120. package/src/examples/advancedValueComparer.ts +0 -31
  121. package/src/grammar/ParserBase.ts +0 -715
  122. package/src/grammar/createTokens.ts +0 -512
  123. package/src/grammar/index.ts +0 -4
  124. package/src/parser.ts +0 -183
@@ -1,3 +0,0 @@
1
- export { createTokens } from "./createTokens.js";
2
- export { ParserBase } from "./ParserBase.js";
3
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/grammar/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAChD,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA"}
@@ -1,6 +0,0 @@
1
- import { createTokens } from "./createTokens.js";
2
- import { ParserBase } from "./ParserBase.js";
3
- export {
4
- ParserBase,
5
- createTokens
6
- };
package/dist/parser.d.ts DELETED
@@ -1,58 +0,0 @@
1
- /**
2
- * The parser's methods are often long and have a lot of documentation per method, so it's methods have been split into mixins. They can be found in the `./methods` folder.
3
- *
4
- * Writing from within any of these methods is like writing a method from here except:
5
- * - `this` calls are wrapped in `(this as any as Parser<T>)`
6
- * - private method/property access requires `// @ts-expect-error`.
7
- * - recursion with hidden parameters requires re-typing this (see evaluate/validate for examples) since otherwise if we only retyped the function it would become unbound from `this`.
8
- *
9
- * Docs work like normal (on methods). From the outside, users of the library cannot even tell the class is composed of mixins.
10
- */
11
- import type { Mixin } from "@alanscodelog/utils";
12
- import { createTokens } from "./grammar/createTokens.js";
13
- import { ParserBase } from "./grammar/ParserBase.js";
14
- import { AutocompleteMixin } from "./methods/autocomplete.js";
15
- import { AutoreplaceMixin } from "./methods/autoreplace.js";
16
- import { Autosuggest } from "./methods/autosuggest.js";
17
- import { EvaluateMixin } from "./methods/evaluate.js";
18
- import { GetBestIndexesMixin } from "./methods/getBestIndex.js";
19
- import { GetIndexMixin } from "./methods/getIndexes.js";
20
- import { NormalizeMixin, ValidateMixin } from "./methods/index.js";
21
- import type { ParserResults } from "./types/ast.js";
22
- import type { FullParserOptions, ParserOptions } from "./types/parser.js";
23
- /**
24
- * Creates the main parser class which handles all functionality (evaluation, validation, etc).
25
- */
26
- export declare class Parser<T extends {} = {}> {
27
- options: FullParserOptions<T>;
28
- private readonly rawOptions;
29
- parser: ParserBase<T>;
30
- private readonly lexer;
31
- private readonly tokens;
32
- info: ReturnType<typeof createTokens>["info"];
33
- constructor(options?: ParserOptions<T>);
34
- /**
35
- * Parses a string.
36
- */
37
- parse(input: string): ParserResults;
38
- private evaluationOptionsChecked;
39
- _checkEvaluationOptions(): void;
40
- private validationOptionsChecked;
41
- _checkValidationOptions(): void;
42
- /**
43
- * Generates a railroad diagram for debugging. Does not 100% represent how things are actually handled internally.
44
- *
45
- * Not exposed because it uses the raw chevrotain tokens.
46
- *
47
- * **Note: It is not 100% accurate. Some special cases are parsed one way but handled internally differently.**
48
- */
49
- private _generateRailRoadDiagram;
50
- /**
51
- * For debugging.
52
- * Not exposed because it returns the raw chevrotain tokens.
53
- */
54
- private _lex;
55
- }
56
- export interface Parser<T extends {} = {}> extends Mixin<AutocompleteMixin<T> | AutoreplaceMixin | Autosuggest<T> | EvaluateMixin<T> | ValidateMixin<T> | NormalizeMixin<T> | GetIndexMixin<T> | GetBestIndexesMixin>, AutocompleteMixin<T>, AutoreplaceMixin, Autosuggest<T>, EvaluateMixin<T>, ValidateMixin<T>, NormalizeMixin<T>, GetIndexMixin<T>, GetBestIndexesMixin {
57
- }
58
- //# sourceMappingURL=parser.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"parser.d.ts","sourceRoot":"","sources":["../src/parser.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAA;AAKhD,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAA;AACxD,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAA;AAKpD,OAAO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,SAAS,EAAE,GAAG,EAAE,CAAE,SAAQ,KAAK,CACrD,iBAAiB,CAAC,CAAC,CAAC,GACpB,gBAAgB,GAChB,WAAW,CAAC,CAAC,CAAC,GACd,aAAa,CAAC,CAAC,CAAC,GAChB,aAAa,CAAC,CAAC,CAAC,GAChB,cAAc,CAAC,CAAC,CAAC,GACjB,aAAa,CAAC,CAAC,CAAC,GAChB,mBAAmB,CACrB,EACA,iBAAiB,CAAC,CAAC,CAAC,EACpB,gBAAgB,EAChB,WAAW,CAAC,CAAC,CAAC,EACd,aAAa,CAAC,CAAC,CAAC,EAChB,aAAa,CAAG,CAAC,CAAE,EACnB,cAAc,CAAC,CAAC,CAAC,EACjB,aAAa,CAAC,CAAC,CAAC,EAChB,mBAAmB;CAClB"}
package/dist/parser.js DELETED
@@ -1,136 +0,0 @@
1
- var __defProp = Object.defineProperty;
2
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
- var __publicField = (obj, key, value) => {
4
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
- return value;
6
- };
7
- import { mixin, isWhitespace } from "@alanscodelog/utils";
8
- import { createSyntaxDiagramsCode } from "chevrotain";
9
- import { token } from "./ast/handlers.js";
10
- import { createTokens } from "./grammar/createTokens.js";
11
- import { ParserBase } from "./grammar/ParserBase.js";
12
- import { BooleanParserLibraryError } from "./helpers/errors.js";
13
- import { checkParserOpts } from "./helpers/parser/checkParserOpts.js";
14
- import { getUnclosedRightParenCount } from "./helpers/parser/getUnclosedRightParenCount.js";
15
- import { ERROR_CODES } from "./types/errors.js";
16
- import { parseParserOptions } from "./helpers/parser/parseParserOptions.js";
17
- import { seal } from "./helpers/parser/seal.js";
18
- import { AutocompleteMixin } from "./methods/autocomplete.js";
19
- import { AutoreplaceMixin } from "./methods/autoreplace.js";
20
- import { Autosuggest } from "./methods/autosuggest.js";
21
- import { EvaluateMixin } from "./methods/evaluate.js";
22
- import { GetBestIndexesMixin } from "./methods/getBestIndex.js";
23
- import { GetIndexMixin } from "./methods/getIndexes.js";
24
- import { NormalizeMixin } from "./methods/normalize.js";
25
- import { ValidateMixin } from "./methods/validate.js";
26
- class Parser {
27
- constructor(options) {
28
- __publicField(this, "options");
29
- __publicField(this, "rawOptions");
30
- __publicField(this, "parser");
31
- __publicField(this, "lexer");
32
- __publicField(this, "tokens");
33
- __publicField(this, "info");
34
- // needed for evaluate and validate so they are only checked on demand
35
- __publicField(this, "evaluationOptionsChecked", false);
36
- __publicField(this, "validationOptionsChecked", false);
37
- this.rawOptions = options ?? {};
38
- const opts = parseParserOptions(this.rawOptions);
39
- checkParserOpts(opts);
40
- this.options = opts;
41
- const { lexer, tokens, info } = createTokens(opts);
42
- this.lexer = lexer;
43
- this.tokens = tokens;
44
- this.info = info;
45
- this.parser = new ParserBase(opts, this.tokens, this.info);
46
- }
47
- /**
48
- * Parses a string.
49
- */
50
- parse(input) {
51
- var _a;
52
- if (isWhitespace(input)) {
53
- return token.value(void 0, { start: 0, end: 0 });
54
- }
55
- let lexed = this._lex(input);
56
- const shift = getUnclosedRightParenCount(lexed.tokens, this.tokens);
57
- if (shift) {
58
- input = "(".repeat(shift) + input;
59
- lexed = this._lex(input);
60
- }
61
- this.parser.shift = shift;
62
- this.parser.input = lexed.tokens;
63
- this.parser.rawInput = input;
64
- try {
65
- if (lexed.errors.length > 0)
66
- throw new Error("Unexpected Lexer Errors");
67
- this.parser.input = lexed.tokens;
68
- const res = this.parser.main();
69
- if (res === void 0) {
70
- throw new Error("throw");
71
- }
72
- if (!((_a = arguments[1]) == null ? void 0 : _a.unsealed))
73
- seal(res);
74
- return res;
75
- } catch (error) {
76
- if (error.message === "throw")
77
- error = void 0;
78
- const err = new BooleanParserLibraryError(ERROR_CODES.PARSER_ERROR, {
79
- input,
80
- options: this.rawOptions,
81
- "parsed options": this.options,
82
- error,
83
- "lexed tokens": lexed.tokens,
84
- "lexer errors": lexed.errors,
85
- "parser errors": this.parser.errors
86
- });
87
- throw err;
88
- }
89
- }
90
- // eslint-disable-next-line @typescript-eslint/naming-convention
91
- _checkEvaluationOptions() {
92
- if (!this.evaluationOptionsChecked) {
93
- checkParserOpts(this.options, true);
94
- this.evaluationOptionsChecked = true;
95
- }
96
- }
97
- // eslint-disable-next-line @typescript-eslint/naming-convention
98
- _checkValidationOptions() {
99
- if (!this.validationOptionsChecked) {
100
- checkParserOpts(this.options, false, true);
101
- this.validationOptionsChecked = true;
102
- }
103
- }
104
- /**
105
- * Generates a railroad diagram for debugging. Does not 100% represent how things are actually handled internally.
106
- *
107
- * Not exposed because it uses the raw chevrotain tokens.
108
- *
109
- * **Note: It is not 100% accurate. Some special cases are parsed one way but handled internally differently.**
110
- */
111
- _generateRailRoadDiagram() {
112
- const serialized = this.parser.getSerializedGastProductions();
113
- const html = createSyntaxDiagramsCode(serialized);
114
- return html;
115
- }
116
- /**
117
- * For debugging.
118
- * Not exposed because it returns the raw chevrotain tokens.
119
- */
120
- _lex(input) {
121
- return this.lexer.tokenize(input);
122
- }
123
- }
124
- mixin(Parser, [
125
- AutocompleteMixin,
126
- AutoreplaceMixin,
127
- Autosuggest,
128
- EvaluateMixin,
129
- ValidateMixin,
130
- NormalizeMixin,
131
- GetIndexMixin,
132
- GetBestIndexesMixin
133
- ]);
134
- export {
135
- Parser
136
- };
@@ -1,31 +0,0 @@
1
- import type { ValueQuery } from "../types/parser.js"
2
-
3
- /* TODO TOUPDATE */
4
- type Operators = ">" | "contains"
5
- type Prefixes = "num"
6
- export function valueComparer(contextValue: string, { operator, prefix, value, isRegex, regexFlags }: ValueQuery): boolean {
7
- let finalValue: any = value
8
- if (prefix) {
9
- const val = value as string // it's always a string if prefixed
10
- switch (prefix as Prefixes) {
11
- case "num": finalValue = parseInt(val, 2); break
12
- // ...
13
- }
14
- }
15
- if (isRegex) {
16
- const val = value as string // it's always a string and never prefixed if it's a regex
17
- const regex = new RegExp(val, regexFlags)
18
- return contextValue.match(regex) !== null
19
- }
20
- if (operator) {
21
- switch (operator as Operators) {
22
- // custom operators
23
- case ">": return contextValue > finalValue
24
- // ...
25
- // custom expanded operators
26
- case "contains": return contextValue.includes(finalValue)
27
- // ...
28
- }
29
- }
30
- return contextValue === finalValue
31
- }