@valbuild/core 0.61.0 → 0.62.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.
@@ -1,643 +0,0 @@
1
- 'use strict';
2
-
3
- var index$1 = require('./index-f2552460.cjs.prod.js');
4
- var result = require('./result-26f67b40.cjs.prod.js');
5
-
6
- var WHITE_SPACE = ["\n", "\r", "\t", " "];
7
- function tokenize(input) {
8
- var tokens = [];
9
- var cursor = 0;
10
- while (cursor < input.length) {
11
- var _char = input[cursor];
12
- var peek = input[cursor + 1];
13
- // TODO: remove this not used any more
14
- if (_char === "!" && peek === "(") {
15
- tokens.push({
16
- type: "!(",
17
- span: [cursor, cursor + 1]
18
- });
19
- cursor += 2;
20
- } else if (_char === "(") {
21
- tokens.push({
22
- type: "(",
23
- span: [cursor, cursor]
24
- });
25
- cursor++;
26
- } else if (_char === ")") {
27
- tokens.push({
28
- type: ")",
29
- span: [cursor, cursor]
30
- });
31
- cursor++;
32
- } else if (_char === "'" || _char === "}") {
33
- var _start = cursor;
34
- var value = "";
35
- var unescapedValue = "";
36
- var escaped = false;
37
- if (_char === "}") {
38
- tokens.push({
39
- type: "}",
40
- span: [cursor, cursor]
41
- });
42
- } else if (_char === "'") {
43
- tokens.push({
44
- type: "'",
45
- span: [cursor, cursor]
46
- });
47
- }
48
- while (cursor < input.length) {
49
- if (_char === "\\") {
50
- escaped = !escaped;
51
- } else {
52
- escaped = false;
53
- }
54
- if (peek === "'" && !escaped) {
55
- cursor += 2;
56
- break;
57
- } else if (_char === "$" && peek === "{") {
58
- cursor += 2;
59
- break;
60
- }
61
- cursor++;
62
- _char = input[cursor];
63
- peek = input[cursor + 1];
64
- if (!(_char === "$" && peek === "{") && cursor < input.length) {
65
- if (!(_char === "\\" && !escaped // counter-intuitive, but escape just became false if this was a backslash we want to escape
66
- )) {
67
- value += _char;
68
- }
69
- unescapedValue += _char;
70
- }
71
- }
72
- var cursorOffset = peek === "'" && !escaped ? 2 : _char === "$" && peek === "{" ? 3 : 1;
73
- if (value) {
74
- tokens.push(index$1._objectSpread2({
75
- type: "string",
76
- span: [_start + 1, cursor - cursorOffset],
77
- value: value
78
- }, unescapedValue !== value && {
79
- unescapedValue: unescapedValue
80
- }));
81
- }
82
- if (peek === "'" && !escaped) {
83
- tokens.push({
84
- type: "'",
85
- span: [cursor - 1, cursor - 1]
86
- });
87
- } else if (_char === "$" && peek === "{") {
88
- tokens.push({
89
- type: "${",
90
- span: [cursor - cursorOffset + 1, cursor - 1]
91
- });
92
- }
93
- } else if (WHITE_SPACE.includes(_char)) {
94
- var _start2 = cursor;
95
- while (WHITE_SPACE.includes(input[cursor]) && cursor < input.length) {
96
- cursor++;
97
- }
98
- tokens.push({
99
- type: "ws",
100
- span: [_start2, cursor - 1]
101
- });
102
- } else {
103
- var _value = "";
104
- var _start3 = cursor;
105
- do {
106
- _char = input[cursor];
107
- peek = input[cursor + 1];
108
- _value += _char;
109
- cursor++;
110
- } while (!WHITE_SPACE.includes(peek) && peek !== ")" && peek !== "'" && cursor < input.length);
111
- tokens.push({
112
- type: "token",
113
- span: [_start3, cursor - 1],
114
- value: _value
115
- });
116
- }
117
- }
118
- return [tokens, cursor];
119
- }
120
-
121
- var ParserError = /*#__PURE__*/index$1._createClass(function ParserError(message, span) {
122
- index$1._classCallCheck(this, ParserError);
123
- this.message = message;
124
- this.span = span;
125
- });
126
- function parseTokens(inputTokens) {
127
- var tokens = inputTokens.slice();
128
- function slurpCall(first, isAnon) {
129
- var _tokens$, _tokens$2, _tokens$3, _tokens$7, _tokens$8, _args$slice$0$span;
130
- // peek
131
- if (((_tokens$ = tokens[0]) === null || _tokens$ === void 0 ? void 0 : _tokens$.type) === "ws" && ((_tokens$2 = tokens[1]) === null || _tokens$2 === void 0 ? void 0 : _tokens$2.type) === ")" || ((_tokens$3 = tokens[0]) === null || _tokens$3 === void 0 ? void 0 : _tokens$3.type) === ")") {
132
- slurpWs();
133
- tokens.shift();
134
- return result.ok(new index$1.Sym("()", [first.span[0], first.span[1] + 1]));
135
- }
136
- var args = [];
137
- var completed = false;
138
- while (!completed) {
139
- var _res = slurp();
140
- if (result.isOk(_res)) {
141
- var _tokens$4, _tokens$5, _tokens$6;
142
- args.push(_res.value);
143
- completed = ((_tokens$4 = tokens[0]) === null || _tokens$4 === void 0 ? void 0 : _tokens$4.type) !== "ws" || ((_tokens$5 = tokens[0]) === null || _tokens$5 === void 0 ? void 0 : _tokens$5.type) === "ws" && ((_tokens$6 = tokens[1]) === null || _tokens$6 === void 0 ? void 0 : _tokens$6.type) === ")";
144
- } else {
145
- return _res;
146
- }
147
- }
148
- if (((_tokens$7 = tokens[0]) === null || _tokens$7 === void 0 ? void 0 : _tokens$7.type) === "ws" && ((_tokens$8 = tokens[1]) === null || _tokens$8 === void 0 ? void 0 : _tokens$8.type) === ")") {
149
- tokens.shift();
150
- }
151
- var last = tokens.shift();
152
- if ((last === null || last === void 0 ? void 0 : last.type) !== ")") {
153
- return result.err(new ParserError("unbalanced parens: missing a ')'", [first.span[0], first.span[1] + 1]));
154
- }
155
- return result.ok(new index$1.Call(args, isAnon, [first.span[0], ((_args$slice$0$span = args.slice(-1)[0].span) === null || _args$slice$0$span === void 0 ? void 0 : _args$slice$0$span[1]) || -1]));
156
- }
157
- function slurpWs() {
158
- while (((_tokens$9 = tokens[0]) === null || _tokens$9 === void 0 ? void 0 : _tokens$9.type) === "ws") {
159
- var _tokens$9;
160
- tokens.shift();
161
- }
162
- }
163
- function slurpTemplate(first) {
164
- var children = [];
165
- while (tokens.length > 0) {
166
- var _tokens$10;
167
- if (((_tokens$10 = tokens[0]) === null || _tokens$10 === void 0 ? void 0 : _tokens$10.type) === "'") {
168
- break;
169
- }
170
- var nextToken = tokens.shift();
171
- if ((nextToken === null || nextToken === void 0 ? void 0 : nextToken.type) === "${") {
172
- var _res2 = slurp();
173
- if (result.isOk(_res2)) {
174
- children.push(_res2.value);
175
- var _last = tokens.shift();
176
- if (!_last) {
177
- var _children$slice$0$spa;
178
- return result.err(new ParserError("unbalanced string template: missing a '}'", [first.span[0], (_children$slice$0$spa = children.slice(-1)[0].span) === null || _children$slice$0$spa === void 0 ? void 0 : _children$slice$0$spa[1]]));
179
- } else if (_last.type !== "}") {
180
- return result.err(new ParserError("unbalanced string template: expected '}'", _last.span));
181
- }
182
- } else {
183
- return _res2;
184
- }
185
- } else if ((nextToken === null || nextToken === void 0 ? void 0 : nextToken.type) === "string") {
186
- children.push(new index$1.StringLiteral(nextToken.unescapedValue || nextToken.value || "", nextToken.span));
187
- }
188
- }
189
- var last = tokens.shift();
190
- if (!last) {
191
- var _children$slice$0$spa2;
192
- return result.err(new ParserError("unbalanced string template: missing a '''", [first.span[0], (_children$slice$0$spa2 = children.slice(-1)[0].span) === null || _children$slice$0$spa2 === void 0 ? void 0 : _children$slice$0$spa2[1]]));
193
- } else if (last.type !== "'") {
194
- return result.err(new ParserError("unbalanced string template: expected '''", last.span));
195
- }
196
- return result.ok(new index$1.StringTemplate(children, [first.span[0], last.span[1]]));
197
- }
198
- function slurpString(first) {
199
- var _tokens$11, _tokens$12, _tokens$13;
200
- if (((_tokens$11 = tokens[0]) === null || _tokens$11 === void 0 ? void 0 : _tokens$11.type) === "string" && ((_tokens$12 = tokens[1]) === null || _tokens$12 === void 0 ? void 0 : _tokens$12.type) === "'") {
201
- var stringToken = tokens.shift();
202
- var last = tokens.shift();
203
- if (!last || !stringToken) {
204
- throw Error("Unexpected error: stringToken or last is undefined");
205
- }
206
- return result.ok(new index$1.StringLiteral(stringToken.unescapedValue || stringToken.value || "", [first.span[0], last.span[1]]));
207
- } else if (((_tokens$13 = tokens[0]) === null || _tokens$13 === void 0 ? void 0 : _tokens$13.type) === "'") {
208
- var _last2 = tokens.shift();
209
- if (!_last2) {
210
- throw Error("Unexpected error: last is undefined");
211
- }
212
- return result.ok(new index$1.StringLiteral("", [first.span[0], _last2.span[1]]));
213
- } else {
214
- return slurpTemplate(first);
215
- }
216
- }
217
- function slurp() {
218
- slurpWs();
219
- var first = tokens.shift();
220
- if (!first) {
221
- return result.err(new ParserError("expected '(', '!(', string or literal", [0, 0]));
222
- }
223
- if (first.type === "(" || first.type === "!(") {
224
- return slurpCall(first, first.type === "!(");
225
- } else if (first.type === "'") {
226
- return slurpString(first);
227
- } else if (first.type === "token") {
228
- var _first$value, _first$value2, _first$value3, _first$value4, _first$value5, _first$value6;
229
- if ((_first$value = first.value) !== null && _first$value !== void 0 && _first$value.includes("(") || (_first$value2 = first.value) !== null && _first$value2 !== void 0 && _first$value2.includes(")")) {
230
- return result.err(new ParserError("unexpected token: '(' and ')' are not allowed in tokens", first.span));
231
- }
232
- if ((_first$value3 = first.value) !== null && _first$value3 !== void 0 && _first$value3.includes("'")) {
233
- return result.err(new ParserError('unexpected token: "\'" is not allowed in tokens', first.span));
234
- }
235
- if ((_first$value4 = first.value) !== null && _first$value4 !== void 0 && _first$value4.includes(".")) {
236
- return result.err(new ParserError('unexpected token: "." is not allowed in tokens', first.span));
237
- }
238
- if ((_first$value5 = first.value) !== null && _first$value5 !== void 0 && _first$value5.includes("{") || (_first$value6 = first.value) !== null && _first$value6 !== void 0 && _first$value6.includes("}")) {
239
- return result.err(new ParserError("unexpected token: '{' and '}' are not allowed in tokens", first.span));
240
- }
241
- return result.ok(new index$1.Sym(first.value || "", first.span));
242
- } else {
243
- return result.err(new ParserError("expected '(', '!(' or literal or token".concat(first.value || first.type ? ", got: '".concat(first.value || first.type, "'") : ""), first.span));
244
- }
245
- }
246
- var res = slurp();
247
- slurpWs();
248
- if (result.isErr(res)) {
249
- return res;
250
- }
251
- if (tokens.length > 0) {
252
- return result.err(new ParserError("expected end of input, superfluous tokens", [tokens[0].span[0], tokens.slice(-1)[0].span[1]]));
253
- }
254
- return res;
255
- }
256
- function parse(input) {
257
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
258
- var _tokenize = tokenize(input),
259
- _tokenize2 = index$1._slicedToArray(_tokenize, 2),
260
- tokens = _tokenize2[0];
261
- _tokenize2[1]; // TODO: we can use cursor to improve error messages / spans
262
- return parseTokens(tokens);
263
- }
264
-
265
- /* eslint-disable @typescript-eslint/no-unused-vars */
266
-
267
- /**
268
- * Selectors can be used to select parts of a Val module.
269
- * Unlike queries, joins, aggregates etc is and will not be supported.
270
- *
271
- * They are designed to be be used as if they were "normal" JSON data,
272
- * though some concessions had to be made because of TypeScript limitations.
273
- *
274
- * Selectors works equally on source content, defined in code, and remote content.
275
- *
276
- * @example
277
- * // Select the title of a document
278
- * const titles = useVal(docsVal.map((doc) => doc.title));
279
- *
280
- * @example
281
- * // Match on a union type
282
- * const titles = useVal(docsVal.map((doc) => doc.fold("type")({
283
- * newsletter: (newsletter) => newsletter.title,
284
- * email: (email) => email.subject,
285
- * }));
286
- *
287
- */
288
-
289
- /**
290
- * @internal
291
- */
292
- var GetSchema = Symbol("GetSchema");
293
- /**
294
- /**
295
- * @internal
296
- */
297
- var Path = Symbol("Path");
298
- /**
299
- * @internal
300
- */
301
- var SourceOrExpr = Symbol("SourceOrExpr");
302
-
303
- /**
304
- * Use this type to convert types that accepts both Source and Selectors
305
- *
306
- * An example would be where literals are supported like in most higher order functions (e.g. map in array)
307
- **/
308
-
309
- function hasOwn(obj, prop) {
310
- return Object.prototype.hasOwnProperty.call(obj, prop);
311
- }
312
- function _andThen(f, source, path) {
313
- if (source) {
314
- return newSelectorProxy(f(newSelectorProxy(source, path)));
315
- }
316
- return newSelectorProxy(source, path);
317
- }
318
- function isSelector(source) {
319
- return index$1._typeof(source) === "object" && source !== null && (SourceOrExpr in source || Path in source);
320
- }
321
- function newSelectorProxy(source, path, moduleSchema) {
322
- if (index$1._typeof(source) === "object") {
323
- if (isSelector(source)) {
324
- return source;
325
- } else if (index$1.isSerializedVal(source)) {
326
- return newSelectorProxy(source.val, source.valPath);
327
- }
328
- }
329
- if (source && source[index$1.FILE_REF_PROP] && source[index$1.VAL_EXTENSION] === "file") {
330
- var fileRef = source[index$1.FILE_REF_PROP];
331
- if (typeof fileRef !== "string") {
332
- throw Error("Invalid file ref: " + fileRef);
333
- }
334
- return newSelectorProxy(index$1.convertFileSource(source), path, moduleSchema);
335
- }
336
- switch (index$1._typeof(source)) {
337
- case "function":
338
- case "symbol":
339
- throw Error("Invalid selector type: ".concat(index$1._typeof(source), ": ").concat(source));
340
- case "object":
341
- // Handles both objects and arrays!
342
- if (source !== null) {
343
- return new Proxy(source, {
344
- // TODO: see proxy docs if we want more traps
345
- has: function has(target, prop) {
346
- if (prop === SourceOrExpr) {
347
- return true;
348
- }
349
- if (prop === Path) {
350
- return true;
351
- }
352
- if (prop === "andThen") {
353
- return true;
354
- }
355
- if (prop === GetSchema) {
356
- return true;
357
- }
358
- return prop in target;
359
- },
360
- get: function get(target, prop) {
361
- if (prop === SourceOrExpr) {
362
- return source;
363
- }
364
- if (prop === Path) {
365
- return path;
366
- }
367
- if (prop === GetSchema) {
368
- return moduleSchema;
369
- }
370
- if (prop === "andThen") {
371
- return function (f) {
372
- return _andThen(f, source, path);
373
- };
374
- }
375
- if (Array.isArray(target)) {
376
- if (prop === "filter") {
377
- return function (f) {
378
- var filtered = target.map(function (a, i) {
379
- return newSelectorProxy(a, createValPathOfItem(path, i), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.item);
380
- }).filter(function (a) {
381
- if (f && f instanceof index$1.Schema) {
382
- return f.assert(unValify(a));
383
- } else {
384
- return unValify(f(a));
385
- }
386
- });
387
- return newSelectorProxy(filtered, path, moduleSchema);
388
- };
389
- } else if (prop === "map") {
390
- return function (f) {
391
- var filtered = target.map(function (a, i) {
392
- var valueOrSelector = f(newSelectorProxy(a, createValPathOfItem(path, i), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.item), newSelectorProxy(i));
393
- if (isSelector(valueOrSelector)) {
394
- return valueOrSelector;
395
- }
396
- return newSelectorProxy(valueOrSelector);
397
- });
398
- return newSelectorProxy(filtered, path, moduleSchema);
399
- };
400
- }
401
- }
402
- if (Array.isArray(target) && prop === "length") {
403
- return newSelectorProxy(target.length);
404
- }
405
- var reflectedValue = Reflect.get(target, prop);
406
- if (hasOwn(source, prop)) {
407
- if (!Number.isNaN(Number(prop))) {
408
- return newSelectorProxy(reflectedValue, createValPathOfItem(path, Number(prop)), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.item);
409
- }
410
- return newSelectorProxy(reflectedValue, createValPathOfItem(path, prop), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.items[prop]);
411
- }
412
- return reflectedValue;
413
- }
414
- });
415
- }
416
- // intentional fallthrough
417
- // eslint-disable-next-line no-fallthrough
418
- default:
419
- return index$1._defineProperty(index$1._defineProperty(index$1._defineProperty({
420
- eq: function eq(other) {
421
- var otherValue = other;
422
- if (isSelector(other)) {
423
- otherValue = other[SourceOrExpr];
424
- if (otherValue instanceof index$1.Expr) {
425
- throw Error("TODO: Cannot evaluate equality with an Expr");
426
- }
427
- }
428
- return newSelectorProxy(source === otherValue, undefined);
429
- },
430
- andThen: function andThen(f) {
431
- return _andThen(f, source === undefined ? null : source, path);
432
- }
433
- }, SourceOrExpr, source === undefined ? null : source), Path, path), GetSchema, moduleSchema);
434
- }
435
- }
436
- function createValPathOfItem(arrayPath, prop) {
437
- if (index$1._typeof(prop) === "symbol") {
438
- throw Error("Cannot create val path of array item with symbol prop: ".concat(prop.toString()));
439
- }
440
- return arrayPath && "".concat(arrayPath, ".").concat(JSON.stringify(prop));
441
- }
442
-
443
- // TODO: could we do .val on the objects instead?
444
- function unValify(valueOrSelector) {
445
- if (index$1._typeof(valueOrSelector) === "object" && (SourceOrExpr in valueOrSelector || Path in valueOrSelector)) {
446
- var selectorValue = valueOrSelector[SourceOrExpr];
447
- return selectorValue;
448
- }
449
- return valueOrSelector;
450
- }
451
-
452
- var EvalError = /*#__PURE__*/function () {
453
- function EvalError(message, expr) {
454
- index$1._classCallCheck(this, EvalError);
455
- this.message = message;
456
- this.expr = expr;
457
- }
458
- index$1._createClass(EvalError, [{
459
- key: "toString",
460
- value: function toString() {
461
- return "".concat(this.message, " in: ").concat(this.expr.transpile());
462
- }
463
- }]);
464
- return EvalError;
465
- }();
466
- var MAX_STACK_SIZE = 100; // an arbitrary semi-large number
467
- function evaluateSync(expr, getSource, stack) {
468
- // TODO: amount of evaluates should be limited?
469
- if (stack.length > MAX_STACK_SIZE) {
470
- throw new EvalError("Stack overflow. Final frames: ".concat(stack.slice(-10).map(function (frame, i) {
471
- return frame.map(function (s, j) {
472
- return "@[".concat(i, ",").concat(j, "]: ").concat(JSON.stringify(s));
473
- }).join(", ");
474
- }).join(" -> ")), expr);
475
- }
476
- if (expr instanceof index$1.Call) {
477
- if (expr.children[0] instanceof index$1.Sym) {
478
- if (expr.children[0].value === "val") {
479
- if (expr.isAnon) {
480
- throw new EvalError("cannot call 'val' as anonymous function", expr);
481
- }
482
- if (expr.children[1] instanceof index$1.StringLiteral) {
483
- var _path = expr.children[1].value;
484
- return newSelectorProxy(getSource(_path), _path);
485
- } else {
486
- throw new EvalError("argument of 'val' must be a string literal", expr);
487
- }
488
- } else if (expr.children[0].value === "json") {
489
- if (expr.children.length !== 2) {
490
- throw new EvalError("must call 'json' with exactly one argument", expr);
491
- }
492
- var value = evaluateSync(expr.children[1], getSource, stack);
493
- var valObj = value[SourceOrExpr];
494
- var valPath = value[Path];
495
- if (typeof valObj !== "string") {
496
- throw new EvalError("cannot parse JSON: ".concat(JSON.stringify(valObj), ", expected string"), expr.children[1]);
497
- }
498
- try {
499
- var serialized = JSON.parse(valObj);
500
- if (index$1.isSerializedVal(serialized)) {
501
- return newSelectorProxy(serialized.val, serialized.valPath);
502
- }
503
- var parsedValue = newSelectorProxy(JSON.parse(valObj), valPath);
504
- return parsedValue;
505
- } catch (e) {
506
- if (e instanceof SyntaxError) {
507
- throw new EvalError("cannot parse JSON: ".concat(valObj, ", ").concat(e.message, " - value: ").concat(JSON.stringify(value)), expr.children[1]);
508
- }
509
- throw e;
510
- }
511
- } else if (expr.children[0].value === "stringify") {
512
- // TODO: remove stringify
513
- if (expr.children.length !== 2) {
514
- throw new EvalError("must call 'stringify' with exactly one argument", expr);
515
- }
516
- var res = evaluateSync(expr.children[1], getSource, stack);
517
- return newSelectorProxy(JSON.stringify(res[SourceOrExpr]));
518
- }
519
- }
520
- var prop = evaluateSync(expr.children[0], getSource, stack)[SourceOrExpr];
521
- if (expr.children.length === 1) {
522
- // TODO: return if literal only?
523
- return newSelectorProxy(prop);
524
- }
525
- var obj = evaluateSync(expr.children[1], getSource, stack);
526
- if (typeof prop !== "string" && typeof prop !== "number") {
527
- throw new EvalError("cannot access ".concat(JSON.stringify(obj), " with property ").concat(JSON.stringify(prop), ": is not a string or number"), expr);
528
- }
529
- if (prop in obj) {
530
- if (expr.isAnon) {
531
- // anon functions:
532
- var maybeFunction = obj[prop];
533
- if (typeof maybeFunction !== "function") {
534
- throw new EvalError("cannot access property ".concat(JSON.stringify(prop), " of ").concat(JSON.stringify(obj), ": required higher ordered function got ").concat(index$1._typeof(obj[prop])), expr);
535
- }
536
- if (expr.children[0] instanceof index$1.Sym) {
537
- return maybeFunction(function () {
538
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
539
- args[_key] = arguments[_key];
540
- }
541
- return evaluateSync(expr.children[2], getSource, stack.concat([args]));
542
- });
543
- } else {
544
- throw new EvalError("cannot call an expression that is not a symbol, got: '".concat(expr.children[0].type, "'"), expr);
545
- }
546
- } else {
547
- // non-anon functions:
548
- if (expr.children[0] instanceof index$1.Sym) {
549
- if (expr.children[0].value === "val") {
550
- if (expr.children[1] instanceof index$1.StringLiteral) {
551
- var _path2 = expr.children[1].value;
552
- return newSelectorProxy(getSource(_path2), _path2);
553
- } else {
554
- throw new EvalError("argument of 'val' must be a string literal", expr);
555
- }
556
- }
557
- }
558
- var args = expr.children.slice(2);
559
- if (args.length > 0) {
560
- var _maybeFunction = obj[prop];
561
- if (typeof _maybeFunction !== "function") {
562
- throw new EvalError("cannot access property ".concat(JSON.stringify(prop), " of ").concat(JSON.stringify(obj), ": required function got ").concat(index$1._typeof(obj[prop])), expr);
563
- }
564
- return _maybeFunction.apply(void 0, index$1._toConsumableArray(args.map(function (arg) {
565
- return evaluateSync(arg, getSource, stack);
566
- })));
567
- }
568
- var maybeValue = obj[prop];
569
- if (typeof maybeValue === "function") {
570
- throw new EvalError("cannot access property ".concat(JSON.stringify(prop), " of ").concat(JSON.stringify(obj), ": required value got ").concat(index$1._typeof(obj[prop])), expr);
571
- }
572
- return maybeValue;
573
- }
574
- }
575
- } else if (expr instanceof index$1.Sym) {
576
- if (expr.value.startsWith("@")) {
577
- var _stack$Number;
578
- var _expr$value$slice$spl = expr.value.slice(2, -1).split(","),
579
- _expr$value$slice$spl2 = index$1._slicedToArray(_expr$value$slice$spl, 3),
580
- i = _expr$value$slice$spl2[0],
581
- j = _expr$value$slice$spl2[1],
582
- rest = _expr$value$slice$spl2[2];
583
- if (rest) {
584
- throw new EvalError("cannot access stack: too many indices", expr);
585
- }
586
- var stackValue = (_stack$Number = stack[Number(i)]) === null || _stack$Number === void 0 ? void 0 : _stack$Number[Number(j)];
587
- if (stackValue === undefined) {
588
- throw new EvalError("cannot access stack: out of bounds", expr);
589
- }
590
- return stackValue;
591
- } else if (expr.value === "()") {
592
- return newSelectorProxy(null);
593
- }
594
- return newSelectorProxy(expr.value);
595
- } else if (expr instanceof index$1.StringLiteral) {
596
- return newSelectorProxy(expr.value);
597
- } else if (expr instanceof index$1.StringTemplate) {
598
- return newSelectorProxy(expr.children.map(function (child) {
599
- if (child instanceof index$1.Sym && child.value === "()") {
600
- return "null";
601
- }
602
- var evalRes = evaluateSync(child, getSource, stack);
603
- if (child.type === "StringLiteral" || child.type === "StringTemplate") {
604
- return evalRes[SourceOrExpr];
605
- }
606
- if (Path in evalRes) {
607
- // a selector, so serialize to Val
608
- return JSON.stringify({
609
- val: evalRes[SourceOrExpr],
610
- valPath: evalRes[Path]
611
- });
612
- }
613
- return JSON.stringify(evalRes[SourceOrExpr]);
614
- }).join(""));
615
- }
616
- throw new EvalError("could not evaluate", expr);
617
- }
618
- function evaluate(expr, source, stack) {
619
- try {
620
- return result.ok(evaluateSync(expr, source, stack));
621
- } catch (err) {
622
- if (err instanceof EvalError) {
623
- return result.err(err);
624
- }
625
- throw err;
626
- }
627
- }
628
-
629
- var index = /*#__PURE__*/Object.freeze({
630
- __proto__: null,
631
- parse: parse,
632
- Call: index$1.Call,
633
- Expr: index$1.Expr,
634
- NilSym: index$1.NilSym,
635
- StringLiteral: index$1.StringLiteral,
636
- StringTemplate: index$1.StringTemplate,
637
- Sym: index$1.Sym,
638
- evaluate: evaluate
639
- });
640
-
641
- exports.evaluate = evaluate;
642
- exports.index = index;
643
- exports.parse = parse;