pacc 6.9.0 → 6.9.1
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/package.json +1 -1
- package/src/expression.mjs +43 -12
package/package.json
CHANGED
package/src/expression.mjs
CHANGED
|
@@ -53,19 +53,39 @@ export function binop(op, left, right, fallback) {
|
|
|
53
53
|
return fallback(op, left, right);
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
+
/**
|
|
57
|
+
*
|
|
58
|
+
* @param {string} message
|
|
59
|
+
*/
|
|
60
|
+
function error(message) {
|
|
61
|
+
const error = new Error(message);
|
|
62
|
+
throw error;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
*
|
|
66
|
+
* @param {Token} op
|
|
67
|
+
* @param {AST} left
|
|
68
|
+
* @param {AST} right
|
|
69
|
+
*/
|
|
70
|
+
function binopError(op, left, right) {
|
|
71
|
+
error(`Unexpected '${op.str || op}'`);
|
|
72
|
+
}
|
|
73
|
+
|
|
56
74
|
export function parse(input, context = { globals }) {
|
|
57
75
|
input = tokens(input);
|
|
58
76
|
|
|
59
77
|
let node, token, value;
|
|
60
78
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
79
|
+
const predicateIteratorEval = (node, current) => {
|
|
80
|
+
if (current instanceof Set) {
|
|
81
|
+
current = [...current];
|
|
82
|
+
} else if (current instanceof Map) {
|
|
83
|
+
current = [...current.values()];
|
|
84
|
+
}
|
|
85
|
+
return current
|
|
86
|
+
.filter(item => node.predicate(node, item))
|
|
87
|
+
.map(item => node.right.eval(node.right, item));
|
|
88
|
+
};
|
|
69
89
|
const pathEval = (node, current) => {
|
|
70
90
|
let result = current;
|
|
71
91
|
|
|
@@ -207,15 +227,26 @@ export function parse(input, context = { globals }) {
|
|
|
207
227
|
}
|
|
208
228
|
}
|
|
209
229
|
if (last === DOT) {
|
|
210
|
-
if (left.path) {
|
|
211
|
-
left.path.push(...right.path);
|
|
212
|
-
return left;
|
|
213
|
-
}
|
|
214
230
|
switch (typeof left) {
|
|
215
231
|
case "number":
|
|
216
232
|
right.path.unshift(left);
|
|
217
233
|
return right;
|
|
218
234
|
}
|
|
235
|
+
|
|
236
|
+
if (left.path) {
|
|
237
|
+
left.path.push(...right.path);
|
|
238
|
+
return left;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
if (left.eval) {
|
|
242
|
+
return {
|
|
243
|
+
eval: predicateIteratorEval,
|
|
244
|
+
predicate: left.eval,
|
|
245
|
+
right
|
|
246
|
+
// path: right.path
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
|
|
219
250
|
return { eval: pathEval, path: [left.token, right.token] };
|
|
220
251
|
}
|
|
221
252
|
|