@jbrowse/jexl 3.1.0 → 3.3.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 +15 -0
- package/dist/Expression.d.ts +3 -1
- package/dist/Expression.js +3 -8
- package/dist/Expression.js.map +1 -1
- package/dist/Jexl.d.ts +6 -6
- package/dist/Jexl.js +7 -1
- package/dist/Jexl.js.map +1 -1
- package/dist/Lexer.d.ts +0 -1
- package/dist/Lexer.js +5 -8
- package/dist/Lexer.js.map +1 -1
- package/dist/evaluator/compile.js +34 -16
- package/dist/evaluator/compile.js.map +1 -1
- package/dist/grammar.d.ts +19 -2
- package/dist/grammar.js +8 -8
- package/dist/grammar.js.map +1 -1
- package/dist/parser/Parser.d.ts +2 -1
- package/dist/parser/Parser.js +29 -12
- package/dist/parser/Parser.js.map +1 -1
- package/dist/parser/handlers.d.ts +8 -2
- package/dist/parser/handlers.js +32 -7
- package/dist/parser/handlers.js.map +1 -1
- package/dist/parser/states.js +19 -20
- package/dist/parser/states.js.map +1 -1
- package/esm/Expression.d.ts +3 -1
- package/esm/Expression.js +3 -8
- package/esm/Expression.js.map +1 -1
- package/esm/Jexl.d.ts +6 -6
- package/esm/Jexl.js +7 -1
- package/esm/Jexl.js.map +1 -1
- package/esm/Lexer.d.ts +0 -1
- package/esm/Lexer.js +5 -8
- package/esm/Lexer.js.map +1 -1
- package/esm/evaluator/compile.js +34 -16
- package/esm/evaluator/compile.js.map +1 -1
- package/esm/grammar.d.ts +19 -2
- package/esm/grammar.js +8 -8
- package/esm/grammar.js.map +1 -1
- package/esm/parser/Parser.d.ts +2 -1
- package/esm/parser/Parser.js +29 -12
- package/esm/parser/Parser.js.map +1 -1
- package/esm/parser/handlers.d.ts +8 -2
- package/esm/parser/handlers.js +29 -7
- package/esm/parser/handlers.js.map +1 -1
- package/esm/parser/states.js +19 -20
- package/esm/parser/states.js.map +1 -1
- package/package.json +1 -1
- package/src/Expression.ts +3 -9
- package/src/Jexl.ts +18 -9
- package/src/Lexer.ts +5 -8
- package/src/evaluator/compile.ts +51 -44
- package/src/grammar.ts +30 -11
- package/src/parser/Parser.ts +35 -17
- package/src/parser/handlers.ts +33 -10
- package/src/parser/states.ts +20 -20
package/README.md
CHANGED
|
@@ -129,6 +129,21 @@ jexl.eval('max(temperature, threshold)')
|
|
|
129
129
|
// evaluates with context
|
|
130
130
|
```
|
|
131
131
|
|
|
132
|
+
Functions live in one global pool and have no receiver, so a call written
|
|
133
|
+
against a value passes that value as the first argument: `a.b(x)` means
|
|
134
|
+
`b(a, x)`. Since functions are conventionally written to take their subject
|
|
135
|
+
first, the two spellings read differently but do the same thing:
|
|
136
|
+
|
|
137
|
+
```javascript
|
|
138
|
+
jexl.addFunction('split', (str, sep) => String(str ?? '').split(sep))
|
|
139
|
+
|
|
140
|
+
jexl.eval("refName.split(' ')[0]", { refName: 'chr1 description' }) // "chr1"
|
|
141
|
+
jexl.eval("split(refName, ' ')[0]", { refName: 'chr1 description' }) // "chr1"
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
Note that this is a naming convention, not method dispatch: `a.b(x)` calls the
|
|
145
|
+
function named `b` in the pool, never a method on the value of `a`.
|
|
146
|
+
|
|
132
147
|
### Variable Assignment
|
|
133
148
|
|
|
134
149
|
Assign values to variables using `=` (no `let`, `var`, or `const` needed). Assignments mutate the context and return the assigned value:
|
package/dist/Expression.d.ts
CHANGED
|
@@ -5,6 +5,9 @@ import type { AstNode } from './types.ts';
|
|
|
5
5
|
declare class Expression {
|
|
6
6
|
_grammar: Grammar;
|
|
7
7
|
_exprStr: string;
|
|
8
|
+
/** The parsed tree, exposed for inspection. Legitimately null for an
|
|
9
|
+
* expression with no tokens, which is why compilation is tracked by
|
|
10
|
+
* `_compiled` rather than by this being set. */
|
|
8
11
|
_ast: AstNode | null;
|
|
9
12
|
_lexer: Lexer;
|
|
10
13
|
_compiled: boolean;
|
|
@@ -33,6 +36,5 @@ declare class Expression {
|
|
|
33
36
|
* @throws {*} on error
|
|
34
37
|
*/
|
|
35
38
|
eval(context?: {}): import("./types.ts").JexlValue;
|
|
36
|
-
_getAst(): AstNode | null;
|
|
37
39
|
}
|
|
38
40
|
export default Expression;
|
package/dist/Expression.js
CHANGED
|
@@ -14,6 +14,9 @@ const Parser_ts_1 = __importDefault(require("./parser/Parser.js"));
|
|
|
14
14
|
class Expression {
|
|
15
15
|
_grammar;
|
|
16
16
|
_exprStr;
|
|
17
|
+
/** The parsed tree, exposed for inspection. Legitimately null for an
|
|
18
|
+
* expression with no tokens, which is why compilation is tracked by
|
|
19
|
+
* `_compiled` rather than by this being set. */
|
|
17
20
|
_ast;
|
|
18
21
|
_lexer;
|
|
19
22
|
_compiled = false;
|
|
@@ -66,14 +69,6 @@ class Expression {
|
|
|
66
69
|
}
|
|
67
70
|
return this._fn(new Evaluator_ts_1.default(this._grammar, context));
|
|
68
71
|
}
|
|
69
|
-
_getAst() {
|
|
70
|
-
// tracked separately from _ast, which is legitimately null for an
|
|
71
|
-
// expression with no tokens and would otherwise recompile on every eval
|
|
72
|
-
if (!this._compiled) {
|
|
73
|
-
this.compile();
|
|
74
|
-
}
|
|
75
|
-
return this._ast;
|
|
76
|
-
}
|
|
77
72
|
}
|
|
78
73
|
exports.default = Expression;
|
|
79
74
|
//# sourceMappingURL=Expression.js.map
|
package/dist/Expression.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Expression.js","sourceRoot":"","sources":["../src/Expression.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;;AAEH,0DAA8B;AAC9B,4EAAgD;AAChD,uDAAmD;AACnD,mEAAuC;AAMvC,MAAM,UAAU;IACd,QAAQ,CAAS;IACjB,QAAQ,CAAQ;IAChB,IAAI,CAAgB;IACpB,MAAM,CAAO;IACb,SAAS,GAAG,KAAK,CAAA;IACjB,2EAA2E;IAC3E,GAAG,GAAwB,IAAI,CAAA;IAE/B;;;;;;;OAOG;IACH,YAAY,OAAgB,EAAE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I,CAAC,SAAS,EAAE,CAAC;YACpB,IAAI,CAAC,OAAO,EAAE,CAAA;QAChB,CAAC;QACD,qEAAqE;QACrE,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC;YACd,OAAO,SAAS,CAAA;QAClB,CAAC;QACD,OAAO,IAAI,CAAC,GAAG,CAAC,IAAI,sBAAS,CAAC,IAAI,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC,CAAA;IACxD,CAAC;CACF;AAED,kBAAe,UAAU,CAAA"}
|
package/dist/Jexl.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import Expression from './Expression.ts';
|
|
2
2
|
import Lexer from './Lexer.ts';
|
|
3
|
-
import type {
|
|
3
|
+
import type { BinaryOpEvalOnDemand, Grammar, GrammarElement, GrammarFn, UncheckedFn } from './grammar.ts';
|
|
4
4
|
import type { JexlValue } from './types.ts';
|
|
5
5
|
/**
|
|
6
6
|
* Jexl is the Javascript Expression Language, capable of parsing and
|
|
@@ -31,7 +31,7 @@ declare class Jexl {
|
|
|
31
31
|
* left.eval() or right.eval() will return that operand's actual value.
|
|
32
32
|
* This is useful to conditionally evaluate operands.
|
|
33
33
|
*/
|
|
34
|
-
addBinaryOp(operator: string, precedence: number, fn:
|
|
34
|
+
addBinaryOp(operator: string, precedence: number, fn: UncheckedFn, manualEval?: false): void;
|
|
35
35
|
addBinaryOp(operator: string, precedence: number, fn: BinaryOpEvalOnDemand, manualEval: true): void;
|
|
36
36
|
/**
|
|
37
37
|
* Adds or replaces an expression function in this Jexl instance.
|
|
@@ -41,14 +41,14 @@ declare class Jexl {
|
|
|
41
41
|
* expression function is invoked. It will be provided with each argument
|
|
42
42
|
* supplied in the expression, in the same order.
|
|
43
43
|
*/
|
|
44
|
-
addFunction(name: string, fn:
|
|
44
|
+
addFunction(name: string, fn: UncheckedFn): void;
|
|
45
45
|
/**
|
|
46
46
|
* Syntactic sugar for calling {@link #addFunction} repeatedly. This function
|
|
47
47
|
* accepts a map of one or more expression function names to their javascript
|
|
48
48
|
* function counterpart.
|
|
49
49
|
* @param {{}} map A map of expression function names to javascript functions
|
|
50
50
|
*/
|
|
51
|
-
addFunctions(map: Record<string,
|
|
51
|
+
addFunctions(map: Record<string, UncheckedFn>): void;
|
|
52
52
|
/**
|
|
53
53
|
* Adds a unary operator to Jexl. Unary operators are currently only supported
|
|
54
54
|
* on the left side of the value on which it will operate.
|
|
@@ -57,7 +57,7 @@ declare class Jexl {
|
|
|
57
57
|
* will be called with one argument: the literal value to the right of the
|
|
58
58
|
* operator. It should return the resulting value.
|
|
59
59
|
*/
|
|
60
|
-
addUnaryOp(operator: string, fn:
|
|
60
|
+
addUnaryOp(operator: string, fn: UncheckedFn): void;
|
|
61
61
|
/**
|
|
62
62
|
* Creates an Expression object from the given Jexl expression string, and
|
|
63
63
|
* immediately compiles it. The returned Expression object can then be
|
|
@@ -79,7 +79,7 @@ declare class Jexl {
|
|
|
79
79
|
* @param {string} name The name of the expression function
|
|
80
80
|
* @returns {function} The expression function
|
|
81
81
|
*/
|
|
82
|
-
getFunction(name: string):
|
|
82
|
+
getFunction(name: string): GrammarFn | undefined;
|
|
83
83
|
/**
|
|
84
84
|
* Evaluates a Jexl string within an optional context.
|
|
85
85
|
* @param {string} expression The Jexl expression to be evaluated
|
package/dist/Jexl.js
CHANGED
|
@@ -36,7 +36,11 @@ class Jexl {
|
|
|
36
36
|
precedence,
|
|
37
37
|
evalOnDemand: fn
|
|
38
38
|
}
|
|
39
|
-
: {
|
|
39
|
+
: {
|
|
40
|
+
type: 'binaryOp',
|
|
41
|
+
precedence,
|
|
42
|
+
eval: fn
|
|
43
|
+
});
|
|
40
44
|
}
|
|
41
45
|
/**
|
|
42
46
|
* Adds or replaces an expression function in this Jexl instance.
|
|
@@ -47,6 +51,8 @@ class Jexl {
|
|
|
47
51
|
* supplied in the expression, in the same order.
|
|
48
52
|
*/
|
|
49
53
|
addFunction(name, fn) {
|
|
54
|
+
// jexl hands the function whatever the operands evaluated to, which it
|
|
55
|
+
// cannot reconcile with the caller's parameter types — see UncheckedFn
|
|
50
56
|
this._grammar.functions[name] = fn;
|
|
51
57
|
}
|
|
52
58
|
/**
|
package/dist/Jexl.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Jexl.js","sourceRoot":"","sources":["../src/Jexl.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;;;AAEH,oEAAwC;AACxC,0DAA8B;AAC9B,6CAAyC;
|
|
1
|
+
{"version":3,"file":"Jexl.js","sourceRoot":"","sources":["../src/Jexl.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;;;AAEH,oEAAwC;AACxC,0DAA8B;AAC9B,6CAAyC;AAazC;;;;;GAKG;AACH,MAAM,IAAI;IACR,QAAQ,CAAS;IACjB,0EAA0E;IAC1E,gEAAgE;IAChE,MAAM,CAAO;IAEb;QACE,IAAI,CAAC,QAAQ,GAAG,IAAA,uBAAU,GAAE,CAAA;QAC5B,IAAI,CAAC,MAAM,GAAG,IAAI,kBAAK,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,EAAsC,EACtC,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;gBACE,IAAI,EAAE,UAAU;gBAChB,UAAU;gBACV,IAAI,EAAE,EAA6B;aACpC,CACN,CAAA;IACH,CAAC;IAED;;;;;;;OAOG;IACH,WAAW,CAAC,IAAY,EAAE,EAAe;QACvC,uEAAuE;QACvE,uEAAuE;QACvE,IAAI,CAAC,QAAQ,CAAC,S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, Token } from './types.ts';
|
|
|
13
13
|
declare class Lexer {
|
|
14
14
|
_grammar: Grammar;
|
|
15
15
|
_splitRegex?: RegExp;
|
|
16
|
-
_escQuoteRegexCache: Map<string, RegExp>;
|
|
17
16
|
constructor(grammar: Grammar);
|
|
18
17
|
/**
|
|
19
18
|
* Discards the memoized split regex, so that it is rebuilt on the next
|
package/dist/Lexer.js
CHANGED
|
@@ -7,6 +7,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
7
7
|
const numericRegex = /^-?(?:(?:[0-9]*\.[0-9]+)|[0-9]+)$/;
|
|
8
8
|
const identRegex = /^[a-zA-Zа-яА-Я_\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u00FF$][a-zA-Zа-яА-Я0-9_\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u00FF$]*$/;
|
|
9
9
|
const escEscRegex = /\\\\/g;
|
|
10
|
+
// a string literal opens with one of exactly two quote characters, so the two
|
|
11
|
+
// unescaping regexes can just be named rather than built and cached per quote
|
|
12
|
+
const escQuoteRegex = { "'": /\\'/g, '"': /\\"/g };
|
|
10
13
|
const whitespaceRegex = /^\s*$/;
|
|
11
14
|
const preOpRegexElems = [
|
|
12
15
|
// Template strings
|
|
@@ -54,7 +57,6 @@ const minusNegatesAfter = new Set([
|
|
|
54
57
|
class Lexer {
|
|
55
58
|
_grammar;
|
|
56
59
|
_splitRegex;
|
|
57
|
-
_escQuoteRegexCache = new Map();
|
|
58
60
|
constructor(grammar) {
|
|
59
61
|
this._grammar = grammar;
|
|
60
62
|
}
|
|
@@ -277,15 +279,10 @@ class Lexer {
|
|
|
277
279
|
* @private
|
|
278
280
|
*/
|
|
279
281
|
_unquote(str) {
|
|
280
|
-
const quote = str
|
|
281
|
-
let escQuoteRegex = this._escQuoteRegexCache.get(quote);
|
|
282
|
-
if (!escQuoteRegex) {
|
|
283
|
-
escQuoteRegex = new RegExp('\\\\' + quote, 'g');
|
|
284
|
-
this._escQuoteRegexCache.set(quote, escQuoteRegex);
|
|
285
|
-
}
|
|
282
|
+
const quote = str.startsWith('"') ? '"' : "'";
|
|
286
283
|
return str
|
|
287
284
|
.slice(1, -1)
|
|
288
|
-
.replaceAll(escQuoteRegex, quote)
|
|
285
|
+
.replaceAll(escQuoteRegex[quote], quote)
|
|
289
286
|
.replaceAll(escEscRegex, '\\');
|
|
290
287
|
}
|
|
291
288
|
_parseTemplateString(str) {
|
package/dist/Lexer.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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,eAAe,GAAG;IACtB,mBAAmB;IACnB,uBAAuB;IACvB,UAAU;IACV,MAAM,CAAC,GAAG,CAAA,qBAAqB;IAC/B,MAAM,CAAC,GAAG,CAAA,qBAAqB;IAC/B,aAAa;IACb,MAAM,CAAC,GAAG,CAAA,KAAK;IACf,WAAW;IACX,MAAM,CAAC,GAAG,CAAA,UAAU;IACpB,MAAM,CAAC,GAAG,CAAA,WAAW;CACtB,CAAA;AACD,MAAM,gBAAgB,GAAG;IACvB,cAAc;IACd,wHAAwH;IACxH,qCAAqC;IACrC,MAAM,CAAC,GAAG,CAAA,+BAA+B;CAC1C,CAAA;AACD,MAAM,eAAe,GAAG,GAAU,EAAE,CAAC,CAAC;IACpC,IAAI,EAAE,SAAS;IACf,KAAK,EAAE,GAAG;IACV,GAAG,EAAE,GAAG;CACT,CAAC,CAAA;AACF,MAAM,iBAAiB,GAAG,IAAI,GAAG,CAAC;IAChC,UAAU;IACV,SAAS;IACT,WAAW;IACX,aAAa;IACb,UAAU;IACV,OAAO;IACP,OAAO;IACP,WAAW;CACZ,CAAC,CAAA;AAEF;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6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
7
|
exports.compileAst = compileAst;
|
|
8
|
+
/**
|
|
9
|
+
* Writes a key that a plain assignment would mishandle. Storing to
|
|
10
|
+
* "__proto__" invokes the prototype setter rather than creating a property, so
|
|
11
|
+
* an object literal or assignment using that key silently lost its value (and
|
|
12
|
+
* re-pointed the target's prototype). Defining the property instead matches
|
|
13
|
+
* what `JSON.parse('{"__proto__":1}')` produces: an ordinary own property.
|
|
14
|
+
*/
|
|
15
|
+
function defineOwn(target, key, value) {
|
|
16
|
+
Object.defineProperty(target, key, {
|
|
17
|
+
value,
|
|
18
|
+
writable: true,
|
|
19
|
+
enumerable: true,
|
|
20
|
+
configurable: true
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
function assignOwn(target, key, value) {
|
|
24
|
+
target[key] = value;
|
|
25
|
+
}
|
|
8
26
|
/** Renders an interpolated value for a template literal. */
|
|
9
27
|
function stringify(value) {
|
|
10
28
|
if (value == null) {
|
|
@@ -36,13 +54,16 @@ function stringify(value) {
|
|
|
36
54
|
* @throws {Error} if the tree contains an unrecognized node type
|
|
37
55
|
*/
|
|
38
56
|
function compileAst(ast, grammar) {
|
|
39
|
-
|
|
57
|
+
// AstNode types its `type` as a plain string so the Parser can build the tree
|
|
58
|
+
// loosely; narrowing to the union once, here, lets every case below see its
|
|
59
|
+
// own node type instead of repeating the same cast in each branch
|
|
60
|
+
const node = ast;
|
|
61
|
+
switch (node.type) {
|
|
40
62
|
case 'Literal': {
|
|
41
|
-
const { value } =
|
|
63
|
+
const { value } = node;
|
|
42
64
|
return () => value;
|
|
43
65
|
}
|
|
44
66
|
case 'Identifier': {
|
|
45
|
-
const node = ast;
|
|
46
67
|
const name = node.value;
|
|
47
68
|
if (!node.from) {
|
|
48
69
|
return node.relative
|
|
@@ -63,7 +84,6 @@ function compileAst(ast, grammar) {
|
|
|
63
84
|
};
|
|
64
85
|
}
|
|
65
86
|
case 'BinaryExpression': {
|
|
66
|
-
const node = ast;
|
|
67
87
|
const op = grammar.elements[node.operator];
|
|
68
88
|
if (op?.type !== 'binaryOp') {
|
|
69
89
|
// matches the tree-walking behaviour: an unknown operator yields
|
|
@@ -89,7 +109,6 @@ function compileAst(ast, grammar) {
|
|
|
89
109
|
return (ev) => fn(left(ev), right(ev));
|
|
90
110
|
}
|
|
91
111
|
case 'UnaryExpression': {
|
|
92
|
-
const node = ast;
|
|
93
112
|
const right = compileAst(node.right, grammar);
|
|
94
113
|
const elem = grammar.elements[node.operator];
|
|
95
114
|
const fn = elem?.type === 'unaryOp'
|
|
@@ -107,7 +126,6 @@ function compileAst(ast, grammar) {
|
|
|
107
126
|
return (ev) => fn(right(ev));
|
|
108
127
|
}
|
|
109
128
|
case 'ConditionalExpression': {
|
|
110
|
-
const node = ast;
|
|
111
129
|
const test = compileAst(node.test, grammar);
|
|
112
130
|
const consequent = node.consequent
|
|
113
131
|
? compileAst(node.consequent, grammar)
|
|
@@ -125,7 +143,6 @@ function compileAst(ast, grammar) {
|
|
|
125
143
|
};
|
|
126
144
|
}
|
|
127
145
|
case 'FilterExpression': {
|
|
128
|
-
const node = ast;
|
|
129
146
|
if (node.relative) {
|
|
130
147
|
// thrown on evaluation rather than compilation, as before
|
|
131
148
|
return () => {
|
|
@@ -146,7 +163,7 @@ function compileAst(ast, grammar) {
|
|
|
146
163
|
};
|
|
147
164
|
}
|
|
148
165
|
case 'ArrayLiteral': {
|
|
149
|
-
const items =
|
|
166
|
+
const items = node.value.map((item) => compileAst(item, grammar));
|
|
150
167
|
const len = items.length;
|
|
151
168
|
return (ev) => {
|
|
152
169
|
const out = new Array(len);
|
|
@@ -157,21 +174,23 @@ function compileAst(ast, grammar) {
|
|
|
157
174
|
};
|
|
158
175
|
}
|
|
159
176
|
case 'ObjectLiteral': {
|
|
160
|
-
const entries = Object.entries(
|
|
177
|
+
const entries = Object.entries(node.value);
|
|
161
178
|
const keys = entries.map(([key]) => key);
|
|
162
|
-
const values = entries.map(([,
|
|
179
|
+
const values = entries.map(([, value]) => compileAst(value, grammar));
|
|
163
180
|
const len = keys.length;
|
|
181
|
+
// resolved once, here, so the common case keeps its plain store
|
|
182
|
+
const store = keys.includes('__proto__') ? defineOwn : assignOwn;
|
|
164
183
|
return (ev) => {
|
|
165
184
|
const out = {};
|
|
166
185
|
for (let i = 0; i < len; i++) {
|
|
167
|
-
out
|
|
186
|
+
store(out, keys[i], values[i](ev));
|
|
168
187
|
}
|
|
169
188
|
return out;
|
|
170
189
|
};
|
|
171
190
|
}
|
|
172
191
|
case 'TemplateLiteral': {
|
|
173
192
|
// every part renders to a string, so these are narrower than CompiledNode
|
|
174
|
-
const parts =
|
|
193
|
+
const parts = node.parts.map((part) => {
|
|
175
194
|
if (part.type === 'static') {
|
|
176
195
|
const { value } = part;
|
|
177
196
|
return () => value;
|
|
@@ -188,7 +207,6 @@ function compileAst(ast, grammar) {
|
|
|
188
207
|
};
|
|
189
208
|
}
|
|
190
209
|
case 'FunctionCall': {
|
|
191
|
-
const node = ast;
|
|
192
210
|
const { name } = node;
|
|
193
211
|
const args = node.args.map((arg) => compileAst(arg, grammar));
|
|
194
212
|
// hasOwn, so that inherited Object.prototype members such as `toString`
|
|
@@ -233,7 +251,7 @@ function compileAst(ast, grammar) {
|
|
|
233
251
|
}
|
|
234
252
|
}
|
|
235
253
|
case 'SequenceExpression': {
|
|
236
|
-
const exprs =
|
|
254
|
+
const exprs = node.expressions.map((expr) => compileAst(expr, grammar));
|
|
237
255
|
const len = exprs.length;
|
|
238
256
|
return (ev) => {
|
|
239
257
|
let last;
|
|
@@ -244,12 +262,12 @@ function compileAst(ast, grammar) {
|
|
|
244
262
|
};
|
|
245
263
|
}
|
|
246
264
|
case 'AssignmentExpression': {
|
|
247
|
-
const node = ast;
|
|
248
265
|
const name = node.left.value;
|
|
249
266
|
const right = compileAst(node.right, grammar);
|
|
267
|
+
const store = name === '__proto__' ? defineOwn : assignOwn;
|
|
250
268
|
return (ev) => {
|
|
251
269
|
const value = right(ev);
|
|
252
|
-
ev._context
|
|
270
|
+
store(ev._context, name, value);
|
|
253
271
|
return value;
|
|
254
272
|
};
|
|
255
273
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compile.js","sourceRoot":"","sources":["../../src/evaluator/compile.ts"],"names":[],"mappings":";AAAA;;;GAGG;;
|
|
1
|
+
{"version":3,"file":"compile.js","sourceRoot":"","sources":["../../src/evaluator/compile.ts"],"names":[],"mappings":";AAAA;;;GAGG;;AAyEH,gCAgPC;AA7SD;;;;;;GAMG;AACH,SAAS,SAAS,CAChB,MAAiC,EACjC,GAAW,EACX,KAAgB;IAEhB,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,GAAG,EAAE;QACjC,KAAK;QACL,QAAQ,EAAE,IAAI;QACd,UAAU,EAAE,IAAI;QAChB,YAAY,EAAE,IAAI;KACnB,CAAC,CAAA;AACJ,CAAC;AAED,SAAS,SAAS,CAChB,MAAiC,EACjC,GAAW,EACX,KAAgB;IAEhB,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,CAAA;AACrB,CAAC;AAED,4DAA4D;AAC5D,SAAS,SAAS,CAAC,KAAgB;IACjC,IAAI,KAAK,IAAI,IAAI,EAAE,CAAC;QAClB,OAAO,EAAE,CAAA;IACX,CAAC;IACD,IACE,OAAO,KAAK,KAAK,QAAQ;QACzB,OAAO,KAAK,KAAK,QAAQ;QACzB,OAAO,KAAK,KAAK,SAAS,EAC1B,CAAC;QACD,OAAO,MAAM,CAAC,KAAK,CAAC,CAAA;IACtB,CAAC;IACD,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAA;AAC9B,CAAC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,SAAgB,UAAU,CAAC,GAAY,EAAE,OAAgB;IACvD,8EAA8E;IAC9E,4EAA4E;IAC5E,kEAAkE;IAClE,MAAM,IAAI,GAAG,GAAmB,CAAA;IAChC,QAAQ,IAAI,CAAC,IAAI,EAAE,CAAC;Q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|
export type BinaryOpEval = (left: JexlValue, right: JexlValue) => JexlValue;
|
|
3
|
+
/**
|
|
4
|
+
* A function or operator implementation as its author writes it, for the
|
|
5
|
+
* registration methods on {@link Jexl} to accept.
|
|
6
|
+
*
|
|
7
|
+
* Jexl cannot check what a registered callback will be handed. The arguments
|
|
8
|
+
* are whatever the expression evaluated to, drawn from a context supplied at
|
|
9
|
+
* evaluation time, and that context routinely holds values jexl has no literal
|
|
10
|
+
* for — a host object with methods, a callback, a class instance. Requiring
|
|
11
|
+
* {@link JexlValue} parameters therefore rejected every callback actually
|
|
12
|
+
* written against such a value, since parameters are contravariant. The
|
|
13
|
+
* argument types are the caller's business; jexl only promises to pass along
|
|
14
|
+
* whatever the operands evaluated to.
|
|
15
|
+
*/
|
|
16
|
+
export type UncheckedFn = (...args: never[]) => unknown;
|
|
3
17
|
export type BinaryOpEvalOnDemand = (left: {
|
|
4
18
|
eval: () => JexlValue;
|
|
5
19
|
}, right: {
|
|
@@ -17,10 +31,11 @@ export interface BinaryOp {
|
|
|
17
31
|
*/
|
|
18
32
|
unaryEval?: (right: JexlValue) => JexlValue;
|
|
19
33
|
}
|
|
34
|
+
export type UnaryOpEval = (right: JexlValue) => JexlValue;
|
|
20
35
|
export interface UnaryOp {
|
|
21
36
|
type: 'unaryOp';
|
|
22
37
|
precedence: number;
|
|
23
|
-
eval:
|
|
38
|
+
eval: UnaryOpEval;
|
|
24
39
|
}
|
|
25
40
|
/**
|
|
26
41
|
* The punctuation elements of the grammar. Unlike operators these carry no
|
|
@@ -32,9 +47,11 @@ export interface SimpleElement {
|
|
|
32
47
|
type: 'dot' | 'openBracket' | 'closeBracket' | 'openCurl' | 'closeCurl' | 'colon' | 'comma' | 'openParen' | 'closeParen' | 'question' | 'semicolon';
|
|
33
48
|
}
|
|
34
49
|
export type GrammarElement = BinaryOp | UnaryOp | SimpleElement;
|
|
50
|
+
/** A registered function, as jexl calls it once the operands are evaluated. */
|
|
51
|
+
export type GrammarFn = (...args: JexlValue[]) => JexlValue;
|
|
35
52
|
export interface Grammar {
|
|
36
53
|
elements: Record<string, GrammarElement>;
|
|
37
|
-
functions: Record<string,
|
|
54
|
+
functions: Record<string, GrammarFn>;
|
|
38
55
|
}
|
|
39
56
|
/**
|
|
40
57
|
* Returns the binding power of a grammar element, or 0 for elements that
|
package/dist/grammar.js
CHANGED
|
@@ -126,17 +126,17 @@ const getGrammar = () => ({
|
|
|
126
126
|
precedence: 20,
|
|
127
127
|
eval: (left, right) => {
|
|
128
128
|
if (typeof right === 'string') {
|
|
129
|
-
|
|
129
|
+
// only a primitive has a meaningful substring form. An absent or
|
|
130
|
+
// structured left operand is not "in" a string, and must not be
|
|
131
|
+
// coerced to '' — every string contains the empty string, so that
|
|
132
|
+
// made `feature.missingAttr in "someString"` true for every feature.
|
|
133
|
+
return typeof left === 'string' ||
|
|
130
134
|
typeof left === 'number' ||
|
|
131
135
|
typeof left === 'boolean'
|
|
132
|
-
? String(left)
|
|
133
|
-
:
|
|
134
|
-
return right.includes(leftStr);
|
|
136
|
+
? right.includes(String(left))
|
|
137
|
+
: false;
|
|
135
138
|
}
|
|
136
|
-
|
|
137
|
-
return right.includes(left);
|
|
138
|
-
}
|
|
139
|
-
return false;
|
|
139
|
+
return Array.isArray(right) ? right.includes(left) : false;
|
|
140
140
|
}
|
|
141
141
|
},
|
|
142
142
|
'!': {
|
package/dist/grammar.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"grammar.js","sourceRoot":"","sources":["../src/grammar.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;
|
|
1
|
+
{"version":3,"file":"grammar.js","sourceRoot":"","sources":["../src/grammar.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAoFH,oCAIC;AARD;;;GAGG;AACH,SAAgB,YAAY,CAAC,IAAgC;IAC3D,OAAO,IAAI,IAAI,CAAC,IAAI,CAAC,IAAI,KAAK,UAAU,IAAI,IAAI,CAAC,IAAI,KAAK,SAAS,CAAC;Q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K,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;wBAC9B,CAAC,CAAC,KAAK,CAAA;gBACX,CAAC;gBACD,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,CAAA;YAC5D,CAAC;SACF;QACD,GAAG,EAAE;YACH,IAAI,EAAE,SAAS;YACf,UAAU,EAAE,QAAQ;YACpB,IAAI,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,KAAK;SACxB;QACD,GAAG,EAAE;YACH,IAAI,EAAE,UAAU;YAChB,UAAU,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;AAzJW,QAAA,UAAU,cAyJrB"}
|
package/dist/parser/Parser.d.ts
CHANGED
|
@@ -103,8 +103,9 @@ declare class Parser {
|
|
|
103
103
|
* Prepares the Parser to accept a subexpression by (re)instantiating the
|
|
104
104
|
* subParser.
|
|
105
105
|
* @param {string} [exprStr] The expression string to prefix to the new Parser
|
|
106
|
+
* @returns {Parser} the new subParser, also stored on this instance
|
|
106
107
|
* @private
|
|
107
108
|
*/
|
|
108
|
-
_startSubExpression(exprStr?: string):
|
|
109
|
+
_startSubExpression(exprStr?: string): Parser;
|
|
109
110
|
}
|
|
110
111
|
export default Parser;
|
package/dist/parser/Parser.js
CHANGED
|
@@ -39,6 +39,21 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
39
39
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
40
40
|
const handlers = __importStar(require("./handlers.js"));
|
|
41
41
|
const states_ts_1 = require("./states.js");
|
|
42
|
+
/**
|
|
43
|
+
* The handler to run for a token type when the state it appears in doesn't
|
|
44
|
+
* name one of its own. Token types absent here — the brackets, parens and
|
|
45
|
+
* separators — carry no behavior beyond the state transition the state machine
|
|
46
|
+
* gives them.
|
|
47
|
+
*/
|
|
48
|
+
const tokenHandlers = {
|
|
49
|
+
binaryOp: handlers.binaryOp,
|
|
50
|
+
dot: handlers.dot,
|
|
51
|
+
identifier: handlers.identifier,
|
|
52
|
+
literal: handlers.literal,
|
|
53
|
+
semicolon: handlers.semicolon,
|
|
54
|
+
templateString: handlers.templateString,
|
|
55
|
+
unaryOp: handlers.unaryOp
|
|
56
|
+
};
|
|
42
57
|
/**
|
|
43
58
|
* The Parser is a state machine that converts tokens from the {@link Lexer}
|
|
44
59
|
* into an Abstract Syntax Tree (AST), capable of being evaluated in any
|
|
@@ -98,10 +113,8 @@ class Parser {
|
|
|
98
113
|
const startExpr = this._exprStr;
|
|
99
114
|
this._exprStr += token.raw;
|
|
100
115
|
if (state.subHandler) {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
}
|
|
104
|
-
const stopState = this._subParser.addToken(token);
|
|
116
|
+
const subParser = this._subParser ?? this._startSubExpression(startExpr);
|
|
117
|
+
const stopState = subParser.addToken(token);
|
|
105
118
|
if (stopState) {
|
|
106
119
|
this._endSubExpression();
|
|
107
120
|
if (stopState === '_semicolon') {
|
|
@@ -120,13 +133,10 @@ class Parser {
|
|
|
120
133
|
}
|
|
121
134
|
else if (state.tokenTypes?.[token.type]) {
|
|
122
135
|
const typeOpts = state.tokenTypes[token.type];
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
if (handleFunc) {
|
|
128
|
-
handleFunc.call(this, token);
|
|
129
|
-
}
|
|
136
|
+
// the state's own handler wins over the token type's default one, and a
|
|
137
|
+
// token type with neither only drives the transition below
|
|
138
|
+
const handleFunc = typeOpts.handler ?? tokenHandlers[token.type];
|
|
139
|
+
handleFunc?.call(this, token);
|
|
130
140
|
if (typeOpts.toState) {
|
|
131
141
|
this._state = typeOpts.toState;
|
|
132
142
|
}
|
|
@@ -159,7 +169,12 @@ class Parser {
|
|
|
159
169
|
* the expression, indicating that the expression is incomplete
|
|
160
170
|
*/
|
|
161
171
|
complete() {
|
|
162
|
-
|
|
172
|
+
// `_subParser` counts as unfinished business alongside `_cursor`: a group
|
|
173
|
+
// that opened before anything was placed in the tree, as in "(1", leaves
|
|
174
|
+
// the cursor null, and testing the cursor alone let the missing ")" pass
|
|
175
|
+
// silently and evaluate as if it were closed.
|
|
176
|
+
if ((this._cursor || this._subParser) &&
|
|
177
|
+
!states_ts_1.states[this._state].completable) {
|
|
163
178
|
throw new Error(`Unexpected end of expression: ${this._exprStr}`);
|
|
164
179
|
}
|
|
165
180
|
if (this._subParser) {
|
|
@@ -243,6 +258,7 @@ class Parser {
|
|
|
243
258
|
* Prepares the Parser to accept a subexpression by (re)instantiating the
|
|
244
259
|
* subParser.
|
|
245
260
|
* @param {string} [exprStr] The expression string to prefix to the new Parser
|
|
261
|
+
* @returns {Parser} the new subParser, also stored on this instance
|
|
246
262
|
* @private
|
|
247
263
|
*/
|
|
248
264
|
_startSubExpression(exprStr) {
|
|
@@ -255,6 +271,7 @@ class Parser {
|
|
|
255
271
|
endStates = { ...endStates, semicolon: '_semicolon' };
|
|
256
272
|
}
|
|
257
273
|
this._subParser = new Parser(this._grammar, this._lexer, exprStr, endStates);
|
|
274
|
+
return this._subParser;
|
|
258
275
|
}
|
|
259
276
|
}
|
|
260
277
|
exports.default = Parser;
|