@jbrowse/jexl 3.0.2 → 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.
- package/README.md +11 -2
- package/dist/Expression.d.ts +16 -6
- package/dist/Expression.js +34 -8
- package/dist/Expression.js.map +1 -1
- package/dist/Jexl.d.ts +13 -12
- package/dist/Jexl.js +31 -30
- package/dist/Jexl.js.map +1 -1
- package/dist/Lexer.d.ts +9 -8
- package/dist/Lexer.js +58 -18
- package/dist/Lexer.js.map +1 -1
- package/dist/evaluator/Evaluator.d.ts +20 -34
- package/dist/evaluator/Evaluator.js +19 -64
- package/dist/evaluator/Evaluator.js.map +1 -1
- package/dist/evaluator/compile.d.ts +27 -0
- package/dist/evaluator/compile.js +261 -0
- package/dist/evaluator/compile.js.map +1 -0
- package/dist/grammar.d.ts +29 -9
- package/dist/grammar.js +19 -3
- package/dist/grammar.js.map +1 -1
- package/dist/parser/Parser.d.ts +5 -7
- package/dist/parser/Parser.js +17 -2
- package/dist/parser/Parser.js.map +1 -1
- package/dist/parser/handlers.d.ts +0 -6
- package/dist/parser/handlers.js +54 -46
- package/dist/parser/handlers.js.map +1 -1
- package/dist/parser/states.d.ts +2 -2
- package/dist/parser/states.js +0 -20
- package/dist/parser/states.js.map +1 -1
- package/dist/types.d.ts +18 -7
- package/esm/Expression.d.ts +16 -6
- package/esm/Expression.js +34 -8
- package/esm/Expression.js.map +1 -1
- package/esm/Jexl.d.ts +13 -12
- package/esm/Jexl.js +31 -30
- package/esm/Jexl.js.map +1 -1
- package/esm/Lexer.d.ts +9 -8
- package/esm/Lexer.js +58 -18
- package/esm/Lexer.js.map +1 -1
- package/esm/evaluator/Evaluator.d.ts +20 -34
- package/esm/evaluator/Evaluator.js +19 -31
- package/esm/evaluator/Evaluator.js.map +1 -1
- package/esm/evaluator/compile.d.ts +27 -0
- package/esm/evaluator/compile.js +258 -0
- package/esm/evaluator/compile.js.map +1 -0
- package/esm/grammar.d.ts +29 -9
- package/esm/grammar.js +18 -3
- package/esm/grammar.js.map +1 -1
- package/esm/parser/Parser.d.ts +5 -7
- package/esm/parser/Parser.js +17 -2
- package/esm/parser/Parser.js.map +1 -1
- package/esm/parser/handlers.d.ts +0 -6
- package/esm/parser/handlers.js +54 -45
- package/esm/parser/handlers.js.map +1 -1
- package/esm/parser/states.d.ts +2 -2
- package/esm/parser/states.js +0 -20
- package/esm/parser/states.js.map +1 -1
- package/esm/types.d.ts +18 -7
- package/package.json +25 -28
- package/src/Expression.ts +33 -13
- package/src/Jexl.ts +59 -21
- package/src/Lexer.ts +60 -27
- package/src/evaluator/Evaluator.ts +27 -44
- package/src/evaluator/compile.ts +310 -0
- package/src/grammar.ts +66 -17
- package/src/parser/Parser.ts +24 -20
- package/src/parser/handlers.ts +96 -73
- package/src/parser/states.ts +2 -22
- package/src/types.ts +22 -7
- package/CHANGELOG.md +0 -249
- package/dist/evaluator/handlers.d.ts +0 -112
- package/dist/evaluator/handlers.js +0 -208
- package/dist/evaluator/handlers.js.map +0 -1
- package/esm/evaluator/handlers.d.ts +0 -112
- package/esm/evaluator/handlers.js +0 -194
- package/esm/evaluator/handlers.js.map +0 -1
- package/src/evaluator/handlers.ts +0 -212
package/esm/Jexl.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
* Copyright 2020 Tom Shawver
|
|
4
4
|
*/
|
|
5
5
|
import Expression from "./Expression.js";
|
|
6
|
+
import Lexer from "./Lexer.js";
|
|
6
7
|
import { getGrammar } from "./grammar.js";
|
|
7
8
|
/**
|
|
8
9
|
* Jexl is the Javascript Expression Language, capable of parsing and
|
|
@@ -11,35 +12,25 @@ import { getGrammar } from "./grammar.js";
|
|
|
11
12
|
* @constructor
|
|
12
13
|
*/
|
|
13
14
|
class Jexl {
|
|
15
|
+
_grammar;
|
|
16
|
+
// shared by every Expression this instance creates, so that the expensive
|
|
17
|
+
// element-splitting regex is built once rather than per compile
|
|
18
|
+
_lexer;
|
|
14
19
|
constructor() {
|
|
15
20
|
this._grammar = getGrammar();
|
|
21
|
+
this._lexer = new Lexer(this._grammar);
|
|
16
22
|
this.expr = this.expr.bind(this);
|
|
17
23
|
}
|
|
18
|
-
/**
|
|
19
|
-
* Adds a binary operator to Jexl at the specified precedence. The higher the
|
|
20
|
-
* precedence, the earlier the operator is applied in the order of operations.
|
|
21
|
-
* For example, * has a higher precedence than +, because multiplication comes
|
|
22
|
-
* before division.
|
|
23
|
-
*
|
|
24
|
-
* Please see grammar.js for a listing of all default operators and their
|
|
25
|
-
* precedence values in order to choose the appropriate precedence for the
|
|
26
|
-
* new operator.
|
|
27
|
-
* @param {string} operator The operator string to be added
|
|
28
|
-
* @param {number} precedence The operator's precedence
|
|
29
|
-
* @param {function} fn A function to run to calculate the result. The function
|
|
30
|
-
* will be called with two arguments: left and right, denoting the values
|
|
31
|
-
* on either side of the operator. It should return the resulting value.
|
|
32
|
-
* @param {boolean} [manualEval] If true, the `left` and `right` arguments
|
|
33
|
-
* will be wrapped in objects with an `eval` function. Calling
|
|
34
|
-
* left.eval() or right.eval() will return that operand's actual value.
|
|
35
|
-
* This is useful to conditionally evaluate operands.
|
|
36
|
-
*/
|
|
37
24
|
addBinaryOp(operator, precedence, fn, manualEval) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
25
|
+
// the overloads above pair `fn` with `manualEval`; the implementation
|
|
26
|
+
// signature can't express that correlation, hence the assertions
|
|
27
|
+
this._addGrammarElement(operator, manualEval
|
|
28
|
+
? {
|
|
29
|
+
type: 'binaryOp',
|
|
30
|
+
precedence,
|
|
31
|
+
evalOnDemand: fn
|
|
32
|
+
}
|
|
33
|
+
: { type: 'binaryOp', precedence, eval: fn });
|
|
43
34
|
}
|
|
44
35
|
/**
|
|
45
36
|
* Adds or replaces an expression function in this Jexl instance.
|
|
@@ -72,7 +63,7 @@ class Jexl {
|
|
|
72
63
|
addUnaryOp(operator, fn) {
|
|
73
64
|
this._addGrammarElement(operator, {
|
|
74
65
|
type: 'unaryOp',
|
|
75
|
-
|
|
66
|
+
precedence: Infinity,
|
|
76
67
|
eval: fn
|
|
77
68
|
});
|
|
78
69
|
}
|
|
@@ -95,7 +86,7 @@ class Jexl {
|
|
|
95
86
|
* @returns {Expression} The Expression object representing the given string
|
|
96
87
|
*/
|
|
97
88
|
createExpression(expression) {
|
|
98
|
-
return new Expression(this._grammar, expression);
|
|
89
|
+
return new Expression(this._grammar, expression, this._lexer);
|
|
99
90
|
}
|
|
100
91
|
/**
|
|
101
92
|
* Retrieves a previously set expression function.
|
|
@@ -125,10 +116,18 @@ class Jexl {
|
|
|
125
116
|
*/
|
|
126
117
|
expr(strs, ...args) {
|
|
127
118
|
let exprStr = '';
|
|
128
|
-
for (
|
|
129
|
-
exprStr +=
|
|
119
|
+
for (const [idx, str] of strs.entries()) {
|
|
120
|
+
exprStr += str;
|
|
130
121
|
if (idx < args.length) {
|
|
131
|
-
|
|
122
|
+
const arg = args[idx];
|
|
123
|
+
if (typeof arg === 'string' ||
|
|
124
|
+
typeof arg === 'number' ||
|
|
125
|
+
typeof arg === 'boolean') {
|
|
126
|
+
exprStr += String(arg);
|
|
127
|
+
}
|
|
128
|
+
else if (arg != null) {
|
|
129
|
+
exprStr += JSON.stringify(arg);
|
|
130
|
+
}
|
|
132
131
|
}
|
|
133
132
|
}
|
|
134
133
|
return this.createExpression(exprStr);
|
|
@@ -139,8 +138,9 @@ class Jexl {
|
|
|
139
138
|
*/
|
|
140
139
|
removeOp(operator) {
|
|
141
140
|
const elem = this._grammar.elements[operator];
|
|
142
|
-
if (elem
|
|
141
|
+
if (elem?.type === 'binaryOp' || elem?.type === 'unaryOp') {
|
|
143
142
|
Reflect.deleteProperty(this._grammar.elements, operator);
|
|
143
|
+
this._lexer._clearCache();
|
|
144
144
|
}
|
|
145
145
|
}
|
|
146
146
|
/**
|
|
@@ -152,6 +152,7 @@ class Jexl {
|
|
|
152
152
|
*/
|
|
153
153
|
_addGrammarElement(str, obj) {
|
|
154
154
|
this._grammar.elements[str] = obj;
|
|
155
|
+
this._lexer._clearCache();
|
|
155
156
|
}
|
|
156
157
|
}
|
|
157
158
|
const jexlInstance = new Jexl();
|
package/esm/Jexl.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Jexl.js","sourceRoot":"","sources":["../src/Jexl.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,UAAU,MAAM,iBAAiB,CAAA;AACxC,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;
|
|
1
|
+
{"version":3,"file":"Jexl.js","sourceRoot":"","sources":["../src/Jexl.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,UAAU,MAAM,iBAAiB,CAAA;AACxC,OAAO,KAAK,MAAM,YAAY,CAAA;AAC9B,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AAUzC;;;;;GAKG;AACH,MAAM,IAAI;IACR,QAAQ,CAAS;IACjB,0EAA0E;IAC1E,gEAAgE;IAChE,MAAM,CAAO;IAEb;QACE,IAAI,CAAC,QAAQ,GAAG,UAAU,EAAE,CAAA;QAC5B,IAAI,CAAC,MAAM,GAAG,IAAI,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;QACtC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IAClC,CAAC;IAiCD,WAAW,CACT,QAAgB,EAChB,UAAkB,EAClB,EAAuC,EACvC,UAAoB;QAEpB,sEAAsE;QACtE,iEAAiE;QACjE,IAAI,CAAC,kBAAkB,CACrB,QAAQ,EACR,UAAU;YACR,CAAC,CAAC;gBACE,IAAI,EAAE,UAAU;gBAChB,UAAU;gBACV,YAAY,EAAE,EAA0B;aACzC;YACH,CAAC,CAAC,EAAE,IAAI,EAAE,UAAU,EAAE,UAAU,EAAE,IAAI,EAAE,EAAkB,EAAE,CAC/D,CAAA;IACH,CAAC;I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;AAC3B,OAAO,EAAE,IAAI,EAAE,CAAA"}
|
package/esm/Lexer.d.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
elements: Record<string, any>;
|
|
4
|
-
}
|
|
1
|
+
import type { Grammar } from './grammar.ts';
|
|
2
|
+
import type { TemplatePart, Token } from './types.ts';
|
|
5
3
|
/**
|
|
6
4
|
* Lexer is a collection of stateless, statically-accessed functions for the
|
|
7
5
|
* lexical parsing of a Jexl string. Its responsibility is to identify the
|
|
@@ -17,6 +15,12 @@ declare class Lexer {
|
|
|
17
15
|
_splitRegex?: RegExp;
|
|
18
16
|
_escQuoteRegexCache: Map<string, RegExp>;
|
|
19
17
|
constructor(grammar: Grammar);
|
|
18
|
+
/**
|
|
19
|
+
* Discards the memoized split regex, so that it is rebuilt on the next
|
|
20
|
+
* tokenize. Must be called whenever the grammar's elements change, since the
|
|
21
|
+
* regex is derived from their keys.
|
|
22
|
+
*/
|
|
23
|
+
_clearCache(): void;
|
|
20
24
|
/**
|
|
21
25
|
* Splits a Jexl expression string into an array of expression elements.
|
|
22
26
|
* @param {string} str A Jexl expression string
|
|
@@ -121,10 +125,7 @@ declare class Lexer {
|
|
|
121
125
|
* @private
|
|
122
126
|
*/
|
|
123
127
|
_unquote(str: string): string;
|
|
124
|
-
_parseTemplateString(str: string):
|
|
125
|
-
type: "static" | "interpolation";
|
|
126
|
-
value: string;
|
|
127
|
-
}[];
|
|
128
|
+
_parseTemplateString(str: string): TemplatePart[];
|
|
128
129
|
_unescapeTemplateString(str: string): string;
|
|
129
130
|
}
|
|
130
131
|
export default Lexer;
|
package/esm/Lexer.js
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
const numericRegex = /^-?(?:(?:[0-9]*\.[0-9]+)|[0-9]+)$/;
|
|
6
6
|
const identRegex = /^[a-zA-Zа-яА-Я_\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u00FF$][a-zA-Zа-яА-Я0-9_\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u00FF$]*$/;
|
|
7
|
-
const escEscRegex =
|
|
7
|
+
const escEscRegex = /\\\\/g;
|
|
8
8
|
const whitespaceRegex = /^\s*$/;
|
|
9
9
|
const preOpRegexElems = [
|
|
10
10
|
// Template strings
|
|
@@ -24,6 +24,11 @@ const postOpRegexElems = [
|
|
|
24
24
|
// Numerics (without negative symbol)
|
|
25
25
|
String.raw `(?:(?:[0-9]*\.[0-9]+)|[0-9]+)`
|
|
26
26
|
];
|
|
27
|
+
const unaryMinusToken = () => ({
|
|
28
|
+
type: 'unaryOp',
|
|
29
|
+
value: '-',
|
|
30
|
+
raw: '-'
|
|
31
|
+
});
|
|
27
32
|
const minusNegatesAfter = new Set([
|
|
28
33
|
'binaryOp',
|
|
29
34
|
'unaryOp',
|
|
@@ -45,10 +50,20 @@ const minusNegatesAfter = new Set([
|
|
|
45
50
|
* @type {{}}
|
|
46
51
|
*/
|
|
47
52
|
class Lexer {
|
|
53
|
+
_grammar;
|
|
54
|
+
_splitRegex;
|
|
55
|
+
_escQuoteRegexCache = new Map();
|
|
48
56
|
constructor(grammar) {
|
|
49
|
-
this._escQuoteRegexCache = new Map();
|
|
50
57
|
this._grammar = grammar;
|
|
51
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* Discards the memoized split regex, so that it is rebuilt on the next
|
|
61
|
+
* tokenize. Must be called whenever the grammar's elements change, since the
|
|
62
|
+
* regex is derived from their keys.
|
|
63
|
+
*/
|
|
64
|
+
_clearCache() {
|
|
65
|
+
this._splitRegex = undefined;
|
|
66
|
+
}
|
|
52
67
|
/**
|
|
53
68
|
* Splits a Jexl expression string into an array of expression elements.
|
|
54
69
|
* @param {string} str A Jexl expression string
|
|
@@ -72,21 +87,35 @@ class Lexer {
|
|
|
72
87
|
getTokens(elements) {
|
|
73
88
|
const tokens = [];
|
|
74
89
|
let negate = false;
|
|
75
|
-
for (
|
|
76
|
-
if (this._isWhitespace(
|
|
77
|
-
|
|
78
|
-
|
|
90
|
+
for (const element of elements) {
|
|
91
|
+
if (this._isWhitespace(element)) {
|
|
92
|
+
const last = tokens.at(-1);
|
|
93
|
+
if (last) {
|
|
94
|
+
last.raw += element;
|
|
79
95
|
}
|
|
80
96
|
}
|
|
81
|
-
else if (
|
|
97
|
+
else if (element === '-' && this._isNegative(tokens)) {
|
|
98
|
+
// a second prefix minus in a row ("- -x"): emit the pending one as a
|
|
99
|
+
// unary operator so this one can negate whatever comes next
|
|
100
|
+
if (negate) {
|
|
101
|
+
tokens.push(unaryMinusToken());
|
|
102
|
+
}
|
|
82
103
|
negate = true;
|
|
83
104
|
}
|
|
84
|
-
else {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
105
|
+
else if (negate) {
|
|
106
|
+
negate = false;
|
|
107
|
+
if (numericRegex.exec(element)) {
|
|
108
|
+
// fold the sign into the number, so "-1" stays a single literal
|
|
109
|
+
tokens.push(this._createToken('-' + element));
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
// anything else gets a standalone prefix operator, letting "-x",
|
|
113
|
+
// "-(a + b)" and "-foo.bar" negate a computed value
|
|
114
|
+
tokens.push(unaryMinusToken(), this._createToken(element));
|
|
88
115
|
}
|
|
89
|
-
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
tokens.push(this._createToken(element));
|
|
90
119
|
}
|
|
91
120
|
}
|
|
92
121
|
// Catch a - at the end of the string. Let the parser handle that issue.
|
|
@@ -156,7 +185,7 @@ class Lexer {
|
|
|
156
185
|
else if (element === 'true' || element === 'false') {
|
|
157
186
|
token.value = element === 'true';
|
|
158
187
|
}
|
|
159
|
-
else if (this._grammar.elements
|
|
188
|
+
else if (Object.hasOwn(this._grammar.elements, element)) {
|
|
160
189
|
token.type = this._grammar.elements[element].type;
|
|
161
190
|
}
|
|
162
191
|
else if (identRegex.exec(element)) {
|
|
@@ -254,8 +283,8 @@ class Lexer {
|
|
|
254
283
|
}
|
|
255
284
|
return str
|
|
256
285
|
.slice(1, -1)
|
|
257
|
-
.
|
|
258
|
-
.
|
|
286
|
+
.replaceAll(escQuoteRegex, quote)
|
|
287
|
+
.replaceAll(escEscRegex, '\\');
|
|
259
288
|
}
|
|
260
289
|
_parseTemplateString(str) {
|
|
261
290
|
const parts = [];
|
|
@@ -277,14 +306,25 @@ class Lexer {
|
|
|
277
306
|
const interpStart = current + 2;
|
|
278
307
|
current += 2;
|
|
279
308
|
while (current < str.length && braceDepth > 0) {
|
|
280
|
-
|
|
309
|
+
const char = str[current];
|
|
310
|
+
if (char === '\\') {
|
|
281
311
|
current += 2;
|
|
282
312
|
continue;
|
|
283
313
|
}
|
|
284
|
-
|
|
314
|
+
// skip over string literals so that braces inside them, as in
|
|
315
|
+
// `${ f('}') }`, don't unbalance the depth count
|
|
316
|
+
if (char === '"' || char === "'") {
|
|
317
|
+
current++;
|
|
318
|
+
while (current < str.length && str[current] !== char) {
|
|
319
|
+
current += str[current] === '\\' ? 2 : 1;
|
|
320
|
+
}
|
|
321
|
+
current++;
|
|
322
|
+
continue;
|
|
323
|
+
}
|
|
324
|
+
if (char === '{') {
|
|
285
325
|
braceDepth++;
|
|
286
326
|
}
|
|
287
|
-
if (
|
|
327
|
+
else if (char === '}') {
|
|
288
328
|
braceDepth--;
|
|
289
329
|
}
|
|
290
330
|
current++;
|
package/esm/Lexer.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Lexer.js","sourceRoot":"","sources":["../src/Lexer.ts"],"names":[],"mappings":"AAAA;;;GAGG;
|
|
1
|
+
{"version":3,"file":"Lexer.js","sourceRoot":"","sources":["../src/Lexer.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAKH,MAAM,YAAY,GAAG,mCAAmC,CAAA;AACxD,MAAM,UAAU,GACd,sHAAsH,CAAA;AACxH,MAAM,WAAW,GAAG,OAAO,CAAA;AAC3B,MAAM,eAAe,GAAG,OAAO,CAAA;AAC/B,MAAM,e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UAAU,EAAE,CAAA;oBACd,CAAC;yBAAM,IAAI,IAAI,KAAK,GAAG,EAAE,CAAC;wBACxB,UAAU,EAAE,CAAA;oBACd,CAAC;oBACD,OAAO,EAAE,CAAA;gBACX,CAAC;gBAED,IAAI,UAAU,KAAK,CAAC,EAAE,CAAC;oBACrB,MAAM,IAAI,KAAK,CAAC,8CAA8C,GAAG,EAAE,CAAC,CAAA;gBACtE,CAAC;gBAED,KAAK,CAAC,IAAI,CAAC;oBACT,IAAI,EAAE,eAAe;oBACrB,KAAK,EAAE,GAAG,CAAC,KAAK,CAAC,WAAW,EAAE,OAAO,GAAG,CAAC,CAAC;iBAC3C,CAAC,CAAA;gBAEF,WAAW,GAAG,OAAO,CAAA;YACvB,CAAC;iBAAM,CAAC;gBACN,OAAO,EAAE,CAAA;YACX,CAAC;QACH,CAAC;QAED,IAAI,OAAO,GAAG,WAAW,EAAE,CAAC;YAC1B,KAAK,CAAC,IAAI,CAAC;gBACT,IAAI,EAAE,QAAQ;gBACd,KAAK,EAAE,GAAG,CAAC,KAAK,CAAC,WAAW,EAAE,OAAO,CAAC;aACvC,CAAC,CAAA;QACJ,CAAC;QAED,OAAO,KAAK,CAAA;IACd,CAAC;IAED,uBAAuB,CAAC,GAAW;QACjC,OAAO,GAAG;aACP,UAAU,CAAC,KAAK,EAAE,GAAG,CAAC;aACtB,UAAU,CAAC,MAAM,CAAC,GAAG,CAAA,IAAI,EAAE,GAAG,CAAC;aAC/B,UAAU,CAAC,MAAM,EAAE,IAAI,CAAC,CAAA;IAC7B,CAAC;CACF;AAED,eAAe,KAAK,CAAA"}
|
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
elements: Record<string, any>;
|
|
4
|
-
[key: string]: any;
|
|
5
|
-
}
|
|
1
|
+
import type { Grammar } from '../grammar.ts';
|
|
2
|
+
import type { AstNode, JexlValue } from '../types.ts';
|
|
6
3
|
/**
|
|
7
|
-
* The Evaluator
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
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
|
|
4
|
+
* The Evaluator carries everything an expression is evaluated against: the
|
|
5
|
+
* grammar, the context, and a relative context to be used as the root for
|
|
6
|
+
* relative identifiers. When any of these change, a new instance is required.
|
|
7
|
+
* However, a single instance can be used to evaluate many different
|
|
13
8
|
* expressions, and does not have to be reinstantiated for each.
|
|
9
|
+
*
|
|
10
|
+
* The work of evaluation itself lives in {@link compileAst}, which lowers a
|
|
11
|
+
* tree to closures. Prefer compiling once and reusing the result — that is what
|
|
12
|
+
* {@link Expression} does — over calling {@link #eval}, which compiles the tree
|
|
13
|
+
* it is given every time.
|
|
14
|
+
*
|
|
14
15
|
* @param {{}} grammar A grammar object against which to evaluate the expression
|
|
15
16
|
* tree
|
|
16
17
|
* @param {{}} [context] A map of variable keys to their values. This will be
|
|
@@ -20,32 +21,17 @@ interface Grammar {
|
|
|
20
21
|
*/
|
|
21
22
|
declare class Evaluator {
|
|
22
23
|
_grammar: Grammar;
|
|
23
|
-
_context:
|
|
24
|
-
_relContext:
|
|
25
|
-
constructor(grammar: Grammar, context?:
|
|
24
|
+
_context: Record<string, JexlValue>;
|
|
25
|
+
_relContext: Record<string, JexlValue>;
|
|
26
|
+
constructor(grammar: Grammar, context?: Record<string, JexlValue>, relativeContext?: Record<string, JexlValue>);
|
|
26
27
|
/**
|
|
27
|
-
*
|
|
28
|
+
* Compiles and evaluates an expression tree within the configured context.
|
|
29
|
+
* Convenient for a one-shot evaluation; for repeated evaluation of the same
|
|
30
|
+
* tree, hold onto the closure from {@link compileAst} instead so the tree is
|
|
31
|
+
* only lowered once.
|
|
28
32
|
* @param {{}} ast An expression tree object
|
|
29
33
|
* @returns {*} the resulting value of the expression.
|
|
30
34
|
*/
|
|
31
|
-
eval(ast: AstNode):
|
|
32
|
-
/**
|
|
33
|
-
* Evaluates each expression within an array, and delivers the response as an
|
|
34
|
-
* array with the resulting values at the same indexes as their originating
|
|
35
|
-
* expressions.
|
|
36
|
-
* @param {Array<string>} arr An array of expression strings to be evaluated
|
|
37
|
-
* @returns {Array<{}>} the result array
|
|
38
|
-
*/
|
|
39
|
-
evalArray(arr: AstNode[]): any[];
|
|
40
|
-
/**
|
|
41
|
-
* Evaluates each expression within a map, and delivers the response as a map
|
|
42
|
-
* with the same keys, but with the evaluated result for each as their value.
|
|
43
|
-
* @param {{}} map A map of expression names to expression trees to be
|
|
44
|
-
* evaluated
|
|
45
|
-
* @returns {{}} the result map.
|
|
46
|
-
*/
|
|
47
|
-
evalMap(map: Record<string, AstNode>): {
|
|
48
|
-
[k: string]: any;
|
|
49
|
-
};
|
|
35
|
+
eval(ast: AstNode): JexlValue;
|
|
50
36
|
}
|
|
51
37
|
export default Evaluator;
|
|
@@ -2,15 +2,19 @@
|
|
|
2
2
|
* Jexl
|
|
3
3
|
* Copyright 2020 Tom Shawver
|
|
4
4
|
*/
|
|
5
|
-
import
|
|
5
|
+
import { compileAst } from "./compile.js";
|
|
6
6
|
/**
|
|
7
|
-
* The Evaluator
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
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
|
-
*
|
|
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
|
|
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,
|
|
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;
|