@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/README.md
CHANGED
|
@@ -83,14 +83,15 @@ jexl.eval('`Price: \\$100`')
|
|
|
83
83
|
|
|
84
84
|
### Operators
|
|
85
85
|
|
|
86
|
-
**Unary:** `!` (
|
|
86
|
+
**Unary:** `!` (logical negation), `-` (arithmetic negation)
|
|
87
87
|
|
|
88
88
|
**Binary:**
|
|
89
89
|
|
|
90
90
|
- Arithmetic: `+`, `-`, `*`, `/`, `//` (floor division), `%`, `^` (power)
|
|
91
91
|
- Comparison: `==`, `!=`, `>`, `>=`, `<`, `<=`, `in`
|
|
92
92
|
- Logical: `&&`, `||`
|
|
93
|
-
- Assignment: `=` (assigns value to variable
|
|
93
|
+
- Assignment: `=` (assigns a value to a bare variable name; `a.b = 1` is not
|
|
94
|
+
supported)
|
|
94
95
|
|
|
95
96
|
**Ternary:** `condition ? consequent : alternate`
|
|
96
97
|
|
|
@@ -206,3 +207,11 @@ jexl.eval('~3.7') // 3
|
|
|
206
207
|
## License
|
|
207
208
|
|
|
208
209
|
MIT License, same as TomFrost/Jexl
|
|
210
|
+
|
|
211
|
+
## Publishing
|
|
212
|
+
|
|
213
|
+
[Trusted publishing](https://docs.npmjs.com/about-trusted-publishing) via GitHub Actions.
|
|
214
|
+
|
|
215
|
+
```bash
|
|
216
|
+
npm version patch # or minor/major
|
|
217
|
+
```
|
package/dist/Expression.d.ts
CHANGED
|
@@ -1,13 +1,23 @@
|
|
|
1
|
+
import Lexer from './Lexer.ts';
|
|
2
|
+
import type { CompiledNode } from './evaluator/compile.ts';
|
|
3
|
+
import type { Grammar } from './grammar.ts';
|
|
1
4
|
import type { AstNode } from './types.ts';
|
|
2
|
-
interface Grammar {
|
|
3
|
-
elements: Record<string, any>;
|
|
4
|
-
[key: string]: any;
|
|
5
|
-
}
|
|
6
5
|
declare class Expression {
|
|
7
6
|
_grammar: Grammar;
|
|
8
7
|
_exprStr: string;
|
|
9
8
|
_ast: AstNode | null;
|
|
10
|
-
|
|
9
|
+
_lexer: Lexer;
|
|
10
|
+
_compiled: boolean;
|
|
11
|
+
_fn: CompiledNode | null;
|
|
12
|
+
/**
|
|
13
|
+
* @param {{}} grammar The grammar to compile and evaluate against
|
|
14
|
+
* @param {string} exprStr The Jexl expression string
|
|
15
|
+
* @param {Lexer} [lexer] A Lexer to reuse. Lexers memoize the regex used to
|
|
16
|
+
* split expressions, which is expensive to build, so passing a shared
|
|
17
|
+
* one avoids paying that cost per expression. It must be invalidated
|
|
18
|
+
* via {@link Lexer#_clearCache} whenever the grammar's elements change.
|
|
19
|
+
*/
|
|
20
|
+
constructor(grammar: Grammar, exprStr: string, lexer?: Lexer);
|
|
11
21
|
/**
|
|
12
22
|
* Forces a compilation of the expression string that this Expression object
|
|
13
23
|
* was constructed with. This function can be called multiple times; useful
|
|
@@ -22,7 +32,7 @@ declare class Expression {
|
|
|
22
32
|
* @returns {*} the result of the evaluation.
|
|
23
33
|
* @throws {*} on error
|
|
24
34
|
*/
|
|
25
|
-
eval(context?: {}):
|
|
35
|
+
eval(context?: {}): import("./types.ts").JexlValue;
|
|
26
36
|
_getAst(): AstNode | null;
|
|
27
37
|
}
|
|
28
38
|
export default Expression;
|
package/dist/Expression.js
CHANGED
|
@@ -9,12 +9,29 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
9
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
10
|
const Lexer_ts_1 = __importDefault(require("./Lexer.js"));
|
|
11
11
|
const Evaluator_ts_1 = __importDefault(require("./evaluator/Evaluator.js"));
|
|
12
|
+
const compile_ts_1 = require("./evaluator/compile.js");
|
|
12
13
|
const Parser_ts_1 = __importDefault(require("./parser/Parser.js"));
|
|
13
14
|
class Expression {
|
|
14
|
-
|
|
15
|
+
_grammar;
|
|
16
|
+
_exprStr;
|
|
17
|
+
_ast;
|
|
18
|
+
_lexer;
|
|
19
|
+
_compiled = false;
|
|
20
|
+
// the AST lowered to closures, so repeated eval() calls skip node dispatch
|
|
21
|
+
_fn = null;
|
|
22
|
+
/**
|
|
23
|
+
* @param {{}} grammar The grammar to compile and evaluate against
|
|
24
|
+
* @param {string} exprStr The Jexl expression string
|
|
25
|
+
* @param {Lexer} [lexer] A Lexer to reuse. Lexers memoize the regex used to
|
|
26
|
+
* split expressions, which is expensive to build, so passing a shared
|
|
27
|
+
* one avoids paying that cost per expression. It must be invalidated
|
|
28
|
+
* via {@link Lexer#_clearCache} whenever the grammar's elements change.
|
|
29
|
+
*/
|
|
30
|
+
constructor(grammar, exprStr, lexer) {
|
|
15
31
|
this._grammar = grammar;
|
|
16
32
|
this._exprStr = exprStr;
|
|
17
33
|
this._ast = null;
|
|
34
|
+
this._lexer = lexer ?? new Lexer_ts_1.default(grammar);
|
|
18
35
|
}
|
|
19
36
|
/**
|
|
20
37
|
* Forces a compilation of the expression string that this Expression object
|
|
@@ -23,11 +40,13 @@ class Expression {
|
|
|
23
40
|
* @returns {Expression} this Expression instance, for convenience
|
|
24
41
|
*/
|
|
25
42
|
compile() {
|
|
26
|
-
const
|
|
27
|
-
const
|
|
28
|
-
const tokens = lexer.tokenize(this._exprStr);
|
|
43
|
+
const parser = new Parser_ts_1.default(this._grammar, this._lexer);
|
|
44
|
+
const tokens = this._lexer.tokenize(this._exprStr);
|
|
29
45
|
parser.addTokens(tokens);
|
|
30
46
|
this._ast = parser.complete();
|
|
47
|
+
// lower the tree to closures once, here, so that eval() is just a call
|
|
48
|
+
this._fn = this._ast ? (0, compile_ts_1.compileAst)(this._ast, this._grammar) : null;
|
|
49
|
+
this._compiled = true;
|
|
31
50
|
return this;
|
|
32
51
|
}
|
|
33
52
|
/**
|
|
@@ -38,12 +57,19 @@ class Expression {
|
|
|
38
57
|
* @throws {*} on error
|
|
39
58
|
*/
|
|
40
59
|
eval(context = {}) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
60
|
+
if (!this._compiled) {
|
|
61
|
+
this.compile();
|
|
62
|
+
}
|
|
63
|
+
// an expression with no tokens (empty or whitespace-only) has no AST
|
|
64
|
+
if (!this._fn) {
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
return this._fn(new Evaluator_ts_1.default(this._grammar, context));
|
|
44
68
|
}
|
|
45
69
|
_getAst() {
|
|
46
|
-
|
|
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) {
|
|
47
73
|
this.compile();
|
|
48
74
|
}
|
|
49
75
|
return this._ast;
|
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,mEAAuC;
|
|
1
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AE;QACf,IAAI,CAAC,IAAI,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;IAED,OAAO;QACL,kEAAkE;QAClE,wEAAwE;QACxE,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;YACpB,IAAI,CAAC,OAAO,EAAE,CAAA;QAChB,CAAC;QACD,OAAO,IAAI,CAAC,IAAI,CAAA;IAClB,CAAC;CACF;AAED,kBAAe,UAAU,CAAA"}
|
package/dist/Jexl.d.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import Expression from './Expression.ts';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}
|
|
2
|
+
import Lexer from './Lexer.ts';
|
|
3
|
+
import type { BinaryOpEval, BinaryOpEvalOnDemand, Grammar, GrammarElement } from './grammar.ts';
|
|
4
|
+
import type { JexlValue } from './types.ts';
|
|
6
5
|
/**
|
|
7
6
|
* Jexl is the Javascript Expression Language, capable of parsing and
|
|
8
7
|
* evaluating basic to complex expression strings, combined with advanced
|
|
@@ -11,6 +10,7 @@ interface Grammar {
|
|
|
11
10
|
*/
|
|
12
11
|
declare class Jexl {
|
|
13
12
|
_grammar: Grammar;
|
|
13
|
+
_lexer: Lexer;
|
|
14
14
|
constructor();
|
|
15
15
|
/**
|
|
16
16
|
* Adds a binary operator to Jexl at the specified precedence. The higher the
|
|
@@ -31,7 +31,8 @@ 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: BinaryOpEval, manualEval?: false): void;
|
|
35
|
+
addBinaryOp(operator: string, precedence: number, fn: BinaryOpEvalOnDemand, manualEval: true): void;
|
|
35
36
|
/**
|
|
36
37
|
* Adds or replaces an expression function in this Jexl instance.
|
|
37
38
|
* @param {string} name The name of the expression function, as it will be
|
|
@@ -40,14 +41,14 @@ declare class Jexl {
|
|
|
40
41
|
* expression function is invoked. It will be provided with each argument
|
|
41
42
|
* supplied in the expression, in the same order.
|
|
42
43
|
*/
|
|
43
|
-
addFunction(name: string, fn: (...args:
|
|
44
|
+
addFunction(name: string, fn: (...args: JexlValue[]) => JexlValue): void;
|
|
44
45
|
/**
|
|
45
46
|
* Syntactic sugar for calling {@link #addFunction} repeatedly. This function
|
|
46
47
|
* accepts a map of one or more expression function names to their javascript
|
|
47
48
|
* function counterpart.
|
|
48
49
|
* @param {{}} map A map of expression function names to javascript functions
|
|
49
50
|
*/
|
|
50
|
-
addFunctions(map: Record<string, (...args:
|
|
51
|
+
addFunctions(map: Record<string, (...args: JexlValue[]) => JexlValue>): void;
|
|
51
52
|
/**
|
|
52
53
|
* Adds a unary operator to Jexl. Unary operators are currently only supported
|
|
53
54
|
* on the left side of the value on which it will operate.
|
|
@@ -56,7 +57,7 @@ declare class Jexl {
|
|
|
56
57
|
* will be called with one argument: the literal value to the right of the
|
|
57
58
|
* operator. It should return the resulting value.
|
|
58
59
|
*/
|
|
59
|
-
addUnaryOp(operator: string, fn: (right:
|
|
60
|
+
addUnaryOp(operator: string, fn: (right: JexlValue) => JexlValue): void;
|
|
60
61
|
/**
|
|
61
62
|
* Creates an Expression object from the given Jexl expression string, and
|
|
62
63
|
* immediately compiles it. The returned Expression object can then be
|
|
@@ -78,7 +79,7 @@ declare class Jexl {
|
|
|
78
79
|
* @param {string} name The name of the expression function
|
|
79
80
|
* @returns {function} The expression function
|
|
80
81
|
*/
|
|
81
|
-
getFunction(name: string): (...args:
|
|
82
|
+
getFunction(name: string): ((...args: JexlValue[]) => JexlValue) | undefined;
|
|
82
83
|
/**
|
|
83
84
|
* Evaluates a Jexl string within an optional context.
|
|
84
85
|
* @param {string} expression The Jexl expression to be evaluated
|
|
@@ -87,14 +88,14 @@ declare class Jexl {
|
|
|
87
88
|
* @returns {*} the result of the evaluation.
|
|
88
89
|
* @throws {*} on error
|
|
89
90
|
*/
|
|
90
|
-
eval(expression: string, context?: {}):
|
|
91
|
+
eval(expression: string, context?: {}): JexlValue;
|
|
91
92
|
/**
|
|
92
93
|
* A JavaScript template literal to allow expressions to be defined by the
|
|
93
94
|
* syntax: expr`40 + 2`
|
|
94
95
|
* @param {Array<string>} strs
|
|
95
96
|
* @param {...any} args
|
|
96
97
|
*/
|
|
97
|
-
expr(strs: TemplateStringsArray, ...args:
|
|
98
|
+
expr(strs: TemplateStringsArray, ...args: JexlValue[]): Expression;
|
|
98
99
|
/**
|
|
99
100
|
* Removes a binary or unary operator from the Jexl grammar.
|
|
100
101
|
* @param {string} operator The operator string to be removed
|
|
@@ -107,7 +108,7 @@ declare class Jexl {
|
|
|
107
108
|
* grammar element
|
|
108
109
|
* @private
|
|
109
110
|
*/
|
|
110
|
-
_addGrammarElement(str: string, obj:
|
|
111
|
+
_addGrammarElement(str: string, obj: GrammarElement): void;
|
|
111
112
|
}
|
|
112
113
|
declare const jexlInstance: Jexl;
|
|
113
114
|
export default jexlInstance;
|
package/dist/Jexl.js
CHANGED
|
@@ -9,6 +9,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
9
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
10
|
exports.Jexl = void 0;
|
|
11
11
|
const Expression_ts_1 = __importDefault(require("./Expression.js"));
|
|
12
|
+
const Lexer_ts_1 = __importDefault(require("./Lexer.js"));
|
|
12
13
|
const grammar_ts_1 = require("./grammar.js");
|
|
13
14
|
/**
|
|
14
15
|
* Jexl is the Javascript Expression Language, capable of parsing and
|
|
@@ -17,35 +18,25 @@ const grammar_ts_1 = require("./grammar.js");
|
|
|
17
18
|
* @constructor
|
|
18
19
|
*/
|
|
19
20
|
class Jexl {
|
|
21
|
+
_grammar;
|
|
22
|
+
// shared by every Expression this instance creates, so that the expensive
|
|
23
|
+
// element-splitting regex is built once rather than per compile
|
|
24
|
+
_lexer;
|
|
20
25
|
constructor() {
|
|
21
26
|
this._grammar = (0, grammar_ts_1.getGrammar)();
|
|
27
|
+
this._lexer = new Lexer_ts_1.default(this._grammar);
|
|
22
28
|
this.expr = this.expr.bind(this);
|
|
23
29
|
}
|
|
24
|
-
/**
|
|
25
|
-
* Adds a binary operator to Jexl at the specified precedence. The higher the
|
|
26
|
-
* precedence, the earlier the operator is applied in the order of operations.
|
|
27
|
-
* For example, * has a higher precedence than +, because multiplication comes
|
|
28
|
-
* before division.
|
|
29
|
-
*
|
|
30
|
-
* Please see grammar.js for a listing of all default operators and their
|
|
31
|
-
* precedence values in order to choose the appropriate precedence for the
|
|
32
|
-
* new operator.
|
|
33
|
-
* @param {string} operator The operator string to be added
|
|
34
|
-
* @param {number} precedence The operator's precedence
|
|
35
|
-
* @param {function} fn A function to run to calculate the result. The function
|
|
36
|
-
* will be called with two arguments: left and right, denoting the values
|
|
37
|
-
* on either side of the operator. It should return the resulting value.
|
|
38
|
-
* @param {boolean} [manualEval] If true, the `left` and `right` arguments
|
|
39
|
-
* will be wrapped in objects with an `eval` function. Calling
|
|
40
|
-
* left.eval() or right.eval() will return that operand's actual value.
|
|
41
|
-
* This is useful to conditionally evaluate operands.
|
|
42
|
-
*/
|
|
43
30
|
addBinaryOp(operator, precedence, fn, manualEval) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
31
|
+
// the overloads above pair `fn` with `manualEval`; the implementation
|
|
32
|
+
// signature can't express that correlation, hence the assertions
|
|
33
|
+
this._addGrammarElement(operator, manualEval
|
|
34
|
+
? {
|
|
35
|
+
type: 'binaryOp',
|
|
36
|
+
precedence,
|
|
37
|
+
evalOnDemand: fn
|
|
38
|
+
}
|
|
39
|
+
: { type: 'binaryOp', precedence, eval: fn });
|
|
49
40
|
}
|
|
50
41
|
/**
|
|
51
42
|
* Adds or replaces an expression function in this Jexl instance.
|
|
@@ -78,7 +69,7 @@ class Jexl {
|
|
|
78
69
|
addUnaryOp(operator, fn) {
|
|
79
70
|
this._addGrammarElement(operator, {
|
|
80
71
|
type: 'unaryOp',
|
|
81
|
-
|
|
72
|
+
precedence: Infinity,
|
|
82
73
|
eval: fn
|
|
83
74
|
});
|
|
84
75
|
}
|
|
@@ -101,7 +92,7 @@ class Jexl {
|
|
|
101
92
|
* @returns {Expression} The Expression object representing the given string
|
|
102
93
|
*/
|
|
103
94
|
createExpression(expression) {
|
|
104
|
-
return new Expression_ts_1.default(this._grammar, expression);
|
|
95
|
+
return new Expression_ts_1.default(this._grammar, expression, this._lexer);
|
|
105
96
|
}
|
|
106
97
|
/**
|
|
107
98
|
* Retrieves a previously set expression function.
|
|
@@ -131,10 +122,18 @@ class Jexl {
|
|
|
131
122
|
*/
|
|
132
123
|
expr(strs, ...args) {
|
|
133
124
|
let exprStr = '';
|
|
134
|
-
for (
|
|
135
|
-
exprStr +=
|
|
125
|
+
for (const [idx, str] of strs.entries()) {
|
|
126
|
+
exprStr += str;
|
|
136
127
|
if (idx < args.length) {
|
|
137
|
-
|
|
128
|
+
const arg = args[idx];
|
|
129
|
+
if (typeof arg === 'string' ||
|
|
130
|
+
typeof arg === 'number' ||
|
|
131
|
+
typeof arg === 'boolean') {
|
|
132
|
+
exprStr += String(arg);
|
|
133
|
+
}
|
|
134
|
+
else if (arg != null) {
|
|
135
|
+
exprStr += JSON.stringify(arg);
|
|
136
|
+
}
|
|
138
137
|
}
|
|
139
138
|
}
|
|
140
139
|
return this.createExpression(exprStr);
|
|
@@ -145,8 +144,9 @@ class Jexl {
|
|
|
145
144
|
*/
|
|
146
145
|
removeOp(operator) {
|
|
147
146
|
const elem = this._grammar.elements[operator];
|
|
148
|
-
if (elem
|
|
147
|
+
if (elem?.type === 'binaryOp' || elem?.type === 'unaryOp') {
|
|
149
148
|
Reflect.deleteProperty(this._grammar.elements, operator);
|
|
149
|
+
this._lexer._clearCache();
|
|
150
150
|
}
|
|
151
151
|
}
|
|
152
152
|
/**
|
|
@@ -158,6 +158,7 @@ class Jexl {
|
|
|
158
158
|
*/
|
|
159
159
|
_addGrammarElement(str, obj) {
|
|
160
160
|
this._grammar.elements[str] = obj;
|
|
161
|
+
this._lexer._clearCache();
|
|
161
162
|
}
|
|
162
163
|
}
|
|
163
164
|
exports.Jexl = Jexl;
|
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,6CAAyC;
|
|
1
|
+
{"version":3,"file":"Jexl.js","sourceRoot":"","sources":["../src/Jexl.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;;;AAEH,oEAAwC;AACxC,0DAA8B;AAC9B,6CAAyC;AAUzC;;;;;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,EAAuC,EACvC,UAAoB;QAEpB,sEAAsE;QACtE,iEAAiE;QACjE,IAAI,CAAC,kBAAkB,CACrB,QAAQ,EACR,UAAU;YACR,CAAC,CAAC;gBACE,IAAI,EAAE,UAAU;g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AAG,GAAG,CAAA;QACjC,IAAI,CAAC,MAAM,CAAC,WAAW,EAAE,CAAA;IAC3B,CAAC;CACF;AAIQ,oBAAI;AAFb,MAAM,YAAY,GAAG,IAAI,IAAI,EAAE,CAAA;AAC/B,kBAAe,YAAY,CAAA"}
|
package/dist/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/dist/Lexer.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
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
|
-
const escEscRegex =
|
|
9
|
+
const escEscRegex = /\\\\/g;
|
|
10
10
|
const whitespaceRegex = /^\s*$/;
|
|
11
11
|
const preOpRegexElems = [
|
|
12
12
|
// Template strings
|
|
@@ -26,6 +26,11 @@ const postOpRegexElems = [
|
|
|
26
26
|
// Numerics (without negative symbol)
|
|
27
27
|
String.raw `(?:(?:[0-9]*\.[0-9]+)|[0-9]+)`
|
|
28
28
|
];
|
|
29
|
+
const unaryMinusToken = () => ({
|
|
30
|
+
type: 'unaryOp',
|
|
31
|
+
value: '-',
|
|
32
|
+
raw: '-'
|
|
33
|
+
});
|
|
29
34
|
const minusNegatesAfter = new Set([
|
|
30
35
|
'binaryOp',
|
|
31
36
|
'unaryOp',
|
|
@@ -47,10 +52,20 @@ const minusNegatesAfter = new Set([
|
|
|
47
52
|
* @type {{}}
|
|
48
53
|
*/
|
|
49
54
|
class Lexer {
|
|
55
|
+
_grammar;
|
|
56
|
+
_splitRegex;
|
|
57
|
+
_escQuoteRegexCache = new Map();
|
|
50
58
|
constructor(grammar) {
|
|
51
|
-
this._escQuoteRegexCache = new Map();
|
|
52
59
|
this._grammar = grammar;
|
|
53
60
|
}
|
|
61
|
+
/**
|
|
62
|
+
* Discards the memoized split regex, so that it is rebuilt on the next
|
|
63
|
+
* tokenize. Must be called whenever the grammar's elements change, since the
|
|
64
|
+
* regex is derived from their keys.
|
|
65
|
+
*/
|
|
66
|
+
_clearCache() {
|
|
67
|
+
this._splitRegex = undefined;
|
|
68
|
+
}
|
|
54
69
|
/**
|
|
55
70
|
* Splits a Jexl expression string into an array of expression elements.
|
|
56
71
|
* @param {string} str A Jexl expression string
|
|
@@ -74,21 +89,35 @@ class Lexer {
|
|
|
74
89
|
getTokens(elements) {
|
|
75
90
|
const tokens = [];
|
|
76
91
|
let negate = false;
|
|
77
|
-
for (
|
|
78
|
-
if (this._isWhitespace(
|
|
79
|
-
|
|
80
|
-
|
|
92
|
+
for (const element of elements) {
|
|
93
|
+
if (this._isWhitespace(element)) {
|
|
94
|
+
const last = tokens.at(-1);
|
|
95
|
+
if (last) {
|
|
96
|
+
last.raw += element;
|
|
81
97
|
}
|
|
82
98
|
}
|
|
83
|
-
else if (
|
|
99
|
+
else if (element === '-' && this._isNegative(tokens)) {
|
|
100
|
+
// a second prefix minus in a row ("- -x"): emit the pending one as a
|
|
101
|
+
// unary operator so this one can negate whatever comes next
|
|
102
|
+
if (negate) {
|
|
103
|
+
tokens.push(unaryMinusToken());
|
|
104
|
+
}
|
|
84
105
|
negate = true;
|
|
85
106
|
}
|
|
86
|
-
else {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
107
|
+
else if (negate) {
|
|
108
|
+
negate = false;
|
|
109
|
+
if (numericRegex.exec(element)) {
|
|
110
|
+
// fold the sign into the number, so "-1" stays a single literal
|
|
111
|
+
tokens.push(this._createToken('-' + element));
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
// anything else gets a standalone prefix operator, letting "-x",
|
|
115
|
+
// "-(a + b)" and "-foo.bar" negate a computed value
|
|
116
|
+
tokens.push(unaryMinusToken(), this._createToken(element));
|
|
90
117
|
}
|
|
91
|
-
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
tokens.push(this._createToken(element));
|
|
92
121
|
}
|
|
93
122
|
}
|
|
94
123
|
// Catch a - at the end of the string. Let the parser handle that issue.
|
|
@@ -158,7 +187,7 @@ class Lexer {
|
|
|
158
187
|
else if (element === 'true' || element === 'false') {
|
|
159
188
|
token.value = element === 'true';
|
|
160
189
|
}
|
|
161
|
-
else if (this._grammar.elements
|
|
190
|
+
else if (Object.hasOwn(this._grammar.elements, element)) {
|
|
162
191
|
token.type = this._grammar.elements[element].type;
|
|
163
192
|
}
|
|
164
193
|
else if (identRegex.exec(element)) {
|
|
@@ -256,8 +285,8 @@ class Lexer {
|
|
|
256
285
|
}
|
|
257
286
|
return str
|
|
258
287
|
.slice(1, -1)
|
|
259
|
-
.
|
|
260
|
-
.
|
|
288
|
+
.replaceAll(escQuoteRegex, quote)
|
|
289
|
+
.replaceAll(escEscRegex, '\\');
|
|
261
290
|
}
|
|
262
291
|
_parseTemplateString(str) {
|
|
263
292
|
const parts = [];
|
|
@@ -279,14 +308,25 @@ class Lexer {
|
|
|
279
308
|
const interpStart = current + 2;
|
|
280
309
|
current += 2;
|
|
281
310
|
while (current < str.length && braceDepth > 0) {
|
|
282
|
-
|
|
311
|
+
const char = str[current];
|
|
312
|
+
if (char === '\\') {
|
|
283
313
|
current += 2;
|
|
284
314
|
continue;
|
|
285
315
|
}
|
|
286
|
-
|
|
316
|
+
// skip over string literals so that braces inside them, as in
|
|
317
|
+
// `${ f('}') }`, don't unbalance the depth count
|
|
318
|
+
if (char === '"' || char === "'") {
|
|
319
|
+
current++;
|
|
320
|
+
while (current < str.length && str[current] !== char) {
|
|
321
|
+
current += str[current] === '\\' ? 2 : 1;
|
|
322
|
+
}
|
|
323
|
+
current++;
|
|
324
|
+
continue;
|
|
325
|
+
}
|
|
326
|
+
if (char === '{') {
|
|
287
327
|
braceDepth++;
|
|
288
328
|
}
|
|
289
|
-
if (
|
|
329
|
+
else if (char === '}') {
|
|
290
330
|
braceDepth--;
|
|
291
331
|
}
|
|
292
332
|
current++;
|
package/dist/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;;AA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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;
|