@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.
- package/README.md +24 -25
- 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 +61 -19
- 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 +33 -5
- 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 +5 -21
- 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 +61 -19
- 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 +33 -5
- 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 +5 -21
- package/esm/parser/states.js.map +1 -1
- package/esm/types.d.ts +18 -7
- package/package.json +27 -29
- package/src/Expression.ts +33 -13
- package/src/Jexl.ts +59 -21
- package/src/Lexer.ts +63 -28
- 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 +35 -21
- package/src/parser/handlers.ts +96 -73
- package/src/parser/states.ts +7 -23
- 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/dist/parser/Parser.d.ts
CHANGED
|
@@ -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,
|
|
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,
|
|
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):
|
|
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():
|
|
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.
|
package/dist/parser/Parser.js
CHANGED
|
@@ -57,6 +57,20 @@ const states_ts_1 = require("./states.js");
|
|
|
57
57
|
* instead of boolean false.
|
|
58
58
|
*/
|
|
59
59
|
class Parser {
|
|
60
|
+
_grammar;
|
|
61
|
+
_lexer;
|
|
62
|
+
_state;
|
|
63
|
+
_tree;
|
|
64
|
+
_exprStr;
|
|
65
|
+
_relative;
|
|
66
|
+
_stopMap;
|
|
67
|
+
_cursor;
|
|
68
|
+
_subParser;
|
|
69
|
+
_parentStop;
|
|
70
|
+
_nextIdentEncapsulate;
|
|
71
|
+
_nextIdentRelative;
|
|
72
|
+
_curObjKey;
|
|
73
|
+
_sequenceExpressions;
|
|
60
74
|
constructor(grammar, lexer, prefix, stopMap) {
|
|
61
75
|
this._grammar = grammar;
|
|
62
76
|
this._lexer = lexer;
|
|
@@ -90,12 +104,20 @@ class Parser {
|
|
|
90
104
|
const stopState = this._subParser.addToken(token);
|
|
91
105
|
if (stopState) {
|
|
92
106
|
this._endSubExpression();
|
|
93
|
-
if (
|
|
107
|
+
if (stopState === '_semicolon') {
|
|
108
|
+
handlers.semicolon.call(this);
|
|
109
|
+
}
|
|
110
|
+
else if (this._parentStop) {
|
|
94
111
|
return stopState;
|
|
95
112
|
}
|
|
96
|
-
|
|
113
|
+
else {
|
|
114
|
+
this._state = stopState;
|
|
115
|
+
}
|
|
97
116
|
}
|
|
98
117
|
}
|
|
118
|
+
else if (token.type === 'semicolon' && this._stopMap[token.type]) {
|
|
119
|
+
return this._stopMap[token.type];
|
|
120
|
+
}
|
|
99
121
|
else if (state.tokenTypes?.[token.type]) {
|
|
100
122
|
const typeOpts = state.tokenTypes[token.type];
|
|
101
123
|
let handleFunc = handlers[token.type];
|
|
@@ -124,7 +146,9 @@ class Parser {
|
|
|
124
146
|
* the {@link Lexer#tokenize} function.
|
|
125
147
|
*/
|
|
126
148
|
addTokens(tokens) {
|
|
127
|
-
|
|
149
|
+
for (const token of tokens) {
|
|
150
|
+
this.addToken(token);
|
|
151
|
+
}
|
|
128
152
|
}
|
|
129
153
|
/**
|
|
130
154
|
* Marks this Parser instance as completed and retrieves the full AST.
|
|
@@ -142,7 +166,9 @@ class Parser {
|
|
|
142
166
|
this._endSubExpression();
|
|
143
167
|
}
|
|
144
168
|
if (this._sequenceExpressions) {
|
|
145
|
-
|
|
169
|
+
if (this._tree) {
|
|
170
|
+
this._sequenceExpressions.push(this._tree);
|
|
171
|
+
}
|
|
146
172
|
const sequence = {
|
|
147
173
|
type: 'SequenceExpression',
|
|
148
174
|
expressions: this._sequenceExpressions
|
|
@@ -181,7 +207,6 @@ class Parser {
|
|
|
181
207
|
this._tree = node;
|
|
182
208
|
}
|
|
183
209
|
else {
|
|
184
|
-
;
|
|
185
210
|
this._cursor.right = node;
|
|
186
211
|
this._setParent(node, this._cursor);
|
|
187
212
|
}
|
|
@@ -226,6 +251,9 @@ class Parser {
|
|
|
226
251
|
this._parentStop = true;
|
|
227
252
|
endStates = this._stopMap;
|
|
228
253
|
}
|
|
254
|
+
if (states_ts_1.states[this._state].completable && !endStates.semicolon) {
|
|
255
|
+
endStates = { ...endStates, semicolon: '_semicolon' };
|
|
256
|
+
}
|
|
229
257
|
this._subParser = new Parser(this._grammar, this._lexer, exprStr, endStates);
|
|
230
258
|
}
|
|
231
259
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Parser.js","sourceRoot":"","sources":["../../src/parser/Parser.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,wDAAyC;AACzC,2CAAoC;
|
|
1
|
+
{"version":3,"file":"Parser.js","sourceRoot":"","sources":["../../src/parser/Parser.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,wDAAyC;AACzC,2CAAoC;AAMpC;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,MAAM;IACV,QAAQ,CAAS;IACjB,MAAM,CAAO;IACb,MAAM,CAAQ;IACd,KAAK,CAAgB;IACrB,QAAQ,CAAQ;IAChB,SAAS,CAAS;IAClB,QAAQ,CAAwB;IAChC,OAAO,CAAiB;IACxB,UAAU,CAAS;IACnB,WAAW,CAAU;IACrB,qBAAqB,CAAU;IAC/B,kBAAkB,CAAU;IAC5B,UAAU,CAAS;IACnB,oBAAoB,CAAY;IAEhC,YACE,OAAgB,EAChB,KAAY,EACZ,MAAe,EACf,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}
|
|
@@ -94,12 +94,6 @@ export declare function ternaryMid(this: Parser, ast: AstNode | null): void;
|
|
|
94
94
|
* test element.
|
|
95
95
|
*/
|
|
96
96
|
export declare function ternaryStart(this: Parser): void;
|
|
97
|
-
/**
|
|
98
|
-
* Handles identifier tokens when used to indicate the name of a transform to
|
|
99
|
-
* be applied.
|
|
100
|
-
* @param {{type: <string>}} token A token object
|
|
101
|
-
*/
|
|
102
|
-
export declare function transform(this: Parser, token: Token): void;
|
|
103
97
|
/**
|
|
104
98
|
* Handles token of type 'unaryOp', indicating that the operation has only
|
|
105
99
|
* one input: a right side.
|
package/dist/parser/handlers.js
CHANGED
|
@@ -21,9 +21,9 @@ exports.subExpression = subExpression;
|
|
|
21
21
|
exports.ternaryEnd = ternaryEnd;
|
|
22
22
|
exports.ternaryMid = ternaryMid;
|
|
23
23
|
exports.ternaryStart = ternaryStart;
|
|
24
|
-
exports.transform = transform;
|
|
25
24
|
exports.unaryOp = unaryOp;
|
|
26
25
|
exports.semicolon = semicolon;
|
|
26
|
+
const grammar_ts_1 = require("../grammar.js");
|
|
27
27
|
/**
|
|
28
28
|
* Handles a subexpression that's used to define a transform argument's value.
|
|
29
29
|
* @param {{type: <string>}} ast The subexpression tree
|
|
@@ -39,10 +39,11 @@ function argVal(ast) {
|
|
|
39
39
|
* initialized with an empty array.
|
|
40
40
|
*/
|
|
41
41
|
function arrayStart() {
|
|
42
|
-
|
|
42
|
+
const node = {
|
|
43
43
|
type: 'ArrayLiteral',
|
|
44
44
|
value: []
|
|
45
|
-
}
|
|
45
|
+
};
|
|
46
|
+
this._placeAtCursor(node);
|
|
46
47
|
}
|
|
47
48
|
/**
|
|
48
49
|
* Handles a subexpression representing an element of an array literal.
|
|
@@ -61,7 +62,10 @@ function arrayVal(ast) {
|
|
|
61
62
|
*/
|
|
62
63
|
function binaryOp(token) {
|
|
63
64
|
if (token.value === '=') {
|
|
64
|
-
|
|
65
|
+
// `from` marks a member access such as `a.b`; only a bare name can be
|
|
66
|
+
// assigned to, since the Evaluator writes straight into the context
|
|
67
|
+
if (this._cursor?.type !== 'Identifier' ||
|
|
68
|
+
this._cursor.from) {
|
|
65
69
|
throw new Error('Left side of assignment must be a variable name');
|
|
66
70
|
}
|
|
67
71
|
const node = {
|
|
@@ -73,17 +77,23 @@ function binaryOp(token) {
|
|
|
73
77
|
this._placeAtCursor(node);
|
|
74
78
|
return;
|
|
75
79
|
}
|
|
76
|
-
const
|
|
80
|
+
const tokenValue = token.value;
|
|
81
|
+
const precedence = (0, grammar_ts_1.precedenceOf)(this._grammar.elements[tokenValue]);
|
|
77
82
|
let parent = this._cursor?._parent;
|
|
78
|
-
while (parent
|
|
79
|
-
parent
|
|
80
|
-
|
|
83
|
+
while (parent) {
|
|
84
|
+
const parentExpr = parent;
|
|
85
|
+
if (!parentExpr.operator) {
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
if ((0, grammar_ts_1.precedenceOf)(this._grammar.elements[parentExpr.operator]) < precedence) {
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
81
91
|
this._cursor = parent;
|
|
82
92
|
parent = parent._parent;
|
|
83
93
|
}
|
|
84
94
|
const node = {
|
|
85
95
|
type: 'BinaryExpression',
|
|
86
|
-
operator:
|
|
96
|
+
operator: tokenValue,
|
|
87
97
|
left: this._cursor
|
|
88
98
|
};
|
|
89
99
|
this._setParent(this._cursor, node);
|
|
@@ -96,13 +106,15 @@ function binaryOp(token) {
|
|
|
96
106
|
* AST.
|
|
97
107
|
*/
|
|
98
108
|
function dot() {
|
|
99
|
-
const
|
|
109
|
+
const cursor = this._cursor;
|
|
110
|
+
// the dot continues an identifier chain unless the cursor is an operator
|
|
111
|
+
// still waiting on its right-hand side, in which case it opens a relative
|
|
112
|
+
// path instead — as in the leading dot of `list[.price > 5]`
|
|
100
113
|
this._nextIdentEncapsulate =
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
(
|
|
104
|
-
|
|
105
|
-
this._nextIdentRelative = !this._cursor || !this._nextIdentEncapsulate;
|
|
114
|
+
!!cursor &&
|
|
115
|
+
cursor.type !== 'UnaryExpression' &&
|
|
116
|
+
(cursor.type !== 'BinaryExpression' || !!cursor.right);
|
|
117
|
+
this._nextIdentRelative = !this._nextIdentEncapsulate;
|
|
106
118
|
if (this._nextIdentRelative) {
|
|
107
119
|
this._relative = true;
|
|
108
120
|
}
|
|
@@ -113,12 +125,13 @@ function dot() {
|
|
|
113
125
|
* @param {{type: <string>}} ast The subexpression tree
|
|
114
126
|
*/
|
|
115
127
|
function filter(ast) {
|
|
116
|
-
|
|
128
|
+
const node = {
|
|
117
129
|
type: 'FilterExpression',
|
|
118
130
|
expr: ast,
|
|
119
131
|
relative: this._subParser.isRelative(),
|
|
120
132
|
subject: this._cursor
|
|
121
|
-
}
|
|
133
|
+
};
|
|
134
|
+
this._placeBeforeCursor(node);
|
|
122
135
|
}
|
|
123
136
|
/**
|
|
124
137
|
* Handles identifier tokens when used to indicate the name of a function to
|
|
@@ -126,12 +139,13 @@ function filter(ast) {
|
|
|
126
139
|
* @param {{type: <string>}} token A token object
|
|
127
140
|
*/
|
|
128
141
|
function functionCall() {
|
|
129
|
-
|
|
142
|
+
const node = {
|
|
130
143
|
type: 'FunctionCall',
|
|
131
144
|
name: this._cursor.value,
|
|
132
145
|
args: [],
|
|
133
146
|
pool: 'functions'
|
|
134
|
-
}
|
|
147
|
+
};
|
|
148
|
+
this._placeBeforeCursor(node);
|
|
135
149
|
}
|
|
136
150
|
/**
|
|
137
151
|
* Handles identifier tokens by adding them as a new node in the AST.
|
|
@@ -161,10 +175,11 @@ function identifier(token) {
|
|
|
161
175
|
* @param {{type: <string>}} token A token object
|
|
162
176
|
*/
|
|
163
177
|
function literal(token) {
|
|
164
|
-
|
|
178
|
+
const node = {
|
|
165
179
|
type: 'Literal',
|
|
166
180
|
value: token.value
|
|
167
|
-
}
|
|
181
|
+
};
|
|
182
|
+
this._placeAtCursor(node);
|
|
168
183
|
}
|
|
169
184
|
/**
|
|
170
185
|
* Handles template string tokens by parsing their interpolation expressions
|
|
@@ -173,7 +188,8 @@ function literal(token) {
|
|
|
173
188
|
*/
|
|
174
189
|
function templateString(token) {
|
|
175
190
|
const parts = [];
|
|
176
|
-
|
|
191
|
+
const tokenParts = Array.isArray(token.value) ? token.value : [];
|
|
192
|
+
for (const part of tokenParts) {
|
|
177
193
|
if (part.type === 'static') {
|
|
178
194
|
parts.push({
|
|
179
195
|
type: 'static',
|
|
@@ -182,7 +198,8 @@ function templateString(token) {
|
|
|
182
198
|
}
|
|
183
199
|
else {
|
|
184
200
|
const subTokens = this._lexer.tokenize(part.value);
|
|
185
|
-
const
|
|
201
|
+
const ParserClass = this.constructor;
|
|
202
|
+
const subParser = new ParserClass(this._grammar, this._lexer);
|
|
186
203
|
subParser.addTokens(subTokens);
|
|
187
204
|
const subAst = subParser.complete();
|
|
188
205
|
if (!subAst) {
|
|
@@ -194,10 +211,11 @@ function templateString(token) {
|
|
|
194
211
|
});
|
|
195
212
|
}
|
|
196
213
|
}
|
|
197
|
-
|
|
214
|
+
const node = {
|
|
198
215
|
type: 'TemplateLiteral',
|
|
199
|
-
parts
|
|
200
|
-
}
|
|
216
|
+
parts
|
|
217
|
+
};
|
|
218
|
+
this._placeAtCursor(node);
|
|
201
219
|
}
|
|
202
220
|
/**
|
|
203
221
|
* Queues a new object literal key to be written once a value is collected.
|
|
@@ -211,10 +229,11 @@ function objKey(token) {
|
|
|
211
229
|
* initialized with an empty object.
|
|
212
230
|
*/
|
|
213
231
|
function objStart() {
|
|
214
|
-
|
|
232
|
+
const node = {
|
|
215
233
|
type: 'ObjectLiteral',
|
|
216
234
|
value: {}
|
|
217
|
-
}
|
|
235
|
+
};
|
|
236
|
+
this._placeAtCursor(node);
|
|
218
237
|
}
|
|
219
238
|
/**
|
|
220
239
|
* Handles an object value by adding its AST to the queued key on the object
|
|
@@ -239,7 +258,7 @@ function subExpression(ast) {
|
|
|
239
258
|
*/
|
|
240
259
|
function ternaryEnd(ast) {
|
|
241
260
|
;
|
|
242
|
-
this._cursor.alternate = ast;
|
|
261
|
+
this._cursor.alternate = ast ?? undefined;
|
|
243
262
|
}
|
|
244
263
|
/**
|
|
245
264
|
* Handles a completed consequent subexpression of a ternary operator.
|
|
@@ -247,7 +266,7 @@ function ternaryEnd(ast) {
|
|
|
247
266
|
*/
|
|
248
267
|
function ternaryMid(ast) {
|
|
249
268
|
;
|
|
250
|
-
this._cursor.consequent = ast;
|
|
269
|
+
this._cursor.consequent = ast ?? undefined;
|
|
251
270
|
}
|
|
252
271
|
/**
|
|
253
272
|
* Handles the start of a new ternary expression by encapsulating the entire
|
|
@@ -255,35 +274,24 @@ function ternaryMid(ast) {
|
|
|
255
274
|
* test element.
|
|
256
275
|
*/
|
|
257
276
|
function ternaryStart() {
|
|
258
|
-
|
|
277
|
+
const node = {
|
|
259
278
|
type: 'ConditionalExpression',
|
|
260
279
|
test: this._tree
|
|
261
280
|
};
|
|
281
|
+
this._tree = node;
|
|
262
282
|
this._cursor = this._tree;
|
|
263
283
|
}
|
|
264
|
-
/**
|
|
265
|
-
* Handles identifier tokens when used to indicate the name of a transform to
|
|
266
|
-
* be applied.
|
|
267
|
-
* @param {{type: <string>}} token A token object
|
|
268
|
-
*/
|
|
269
|
-
function transform(token) {
|
|
270
|
-
this._placeBeforeCursor({
|
|
271
|
-
type: 'FunctionCall',
|
|
272
|
-
name: token.value,
|
|
273
|
-
args: [this._cursor],
|
|
274
|
-
pool: 'transforms'
|
|
275
|
-
});
|
|
276
|
-
}
|
|
277
284
|
/**
|
|
278
285
|
* Handles token of type 'unaryOp', indicating that the operation has only
|
|
279
286
|
* one input: a right side.
|
|
280
287
|
* @param {{type: <string>}} token A token object
|
|
281
288
|
*/
|
|
282
289
|
function unaryOp(token) {
|
|
283
|
-
|
|
290
|
+
const node = {
|
|
284
291
|
type: 'UnaryExpression',
|
|
285
292
|
operator: token.value
|
|
286
|
-
}
|
|
293
|
+
};
|
|
294
|
+
this._placeAtCursor(node);
|
|
287
295
|
}
|
|
288
296
|
/**
|
|
289
297
|
* Handles semicolon separator between expressions
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handlers.js","sourceRoot":"","sources":["../../src/parser/handlers.ts"],"names":[],"mappings":";AAAA;;;GAGG;;
|
|
1
|
+
{"version":3,"file":"handlers.js","sourceRoot":"","sources":["../../src/parser/handlers.ts"],"names":[],"mappings":";AAAA;;;GAGG;;AAyBH,wBAIC;AAMD,gCAMC;AAMD,4BAIC;AAOD,4BA6CC;AAOD,kBAcC;AAOD,wBAQC;AAOD,oCAQC;AAMD,gCAgBC;AAOD,0BAMC;AAOD,wCAoCC;AAMD,wBAEC;AAMD,4BAMC;AAOD,wBAEC;AAOD,sCAEC;AAMD,gCAEC;AAMD,gCAEC;AAOD,oCAOC;AAOD,0BAMC;AAKD,8BAUC;AAtUD,8CAA4C;AAmB5C;;;GAGG;AACH,SAAgB,MAAM,CAAe,GAAmB;IACtD,IAAI,GAAG,EAAE,CAAC;QACR,CAAC;QAAC,IAAI,CAAC,OAAwB,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;IAChD,CAAC;AACH,CAAC;AAED;;;GAGG;AACH,SAAgB,UAAU;IACxB,MAAM,IAAI,GAAiB;QACzB,IAAI,EAAE,cAAc;QACpB,KAAK,EAAE,EAAE;KACV,CAAA;IACD,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,CAAA;AAC3B,CAAC;AAED;;;GAGG;AACH,SAAgB,QAAQ,CAAe,GAAmB;IACxD,IAAI,GAAG,EAAE,CAAC;QACR,CAAC;QAAC,IAAI,CAAC,OAAwB,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;IACjD,CAAC;AACH,CAAC;AAED;;;;GAIG;AACH,SAAgB,QAAQ,CAAe,KAAY;IACjD,IAAI,KAAK,CAAC,KAAK,KAAK,GAAG,EAAE,CAAC;QACxB,sEAAsE;QACtE,oEAAoE;QACpE,IACE,IAAI,CAAC,O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OAAiC,CAAC,SAAS,GAAG,GAAG,IAAI,SAAS,CAAA;AACvE,CAAC;AAED;;;GAGG;AACH,SAAgB,UAAU,CAAe,GAAmB;IAC1D,CAAC;IAAC,IAAI,CAAC,OAAiC,CAAC,UAAU,GAAG,GAAG,IAAI,SAAS,CAAA;AACxE,CAAC;AAED;;;;GAIG;AACH,SAAgB,YAAY;IAC1B,MAAM,IAAI,GAA0B;QAClC,IAAI,EAAE,uBAAuB;QAC7B,IAAI,EAAE,IAAI,CAAC,KAAM;KAClB,CAAA;IACD,IAAI,CAAC,KAAK,GAAG,IAAI,CAAA;IACjB,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,KAAK,CAAA;AAC3B,CAAC;AAED;;;;GAIG;AACH,SAAgB,OAAO,CAAe,KAAY;IAChD,MAAM,IAAI,GAAoB;QAC5B,IAAI,EAAE,iBAAiB;QACvB,QAAQ,EAAE,KAAK,CAAC,KAAe;KAChC,CAAA;IACD,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,CAAA;AAC3B,CAAC;AAED;;GAEG;AACH,SAAgB,SAAS;IACvB,IAAI,CAAC,IAAI,CAAC,oBAAoB,EAAE,CAAC;QAC/B,IAAI,CAAC,oBAAoB,GAAG,CAAC,IAAI,CAAC,KAAM,CAAC,CAAA;IAC3C,CAAC;SAAM,CAAC;QACN,IAAI,CAAC,oBAAoB,CAAC,IAAI,CAAC,IAAI,CAAC,KAAM,CAAC,CAAA;IAC7C,CAAC;IAED,IAAI,CAAC,KAAK,GAAG,IAAI,CAAA;IACjB,IAAI,CAAC,OAAO,GAAG,IAAI,CAAA;IACnB,IAAI,CAAC,MAAM,GAAG,eAAe,CAAA;AAC/B,CAAC"}
|
package/dist/parser/states.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type { AstNode } from '../types.ts';
|
|
1
|
+
import type { AstNode, Token } from '../types.ts';
|
|
2
2
|
import type Parser from './Parser.ts';
|
|
3
3
|
interface TokenTypeOptions {
|
|
4
4
|
toState?: string;
|
|
5
|
-
handler?: (this: Parser,
|
|
5
|
+
handler?: (this: Parser, token: Token) => void;
|
|
6
6
|
}
|
|
7
7
|
interface StateDefinition {
|
|
8
8
|
tokenTypes?: Record<string, TokenTypeOptions>;
|
package/dist/parser/states.js
CHANGED
|
@@ -87,18 +87,13 @@ exports.states = {
|
|
|
87
87
|
expectBinOp: {
|
|
88
88
|
tokenTypes: {
|
|
89
89
|
binaryOp: { toState: 'expectOperand' },
|
|
90
|
-
pipe: { toState: 'expectTransform' },
|
|
91
90
|
dot: { toState: 'traverse' },
|
|
91
|
+
openBracket: { toState: 'filter' },
|
|
92
92
|
question: { toState: 'ternaryMid', handler: h.ternaryStart },
|
|
93
93
|
semicolon: { handler: h.semicolon }
|
|
94
94
|
},
|
|
95
95
|
completable: true
|
|
96
96
|
},
|
|
97
|
-
expectTransform: {
|
|
98
|
-
tokenTypes: {
|
|
99
|
-
identifier: { toState: 'postTransform', handler: h.transform }
|
|
100
|
-
}
|
|
101
|
-
},
|
|
102
97
|
expectObjKey: {
|
|
103
98
|
tokenTypes: {
|
|
104
99
|
literal: { toState: 'expectKeyValSep', handler: h.objKey },
|
|
@@ -111,24 +106,13 @@ exports.states = {
|
|
|
111
106
|
colon: { toState: 'objVal' }
|
|
112
107
|
}
|
|
113
108
|
},
|
|
114
|
-
postTransform: {
|
|
115
|
-
tokenTypes: {
|
|
116
|
-
openParen: { toState: 'argVal' },
|
|
117
|
-
binaryOp: { toState: 'expectOperand' },
|
|
118
|
-
dot: { toState: 'traverse' },
|
|
119
|
-
openBracket: { toState: 'filter' },
|
|
120
|
-
pipe: { toState: 'expectTransform' },
|
|
121
|
-
question: { toState: 'ternaryMid', handler: h.ternaryStart }
|
|
122
|
-
},
|
|
123
|
-
completable: true
|
|
124
|
-
},
|
|
125
109
|
postArgs: {
|
|
126
110
|
tokenTypes: {
|
|
127
111
|
binaryOp: { toState: 'expectOperand' },
|
|
128
112
|
dot: { toState: 'traverse' },
|
|
129
113
|
openBracket: { toState: 'filter' },
|
|
130
|
-
|
|
131
|
-
|
|
114
|
+
question: { toState: 'ternaryMid', handler: h.ternaryStart },
|
|
115
|
+
semicolon: { handler: h.semicolon }
|
|
132
116
|
},
|
|
133
117
|
completable: true
|
|
134
118
|
},
|
|
@@ -138,8 +122,8 @@ exports.states = {
|
|
|
138
122
|
dot: { toState: 'traverse' },
|
|
139
123
|
openBracket: { toState: 'filter' },
|
|
140
124
|
openParen: { toState: 'argVal', handler: h.functionCall },
|
|
141
|
-
|
|
142
|
-
|
|
125
|
+
question: { toState: 'ternaryMid', handler: h.ternaryStart },
|
|
126
|
+
semicolon: { handler: h.semicolon }
|
|
143
127
|
},
|
|
144
128
|
completable: true
|
|
145
129
|
},
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"states.js","sourceRoot":"","sources":["../../src/parser/states.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEH,iDAAkC;AAmBlC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACU,QAAA,MAAM,GAAW;IAC5B,aAAa,EAAE;QACb,UAAU,EAAE;YACV,OAAO,EAAE,EAAE,OAAO,EAAE,aAAa,EAAE;YACnC,cAAc,EAAE,EAAE,OAAO,EAAE,aAAa,EAAE;YAC1C,UAAU,EAAE,EAAE,OAAO,EAAE,YAAY,EAAE;YACrC,OAAO,EAAE,EAAE;YACX,SAAS,EAAE,EAAE,OAAO,EAAE,eAAe,EAAE;YACvC,QAAQ,EAAE,EAAE,OAAO,EAAE,cAAc,EAAE,OAAO,EAAE,CAAC,CAAC,QAAQ,EAAE;YAC1D,GAAG,EAAE,EAAE,OAAO,EAAE,UAAU,EAAE;YAC5B,WAAW,EAAE,EAAE,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,CAAC,CAAC,UAAU,EAAE;SAC5D;KACF;IACD,WAAW,EAAE;QACX,UAAU,EAAE;YACV,QAAQ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YAAY;SAC3B;KACF;IACD,aAAa,EAAE;QACb,UAAU,EAAE,CAAC,CAAC,aAAa;QAC3B,SAAS,EAAE;YACT,UAAU,EAAE,aAAa;SAC1B;KACF;IACD,MAAM,EAAE;QACN,UAAU,EAAE,CAAC,CAAC,MAAM;QACpB,SAAS,EAAE;YACT,KAAK,EAAE,QAAQ;YACf,UAAU,EAAE,UAAU;SACvB;KACF;IACD,MAAM,EAAE;QACN,UAAU,EAAE,CAAC,CAAC,MAAM;QACpB,SAAS,EAAE;YACT,KAAK,EAAE,cAAc;YACrB,SAAS,EAAE,aAAa;SACzB;KACF;IACD,QAAQ,EAAE;QACR,UAAU,EAAE,CAAC,CAAC,QAAQ;QACtB,SAAS,EAAE;YACT,KAAK,EAAE,UAAU;YACjB,YAAY,EAAE,aAAa;SAC5B;KACF;IACD,UAAU,EAAE;QACV,UAAU,EAAE,CAAC,CAAC,UAAU;QACxB,SAAS,EAAE;YACT,KAAK,EAAE,YAAY;SACpB;KACF;IACD,UAAU,EAAE;QACV,UAAU,EAAE,CAAC,CAAC,UAAU;QACxB,WAAW,EAAE,IAAI;KAClB;CACF,CAAA"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,11 +1,19 @@
|
|
|
1
|
+
export interface TemplatePart {
|
|
2
|
+
type: 'static' | 'interpolation';
|
|
3
|
+
value: string;
|
|
4
|
+
}
|
|
5
|
+
export type JexlValue = string | number | boolean | null | undefined | JexlValue[] | {
|
|
6
|
+
[key: string]: JexlValue;
|
|
7
|
+
};
|
|
1
8
|
export interface Token {
|
|
2
9
|
type: string;
|
|
3
|
-
value:
|
|
10
|
+
value: string | number | boolean | TemplatePart[];
|
|
4
11
|
raw: string;
|
|
5
12
|
}
|
|
6
13
|
export interface AstNode {
|
|
7
14
|
type: string;
|
|
8
15
|
_parent?: AstNode;
|
|
16
|
+
right?: AstNode;
|
|
9
17
|
}
|
|
10
18
|
export interface Literal extends AstNode {
|
|
11
19
|
type: 'Literal';
|
|
@@ -36,18 +44,22 @@ export interface ObjectLiteral extends AstNode {
|
|
|
36
44
|
type: 'ObjectLiteral';
|
|
37
45
|
value: Record<string, AstNode>;
|
|
38
46
|
}
|
|
47
|
+
export type TemplateLiteralPart = {
|
|
48
|
+
type: 'static';
|
|
49
|
+
value: string;
|
|
50
|
+
} | {
|
|
51
|
+
type: 'expression';
|
|
52
|
+
value: AstNode;
|
|
53
|
+
};
|
|
39
54
|
export interface TemplateLiteral extends AstNode {
|
|
40
55
|
type: 'TemplateLiteral';
|
|
41
|
-
parts:
|
|
42
|
-
type: 'static' | 'expression';
|
|
43
|
-
value: string | AstNode;
|
|
44
|
-
}[];
|
|
56
|
+
parts: TemplateLiteralPart[];
|
|
45
57
|
}
|
|
46
58
|
export interface FunctionCall extends AstNode {
|
|
47
59
|
type: 'FunctionCall';
|
|
48
60
|
name: string;
|
|
49
61
|
args: AstNode[];
|
|
50
|
-
pool: 'functions'
|
|
62
|
+
pool: 'functions';
|
|
51
63
|
}
|
|
52
64
|
export interface FilterExpression extends AstNode {
|
|
53
65
|
type: 'FilterExpression';
|
|
@@ -69,7 +81,6 @@ export interface AssignmentExpression extends AstNode {
|
|
|
69
81
|
type: 'AssignmentExpression';
|
|
70
82
|
operator: '=';
|
|
71
83
|
left: Identifier;
|
|
72
|
-
right: AstNode;
|
|
73
84
|
}
|
|
74
85
|
export type AstNodeUnion = Literal | Identifier | BinaryExpression | UnaryExpression | ArrayLiteral | ObjectLiteral | TemplateLiteral | FunctionCall | FilterExpression | ConditionalExpression | SequenceExpression | AssignmentExpression;
|
|
75
86
|
export type NodeByType<T extends AstNodeUnion['type']> = Extract<AstNodeUnion, {
|
package/esm/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;
|