@jbrowse/jexl 3.0.1 → 3.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 (76) hide show
  1. package/README.md +24 -25
  2. package/dist/Expression.d.ts +16 -6
  3. package/dist/Expression.js +34 -8
  4. package/dist/Expression.js.map +1 -1
  5. package/dist/Jexl.d.ts +13 -12
  6. package/dist/Jexl.js +31 -30
  7. package/dist/Jexl.js.map +1 -1
  8. package/dist/Lexer.d.ts +9 -8
  9. package/dist/Lexer.js +61 -19
  10. package/dist/Lexer.js.map +1 -1
  11. package/dist/evaluator/Evaluator.d.ts +20 -34
  12. package/dist/evaluator/Evaluator.js +19 -64
  13. package/dist/evaluator/Evaluator.js.map +1 -1
  14. package/dist/evaluator/compile.d.ts +27 -0
  15. package/dist/evaluator/compile.js +261 -0
  16. package/dist/evaluator/compile.js.map +1 -0
  17. package/dist/grammar.d.ts +29 -9
  18. package/dist/grammar.js +19 -3
  19. package/dist/grammar.js.map +1 -1
  20. package/dist/parser/Parser.d.ts +5 -7
  21. package/dist/parser/Parser.js +33 -5
  22. package/dist/parser/Parser.js.map +1 -1
  23. package/dist/parser/handlers.d.ts +0 -6
  24. package/dist/parser/handlers.js +54 -46
  25. package/dist/parser/handlers.js.map +1 -1
  26. package/dist/parser/states.d.ts +2 -2
  27. package/dist/parser/states.js +5 -21
  28. package/dist/parser/states.js.map +1 -1
  29. package/dist/types.d.ts +18 -7
  30. package/esm/Expression.d.ts +16 -6
  31. package/esm/Expression.js +34 -8
  32. package/esm/Expression.js.map +1 -1
  33. package/esm/Jexl.d.ts +13 -12
  34. package/esm/Jexl.js +31 -30
  35. package/esm/Jexl.js.map +1 -1
  36. package/esm/Lexer.d.ts +9 -8
  37. package/esm/Lexer.js +61 -19
  38. package/esm/Lexer.js.map +1 -1
  39. package/esm/evaluator/Evaluator.d.ts +20 -34
  40. package/esm/evaluator/Evaluator.js +19 -31
  41. package/esm/evaluator/Evaluator.js.map +1 -1
  42. package/esm/evaluator/compile.d.ts +27 -0
  43. package/esm/evaluator/compile.js +258 -0
  44. package/esm/evaluator/compile.js.map +1 -0
  45. package/esm/grammar.d.ts +29 -9
  46. package/esm/grammar.js +18 -3
  47. package/esm/grammar.js.map +1 -1
  48. package/esm/parser/Parser.d.ts +5 -7
  49. package/esm/parser/Parser.js +33 -5
  50. package/esm/parser/Parser.js.map +1 -1
  51. package/esm/parser/handlers.d.ts +0 -6
  52. package/esm/parser/handlers.js +54 -45
  53. package/esm/parser/handlers.js.map +1 -1
  54. package/esm/parser/states.d.ts +2 -2
  55. package/esm/parser/states.js +5 -21
  56. package/esm/parser/states.js.map +1 -1
  57. package/esm/types.d.ts +18 -7
  58. package/package.json +27 -29
  59. package/src/Expression.ts +33 -13
  60. package/src/Jexl.ts +59 -21
  61. package/src/Lexer.ts +63 -28
  62. package/src/evaluator/Evaluator.ts +27 -44
  63. package/src/evaluator/compile.ts +310 -0
  64. package/src/grammar.ts +66 -17
  65. package/src/parser/Parser.ts +35 -21
  66. package/src/parser/handlers.ts +96 -73
  67. package/src/parser/states.ts +7 -23
  68. package/src/types.ts +22 -7
  69. package/CHANGELOG.md +0 -249
  70. package/dist/evaluator/handlers.d.ts +0 -112
  71. package/dist/evaluator/handlers.js +0 -208
  72. package/dist/evaluator/handlers.js.map +0 -1
  73. package/esm/evaluator/handlers.d.ts +0 -112
  74. package/esm/evaluator/handlers.js +0 -194
  75. package/esm/evaluator/handlers.js.map +0 -1
  76. package/src/evaluator/handlers.ts +0 -212
