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