@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/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
- _Parser_instances.add(this);
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 = __classPrivateFieldGet(this, _Parser_cache, "f")[expression];
51
+ const cached = this.#cache[expression];
65
52
  if (cached) {
66
53
  return cached;
67
54
  }
68
- const parsedResult = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_doParse).call(this, expression);
69
- __classPrivateFieldGet(this, _Parser_cache, "f")[expression] = parsedResult;
70
- if (Object.keys(__classPrivateFieldGet(this, _Parser_cache, "f")).length > __classPrivateFieldGet(this, _Parser_maxCacheSize, "f")) {
71
- __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_evictCache).call(this);
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
- __classPrivateFieldSet(this, _Parser_cache, {}, "f");
66
+ this.#cache = {};
80
67
  }
81
- }
82
- _Parser_projectionStop = new WeakMap(), _Parser_cache = new WeakMap(), _Parser_maxCacheSize = new WeakMap(), _Parser_tokenizer = new WeakMap(), _Parser_tokens = new WeakMap(), _Parser_index = new WeakMap(), _Parser_instances = new WeakSet(), _Parser_doParse = function _Parser_doParse(expression) {
83
- try {
84
- return __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_parse).call(this, expression);
85
- }
86
- catch (error) {
87
- if (error instanceof LexerError ||
88
- error instanceof IncompleteExpressionError ||
89
- error instanceof ParseError) {
90
- error.setExpression(expression);
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
- return fieldValue;
154
- }, _Parser_processStarToken = function _Parser_processStarToken() {
155
- const left = identity();
156
- let right;
157
- if (__classPrivateFieldGet(this, _Parser_instances, "m", _Parser_currentToken).call(this) === 'rbracket') {
158
- right = identity();
159
- }
160
- else {
161
- right = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_parseProjectionRhs).call(this, BINDING_POWER['star']);
162
- }
163
- return valueProjection(left, right);
164
- }, _Parser_processLParenTokenNud = function _Parser_processLParenTokenNud() {
165
- const expression = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_expression).call(this);
166
- __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_match).call(this, 'rparen');
167
- return expression;
168
- }, _Parser_processFlattenTokenNud = function _Parser_processFlattenTokenNud() {
169
- const left = flatten(identity());
170
- const right = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_parseProjectionRhs).call(this, BINDING_POWER['flatten']);
171
- return projection(left, right);
172
- }, _Parser_processLBracketTokenNud = function _Parser_processLBracketTokenNud() {
173
- if (['number', 'colon'].includes(__classPrivateFieldGet(this, _Parser_instances, "m", _Parser_currentToken).call(this))) {
174
- const right = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_parseIndexExpression).call(this);
175
- return __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_projectIfSlice).call(this, identity(), right);
176
- }
177
- else if (__classPrivateFieldGet(this, _Parser_instances, "m", _Parser_currentToken).call(this) === 'star' &&
178
- __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_lookahead).call(this, 1) === 'rbracket') {
179
- __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_advance).call(this);
180
- __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_advance).call(this);
181
- const right = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_parseProjectionRhs).call(this, BINDING_POWER['star']);
182
- return projection(identity(), right);
183
- }
184
- else {
185
- return __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_parseMultiSelectList).call(this);
186
- }
187
- }, _Parser_processDefaultToken = function _Parser_processDefaultToken(token) {
188
- if (token.type === 'eof') {
189
- throw new IncompleteExpressionError({
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
- throw new ParseError({
196
- lexPosition: token.start,
197
- tokenValue: token.value,
198
- tokenType: token.type,
199
- });
200
- }, _Parser_getLedFunction = function _Parser_getLedFunction(tokenType, leftNode) {
201
- switch (tokenType) {
202
- case 'dot':
203
- return __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_processDotToken).call(this, leftNode);
204
- case 'pipe':
205
- return __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_processPipeToken).call(this, leftNode);
206
- case 'or':
207
- return __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_processOrToken).call(this, leftNode);
208
- case 'and':
209
- return __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_processAndToken).call(this, leftNode);
210
- case 'lparen':
211
- return __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_processLParenToken).call(this, leftNode);
212
- case 'filter':
213
- return __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_processFilterToken).call(this, leftNode);
214
- case 'eq':
215
- case 'ne':
216
- case 'gt':
217
- case 'gte':
218
- case 'lt':
219
- case 'lte':
220
- return __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_parseComparator).call(this, leftNode, tokenType);
221
- case 'flatten':
222
- return __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_processFlattenToken).call(this, leftNode);
223
- case 'lbracket':
224
- return __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_processLBracketToken).call(this, leftNode);
225
- default:
226
- return __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_throwParseError).call(this);
227
- }
228
- }, _Parser_processDotToken = function _Parser_processDotToken(leftNode) {
229
- if (__classPrivateFieldGet(this, _Parser_instances, "m", _Parser_currentToken).call(this) !== 'star') {
230
- const right = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_parseDotRhs).call(this, BINDING_POWER['dot']);
231
- if (leftNode.type === 'subexpression') {
232
- leftNode.children.push(right);
233
- return leftNode;
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
- return subexpression([leftNode, right]);
237
- }
238
- }
239
- else {
240
- // We are creating a value projection
241
- __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_advance).call(this);
242
- const right = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_parseProjectionRhs).call(this, BINDING_POWER['dot']);
243
- return valueProjection(leftNode, right);
244
- }
245
- }, _Parser_processPipeToken = function _Parser_processPipeToken(leftNode) {
246
- const right = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_expression).call(this, BINDING_POWER['pipe']);
247
- return pipe(leftNode, right);
248
- }, _Parser_processOrToken = function _Parser_processOrToken(leftNode) {
249
- const right = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_expression).call(this, BINDING_POWER['or']);
250
- return orExpression(leftNode, right);
251
- }, _Parser_processAndToken = function _Parser_processAndToken(leftNode) {
252
- const right = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_expression).call(this, BINDING_POWER['and']);
253
- return andExpression(leftNode, right);
254
- }, _Parser_processLParenToken = function _Parser_processLParenToken(leftNode) {
255
- const name = leftNode.value;
256
- const args = [];
257
- while (__classPrivateFieldGet(this, _Parser_instances, "m", _Parser_currentToken).call(this) !== 'rparen') {
258
- const expression = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_expression).call(this);
259
- if (__classPrivateFieldGet(this, _Parser_instances, "m", _Parser_currentToken).call(this) === 'comma') {
260
- __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_match).call(this, 'comma');
261
- }
262
- args.push(expression);
263
- }
264
- __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_match).call(this, 'rparen');
265
- return functionExpression(name, args);
266
- }, _Parser_processFilterToken = function _Parser_processFilterToken(leftNode) {
267
- // Filters are projections
268
- const condition = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_expression).call(this, 0);
269
- __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_match).call(this, 'rbracket');
270
- let right;
271
- if (__classPrivateFieldGet(this, _Parser_instances, "m", _Parser_currentToken).call(this) === 'flatten') {
272
- right = identity();
273
- }
274
- else {
275
- right = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_parseProjectionRhs).call(this, BINDING_POWER['flatten']);
276
- }
277
- return filterProjection(leftNode, right, condition);
278
- }, _Parser_processFlattenToken = function _Parser_processFlattenToken(leftNode) {
279
- const left = flatten(leftNode);
280
- const right = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_parseProjectionRhs).call(this, BINDING_POWER['flatten']);
281
- return projection(left, right);
282
- }, _Parser_processLBracketToken = function _Parser_processLBracketToken(leftNode) {
283
- const token = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_lookaheadToken).call(this, 0);
284
- if (['number', 'colon'].includes(token.type)) {
285
- const right = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_parseIndexExpression).call(this);
286
- if (leftNode.type === 'index_expression') {
287
- // Optimization: if the left node is an index expression
288
- // we can avoid creating another node and instead just
289
- // add the right node as a child of the left node.
290
- leftNode.children.push(right);
291
- return leftNode;
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
- return __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_projectIfSlice).call(this, leftNode, right);
295
- }
296
- }
297
- else {
298
- // We have a projection
299
- __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_match).call(this, 'star');
300
- __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_match).call(this, 'rbracket');
301
- const right = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_parseProjectionRhs).call(this, BINDING_POWER['star']);
302
- return projection(leftNode, right);
303
- }
304
- }, _Parser_throwParseError = function _Parser_throwParseError(options) {
305
- const token = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_lookaheadToken).call(this, 0);
306
- throw new ParseError({
307
- lexPosition: options?.lexPosition ?? token.start,
308
- tokenValue: options?.tokenValue ?? token.value,
309
- tokenType: options?.tokenType ?? token.type,
310
- reason: options?.reason,
311
- });
312
- }, _Parser_parseIndexExpression = function _Parser_parseIndexExpression() {
313
- // We're here:
314
- // [<current>
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 if (currentToken === 'number') {
343
- parts[index] = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_lookaheadToken).call(this, 0).value;
344
- __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_advance).call(this);
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
- __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_throwParseError).call(this);
348
- }
349
- currentToken = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_currentToken).call(this);
350
- }
351
- __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_match).call(this, 'rbracket');
352
- return slice(parts[0], parts[1], parts[2]);
353
- }, _Parser_projectIfSlice = function _Parser_projectIfSlice(left, right) {
354
- const idxExpression = indexExpression([left, right]);
355
- if (right.type === 'slice') {
356
- return projection(idxExpression, __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_parseProjectionRhs).call(this, BINDING_POWER['star']));
357
- }
358
- else {
359
- return idxExpression;
360
- }
361
- }, _Parser_parseComparator = function _Parser_parseComparator(left, comparatorChar) {
362
- return comparator(comparatorChar, left, __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_expression).call(this, BINDING_POWER[comparatorChar]));
363
- }, _Parser_parseMultiSelectList = function _Parser_parseMultiSelectList() {
364
- const expressions = [];
365
- while (true) {
366
- const expression = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_expression).call(this);
367
- expressions.push(expression);
368
- if (__classPrivateFieldGet(this, _Parser_instances, "m", _Parser_currentToken).call(this) === 'rbracket') {
369
- break;
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
- __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_match).call(this, 'comma');
373
- }
374
- }
375
- __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_match).call(this, 'rbracket');
376
- return multiSelectList(expressions);
377
- }, _Parser_parseMultiSelectHash = function _Parser_parseMultiSelectHash() {
378
- const pairs = [];
379
- while (true) {
380
- const keyToken = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_lookaheadToken).call(this, 0);
381
- // Before getting the token value, verify it's
382
- // an identifier.
383
- __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_matchMultipleTokens).call(this, ['quoted_identifier', 'unquoted_identifier']); // token types
384
- const keyName = keyToken['value'];
385
- __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_match).call(this, 'colon');
386
- const value = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_expression).call(this, 0);
387
- const node = keyValPair(keyName, value);
388
- pairs.push(node);
389
- if (__classPrivateFieldGet(this, _Parser_instances, "m", _Parser_currentToken).call(this) == 'comma') {
390
- __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_match).call(this, 'comma');
391
- }
392
- else if (__classPrivateFieldGet(this, _Parser_instances, "m", _Parser_currentToken).call(this) == 'rbrace') {
393
- __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_match).call(this, 'rbrace');
394
- break;
395
- }
396
- }
397
- return multiSelectObject(pairs);
398
- }, _Parser_parseProjectionRhs = function _Parser_parseProjectionRhs(bindingPower) {
399
- // Parse the right hand side of the projection.
400
- let right;
401
- if (BINDING_POWER[__classPrivateFieldGet(this, _Parser_instances, "m", _Parser_currentToken).call(this)] < __classPrivateFieldGet(this, _Parser_projectionStop, "f")) {
402
- // BP of 10 are all the tokens that stop a projection.
403
- right = identity();
404
- }
405
- else if (__classPrivateFieldGet(this, _Parser_instances, "m", _Parser_currentToken).call(this) == 'lbracket') {
406
- right = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_expression).call(this, bindingPower);
407
- }
408
- else if (__classPrivateFieldGet(this, _Parser_instances, "m", _Parser_currentToken).call(this) == 'filter') {
409
- right = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_expression).call(this, bindingPower);
410
- }
411
- else if (__classPrivateFieldGet(this, _Parser_instances, "m", _Parser_currentToken).call(this) == 'dot') {
412
- __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_match).call(this, 'dot');
413
- right = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_parseDotRhs).call(this, bindingPower);
414
- }
415
- else {
416
- __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_throwParseError).call(this);
417
- }
418
- return right;
419
- }, _Parser_parseDotRhs = function _Parser_parseDotRhs(bindingPower) {
420
- // From the grammar:
421
- // expression '.' ( identifier /
422
- // multi-select-list /
423
- // multi-select-hash /
424
- // function-expression /
425
- // *
426
- // In terms of tokens that means that after a '.',
427
- // you can have:
428
- const lookahead = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_currentToken).call(this);
429
- // Common case "foo.bar", so first check for an identifier.
430
- if (['quoted_identifier', 'unquoted_identifier', 'star'].includes(lookahead)) {
431
- return __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_expression).call(this, bindingPower);
432
- }
433
- else if (lookahead == 'lbracket') {
434
- __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_match).call(this, 'lbracket');
435
- return __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_parseMultiSelectList).call(this);
436
- }
437
- else if (lookahead == 'lbrace') {
438
- __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_match).call(this, 'lbrace');
439
- return __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_parseMultiSelectHash).call(this);
440
- }
441
- else {
442
- __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_throwParseError).call(this);
443
- }
444
- }, _Parser_match = function _Parser_match(tokenType) {
445
- const currentToken = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_currentToken).call(this);
446
- if (currentToken === tokenType) {
447
- __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_advance).call(this);
448
- }
449
- else {
450
- const token = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_lookaheadToken).call(this, 0);
451
- if (token.type === 'eof') {
452
- throw new IncompleteExpressionError({
453
- lexPosition: token.start,
454
- tokenValue: token.value,
455
- tokenType: token.type,
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
- throw new ParseError({
460
- lexPosition: token.start,
461
- tokenValue: token.value,
462
- tokenType: token.type,
463
- });
616
+ this.#throwParseError();
464
617
  }
618
+ return right;
465
619
  }
466
- }, _Parser_matchMultipleTokens = function _Parser_matchMultipleTokens(tokenTypes) {
467
- const currentToken = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_currentToken).call(this);
468
- if (!tokenTypes.includes(currentToken)) {
469
- const token = __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_lookaheadToken).call(this, 0);
470
- if (token.type === 'eof') {
471
- throw new IncompleteExpressionError({
472
- lexPosition: token.start,
473
- tokenValue: token.value,
474
- tokenType: token.type,
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
- throw new ParseError({
479
- lexPosition: token.start,
480
- tokenValue: token.value,
481
- tokenType: token.type,
482
- });
648
+ this.#throwParseError();
483
649
  }
484
650
  }
485
- __classPrivateFieldGet(this, _Parser_instances, "m", _Parser_advance).call(this);
486
- }, _Parser_advance = function _Parser_advance() {
487
- __classPrivateFieldSet(this, _Parser_index, __classPrivateFieldGet(this, _Parser_index, "f") + 1, "f");
488
- }, _Parser_currentToken = function _Parser_currentToken() {
489
- return __classPrivateFieldGet(this, _Parser_tokens, "f")[__classPrivateFieldGet(this, _Parser_index, "f")].type;
490
- }, _Parser_lookahead = function _Parser_lookahead(number) {
491
- return __classPrivateFieldGet(this, _Parser_tokens, "f")[__classPrivateFieldGet(this, _Parser_index, "f") + number].type;
492
- }, _Parser_lookaheadToken = function _Parser_lookaheadToken(number) {
493
- return __classPrivateFieldGet(this, _Parser_tokens, "f")[__classPrivateFieldGet(this, _Parser_index, "f") + number];
494
- }, _Parser_evictCache = function _Parser_evictCache() {
495
- const newCache = Object.keys(__classPrivateFieldGet(this, _Parser_cache, "f")).reduce((acc, key) => {
496
- if (randomInt(0, 100) > 50) {
497
- acc[key] = __classPrivateFieldGet(this, _Parser_cache, "f")[key];
498
- }
499
- return acc;
500
- }, {});
501
- __classPrivateFieldSet(this, _Parser_cache, newCache, "f");
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 };