@@ -2,15 +2,19 @@
2
2
  * Jexl
3
3
  * Copyright 2020 Tom Shawver
4
4
  */
5
- import * as handlers from "./handlers.js";
5
+ import { compileAst } from "./compile.js";
6
6
  /**
7
- * The Evaluator takes a Jexl expression tree as generated by the
8
- * {@link Parser} and calculates its value within a given context. The
9
- * collection of transforms, context, and a relative context to be used as the
10
- * root for relative identifiers, are all specific to an Evaluator instance.
11
- * When any of these things change, a new instance is required. However, a
12
- * single instance can be used to simultaneously evaluate many different
7
+ * The Evaluator carries everything an expression is evaluated against: the
8
+ * grammar, the context, and a relative context to be used as the root for
9
+ * relative identifiers. When any of these change, a new instance is required.
10
+ * However, a single instance can be used to evaluate many different
13
11
  * expressions, and does not have to be reinstantiated for each.
12
+ *
13
+ * The work of evaluation itself lives in {@link compileAst}, which lowers a
14
+ * tree to closures. Prefer compiling once and reusing the result — that is what
15
+ * {@link Expression} does — over calling {@link #eval}, which compiles the tree
16
+ * it is given every time.
17
+ *
14
18
  * @param {{}} grammar A grammar object against which to evaluate the expression
15
19
  * tree
16
20
  * @param {{}} [context] A map of variable keys to their values. This will be
@@ -19,40 +23,24 @@ import * as handlers from "./handlers.js";
19
23
  * to resolve the value of a relative identifier.
20
24
  */
21
25
  class Evaluator {
26
+ _grammar;
27
+ _context;
28
+ _relContext;
22
29
  constructor(grammar, context, relativeContext) {
23
30
  this._grammar = grammar;
24
31
  this._context = context || {};
25
32
  this._relContext = relativeContext || this._context;
26
33
  }
27
34
  /**
28
- * Evaluates an expression tree within the configured context.
35
+ * Compiles and evaluates an expression tree within the configured context.
36
+ * Convenient for a one-shot evaluation; for repeated evaluation of the same
37
+ * tree, hold onto the closure from {@link compileAst} instead so the tree is
38
+ * only lowered once.
29
39
  * @param {{}} ast An expression tree object
30
40
  * @returns {*} the resulting value of the expression.
31
41
  */
32
42
  eval(ast) {
33
- return handlers[ast.type].call(this, ast);
34
- }
35
- /**
36
- * Evaluates each expression within an array, and delivers the response as an
37
- * array with the resulting values at the same indexes as their originating
38
- * expressions.
39
- * @param {Array<string>} arr An array of expression strings to be evaluated
40
- * @returns {Array<{}>} the result array
41
- */
42
- evalArray(arr) {
43
- return arr.map((elem) => this.eval(elem));
44
- }
45
- /**
46
- * Evaluates each expression within a map, and delivers the response as a map
47
- * with the same keys, but with the evaluated result for each as their value.
48
- * @param {{}} map A map of expression names to expression trees to be
49
- * evaluated
50
- * @returns {{}} the result map.
51
- */
52
- evalMap(map) {
53
- const entries = Object.entries(map);
54
- const vals = entries.map(([_, ast]) => this.eval(ast));
55
- return Object.fromEntries(entries.map(([key], idx) => [key, vals[idx]]));
43
+ return compileAst(ast, this._grammar)(this);
56
44
  }
57
45
  }
58
46
  export default Evaluator;
