@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/cjs/Expression.js
CHANGED
|
@@ -1,27 +1,15 @@
|
|
|
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 _Expression_expression, _Expression_interpreter;
|
|
14
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
3
|
exports.Expression = void 0;
|
|
16
4
|
/**
|
|
17
5
|
* Apply a JMESPath expression to a JSON value.
|
|
18
6
|
*/
|
|
19
7
|
class Expression {
|
|
8
|
+
#expression;
|
|
9
|
+
#interpreter;
|
|
20
10
|
constructor(expression, interpreter) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
__classPrivateFieldSet(this, _Expression_expression, expression, "f");
|
|
24
|
-
__classPrivateFieldSet(this, _Expression_interpreter, interpreter, "f");
|
|
11
|
+
this.#expression = expression;
|
|
12
|
+
this.#interpreter = interpreter;
|
|
25
13
|
}
|
|
26
14
|
/**
|
|
27
15
|
* Evaluate the expression against a JSON value.
|
|
@@ -31,8 +19,7 @@ class Expression {
|
|
|
31
19
|
* @returns The result of applying the expression to the value.
|
|
32
20
|
*/
|
|
33
21
|
visit(value, node) {
|
|
34
|
-
return
|
|
22
|
+
return this.#interpreter.visit(node ?? this.#expression, value);
|
|
35
23
|
}
|
|
36
24
|
}
|
|
37
25
|
exports.Expression = Expression;
|
|
38
|
-
_Expression_expression = new WeakMap(), _Expression_interpreter = new WeakMap();
|
package/lib/cjs/Functions.js
CHANGED
|
@@ -41,12 +41,10 @@ const utils_js_1 = require("./utils.js");
|
|
|
41
41
|
* ```
|
|
42
42
|
*/
|
|
43
43
|
class Functions {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
this.methods = new Set();
|
|
49
|
-
}
|
|
44
|
+
/**
|
|
45
|
+
* A set of all the custom functions available in this and all child classes.
|
|
46
|
+
*/
|
|
47
|
+
methods = new Set();
|
|
50
48
|
/**
|
|
51
49
|
* Get the absolute value of the provided number.
|
|
52
50
|
*
|
package/lib/cjs/Lexer.js
CHANGED
|
@@ -1,16 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
|
-
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");
|
|
5
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
|
-
};
|
|
7
|
-
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
8
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
9
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
10
|
-
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");
|
|
11
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
12
|
-
};
|
|
13
|
-
var _Lexer_instances, _Lexer_position, _Lexer_expression, _Lexer_chars, _Lexer_current, _Lexer_length, _Lexer_consumeComparatorSigns, _Lexer_consumeEqualSign, _Lexer_consumeIdentifier, _Lexer_consumeNegativeNumber, _Lexer_consumeNumber, _Lexer_consumeSquareBracket, _Lexer_initializeForExpression, _Lexer_next, _Lexer_consumeUntil, _Lexer_consumeLiteral, _Lexer_consumeQuotedIdentifier, _Lexer_consumeRawStringLiteral, _Lexer_matchOrElse;
|
|
14
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
3
|
exports.Lexer = void 0;
|
|
16
4
|
const constants_js_1 = require("./constants.js");
|
|
@@ -21,37 +9,11 @@ const errors_js_1 = require("./errors.js");
|
|
|
21
9
|
* This lexer tokenizes a JMESPath expression into a sequence of tokens.
|
|
22
10
|
*/
|
|
23
11
|
class Lexer {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
_Lexer_current.set(this, void 0);
|
|
30
|
-
_Lexer_length.set(this, void 0);
|
|
31
|
-
/**
|
|
32
|
-
* Consume a comparator sign.
|
|
33
|
-
*
|
|
34
|
-
* This method is called when the lexer encounters a comparator sign.
|
|
35
|
-
*
|
|
36
|
-
* @param current The current character
|
|
37
|
-
*/
|
|
38
|
-
_Lexer_consumeComparatorSigns.set(this, (current) => {
|
|
39
|
-
switch (current) {
|
|
40
|
-
case '<':
|
|
41
|
-
return __classPrivateFieldGet(this, _Lexer_instances, "m", _Lexer_matchOrElse).call(this, '=', 'lte', 'lt');
|
|
42
|
-
case '>':
|
|
43
|
-
return __classPrivateFieldGet(this, _Lexer_instances, "m", _Lexer_matchOrElse).call(this, '=', 'gte', 'gt');
|
|
44
|
-
case '!':
|
|
45
|
-
return __classPrivateFieldGet(this, _Lexer_instances, "m", _Lexer_matchOrElse).call(this, '=', 'ne', 'not');
|
|
46
|
-
case '|':
|
|
47
|
-
return __classPrivateFieldGet(this, _Lexer_instances, "m", _Lexer_matchOrElse).call(this, '|', 'or', 'pipe');
|
|
48
|
-
case '&':
|
|
49
|
-
return __classPrivateFieldGet(this, _Lexer_instances, "m", _Lexer_matchOrElse).call(this, '&', 'and', 'expref');
|
|
50
|
-
default:
|
|
51
|
-
return __classPrivateFieldGet(this, _Lexer_instances, "m", _Lexer_consumeEqualSign).call(this);
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
}
|
|
12
|
+
#position;
|
|
13
|
+
#expression;
|
|
14
|
+
#chars;
|
|
15
|
+
#current;
|
|
16
|
+
#length;
|
|
55
17
|
/**
|
|
56
18
|
* Tokenize a JMESPath expression.
|
|
57
19
|
*
|
|
@@ -60,38 +22,38 @@ class Lexer {
|
|
|
60
22
|
* @param expression The JMESPath expression to tokenize.
|
|
61
23
|
*/
|
|
62
24
|
*tokenize(expression) {
|
|
63
|
-
|
|
64
|
-
while (
|
|
65
|
-
if (constants_js_1.SIMPLE_TOKENS.has(
|
|
25
|
+
this.#initializeForExpression(expression);
|
|
26
|
+
while (this.#current !== '' && this.#current !== undefined) {
|
|
27
|
+
if (constants_js_1.SIMPLE_TOKENS.has(this.#current)) {
|
|
66
28
|
yield {
|
|
67
29
|
// We know that SIMPLE_TOKENS has this.#current as a key because
|
|
68
30
|
// we checked for that above.
|
|
69
31
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
70
|
-
type: constants_js_1.SIMPLE_TOKENS.get(
|
|
71
|
-
value:
|
|
72
|
-
start:
|
|
73
|
-
end:
|
|
32
|
+
type: constants_js_1.SIMPLE_TOKENS.get(this.#current),
|
|
33
|
+
value: this.#current,
|
|
34
|
+
start: this.#position,
|
|
35
|
+
end: this.#position + 1,
|
|
74
36
|
};
|
|
75
|
-
|
|
37
|
+
this.#next();
|
|
76
38
|
}
|
|
77
|
-
else if (constants_js_1.START_IDENTIFIER.has(
|
|
78
|
-
yield
|
|
39
|
+
else if (constants_js_1.START_IDENTIFIER.has(this.#current)) {
|
|
40
|
+
yield this.#consumeIdentifier();
|
|
79
41
|
}
|
|
80
|
-
else if (constants_js_1.WHITESPACE.has(
|
|
81
|
-
|
|
42
|
+
else if (constants_js_1.WHITESPACE.has(this.#current)) {
|
|
43
|
+
this.#next();
|
|
82
44
|
}
|
|
83
|
-
else if (
|
|
84
|
-
yield
|
|
45
|
+
else if (this.#current === '[') {
|
|
46
|
+
yield this.#consumeSquareBracket();
|
|
85
47
|
}
|
|
86
|
-
else if (
|
|
87
|
-
yield
|
|
48
|
+
else if (this.#current === `'`) {
|
|
49
|
+
yield this.#consumeRawStringLiteral();
|
|
88
50
|
}
|
|
89
|
-
else if (
|
|
90
|
-
yield
|
|
51
|
+
else if (this.#current === '`') {
|
|
52
|
+
yield this.#consumeLiteral();
|
|
91
53
|
}
|
|
92
|
-
else if (constants_js_1.VALID_NUMBER.has(
|
|
93
|
-
const start =
|
|
94
|
-
const buff =
|
|
54
|
+
else if (constants_js_1.VALID_NUMBER.has(this.#current)) {
|
|
55
|
+
const start = this.#position;
|
|
56
|
+
const buff = this.#consumeNumber();
|
|
95
57
|
yield {
|
|
96
58
|
type: 'number',
|
|
97
59
|
value: parseInt(buff),
|
|
@@ -99,173 +61,282 @@ class Lexer {
|
|
|
99
61
|
end: start + buff.length,
|
|
100
62
|
};
|
|
101
63
|
}
|
|
102
|
-
else if (
|
|
103
|
-
yield
|
|
64
|
+
else if (this.#current === '-') {
|
|
65
|
+
yield this.#consumeNegativeNumber();
|
|
104
66
|
}
|
|
105
|
-
else if (
|
|
106
|
-
yield
|
|
67
|
+
else if (this.#current === '"') {
|
|
68
|
+
yield this.#consumeQuotedIdentifier();
|
|
107
69
|
}
|
|
108
|
-
else if (['<', '>', '!', '=', '|', '&'].includes(
|
|
109
|
-
yield
|
|
70
|
+
else if (['<', '>', '!', '=', '|', '&'].includes(this.#current)) {
|
|
71
|
+
yield this.#consumeComparatorSigns(this.#current);
|
|
110
72
|
}
|
|
111
73
|
else {
|
|
112
|
-
throw new errors_js_1.LexerError(
|
|
74
|
+
throw new errors_js_1.LexerError(this.#position, this.#current);
|
|
113
75
|
}
|
|
114
76
|
}
|
|
115
|
-
yield { type: 'eof', value: '', start:
|
|
77
|
+
yield { type: 'eof', value: '', start: this.#length, end: this.#length };
|
|
116
78
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
type: 'unquoted_identifier',
|
|
140
|
-
value: buff,
|
|
141
|
-
start,
|
|
142
|
-
end: start + buff.length,
|
|
79
|
+
/**
|
|
80
|
+
* Consume a comparator sign.
|
|
81
|
+
*
|
|
82
|
+
* This method is called when the lexer encounters a comparator sign.
|
|
83
|
+
*
|
|
84
|
+
* @param current The current character
|
|
85
|
+
*/
|
|
86
|
+
#consumeComparatorSigns = (current) => {
|
|
87
|
+
switch (current) {
|
|
88
|
+
case '<':
|
|
89
|
+
return this.#matchOrElse('=', 'lte', 'lt');
|
|
90
|
+
case '>':
|
|
91
|
+
return this.#matchOrElse('=', 'gte', 'gt');
|
|
92
|
+
case '!':
|
|
93
|
+
return this.#matchOrElse('=', 'ne', 'not');
|
|
94
|
+
case '|':
|
|
95
|
+
return this.#matchOrElse('|', 'or', 'pipe');
|
|
96
|
+
case '&':
|
|
97
|
+
return this.#matchOrElse('&', 'and', 'expref');
|
|
98
|
+
default:
|
|
99
|
+
return this.#consumeEqualSign();
|
|
100
|
+
}
|
|
143
101
|
};
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
102
|
+
/**
|
|
103
|
+
* Consume an equal sign.
|
|
104
|
+
*
|
|
105
|
+
* This method is called when the lexer encounters an equal sign.
|
|
106
|
+
* It checks if the next character is also an equal sign and returns
|
|
107
|
+
* the corresponding token.
|
|
108
|
+
*/
|
|
109
|
+
#consumeEqualSign() {
|
|
110
|
+
if (this.#next() === '=') {
|
|
111
|
+
this.#next();
|
|
112
|
+
return {
|
|
113
|
+
type: 'eq',
|
|
114
|
+
value: '==',
|
|
115
|
+
start: this.#position - 1,
|
|
116
|
+
end: this.#position,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
throw new errors_js_1.LexerError(this.#position - 1, '=');
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Consume an unquoted identifier.
|
|
125
|
+
*
|
|
126
|
+
* This method is called when the lexer encounters a character that is a valid
|
|
127
|
+
* identifier. It advances the lexer until it finds a character that is not a
|
|
128
|
+
* valid identifier and returns the corresponding token.
|
|
129
|
+
*/
|
|
130
|
+
#consumeIdentifier() {
|
|
131
|
+
const start = this.#position;
|
|
132
|
+
let buff = this.#current;
|
|
133
|
+
while (constants_js_1.VALID_IDENTIFIER.has(this.#next())) {
|
|
134
|
+
buff += this.#current;
|
|
135
|
+
}
|
|
148
136
|
return {
|
|
149
|
-
type: '
|
|
150
|
-
value:
|
|
151
|
-
start
|
|
137
|
+
type: 'unquoted_identifier',
|
|
138
|
+
value: buff,
|
|
139
|
+
start,
|
|
152
140
|
end: start + buff.length,
|
|
153
141
|
};
|
|
154
142
|
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
143
|
+
/**
|
|
144
|
+
* Consume a negative number.
|
|
145
|
+
*
|
|
146
|
+
* This method is called when the lexer encounters a negative sign.
|
|
147
|
+
* It checks if the next character is a number and returns the corresponding token.
|
|
148
|
+
*/
|
|
149
|
+
#consumeNegativeNumber() {
|
|
150
|
+
const start = this.#position;
|
|
151
|
+
const buff = this.#consumeNumber();
|
|
152
|
+
if (buff.length > 1) {
|
|
153
|
+
return {
|
|
154
|
+
type: 'number',
|
|
155
|
+
value: parseInt(buff),
|
|
156
|
+
start: start,
|
|
157
|
+
end: start + buff.length,
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
// If the negative sign is not followed by a number, it is an error.
|
|
162
|
+
throw new errors_js_1.LexerError(start, 'Unknown token after "-"');
|
|
163
|
+
}
|
|
171
164
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
165
|
+
/**
|
|
166
|
+
* Consume a raw string that is a number.
|
|
167
|
+
*
|
|
168
|
+
* It takes the current position and advances
|
|
169
|
+
* the lexer until it finds a character that
|
|
170
|
+
* is not a number.
|
|
171
|
+
*/
|
|
172
|
+
#consumeNumber() {
|
|
173
|
+
let buff = this.#current;
|
|
174
|
+
while (constants_js_1.VALID_NUMBER.has(this.#next())) {
|
|
175
|
+
buff += this.#current;
|
|
176
|
+
}
|
|
177
|
+
return buff;
|
|
175
178
|
}
|
|
176
|
-
|
|
177
|
-
|
|
179
|
+
/**
|
|
180
|
+
* Consume a square bracket.
|
|
181
|
+
*
|
|
182
|
+
* This method is called when the lexer encounters a square bracket.
|
|
183
|
+
* It checks if the next character is a question mark or a closing
|
|
184
|
+
* square bracket and returns the corresponding token.
|
|
185
|
+
*/
|
|
186
|
+
#consumeSquareBracket() {
|
|
187
|
+
const start = this.#position;
|
|
188
|
+
const nextChar = this.#next();
|
|
189
|
+
if (nextChar == ']') {
|
|
190
|
+
this.#next();
|
|
191
|
+
return { type: 'flatten', value: '[]', start: start, end: start + 2 };
|
|
192
|
+
}
|
|
193
|
+
else if (nextChar == '?') {
|
|
194
|
+
this.#next();
|
|
195
|
+
return { type: 'filter', value: '[?', start: start, end: start + 2 };
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
return { type: 'lbracket', value: '[', start: start, end: start + 1 };
|
|
199
|
+
}
|
|
178
200
|
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
201
|
+
/**
|
|
202
|
+
* Initializes the lexer for the given expression.
|
|
203
|
+
*
|
|
204
|
+
* We use a separate method for this instead of the constructor
|
|
205
|
+
* because we want to be able to reuse the same lexer instance
|
|
206
|
+
* and also because we want to be able to expose a public API
|
|
207
|
+
* for tokenizing expressions like `new Lexer().tokenize(expression)`.
|
|
208
|
+
*
|
|
209
|
+
* @param expression The JMESPath expression to tokenize.
|
|
210
|
+
*/
|
|
211
|
+
#initializeForExpression(expression) {
|
|
212
|
+
if (typeof expression !== 'string') {
|
|
213
|
+
throw new errors_js_1.EmptyExpressionError();
|
|
214
|
+
}
|
|
215
|
+
this.#position = 0;
|
|
216
|
+
this.#expression = expression;
|
|
217
|
+
this.#chars = Array.from(expression);
|
|
218
|
+
this.#current = this.#chars[0];
|
|
219
|
+
this.#length = this.#expression.length;
|
|
182
220
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
221
|
+
/**
|
|
222
|
+
* Advance the lexer to the next character in the expression.
|
|
223
|
+
*/
|
|
224
|
+
#next() {
|
|
225
|
+
if (this.#position === this.#length - 1) {
|
|
226
|
+
this.#current = '';
|
|
227
|
+
}
|
|
228
|
+
else {
|
|
229
|
+
this.#position += 1;
|
|
230
|
+
this.#current = this.#chars[this.#position];
|
|
231
|
+
}
|
|
232
|
+
return this.#current;
|
|
191
233
|
}
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
234
|
+
/**
|
|
235
|
+
* Consume until the given delimiter is reached allowing
|
|
236
|
+
* for escaping of the delimiter with a backslash (`\`).
|
|
237
|
+
*
|
|
238
|
+
* @param delimiter The delimiter to consume until.
|
|
239
|
+
*/
|
|
240
|
+
#consumeUntil(delimiter) {
|
|
241
|
+
const start = this.#position;
|
|
242
|
+
let buff = '';
|
|
243
|
+
this.#next();
|
|
244
|
+
while (this.#current !== delimiter) {
|
|
245
|
+
if (this.#current === '\\') {
|
|
246
|
+
buff += '\\';
|
|
247
|
+
this.#next();
|
|
248
|
+
}
|
|
249
|
+
if (this.#current === '') {
|
|
250
|
+
// We've reached the end of the expression (EOF) before
|
|
251
|
+
// we found the delimiter. This is an error.
|
|
252
|
+
throw new errors_js_1.LexerError(start, this.#expression.substring(start));
|
|
253
|
+
}
|
|
254
|
+
buff += this.#current;
|
|
255
|
+
this.#next();
|
|
256
|
+
}
|
|
257
|
+
// Skip the closing delimiter
|
|
258
|
+
this.#next();
|
|
259
|
+
return buff;
|
|
195
260
|
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
261
|
+
/**
|
|
262
|
+
* Process a literal.
|
|
263
|
+
*
|
|
264
|
+
* A literal is a JSON string that is enclosed in backticks.
|
|
265
|
+
*/
|
|
266
|
+
#consumeLiteral() {
|
|
267
|
+
const start = this.#position;
|
|
268
|
+
const lexeme = this.#consumeUntil('`').replace('\\`', '`');
|
|
269
|
+
try {
|
|
270
|
+
const parsedJson = JSON.parse(lexeme);
|
|
271
|
+
return {
|
|
272
|
+
type: 'literal',
|
|
273
|
+
value: parsedJson,
|
|
274
|
+
start,
|
|
275
|
+
end: this.#position - start,
|
|
276
|
+
};
|
|
205
277
|
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
// we found the delimiter. This is an error.
|
|
209
|
-
throw new errors_js_1.LexerError(start, __classPrivateFieldGet(this, _Lexer_expression, "f").substring(start));
|
|
278
|
+
catch (error) {
|
|
279
|
+
throw new errors_js_1.LexerError(start, lexeme);
|
|
210
280
|
}
|
|
211
|
-
buff += __classPrivateFieldGet(this, _Lexer_current, "f");
|
|
212
|
-
__classPrivateFieldGet(this, _Lexer_instances, "m", _Lexer_next).call(this);
|
|
213
281
|
}
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
const
|
|
282
|
+
/**
|
|
283
|
+
* Process a quoted identifier.
|
|
284
|
+
*
|
|
285
|
+
* A quoted identifier is a string that is enclosed in double quotes.
|
|
286
|
+
*/
|
|
287
|
+
#consumeQuotedIdentifier() {
|
|
288
|
+
const start = this.#position;
|
|
289
|
+
const lexeme = '"' + this.#consumeUntil('"') + '"';
|
|
290
|
+
const tokenLen = this.#position - start;
|
|
222
291
|
return {
|
|
223
|
-
type: '
|
|
224
|
-
value:
|
|
292
|
+
type: 'quoted_identifier',
|
|
293
|
+
value: JSON.parse(lexeme),
|
|
225
294
|
start,
|
|
226
|
-
end:
|
|
295
|
+
end: tokenLen,
|
|
227
296
|
};
|
|
228
297
|
}
|
|
229
|
-
|
|
230
|
-
|
|
298
|
+
/**
|
|
299
|
+
* Process a raw string literal.
|
|
300
|
+
*
|
|
301
|
+
* A raw string literal is a string that is enclosed in single quotes.
|
|
302
|
+
*/
|
|
303
|
+
#consumeRawStringLiteral() {
|
|
304
|
+
const start = this.#position;
|
|
305
|
+
const lexeme = this.#consumeUntil(`'`).replace(`\\'`, `'`);
|
|
306
|
+
const tokenLen = this.#position - start;
|
|
307
|
+
return {
|
|
308
|
+
type: 'literal',
|
|
309
|
+
value: lexeme,
|
|
310
|
+
start,
|
|
311
|
+
end: tokenLen,
|
|
312
|
+
};
|
|
231
313
|
}
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
}, _Lexer_matchOrElse = function _Lexer_matchOrElse(expected, matchType, elseType) {
|
|
253
|
-
const start = __classPrivateFieldGet(this, _Lexer_position, "f");
|
|
254
|
-
const current = __classPrivateFieldGet(this, _Lexer_current, "f");
|
|
255
|
-
const nextChar = __classPrivateFieldGet(this, _Lexer_instances, "m", _Lexer_next).call(this);
|
|
256
|
-
if (nextChar === expected) {
|
|
257
|
-
__classPrivateFieldGet(this, _Lexer_instances, "m", _Lexer_next).call(this);
|
|
314
|
+
/**
|
|
315
|
+
* Match the expected character and return the corresponding token type.
|
|
316
|
+
*
|
|
317
|
+
* @param expected The expected character
|
|
318
|
+
* @param matchType The token type to return if the expected character is found
|
|
319
|
+
* @param elseType The token type to return if the expected character is not found
|
|
320
|
+
*/
|
|
321
|
+
#matchOrElse(expected, matchType, elseType) {
|
|
322
|
+
const start = this.#position;
|
|
323
|
+
const current = this.#current;
|
|
324
|
+
const nextChar = this.#next();
|
|
325
|
+
if (nextChar === expected) {
|
|
326
|
+
this.#next();
|
|
327
|
+
return {
|
|
328
|
+
type: matchType,
|
|
329
|
+
value: current + nextChar,
|
|
330
|
+
start,
|
|
331
|
+
end: start + 2,
|
|
332
|
+
};
|
|
333
|
+
}
|
|
258
334
|
return {
|
|
259
|
-
type:
|
|
260
|
-
value: current
|
|
335
|
+
type: elseType,
|
|
336
|
+
value: current,
|
|
261
337
|
start,
|
|
262
|
-
end: start
|
|
338
|
+
end: start,
|
|
263
339
|
};
|
|
264
340
|
}
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
value: current,
|
|
268
|
-
start,
|
|
269
|
-
end: start,
|
|
270
|
-
};
|
|
271
|
-
};
|
|
341
|
+
}
|
|
342
|
+
exports.Lexer = Lexer;
|
package/lib/cjs/ParsedResult.js
CHANGED
|
@@ -4,6 +4,8 @@ exports.ParsedResult = void 0;
|
|
|
4
4
|
const TreeInterpreter_js_1 = require("./TreeInterpreter.js");
|
|
5
5
|
const errors_js_1 = require("./errors.js");
|
|
6
6
|
class ParsedResult {
|
|
7
|
+
expression;
|
|
8
|
+
parsed;
|
|
7
9
|
constructor(expression, parsed) {
|
|
8
10
|
this.expression = expression;
|
|
9
11
|
this.parsed = parsed;
|