@aws-lambda-powertools/jmespath 2.3.0 → 2.5.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/lib/cjs/Expression.js +5 -18
- package/lib/cjs/Functions.js +4 -6
- package/lib/cjs/Lexer.js +275 -204
- package/lib/cjs/ParsedResult.js +2 -0
- package/lib/cjs/Parser.js +676 -433
- package/lib/cjs/TreeInterpreter.js +364 -233
- package/lib/cjs/errors.d.ts +0 -6
- package/lib/cjs/errors.d.ts.map +1 -1
- package/lib/cjs/errors.js +39 -0
- package/lib/esm/Expression.js +5 -18
- package/lib/esm/Functions.js +4 -6
- package/lib/esm/Lexer.js +274 -203
- package/lib/esm/ParsedResult.js +2 -0
- package/lib/esm/Parser.js +675 -432
- package/lib/esm/TreeInterpreter.js +363 -232
- package/lib/esm/errors.d.ts +0 -6
- package/lib/esm/errors.d.ts.map +1 -1
- package/lib/esm/errors.js +39 -0
- package/package.json +2 -2
package/lib/esm/Parser.js
CHANGED
|
@@ -1,15 +1,3 @@
|
|
|
1
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
-
};
|
|
7
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
-
};
|
|
12
|
-
var _Parser_instances, _Parser_projectionStop, _Parser_cache, _Parser_maxCacheSize, _Parser_tokenizer, _Parser_tokens, _Parser_index, _Parser_doParse, _Parser_parse, _Parser_expression, _Parser_getNudFunction, _Parser_processQuotedIdentifier, _Parser_processStarToken, _Parser_processLParenTokenNud, _Parser_processFlattenTokenNud, _Parser_processLBracketTokenNud, _Parser_processDefaultToken, _Parser_getLedFunction, _Parser_processDotToken, _Parser_processPipeToken, _Parser_processOrToken, _Parser_processAndToken, _Parser_processLParenToken, _Parser_processFilterToken, _Parser_processFlattenToken, _Parser_processLBracketToken, _Parser_throwParseError, _Parser_parseIndexExpression, _Parser_parseSliceExpression, _Parser_projectIfSlice, _Parser_parseComparator, _Parser_parseMultiSelectList, _Parser_parseMultiSelectHash, _Parser_parseProjectionRhs, _Parser_parseDotRhs, _Parser_match, _Parser_matchMultipleTokens, _Parser_advance, _Parser_currentToken, _Parser_lookahead, _Parser_lookaheadToken, _Parser_evictCache;
|
|
13
1
|
import { randomInt } from 'node:crypto';
|
|
14
2
|
import { Lexer } from './Lexer.js';
|
|
15
3
|
import { ParsedResult } from './ParsedResult.js';
|
|
@@ -31,25 +19,24 @@ import { IncompleteExpressionError, LexerError, ParseError } from './errors.js';
|
|
|
31
19
|
* - [Top Down Operator Precedence](http://javascript.crockford.com/tdop/tdop.html)
|
|
32
20
|
*/
|
|
33
21
|
class Parser {
|
|
22
|
+
/**
|
|
23
|
+
* The maximum binding power for a token
|
|
24
|
+
* that can stop a projection.
|
|
25
|
+
*/
|
|
26
|
+
#projectionStop = 10;
|
|
27
|
+
/**
|
|
28
|
+
* Cache object
|
|
29
|
+
*/
|
|
30
|
+
#cache = {};
|
|
31
|
+
/**
|
|
32
|
+
* The maximum size of the cache.
|
|
33
|
+
*/
|
|
34
|
+
#maxCacheSize = 128;
|
|
35
|
+
#tokenizer;
|
|
36
|
+
#tokens;
|
|
37
|
+
#index = 0;
|
|
34
38
|
constructor(lookahead = 2) {
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* The maximum binding power for a token
|
|
38
|
-
* that can stop a projection.
|
|
39
|
-
*/
|
|
40
|
-
_Parser_projectionStop.set(this, 10);
|
|
41
|
-
/**
|
|
42
|
-
* Cache object
|
|
43
|
-
*/
|
|
44
|
-
_Parser_cache.set(this, {});
|
|
45
|
-
/**
|
|
46
|
-
* The maximum size of the cache.
|
|
47
|
-
*/
|
|
48
|
-
_Parser_maxCacheSize.set(this, 128);
|
|
49
|
-
_Parser_tokenizer.set(this, void 0);
|
|
50
|
-
_Parser_tokens.set(this, void 0);
|
|
51
|
-
_Parser_index.set(this, 0);
|
|
52
|
-
__classPrivateFieldSet(this, _Parser_tokens, Array.from({ length: lookahead }), "f");
|
|
39
|
+
this.#tokens = Array.from({ length: lookahead });
|
|
53
40
|
}
|
|
54
41
|
/**
|
|
55
42
|
* Parse a JMESPath expression and return the Abstract Syntax Tree (AST)
|
|
@@ -61,14 +48,14 @@ class Parser {
|
|
|
61
48
|
* @param expression The JMESPath expression to parse.
|
|
62
49
|
*/
|
|
63
50
|
parse(expression) {
|
|
64
|
-
const cached =
|
|
51
|
+
const cached = this.#cache[expression];
|
|
65
52
|
if (cached) {
|
|
66
53
|
return cached;
|
|
67
54
|
}
|
|
68
|
-
const parsedResult =
|
|
69
|
-
|
|
70
|
-
if (Object.keys(
|
|
71
|
-
|
|
55
|
+
const parsedResult = this.#doParse(expression);
|
|
56
|
+
this.#cache[expression] = parsedResult;
|
|
57
|
+
if (Object.keys(this.#cache).length > this.#maxCacheSize) {
|
|
58
|
+
this.#evictCache();
|
|
72
59
|
}
|
|
73
60
|
return parsedResult;
|
|
74
61
|
}
|
|
@@ -76,428 +63,684 @@ class Parser {
|
|
|
76
63
|
* Purge the entire cache.
|
|
77
64
|
*/
|
|
78
65
|
purgeCache() {
|
|
79
|
-
|
|
66
|
+
this.#cache = {};
|
|
80
67
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
68
|
+
/**
|
|
69
|
+
* Do the actual parsing of the expression.
|
|
70
|
+
*
|
|
71
|
+
* @param expression The JMESPath expression to parse.
|
|
72
|
+
*/
|
|
73
|
+
#doParse(expression) {
|
|
74
|
+
try {
|
|
75
|
+
return this.#parse(expression);
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
if (error instanceof LexerError ||
|
|
79
|
+
error instanceof IncompleteExpressionError ||
|
|
80
|
+
error instanceof ParseError) {
|
|
81
|
+
error.setExpression(expression);
|
|
82
|
+
throw error;
|
|
83
|
+
}
|
|
91
84
|
throw error;
|
|
92
85
|
}
|
|
93
|
-
throw error;
|
|
94
|
-
}
|
|
95
|
-
}, _Parser_parse = function _Parser_parse(expression) {
|
|
96
|
-
__classPrivateFieldSet(this, _Parser_tokenizer, new Lexer(), "f");
|
|
97
|
-
__classPrivateFieldSet(this, _Parser_tokens, [...__classPrivateFieldGet(this, _Parser_tokenizer, "f").tokenize(expression)], "f");
|
|
98
|
-
__classPrivateFieldSet(this, _Parser_index, 0, "f");
|
|
99
|
-
const parsed = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_expression).call(this, 0);
|
|
100
|
-
if (__classPrivateFieldGet(this, _Parser_instances, "m", _Parser_currentToken).call(this) !== 'eof') {
|
|
101
|
-
__classPrivateFieldGet(this, _Parser_instances, "m", _Parser_throwParseError).call(this);
|
|
102
|
-
}
|
|
103
|
-
return new ParsedResult(expression, parsed);
|
|
104
|
-
}, _Parser_expression = function _Parser_expression(bindingPower = 0) {
|
|
105
|
-
const leftToken = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_lookaheadToken).call(this, 0);
|
|
106
|
-
__classPrivateFieldGet(this, _Parser_instances, "m", _Parser_advance).call(this);
|
|
107
|
-
let left = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_getNudFunction).call(this, leftToken);
|
|
108
|
-
let currentToken = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_currentToken).call(this);
|
|
109
|
-
while (bindingPower < BINDING_POWER[currentToken]) {
|
|
110
|
-
__classPrivateFieldGet(this, _Parser_instances, "m", _Parser_advance).call(this);
|
|
111
|
-
left = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_getLedFunction).call(this, currentToken, left);
|
|
112
|
-
currentToken = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_currentToken).call(this);
|
|
113
|
-
}
|
|
114
|
-
return left;
|
|
115
|
-
}, _Parser_getNudFunction = function _Parser_getNudFunction(token) {
|
|
116
|
-
const { type: tokenType } = token;
|
|
117
|
-
switch (tokenType) {
|
|
118
|
-
case 'literal':
|
|
119
|
-
return literal(token.value);
|
|
120
|
-
case 'unquoted_identifier':
|
|
121
|
-
return field(token.value);
|
|
122
|
-
case 'quoted_identifier':
|
|
123
|
-
return __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_processQuotedIdentifier).call(this, token);
|
|
124
|
-
case 'star':
|
|
125
|
-
return __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_processStarToken).call(this);
|
|
126
|
-
case 'filter':
|
|
127
|
-
return __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_getLedFunction).call(this, tokenType, identity());
|
|
128
|
-
case 'lbrace':
|
|
129
|
-
return __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_parseMultiSelectHash).call(this);
|
|
130
|
-
case 'lparen':
|
|
131
|
-
return __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_processLParenTokenNud).call(this);
|
|
132
|
-
case 'flatten':
|
|
133
|
-
return __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_processFlattenTokenNud).call(this);
|
|
134
|
-
case 'not':
|
|
135
|
-
return notExpression(__classPrivateFieldGet(this, _Parser_instances, "m", _Parser_expression).call(this, BINDING_POWER['not']));
|
|
136
|
-
case 'lbracket':
|
|
137
|
-
return __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_processLBracketTokenNud).call(this);
|
|
138
|
-
case 'current':
|
|
139
|
-
return currentNode();
|
|
140
|
-
case 'expref':
|
|
141
|
-
return expref(__classPrivateFieldGet(this, _Parser_instances, "m", _Parser_expression).call(this, BINDING_POWER['expref']));
|
|
142
|
-
default:
|
|
143
|
-
return __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_processDefaultToken).call(this, token);
|
|
144
|
-
}
|
|
145
|
-
}, _Parser_processQuotedIdentifier = function _Parser_processQuotedIdentifier(token) {
|
|
146
|
-
const fieldValue = field(token.value);
|
|
147
|
-
if (__classPrivateFieldGet(this, _Parser_instances, "m", _Parser_currentToken).call(this) === 'lparen') {
|
|
148
|
-
__classPrivateFieldGet(this, _Parser_instances, "m", _Parser_throwParseError).call(this, {
|
|
149
|
-
lexPosition: 0,
|
|
150
|
-
reason: 'quoted identifiers cannot be used as a function name',
|
|
151
|
-
});
|
|
152
86
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
87
|
+
/**
|
|
88
|
+
* Parse a JMESPath expression and return the parsed result.
|
|
89
|
+
*/
|
|
90
|
+
#parse(expression) {
|
|
91
|
+
this.#tokenizer = new Lexer();
|
|
92
|
+
this.#tokens = [...this.#tokenizer.tokenize(expression)];
|
|
93
|
+
this.#index = 0;
|
|
94
|
+
const parsed = this.#expression(0);
|
|
95
|
+
if (this.#currentToken() !== 'eof') {
|
|
96
|
+
this.#throwParseError();
|
|
97
|
+
}
|
|
98
|
+
return new ParsedResult(expression, parsed);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Process an expression.
|
|
102
|
+
*/
|
|
103
|
+
#expression(bindingPower = 0) {
|
|
104
|
+
const leftToken = this.#lookaheadToken(0);
|
|
105
|
+
this.#advance();
|
|
106
|
+
let left = this.#getNudFunction(leftToken);
|
|
107
|
+
let currentToken = this.#currentToken();
|
|
108
|
+
while (bindingPower < BINDING_POWER[currentToken]) {
|
|
109
|
+
this.#advance();
|
|
110
|
+
left = this.#getLedFunction(currentToken, left);
|
|
111
|
+
currentToken = this.#currentToken();
|
|
112
|
+
}
|
|
113
|
+
return left;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Get the nud function for a token. This is the function that
|
|
117
|
+
* is called when a token is found at the beginning of an expression.
|
|
118
|
+
*
|
|
119
|
+
* @param tokenType The type of token to get the nud function for.
|
|
120
|
+
*/
|
|
121
|
+
#getNudFunction(token) {
|
|
122
|
+
const { type: tokenType } = token;
|
|
123
|
+
switch (tokenType) {
|
|
124
|
+
case 'literal':
|
|
125
|
+
return literal(token.value);
|
|
126
|
+
case 'unquoted_identifier':
|
|
127
|
+
return field(token.value);
|
|
128
|
+
case 'quoted_identifier':
|
|
129
|
+
return this.#processQuotedIdentifier(token);
|
|
130
|
+
case 'star':
|
|
131
|
+
return this.#processStarToken();
|
|
132
|
+
case 'filter':
|
|
133
|
+
return this.#getLedFunction(tokenType, identity());
|
|
134
|
+
case 'lbrace':
|
|
135
|
+
return this.#parseMultiSelectHash();
|
|
136
|
+
case 'lparen':
|
|
137
|
+
return this.#processLParenTokenNud();
|
|
138
|
+
case 'flatten':
|
|
139
|
+
return this.#processFlattenTokenNud();
|
|
140
|
+
case 'not':
|
|
141
|
+
return notExpression(this.#expression(BINDING_POWER['not']));
|
|
142
|
+
case 'lbracket':
|
|
143
|
+
return this.#processLBracketTokenNud();
|
|
144
|
+
case 'current':
|
|
145
|
+
return currentNode();
|
|
146
|
+
case 'expref':
|
|
147
|
+
return expref(this.#expression(BINDING_POWER['expref']));
|
|
148
|
+
default:
|
|
149
|
+
return this.#processDefaultToken(token);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Process a quoted identifier.
|
|
154
|
+
*
|
|
155
|
+
* A quoted identifier is a string that is enclosed in double quotes.
|
|
156
|
+
*
|
|
157
|
+
* @example s."foo"
|
|
158
|
+
*
|
|
159
|
+
* @param token The token to process
|
|
160
|
+
*/
|
|
161
|
+
#processQuotedIdentifier(token) {
|
|
162
|
+
const fieldValue = field(token.value);
|
|
163
|
+
if (this.#currentToken() === 'lparen') {
|
|
164
|
+
this.#throwParseError({
|
|
165
|
+
lexPosition: 0,
|
|
166
|
+
reason: 'quoted identifiers cannot be used as a function name',
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
return fieldValue;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Process a star token.
|
|
173
|
+
*
|
|
174
|
+
* A star token is a syntax that allows you to project all the
|
|
175
|
+
* elements in a list or dictionary.
|
|
176
|
+
*
|
|
177
|
+
* @example foo[*]
|
|
178
|
+
*/
|
|
179
|
+
#processStarToken() {
|
|
180
|
+
const left = identity();
|
|
181
|
+
let right;
|
|
182
|
+
if (this.#currentToken() === 'rbracket') {
|
|
183
|
+
right = identity();
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
right = this.#parseProjectionRhs(BINDING_POWER['star']);
|
|
187
|
+
}
|
|
188
|
+
return valueProjection(left, right);
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Process a left parenthesis token.
|
|
192
|
+
*
|
|
193
|
+
* A left parenthesis token is a syntax that allows you to group
|
|
194
|
+
* expressions together.
|
|
195
|
+
*
|
|
196
|
+
* @example (foo.bar)
|
|
197
|
+
*/
|
|
198
|
+
#processLParenTokenNud() {
|
|
199
|
+
const expression = this.#expression();
|
|
200
|
+
this.#match('rparen');
|
|
201
|
+
return expression;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Process a flatten token.
|
|
205
|
+
*
|
|
206
|
+
* A flatten token is a syntax that allows you to flatten the
|
|
207
|
+
* results of a subexpression.
|
|
208
|
+
*
|
|
209
|
+
* @example foo[].bar
|
|
210
|
+
*/
|
|
211
|
+
#processFlattenTokenNud() {
|
|
212
|
+
const left = flatten(identity());
|
|
213
|
+
const right = this.#parseProjectionRhs(BINDING_POWER['flatten']);
|
|
214
|
+
return projection(left, right);
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Process a left bracket token.
|
|
218
|
+
*
|
|
219
|
+
* A left bracket token is a syntax that allows you to access
|
|
220
|
+
* elements in a list or dictionary.
|
|
221
|
+
*
|
|
222
|
+
* @example foo[0]
|
|
223
|
+
*/
|
|
224
|
+
#processLBracketTokenNud() {
|
|
225
|
+
if (['number', 'colon'].includes(this.#currentToken())) {
|
|
226
|
+
const right = this.#parseIndexExpression();
|
|
227
|
+
return this.#projectIfSlice(identity(), right);
|
|
228
|
+
}
|
|
229
|
+
else if (this.#currentToken() === 'star' &&
|
|
230
|
+
this.#lookahead(1) === 'rbracket') {
|
|
231
|
+
this.#advance();
|
|
232
|
+
this.#advance();
|
|
233
|
+
const right = this.#parseProjectionRhs(BINDING_POWER['star']);
|
|
234
|
+
return projection(identity(), right);
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
return this.#parseMultiSelectList();
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Process a default token.
|
|
242
|
+
*
|
|
243
|
+
* A default token is a syntax that allows you to access
|
|
244
|
+
* elements in a list or dictionary.
|
|
245
|
+
*
|
|
246
|
+
* @param token The token to process
|
|
247
|
+
*/
|
|
248
|
+
#processDefaultToken(token) {
|
|
249
|
+
if (token.type === 'eof') {
|
|
250
|
+
throw new IncompleteExpressionError({
|
|
251
|
+
lexPosition: token.start,
|
|
252
|
+
tokenValue: token.value,
|
|
253
|
+
tokenType: token.type,
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
throw new ParseError({
|
|
190
257
|
lexPosition: token.start,
|
|
191
258
|
tokenValue: token.value,
|
|
192
259
|
tokenType: token.type,
|
|
193
260
|
});
|
|
194
261
|
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
262
|
+
/**
|
|
263
|
+
* Get the led function for a token. This is the function that
|
|
264
|
+
* is called when a token is found in the middle of an expression.
|
|
265
|
+
*
|
|
266
|
+
* @param tokenType The type of token to get the led function for.
|
|
267
|
+
* @param leftNode The left hand side of the expression.
|
|
268
|
+
*/
|
|
269
|
+
#getLedFunction(tokenType, leftNode) {
|
|
270
|
+
switch (tokenType) {
|
|
271
|
+
case 'dot':
|
|
272
|
+
return this.#processDotToken(leftNode);
|
|
273
|
+
case 'pipe':
|
|
274
|
+
return this.#processPipeToken(leftNode);
|
|
275
|
+
case 'or':
|
|
276
|
+
return this.#processOrToken(leftNode);
|
|
277
|
+
case 'and':
|
|
278
|
+
return this.#processAndToken(leftNode);
|
|
279
|
+
case 'lparen':
|
|
280
|
+
return this.#processLParenToken(leftNode);
|
|
281
|
+
case 'filter':
|
|
282
|
+
return this.#processFilterToken(leftNode);
|
|
283
|
+
case 'eq':
|
|
284
|
+
case 'ne':
|
|
285
|
+
case 'gt':
|
|
286
|
+
case 'gte':
|
|
287
|
+
case 'lt':
|
|
288
|
+
case 'lte':
|
|
289
|
+
return this.#parseComparator(leftNode, tokenType);
|
|
290
|
+
case 'flatten':
|
|
291
|
+
return this.#processFlattenToken(leftNode);
|
|
292
|
+
case 'lbracket':
|
|
293
|
+
return this.#processLBracketToken(leftNode);
|
|
294
|
+
default:
|
|
295
|
+
return this.#throwParseError();
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Process a dot token.
|
|
300
|
+
*
|
|
301
|
+
* A dot token is a syntax that allows you to access
|
|
302
|
+
* fields in a dictionary or elements in a list.
|
|
303
|
+
*
|
|
304
|
+
* @example foo.bar
|
|
305
|
+
*
|
|
306
|
+
* @param leftNode The left hand side of the expression.
|
|
307
|
+
*/
|
|
308
|
+
#processDotToken(leftNode) {
|
|
309
|
+
if (this.#currentToken() !== 'star') {
|
|
310
|
+
const right = this.#parseDotRhs(BINDING_POWER['dot']);
|
|
311
|
+
if (leftNode.type === 'subexpression') {
|
|
312
|
+
leftNode.children.push(right);
|
|
313
|
+
return leftNode;
|
|
314
|
+
}
|
|
315
|
+
else {
|
|
316
|
+
return subexpression([leftNode, right]);
|
|
317
|
+
}
|
|
234
318
|
}
|
|
235
319
|
else {
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
const
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
320
|
+
// We are creating a value projection
|
|
321
|
+
this.#advance();
|
|
322
|
+
const right = this.#parseProjectionRhs(BINDING_POWER['dot']);
|
|
323
|
+
return valueProjection(leftNode, right);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* Process a pipe token.
|
|
328
|
+
*
|
|
329
|
+
* A pipe token is a syntax that allows you to combine two
|
|
330
|
+
* expressions using the pipe operator.
|
|
331
|
+
*
|
|
332
|
+
* @example foo | bar
|
|
333
|
+
*
|
|
334
|
+
* @param leftNode The left hand side of the expression.
|
|
335
|
+
*/
|
|
336
|
+
#processPipeToken(leftNode) {
|
|
337
|
+
const right = this.#expression(BINDING_POWER['pipe']);
|
|
338
|
+
return pipe(leftNode, right);
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Process an or token.
|
|
342
|
+
*
|
|
343
|
+
* An or token is a syntax that allows you to combine two
|
|
344
|
+
* expressions using the logical or operator.
|
|
345
|
+
*
|
|
346
|
+
* @example foo || bar
|
|
347
|
+
*
|
|
348
|
+
* @param leftNode The left hand side of the expression.
|
|
349
|
+
*/
|
|
350
|
+
#processOrToken(leftNode) {
|
|
351
|
+
const right = this.#expression(BINDING_POWER['or']);
|
|
352
|
+
return orExpression(leftNode, right);
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* Process an and token.
|
|
356
|
+
*
|
|
357
|
+
* An and token is a syntax that allows you to combine two
|
|
358
|
+
* expressions using the logical and operator.
|
|
359
|
+
*
|
|
360
|
+
* @example foo && bar
|
|
361
|
+
*
|
|
362
|
+
* @param leftNode The left hand side of the expression.
|
|
363
|
+
*/
|
|
364
|
+
#processAndToken(leftNode) {
|
|
365
|
+
const right = this.#expression(BINDING_POWER['and']);
|
|
366
|
+
return andExpression(leftNode, right);
|
|
367
|
+
}
|
|
368
|
+
#processLParenToken(leftNode) {
|
|
369
|
+
const name = leftNode.value;
|
|
370
|
+
const args = [];
|
|
371
|
+
while (this.#currentToken() !== 'rparen') {
|
|
372
|
+
const expression = this.#expression();
|
|
373
|
+
if (this.#currentToken() === 'comma') {
|
|
374
|
+
this.#match('comma');
|
|
375
|
+
}
|
|
376
|
+
args.push(expression);
|
|
377
|
+
}
|
|
378
|
+
this.#match('rparen');
|
|
379
|
+
return functionExpression(name, args);
|
|
380
|
+
}
|
|
381
|
+
#processFilterToken(leftNode) {
|
|
382
|
+
// Filters are projections
|
|
383
|
+
const condition = this.#expression(0);
|
|
384
|
+
this.#match('rbracket');
|
|
385
|
+
let right;
|
|
386
|
+
if (this.#currentToken() === 'flatten') {
|
|
387
|
+
right = identity();
|
|
292
388
|
}
|
|
293
389
|
else {
|
|
294
|
-
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
// | (currentToken)
|
|
317
|
-
if (__classPrivateFieldGet(this, _Parser_instances, "m", _Parser_lookahead).call(this, 0) === 'colon' || __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_lookahead).call(this, 1) === 'colon') {
|
|
318
|
-
return __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_parseSliceExpression).call(this);
|
|
319
|
-
}
|
|
320
|
-
else {
|
|
321
|
-
// Parse the syntax [number]
|
|
322
|
-
const node = index(__classPrivateFieldGet(this, _Parser_instances, "m", _Parser_lookaheadToken).call(this, 0).value);
|
|
323
|
-
__classPrivateFieldGet(this, _Parser_instances, "m", _Parser_advance).call(this);
|
|
324
|
-
__classPrivateFieldGet(this, _Parser_instances, "m", _Parser_match).call(this, 'rbracket');
|
|
325
|
-
return node;
|
|
326
|
-
}
|
|
327
|
-
}, _Parser_parseSliceExpression = function _Parser_parseSliceExpression() {
|
|
328
|
-
// [start:end:step]
|
|
329
|
-
// Where start, end, and step are optional.
|
|
330
|
-
// The last colon is optional as well.
|
|
331
|
-
const parts = [];
|
|
332
|
-
let index = 0;
|
|
333
|
-
let currentToken = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_currentToken).call(this);
|
|
334
|
-
while (currentToken !== 'rbracket' && index < 3) {
|
|
335
|
-
if (currentToken === 'colon') {
|
|
336
|
-
index += 1;
|
|
337
|
-
if (index === 3) {
|
|
338
|
-
__classPrivateFieldGet(this, _Parser_instances, "m", _Parser_throwParseError).call(this);
|
|
390
|
+
right = this.#parseProjectionRhs(BINDING_POWER['flatten']);
|
|
391
|
+
}
|
|
392
|
+
return filterProjection(leftNode, right, condition);
|
|
393
|
+
}
|
|
394
|
+
#processFlattenToken(leftNode) {
|
|
395
|
+
const left = flatten(leftNode);
|
|
396
|
+
const right = this.#parseProjectionRhs(BINDING_POWER['flatten']);
|
|
397
|
+
return projection(left, right);
|
|
398
|
+
}
|
|
399
|
+
#processLBracketToken(leftNode) {
|
|
400
|
+
const token = this.#lookaheadToken(0);
|
|
401
|
+
if (['number', 'colon'].includes(token.type)) {
|
|
402
|
+
const right = this.#parseIndexExpression();
|
|
403
|
+
if (leftNode.type === 'index_expression') {
|
|
404
|
+
// Optimization: if the left node is an index expression
|
|
405
|
+
// we can avoid creating another node and instead just
|
|
406
|
+
// add the right node as a child of the left node.
|
|
407
|
+
leftNode.children.push(right);
|
|
408
|
+
return leftNode;
|
|
409
|
+
}
|
|
410
|
+
else {
|
|
411
|
+
return this.#projectIfSlice(leftNode, right);
|
|
339
412
|
}
|
|
340
|
-
__classPrivateFieldGet(this, _Parser_instances, "m", _Parser_advance).call(this);
|
|
341
413
|
}
|
|
342
|
-
else
|
|
343
|
-
|
|
344
|
-
|
|
414
|
+
else {
|
|
415
|
+
// We have a projection
|
|
416
|
+
this.#match('star');
|
|
417
|
+
this.#match('rbracket');
|
|
418
|
+
const right = this.#parseProjectionRhs(BINDING_POWER['star']);
|
|
419
|
+
return projection(leftNode, right);
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
/**
|
|
423
|
+
* Throw a parse error.
|
|
424
|
+
*
|
|
425
|
+
* This type of error indicates that the parser encountered
|
|
426
|
+
* a syntax error while processing the expression.
|
|
427
|
+
*
|
|
428
|
+
* The error includes the position in the expression where
|
|
429
|
+
* the error occurred, the value of the token that caused
|
|
430
|
+
* the error, the type of the token, and an optional reason.
|
|
431
|
+
*
|
|
432
|
+
* @param options The options to use when throwing the error.
|
|
433
|
+
*/
|
|
434
|
+
#throwParseError(options) {
|
|
435
|
+
const token = this.#lookaheadToken(0);
|
|
436
|
+
throw new ParseError({
|
|
437
|
+
lexPosition: options?.lexPosition ?? token.start,
|
|
438
|
+
tokenValue: options?.tokenValue ?? token.value,
|
|
439
|
+
tokenType: options?.tokenType ?? token.type,
|
|
440
|
+
reason: options?.reason,
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* Process an index expression.
|
|
445
|
+
*
|
|
446
|
+
* An index expression is a syntax that allows you to
|
|
447
|
+
* access elements in a list or dictionary. For example
|
|
448
|
+
* `foo[0]` accesses the first element in the list `foo`.
|
|
449
|
+
*/
|
|
450
|
+
#parseIndexExpression() {
|
|
451
|
+
// We're here:
|
|
452
|
+
// [<current>
|
|
453
|
+
// ^
|
|
454
|
+
// | (currentToken)
|
|
455
|
+
if (this.#lookahead(0) === 'colon' || this.#lookahead(1) === 'colon') {
|
|
456
|
+
return this.#parseSliceExpression();
|
|
345
457
|
}
|
|
346
458
|
else {
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
459
|
+
// Parse the syntax [number]
|
|
460
|
+
const node = index(this.#lookaheadToken(0).value);
|
|
461
|
+
this.#advance();
|
|
462
|
+
this.#match('rbracket');
|
|
463
|
+
return node;
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
/**
|
|
467
|
+
* Process a slice expression.
|
|
468
|
+
*
|
|
469
|
+
* A slice expression is a syntax that allows you to
|
|
470
|
+
* access a range of elements in a list. For example
|
|
471
|
+
* `foo[0:10:2]` accesses every other element in the
|
|
472
|
+
* list `foo` from index 0 to 10.
|
|
473
|
+
*
|
|
474
|
+
* In a slice expression, the first index represents the
|
|
475
|
+
* start of the slice, the second index represents the
|
|
476
|
+
* end of the slice, and the third index represents the
|
|
477
|
+
* step.
|
|
478
|
+
*
|
|
479
|
+
* If the first index is omitted, it defaults to 0.
|
|
480
|
+
* If the second index is omitted, it defaults to the
|
|
481
|
+
* length of the list. If the third index is omitted, it
|
|
482
|
+
* defaults to 1. If the last colon is omitted, it defaults
|
|
483
|
+
* to a single index.
|
|
484
|
+
*/
|
|
485
|
+
#parseSliceExpression() {
|
|
486
|
+
// [start:end:step]
|
|
487
|
+
// Where start, end, and step are optional.
|
|
488
|
+
// The last colon is optional as well.
|
|
489
|
+
const parts = [];
|
|
490
|
+
let index = 0;
|
|
491
|
+
let currentToken = this.#currentToken();
|
|
492
|
+
while (currentToken !== 'rbracket' && index < 3) {
|
|
493
|
+
if (currentToken === 'colon') {
|
|
494
|
+
index += 1;
|
|
495
|
+
if (index === 3) {
|
|
496
|
+
this.#throwParseError();
|
|
497
|
+
}
|
|
498
|
+
this.#advance();
|
|
499
|
+
}
|
|
500
|
+
else if (currentToken === 'number') {
|
|
501
|
+
parts[index] = this.#lookaheadToken(0).value;
|
|
502
|
+
this.#advance();
|
|
503
|
+
}
|
|
504
|
+
else {
|
|
505
|
+
this.#throwParseError();
|
|
506
|
+
}
|
|
507
|
+
currentToken = this.#currentToken();
|
|
508
|
+
}
|
|
509
|
+
this.#match('rbracket');
|
|
510
|
+
return slice(parts[0], parts[1], parts[2]);
|
|
511
|
+
}
|
|
512
|
+
/**
|
|
513
|
+
* Process a projection if the right hand side of the
|
|
514
|
+
* projection is a slice.
|
|
515
|
+
*
|
|
516
|
+
* @param left The left hand side of the projection.
|
|
517
|
+
* @param right The right hand side of the projection.
|
|
518
|
+
*/
|
|
519
|
+
#projectIfSlice(left, right) {
|
|
520
|
+
const idxExpression = indexExpression([left, right]);
|
|
521
|
+
if (right.type === 'slice') {
|
|
522
|
+
return projection(idxExpression, this.#parseProjectionRhs(BINDING_POWER['star']));
|
|
370
523
|
}
|
|
371
524
|
else {
|
|
372
|
-
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
525
|
+
return idxExpression;
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
/**
|
|
529
|
+
* Process a comparator.
|
|
530
|
+
*
|
|
531
|
+
* A comparator is a syntax that allows you to compare
|
|
532
|
+
* two values. For example `foo == bar` compares the
|
|
533
|
+
* value of `foo` with the value of `bar`.
|
|
534
|
+
*
|
|
535
|
+
* @param left The left hand side of the comparator.
|
|
536
|
+
* @param comparatorChar The comparator character.
|
|
537
|
+
*/
|
|
538
|
+
#parseComparator(left, comparatorChar) {
|
|
539
|
+
return comparator(comparatorChar, left, this.#expression(BINDING_POWER[comparatorChar]));
|
|
540
|
+
}
|
|
541
|
+
/**
|
|
542
|
+
* Process a multi-select list.
|
|
543
|
+
*
|
|
544
|
+
* A multi-select list is a syntax that allows you to
|
|
545
|
+
* select multiple elements from a list. For example
|
|
546
|
+
* `foo[*]` selects all elements in the list `foo`.
|
|
547
|
+
*/
|
|
548
|
+
#parseMultiSelectList() {
|
|
549
|
+
const expressions = [];
|
|
550
|
+
while (true) {
|
|
551
|
+
const expression = this.#expression();
|
|
552
|
+
expressions.push(expression);
|
|
553
|
+
if (this.#currentToken() === 'rbracket') {
|
|
554
|
+
break;
|
|
555
|
+
}
|
|
556
|
+
else {
|
|
557
|
+
this.#match('comma');
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
this.#match('rbracket');
|
|
561
|
+
return multiSelectList(expressions);
|
|
562
|
+
}
|
|
563
|
+
/**
|
|
564
|
+
* Process a multi-select hash.
|
|
565
|
+
*
|
|
566
|
+
* A multi-select hash is a syntax that allows you to
|
|
567
|
+
* select multiple key-value pairs from a dictionary.
|
|
568
|
+
* For example `foo{a: a, b: b}` selects the keys `a`
|
|
569
|
+
* and `b` from the dictionary `foo`.
|
|
570
|
+
*/
|
|
571
|
+
#parseMultiSelectHash() {
|
|
572
|
+
const pairs = [];
|
|
573
|
+
while (true) {
|
|
574
|
+
const keyToken = this.#lookaheadToken(0);
|
|
575
|
+
// Before getting the token value, verify it's
|
|
576
|
+
// an identifier.
|
|
577
|
+
this.#matchMultipleTokens(['quoted_identifier', 'unquoted_identifier']); // token types
|
|
578
|
+
const keyName = keyToken['value'];
|
|
579
|
+
this.#match('colon');
|
|
580
|
+
const value = this.#expression(0);
|
|
581
|
+
const node = keyValPair(keyName, value);
|
|
582
|
+
pairs.push(node);
|
|
583
|
+
if (this.#currentToken() == 'comma') {
|
|
584
|
+
this.#match('comma');
|
|
585
|
+
}
|
|
586
|
+
else if (this.#currentToken() == 'rbrace') {
|
|
587
|
+
this.#match('rbrace');
|
|
588
|
+
break;
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
return multiSelectObject(pairs);
|
|
592
|
+
}
|
|
593
|
+
/**
|
|
594
|
+
* Process the right hand side of a projection.
|
|
595
|
+
*
|
|
596
|
+
* @param bindingPower The binding power of the current token.
|
|
597
|
+
*/
|
|
598
|
+
#parseProjectionRhs(bindingPower) {
|
|
599
|
+
// Parse the right hand side of the projection.
|
|
600
|
+
let right;
|
|
601
|
+
if (BINDING_POWER[this.#currentToken()] < this.#projectionStop) {
|
|
602
|
+
// BP of 10 are all the tokens that stop a projection.
|
|
603
|
+
right = identity();
|
|
604
|
+
}
|
|
605
|
+
else if (this.#currentToken() == 'lbracket') {
|
|
606
|
+
right = this.#expression(bindingPower);
|
|
607
|
+
}
|
|
608
|
+
else if (this.#currentToken() == 'filter') {
|
|
609
|
+
right = this.#expression(bindingPower);
|
|
610
|
+
}
|
|
611
|
+
else if (this.#currentToken() == 'dot') {
|
|
612
|
+
this.#match('dot');
|
|
613
|
+
right = this.#parseDotRhs(bindingPower);
|
|
457
614
|
}
|
|
458
615
|
else {
|
|
459
|
-
|
|
460
|
-
lexPosition: token.start,
|
|
461
|
-
tokenValue: token.value,
|
|
462
|
-
tokenType: token.type,
|
|
463
|
-
});
|
|
616
|
+
this.#throwParseError();
|
|
464
617
|
}
|
|
618
|
+
return right;
|
|
465
619
|
}
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
620
|
+
/**
|
|
621
|
+
* Process the right hand side of a dot expression.
|
|
622
|
+
*
|
|
623
|
+
* @param bindingPower The binding power of the current token.
|
|
624
|
+
*/
|
|
625
|
+
#parseDotRhs(bindingPower) {
|
|
626
|
+
// From the grammar:
|
|
627
|
+
// expression '.' ( identifier /
|
|
628
|
+
// multi-select-list /
|
|
629
|
+
// multi-select-hash /
|
|
630
|
+
// function-expression /
|
|
631
|
+
// *
|
|
632
|
+
// In terms of tokens that means that after a '.',
|
|
633
|
+
// you can have:
|
|
634
|
+
const lookahead = this.#currentToken();
|
|
635
|
+
// Common case "foo.bar", so first check for an identifier.
|
|
636
|
+
if (['quoted_identifier', 'unquoted_identifier', 'star'].includes(lookahead)) {
|
|
637
|
+
return this.#expression(bindingPower);
|
|
638
|
+
}
|
|
639
|
+
else if (lookahead == 'lbracket') {
|
|
640
|
+
this.#match('lbracket');
|
|
641
|
+
return this.#parseMultiSelectList();
|
|
642
|
+
}
|
|
643
|
+
else if (lookahead == 'lbrace') {
|
|
644
|
+
this.#match('lbrace');
|
|
645
|
+
return this.#parseMultiSelectHash();
|
|
476
646
|
}
|
|
477
647
|
else {
|
|
478
|
-
|
|
479
|
-
lexPosition: token.start,
|
|
480
|
-
tokenValue: token.value,
|
|
481
|
-
tokenType: token.type,
|
|
482
|
-
});
|
|
648
|
+
this.#throwParseError();
|
|
483
649
|
}
|
|
484
650
|
}
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
};
|
|
651
|
+
/**
|
|
652
|
+
* Process a token and throw an error if it doesn't match the expected token.
|
|
653
|
+
*
|
|
654
|
+
* @param tokenType The expected token type.
|
|
655
|
+
*/
|
|
656
|
+
#match(tokenType) {
|
|
657
|
+
const currentToken = this.#currentToken();
|
|
658
|
+
if (currentToken === tokenType) {
|
|
659
|
+
this.#advance();
|
|
660
|
+
}
|
|
661
|
+
else {
|
|
662
|
+
const token = this.#lookaheadToken(0);
|
|
663
|
+
if (token.type === 'eof') {
|
|
664
|
+
throw new IncompleteExpressionError({
|
|
665
|
+
lexPosition: token.start,
|
|
666
|
+
tokenValue: token.value,
|
|
667
|
+
tokenType: token.type,
|
|
668
|
+
});
|
|
669
|
+
}
|
|
670
|
+
else {
|
|
671
|
+
throw new ParseError({
|
|
672
|
+
lexPosition: token.start,
|
|
673
|
+
tokenValue: token.value,
|
|
674
|
+
tokenType: token.type,
|
|
675
|
+
});
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
/**
|
|
680
|
+
* Process a token and throw an error if it doesn't match the expected token.
|
|
681
|
+
*
|
|
682
|
+
* @param tokenTypes The expected token types.
|
|
683
|
+
*/
|
|
684
|
+
#matchMultipleTokens(tokenTypes) {
|
|
685
|
+
const currentToken = this.#currentToken();
|
|
686
|
+
if (!tokenTypes.includes(currentToken)) {
|
|
687
|
+
const token = this.#lookaheadToken(0);
|
|
688
|
+
if (token.type === 'eof') {
|
|
689
|
+
throw new IncompleteExpressionError({
|
|
690
|
+
lexPosition: token.start,
|
|
691
|
+
tokenValue: token.value,
|
|
692
|
+
tokenType: token.type,
|
|
693
|
+
});
|
|
694
|
+
}
|
|
695
|
+
else {
|
|
696
|
+
throw new ParseError({
|
|
697
|
+
lexPosition: token.start,
|
|
698
|
+
tokenValue: token.value,
|
|
699
|
+
tokenType: token.type,
|
|
700
|
+
});
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
this.#advance();
|
|
704
|
+
}
|
|
705
|
+
/**
|
|
706
|
+
* Advance the index to the next token.
|
|
707
|
+
*/
|
|
708
|
+
#advance() {
|
|
709
|
+
this.#index += 1;
|
|
710
|
+
}
|
|
711
|
+
/**
|
|
712
|
+
* Get the current token type.
|
|
713
|
+
*/
|
|
714
|
+
#currentToken() {
|
|
715
|
+
return this.#tokens[this.#index].type;
|
|
716
|
+
}
|
|
717
|
+
/**
|
|
718
|
+
* Look ahead in the token stream and get the type of the token
|
|
719
|
+
*
|
|
720
|
+
* @param number The number of tokens to look ahead.
|
|
721
|
+
*/
|
|
722
|
+
#lookahead(number) {
|
|
723
|
+
return this.#tokens[this.#index + number].type;
|
|
724
|
+
}
|
|
725
|
+
/**
|
|
726
|
+
* Look ahead in the token stream and get the token
|
|
727
|
+
*
|
|
728
|
+
* @param number The number of tokens to look ahead.
|
|
729
|
+
*/
|
|
730
|
+
#lookaheadToken(number) {
|
|
731
|
+
return this.#tokens[this.#index + number];
|
|
732
|
+
}
|
|
733
|
+
/**
|
|
734
|
+
* Remove half of the cached expressions randomly.
|
|
735
|
+
*/
|
|
736
|
+
#evictCache() {
|
|
737
|
+
const newCache = Object.keys(this.#cache).reduce((acc, key) => {
|
|
738
|
+
if (randomInt(0, 100) > 50) {
|
|
739
|
+
acc[key] = this.#cache[key];
|
|
740
|
+
}
|
|
741
|
+
return acc;
|
|
742
|
+
}, {});
|
|
743
|
+
this.#cache = newCache;
|
|
744
|
+
}
|
|
745
|
+
}
|
|
503
746
|
export { Parser };
|