@@ -1 +1 @@
1
- {"version":3,"file":"Evaluator.js","sourceRoot":"","sources":["../../src/evaluator/Evaluator.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,QAAQ,MAAM,eAAe,CAAA;AASzC;;;;;;;;;;;;;;GAcG;AACH,MAAM,SAAS;IAKb,YAAY,OAAgB,EAAE,OAAa,EAAE,eAAqB;QAChE,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAA;QACvB,IAAI,CAAC,QAAQ,GAAG,OAAO,IAAI,EAAE,CAAA;QAC7B,IAAI,CAAC,WAAW,GAAG,eAAe,IAAI,IAAI,CAAC,QAAQ,CAAA;IACrD,CAAC;IAED;;;;OAIG;IACH,IAAI,CAAC,GAAY;QACf,OAAQ,QAAgB,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,CAAC,CAAA;IACpD,CAAC;IAED;;;;;;OAMG;IACH,SAAS,CAAC,GAAc;QACtB,OAAO,GAAG,CAAC,GAAG,CAAC,CAAC,IAAa,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAA;IACpD,CAAC;IAED;;;;;;OAMG;IACH,OAAO,CAAC,GAA4B;QAClC,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;QACnC,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAA;QACtD,OAAO,MAAM,CAAC,WAAW,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,EAAE,GAAG,EAAE,EAAE,CAAC,CAAC,GAAG,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAA;IAC1E,CAAC;CACF;AAED,eAAe,SAAS,CAAA"}
1
+ {"version":3,"file":"Evaluator.js","sourceRoot":"","sources":["../../src/evaluator/Evaluator.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AAKzC;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,SAAS;IACb,QAAQ,CAAS;IACjB,QAAQ,CAA2B;IACnC,WAAW,CAA2B;IAEtC,YACE,OAAgB,EAChB,OAAmC,EACnC,eAA2C;QAE3C,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAA;QACvB,IAAI,CAAC,QAAQ,GAAG,OAAO,IAAI,EAAE,CAAA;QAC7B,IAAI,CAAC,WAAW,GAAG,eAAe,IAAI,IAAI,CAAC,QAAQ,CAAA;IACrD,CAAC;IAED;;;;;;;OAOG;IACH,IAAI,CAAC,GAAY;QACf,OAAO,UAAU,CAAC,GAAG,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,CAAA;IAC7C,CAAC;CACF;AAED,eAAe,SAAS,CAAA"}
@@ -0,0 +1,27 @@
1
+ import type { Grammar } from '../grammar.ts';
2
+ import type { AstNode, JexlValue } from '../types.ts';
3
+ import type Evaluator from './Evaluator.ts';
4
+ /**
5
+ * An AST node lowered to a closure. Calling it with an Evaluator (which carries
6
+ * the context and grammar) produces the node's value.
7
+ */
8
+ export type CompiledNode = (ev: Evaluator) => JexlValue;
9
+ /**
10
+ * Lowers an expression tree into a tree of closures, resolving each node's type
11
+ * and its operator's implementation once, at compile time, rather than
12
+ * re-dispatching on `node.type` for every node on every evaluation. An
13
+ * expression compiled once and evaluated per-item — the usual shape for
14
+ * per-feature config callbacks — pays the dispatch cost once instead of N
15
+ * times.
16
+ *
17
+ * Operators are bound here, so a grammar change after compilation requires
18
+ * recompiling, which is what {@link Expression#compile} already documents.
19
+ * Functions are looked up per call, since they are commonly registered after
20
+ * an expression has been compiled.
21
+ *
22
+ * @param {{}} ast An expression tree, as produced by the Parser
23
+ * @param {{}} grammar The grammar to resolve operators against
24
+ * @returns {function} a closure returning the expression's value
25
+ * @throws {Error} if the tree contains an unrecognized node type
26
+ */
27
+ export declare function compileAst(ast: AstNode, grammar: Grammar): CompiledNode;
@@ -0,0 +1,258 @@
1
+ /*
2
+ * Jexl
3
+ * Copyright 2020 Tom Shawver
4
+ */
5
+ /** Renders an interpolated value for a template literal. */
6
+ function stringify(value) {
7
+ if (value == null) {
8
+ return '';
9
+ }
10
+ if (typeof value === 'string' ||
11
+ typeof value === 'number' ||
12
+ typeof value === 'boolean') {
13
+ return String(value);
14
+ }
15
+ return JSON.stringify(value);
16
+ }
17
+ /**
18
+ * Lowers an expression tree into a tree of closures, resolving each node's type
19
+ * and its operator's implementation once, at compile time, rather than
20
+ * re-dispatching on `node.type` for every node on every evaluation. An
21
+ * expression compiled once and evaluated per-item — the usual shape for
22
+ * per-feature config callbacks — pays the dispatch cost once instead of N
23
+ * times.
24
+ *
25
+ * Operators are bound here, so a grammar change after compilation requires
26
+ * recompiling, which is what {@link Expression#compile} already documents.
27
+ * Functions are looked up per call, since they are commonly registered after
28
+ * an expression has been compiled.
29
+ *
30
+ * @param {{}} ast An expression tree, as produced by the Parser
31
+ * @param {{}} grammar The grammar to resolve operators against
32
+ * @returns {function} a closure returning the expression's value
33
+ * @throws {Error} if the tree contains an unrecognized node type
34
+ */
35
+ export function compileAst(ast, grammar) {
36
+ switch (ast.type) {
37
+ case 'Literal': {
38
+ const { value } = ast;
39
+ return () => value;
40
+ }
41
+ case 'Identifier': {
42
+ const node = ast;
43
+ const name = node.value;
44
+ if (!node.from) {
45
+ return node.relative
46
+ ? (ev) => ev._relContext[name]
47
+ : (ev) => ev._context[name];
48
+ }
49
+ const from = compileAst(node.from, grammar);
50
+ return (ev) => {
51
+ const context = from(ev);
52
+ if (context == null) {
53
+ return undefined;
54
+ }
55
+ // an identifier chained off an array reads through its first element
56
+ const ctx = Array.isArray(context) ? context[0] : context;
57
+ return ctx == null
58
+ ? undefined
59
+ : ctx[name];
60
+ };
61
+ }
62
+ case 'BinaryExpression': {
63
+ const node = ast;
64
+ const op = grammar.elements[node.operator];
65
+ if (op?.type !== 'binaryOp') {
66
+ // matches the tree-walking behaviour: an unknown operator yields
67
+ // undefined without evaluating either operand
68
+ return () => undefined;
69
+ }
70
+ const left = compileAst(node.left, grammar);
71
+ const right = compileAst(node.right, grammar);
72
+ const { evalOnDemand } = op;
73
+ if (evalOnDemand) {
74
+ // operands stay unevaluated behind an `eval` thunk, so operators such
75
+ // as && and || can short-circuit
76
+ return (ev) => evalOnDemand({ eval: () => left(ev) }, { eval: () => right(ev) });
77
+ }
78
+ const fn = op.eval;
79
+ if (!fn) {
80
+ return (ev) => {
81
+ left(ev);
82
+ right(ev);
83
+ return undefined;
84
+ };
85
+ }
86
+ return (ev) => fn(left(ev), right(ev));
87
+ }
88
+ case 'UnaryExpression': {
89
+ const node = ast;
90
+ const right = compileAst(node.right, grammar);
91
+ const elem = grammar.elements[node.operator];
92
+ const fn = elem?.type === 'unaryOp'
93
+ ? elem.eval
94
+ : elem?.type === 'binaryOp'
95
+ ? elem.unaryEval
96
+ : undefined;
97
+ if (!fn) {
98
+ // the operand is still evaluated for an unknown operator, as before
99
+ return (ev) => {
100
+ right(ev);
101
+ return undefined;
102
+ };
103
+ }
104
+ return (ev) => fn(right(ev));
105
+ }
106
+ case 'ConditionalExpression': {
107
+ const node = ast;
108
+ const test = compileAst(node.test, grammar);
109
+ const consequent = node.consequent
110
+ ? compileAst(node.consequent, grammar)
111
+ : undefined;
112
+ const alternate = node.alternate
113
+ ? compileAst(node.alternate, grammar)
114
+ : undefined;
115
+ return (ev) => {
116
+ const res = test(ev);
117
+ if (res) {
118
+ // an omitted consequent ("a ?: b") yields the test result
119
+ return consequent ? consequent(ev) : res;
120
+ }
121
+ return alternate ? alternate(ev) : undefined;
122
+ };
123
+ }
124
+ case 'FilterExpression': {
125
+ const node = ast;
126
+ if (node.relative) {
127
+ // thrown on evaluation rather than compilation, as before
128
+ return () => {
129
+ throw new Error('Relative filter expressions are not supported');
130
+ };
131
+ }
132
+ const subject = compileAst(node.subject, grammar);
133
+ const index = compileAst(node.expr, grammar);
134
+ return (ev) => {
135
+ const subjectVal = subject(ev);
136
+ const indexVal = index(ev);
137
+ if (subjectVal == null) {
138
+ return undefined;
139
+ }
140
+ return typeof indexVal === 'string' || typeof indexVal === 'number'
141
+ ? subjectVal[indexVal]
142
+ : undefined;
143
+ };
144
+ }
145
+ case 'ArrayLiteral': {
146
+ const items = ast.value.map((item) => compileAst(item, grammar));
147
+ const len = items.length;
148
+ return (ev) => {
149
+ const out = new Array(len);
150
+ for (let i = 0; i < len; i++) {
151
+ out[i] = items[i](ev);
152
+ }
153
+ return out;
154
+ };
155
+ }
156
+ case 'ObjectLiteral': {
157
+ const entries = Object.entries(ast.value);
158
+ const keys = entries.map(([key]) => key);
159
+ const values = entries.map(([, node]) => compileAst(node, grammar));
160
+ const len = keys.length;
161
+ return (ev) => {
162
+ const out = {};
163
+ for (let i = 0; i < len; i++) {
164
+ out[keys[i]] = values[i](ev);
165
+ }
166
+ return out;
167
+ };
168
+ }
169
+ case 'TemplateLiteral': {
170
+ // every part renders to a string, so these are narrower than CompiledNode
171
+ const parts = ast.parts.map((part) => {
172
+ if (part.type === 'static') {
173
+ const { value } = part;
174
+ return () => value;
175
+ }
176
+ const expr = compileAst(part.value, grammar);
177
+ return (ev) => stringify(expr(ev));
178
+ });
179
+ return (ev) => {
180
+ let out = '';
181
+ for (const part of parts) {
182
+ out += part(ev);
183
+ }
184
+ return out;
185
+ };
186
+ }
187
+ case 'FunctionCall': {
188
+ const node = ast;
189
+ const { name } = node;
190
+ const args = node.args.map((arg) => compileAst(arg, grammar));
191
+ // hasOwn, so that inherited Object.prototype members such as `toString`
192
+ // and `constructor` aren't callable as Jexl functions. Resolved per call
193
+ // rather than baked in, since functions are routinely registered after
194
+ // an expression has been compiled.
195
+ const lookup = (ev) => {
196
+ const { functions } = ev._grammar;
197
+ if (!Object.hasOwn(functions, name)) {
198
+ throw new Error(`Jexl Function ${name} is not defined.`);
199
+ }
200
+ return functions[name];
201
+ };
202
+ // the small arities are spelled out so an ordinary call allocates no
203
+ // argument array and needs no spread
204
+ switch (args.length) {
205
+ case 0: {
206
+ return (ev) => lookup(ev)();
207
+ }
208
+ case 1: {
209
+ const [a0] = args;
210
+ return (ev) => lookup(ev)(a0(ev));
211
+ }
212
+ case 2: {
213
+ const [a0, a1] = args;
214
+ return (ev) => lookup(ev)(a0(ev), a1(ev));
215
+ }
216
+ case 3: {
217
+ const [a0, a1, a2] = args;
218
+ return (ev) => lookup(ev)(a0(ev), a1(ev), a2(ev));
219
+ }
220
+ default: {
221
+ const len = args.length;
222
+ return (ev) => {
223
+ const vals = new Array(len);
224
+ for (let i = 0; i < len; i++) {
225
+ vals[i] = args[i](ev);
226
+ }
227
+ return lookup(ev)(...vals);
228
+ };
229
+ }
230
+ }
231
+ }
232
+ case 'SequenceExpression': {
233
+ const exprs = ast.expressions.map((expr) => compileAst(expr, grammar));
234
+ const len = exprs.length;
235
+ return (ev) => {
236
+ let last;
237
+ for (let i = 0; i < len; i++) {
238
+ last = exprs[i](ev);
239
+ }
240
+ return last;
241
+ };
242
+ }
243
+ case 'AssignmentExpression': {
244
+ const node = ast;
245
+ const name = node.left.value;
246
+ const right = compileAst(node.right, grammar);
247
+ return (ev) => {
248
+ const value = right(ev);
249
+ ev._context[name] = value;
250
+ return value;
251
+ };
252
+ }
253
+ default: {
254
+ throw new Error(`Corrupt AST: unknown node type '${ast.type}'`);
255
+ }
256
+ }
257
+ }
258
+ //# sourceMappingURL=compile.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compile.js","sourceRoot":"","sources":["../../src/evaluator/compile.ts"],"names":[],"mappings":"AAAA;;;GAGG;AA2BH,4D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,IAAI,CAAC,KAAM,EAAE,OAAO,CAAC,CAAA;YAC9C,OAAO,CAAC,EAAE,EAAE,EAAE;gBACZ,MAAM,KAAK,GAAG,KAAK,CAAC,EAAE,CAAC,CAAA;gBACvB,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,KAAK,CAAA;gBACzB,OAAO,KAAK,CAAA;YACd,CAAC,CAAA;QACH,CAAC;QAED,OAAO,CAAC,CAAC,CAAC;YACR,MAAM,IAAI,KAAK,CAAC,mCAAmC,GAAG,CAAC,IAAI,GAAG,CAAC,CAAA;QACjE,CAAC;IACH,CAAC;AACH,CAAC"}
package/esm/grammar.d.ts CHANGED
@@ -1,24 +1,44 @@
1
+ import type { JexlValue } from './types.ts';
2
+ export type BinaryOpEval = (left: JexlValue, right: JexlValue) => JexlValue;
3
+ export type BinaryOpEvalOnDemand = (left: {
4
+ eval: () => JexlValue;
5
+ }, right: {
6
+ eval: () => JexlValue;
7
+ }) => JexlValue;
1
8
  export interface BinaryOp {
2
9
  type: 'binaryOp';
3
10
  precedence: number;
4
- eval?: (left: any, right: any) => any;
5
- evalOnDemand?: (left: {
6
- eval: () => any;
7
- }, right: {
8
- eval: () => any;
9
- }) => any;
11
+ eval?: BinaryOpEval;
12
+ evalOnDemand?: BinaryOpEvalOnDemand;
13
+ /**
14
+ * Evaluator for the operator's prefix form, if it has one. The Lexer emits a
15
+ * unaryOp token when such an operator appears where an operand is expected,
16
+ * which is how `-x` is distinguished from `a - x`.
17
+ */
18
+ unaryEval?: (right: JexlValue) => JexlValue;
10
19
  }
11
20
  export interface UnaryOp {
12
21
  type: 'unaryOp';
13
22
  precedence: number;
14
- eval: (right: any) => any;
23
+ eval: (right: JexlValue) => JexlValue;
15
24
  }
25
+ /**
26
+ * The punctuation elements of the grammar. Unlike operators these carry no
27
+ * behavior of their own; the Parser's state machine gives them meaning. The
28
+ * type is a literal union rather than `string` so that `GrammarElement` is a
29
+ * discriminated union, letting operator properties be accessed without casts.
30
+ */
16
31
  export interface SimpleElement {
17
- type: string;
32
+ type: 'dot' | 'openBracket' | 'closeBracket' | 'openCurl' | 'closeCurl' | 'colon' | 'comma' | 'openParen' | 'closeParen' | 'question' | 'semicolon';
18
33
  }
19
34
  export type GrammarElement = BinaryOp | UnaryOp | SimpleElement;
20
35
  export interface Grammar {
21
36
  elements: Record<string, GrammarElement>;
22
- functions: Record<string, (...args: any[]) => any>;
37
+ functions: Record<string, (...args: JexlValue[]) => JexlValue>;
23
38
  }
39
+ /**
40
+ * Returns the binding power of a grammar element, or 0 for elements that
41
+ * aren't operators and therefore don't participate in precedence.
42
+ */
43
+ export declare function precedenceOf(elem: GrammarElement | undefined): number;
24
44
  export declare const getGrammar: () => Grammar;
package/esm/grammar.js CHANGED
@@ -2,6 +2,15 @@
2
2
  * Jexl
3
3
  * Copyright 2020 Tom Shawver
4
4
  */
5
+ /**
6
+ * Returns the binding power of a grammar element, or 0 for elements that
7
+ * aren't operators and therefore don't participate in precedence.
8
+ */
9
+ export function precedenceOf(elem) {
10
+ return elem && (elem.type === 'binaryOp' || elem.type === 'unaryOp')
11
+ ? elem.precedence
12
+ : 0;
13
+ }
5
14
  export const getGrammar = () => ({
6
15
  /**
7
16
  * A map of all expression elements to their properties. Note that changes
@@ -28,7 +37,8 @@ export const getGrammar = () => ({
28
37
  '-': {
29
38
  type: 'binaryOp',
30
39
  precedence: 30,
31
- eval: (left, right) => left - right
40
+ eval: (left, right) => left - right,
41
+ unaryEval: (right) => -right
32
42
  },
33
43
  '*': {
34
44
  type: 'binaryOp',
@@ -53,7 +63,7 @@ export const getGrammar = () => ({
53
63
  '^': {
54
64
  type: 'binaryOp',
55
65
  precedence: 50,
56
- eval: (left, right) => Math.pow(left, right)
66
+ eval: (left, right) => left ** right
57
67
  },
58
68
  '==': {
59
69
  type: 'binaryOp',
@@ -112,7 +122,12 @@ export const getGrammar = () => ({
112
122
  precedence: 20,
113
123
  eval: (left, right) => {
114
124
  if (typeof right === 'string') {
115
- return right.includes(left);
125
+ const leftStr = typeof left === 'string' ||
126
+ typeof left === 'number' ||
127
+ typeof left === 'boolean'
128
+ ? String(left)
129
+ : '';
130
+ return right.includes(leftStr);
116
131
  }
117
132
  if (Array.isArray(right)) {
118
133
  return right.includes(left);
@@ -1 +1 @@
1
- {"version":3,"file":"grammar.js","sourceRoot":"","sources":["../src/grammar.ts"],"names":[],"mappings":"AAAA;;;GAGG;AA4BH,MAAM,CAAC,MAAM,UAAU,GAAG,GAAY,EAAE,CAAC,CAAC;IACxC;;;;OAIG;IACH,QAAQ,EAAE;QACR,GAAG,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE;QACpB,GAAG,EAAE,EAAE,IAAI,EAAE,aAAa,EAAE;QAC5B,GAAG,EAAE,EAAE,IAAI,EAAE,cAAc,EAAE;QAC7B,GAAG,EAAE,EAAE,IAAI,EAAE,UAAU,EAAE;QACzB,GAAG,EAAE,EAAE,IAAI,EAAE,WAAW,EAAE;QAC1B,GAAG,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE;QACtB,GAAG,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE;QACtB,GAAG,EAAE,EAAE,IAAI,EAAE,WAAW,EAAE;QAC1B,GAAG,EAAE,EAAE,IAAI,EAAE,YAAY,EAAE;QAC3B,GAAG,EAAE,EAAE,IAAI,EAAE,UAAU,EAAE;QACzB,GAAG,EAAE,EAAE,IAAI,EAAE,WAAW,EAAE;QAC1B,GAAG,EAAE;YACH,IAAI,EAAE,UAAU;YAChB,UAAU,EAAE,EAAE;YACd,IAAI,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,IAAI,GAAG,KAAK;SACpC;QACD,GAAG,EAAE;YACH,IAAI,EAAE,UAAU;YAChB,UAAU,EAAE,EAAE;YACd,IAAI,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,IAAI,GAAG,KAAK;SACpC;QACD,GAAG,EAAE;YACH,IAAI,EAAE,UAAU;YAChB,UAAU,EAAE,E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AAU,EAAE,CAAC;YACb,IAAI,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE;gBACtB,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAA;YACjD,CAAC;SACF;KACF;IAED;;;;;;;;;;;;;OAaG;IACH,SAAS,EAAE,EAAE;CACd,CAAC,CAAA"}
@@ -1,8 +1,6 @@
1
1
  import type Lexer from '../Lexer.ts';
2
+ import type { Grammar } from '../grammar.ts';
2
3
  import type { AstNode, Token } from '../types.ts';
3
- interface Grammar {
4
- elements: Record<string, any>;
5
- }
6
4
  /**
7
5
  * The Parser is a state machine that converts tokens from the {@link Lexer}
8
6
  * into an Abstract Syntax Tree (AST), capable of being evaluated in any
@@ -27,7 +25,7 @@ declare class Parser {
27
25
  _tree: AstNode | null;
28
26
  _exprStr: string;
29
27
  _relative: boolean;
30
- _stopMap: Record<string, any>;
28
+ _stopMap: Record<string, string>;
31
29
  _cursor?: AstNode | null;
32
30
  _subParser?: Parser;
33
31
  _parentStop?: boolean;
@@ -35,7 +33,7 @@ declare class Parser {
35
33
  _nextIdentRelative?: boolean;
36
34
  _curObjKey?: string;
37
35
  _sequenceExpressions?: AstNode[];
38
- constructor(grammar: Grammar, lexer: Lexer, prefix?: string, stopMap?: Record<string, any>);
36
+ constructor(grammar: Grammar, lexer: Lexer, prefix?: string, stopMap?: Record<string, string>);
39
37
  /**
40
38
  * Processes a new token into the AST and manages the transitions of the state
41
39
  * machine.
@@ -46,7 +44,7 @@ declare class Parser {
46
44
  * @returns {boolean|*} the stopState value if this parser encountered a token
47
45
  * in the stopState mapb false if tokens can continue.
48
46
  */
49
- addToken(token: Token): any;
47
+ addToken(token: Token): string | false;
50
48
  /**
51
49
  * Processes an array of tokens iteratively through the {@link #addToken}
52
50
  * function.
@@ -62,7 +60,7 @@ declare class Parser {
62
60
  * @throws {Error} if the parser is not in a state where it's legal to end
63
61
  * the expression, indicating that the expression is incomplete
64
62
  */
65
- complete(): any;
63
+ complete(): AstNode | null;
66
64
  /**
67
65
  * Indicates whether the expression tree contains a relative path identifier.
68
66
  * @returns {boolean} true if a relative identifier exists false otherwise.
@@ -22,6 +22,20 @@ import { states } from "./states.js";
22
22
  * instead of boolean false.
23
23
  */
24
24
  class Parser {
25
+ _grammar;
26
+ _lexer;
27
+ _state;
28
+ _tree;
29
+ _exprStr;
30
+ _relative;
31
+ _stopMap;
32
+ _cursor;
33
+ _subParser;
34
+ _parentStop;
35
+ _nextIdentEncapsulate;
36
+ _nextIdentRelative;
37
+ _curObjKey;
38
+ _sequenceExpressions;
25
39
  constructor(grammar, lexer, prefix, stopMap) {
26
40
  this._grammar = grammar;
27
41
  this._lexer = lexer;
@@ -55,12 +69,20 @@ class Parser {
55
69
  const stopState = this._subParser.addToken(token);
56
70
  if (stopState) {
57
71
  this._endSubExpression();
58
- if (this._parentStop) {
72
+ if (stopState === '_semicolon') {
73
+ handlers.semicolon.call(this);
74
+ }
75
+ else if (this._parentStop) {
59
76
  return stopState;
60
77
  }
61
- this._state = stopState;
78
+ else {
79
+ this._state = stopState;
80
+ }
62
81
  }
63
82
  }
83
+ else if (token.type === 'semicolon' && this._stopMap[token.type]) {
84
+ return this._stopMap[token.type];
85
+ }
64
86
  else if (state.tokenTypes?.[token.type]) {
65
87
  const typeOpts = state.tokenTypes[token.type];
66
88
  let handleFunc = handlers[token.type];
@@ -89,7 +111,9 @@ class Parser {
89
111
  * the {@link Lexer#tokenize} function.
90
112
  */
91
113
  addTokens(tokens) {
92
- tokens.forEach((token) => this.addToken(token));
114
+ for (const token of tokens) {
115
+ this.addToken(token);
116
+ }
93
117
  }
94
118
  /**
95
119
  * Marks this Parser instance as completed and retrieves the full AST.
@@ -107,7 +131,9 @@ class Parser {
107
131
  this._endSubExpression();
108
132
  }
109
133
  if (this._sequenceExpressions) {
110
- this._sequenceExpressions.push(this._tree);
134
+ if (this._tree) {
135
+ this._sequenceExpressions.push(this._tree);
136
+ }
111
137
  const sequence = {
112
138
  type: 'SequenceExpression',
113
139
  expressions: this._sequenceExpressions
@@ -146,7 +172,6 @@ class Parser {
146
172
  this._tree = node;
147
173
  }
148
174
  else {
149
- ;
150
175
  this._cursor.right = node;
151
176
  this._setParent(node, this._cursor);
152
177
  }
@@ -191,6 +216,9 @@ class Parser {
191
216
  this._parentStop = true;
192
217
  endStates = this._stopMap;
193
218
  }
219
+ if (states[this._state].completable && !endStates.semicolon) {
220
+ endStates = { ...endStates, semicolon: '_semicolon' };
221
+ }
194
222
  this._subParser = new Parser(this._grammar, this._lexer, exprStr, endStates);
195
223
  }
196
224
  }