@valbuild/core 0.14.0 → 0.16.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.
Files changed (66) hide show
  1. package/dist/SelectorProxy-2af1b2b8.cjs.prod.js +756 -0
  2. package/dist/SelectorProxy-63c2d0e2.esm.js +722 -0
  3. package/dist/SelectorProxy-873782a5.cjs.dev.js +756 -0
  4. package/dist/declarations/src/index.d.ts +2 -0
  5. package/dist/declarations/src/initVal.d.ts +1 -1
  6. package/dist/declarations/src/patch/index.d.ts +1 -1
  7. package/dist/declarations/src/patch/util.d.ts +2 -0
  8. package/dist/declarations/src/schema/array.d.ts +3 -2
  9. package/dist/declarations/src/schema/boolean.d.ts +3 -2
  10. package/dist/declarations/src/schema/i18n.d.ts +3 -2
  11. package/dist/declarations/src/schema/image.d.ts +3 -2
  12. package/dist/declarations/src/schema/index.d.ts +5 -2
  13. package/dist/declarations/src/schema/literal.d.ts +3 -2
  14. package/dist/declarations/src/schema/number.d.ts +3 -2
  15. package/dist/declarations/src/schema/object.d.ts +3 -2
  16. package/dist/declarations/src/schema/oneOf.d.ts +3 -2
  17. package/dist/declarations/src/schema/richtext.d.ts +3 -2
  18. package/dist/declarations/src/schema/string.d.ts +3 -2
  19. package/dist/declarations/src/schema/union.d.ts +3 -2
  20. package/dist/declarations/src/schema/validation/ValidationError.d.ts +14 -0
  21. package/dist/declarations/src/schema/validation/ValidationFix.d.ts +2 -0
  22. package/dist/index-2fff5ca8.cjs.dev.js +456 -0
  23. package/dist/{index-06df0a5b.esm.js → index-af761363.esm.js} +2 -555
  24. package/dist/index-cac9ecbd.cjs.prod.js +456 -0
  25. package/dist/ops-1b6e0e35.cjs.prod.js +552 -0
  26. package/dist/ops-74661336.esm.js +541 -0
  27. package/dist/ops-ea4827fc.cjs.dev.js +552 -0
  28. package/dist/valbuild-core.cjs.dev.js +151 -531
  29. package/dist/valbuild-core.cjs.prod.js +151 -531
  30. package/dist/valbuild-core.esm.js +65 -445
  31. package/expr/dist/valbuild-core-expr.cjs.dev.js +8 -8
  32. package/expr/dist/valbuild-core-expr.cjs.prod.js +8 -8
  33. package/expr/dist/valbuild-core-expr.esm.js +2 -2
  34. package/package.json +2 -1
  35. package/patch/dist/valbuild-core-patch.cjs.dev.js +30 -21
  36. package/patch/dist/valbuild-core-patch.cjs.prod.js +30 -21
  37. package/patch/dist/valbuild-core-patch.esm.js +12 -4
  38. package/src/expr/repl.ts +2 -2
  39. package/src/index.ts +5 -0
  40. package/src/initVal.ts +1 -1
  41. package/src/patch/index.ts +1 -0
  42. package/src/patch/util.ts +7 -0
  43. package/src/schema/array.ts +45 -4
  44. package/src/schema/boolean.ts +14 -3
  45. package/src/schema/i18n.ts +4 -2
  46. package/src/schema/image.ts +65 -5
  47. package/src/schema/index.ts +23 -2
  48. package/src/schema/literal.ts +24 -3
  49. package/src/schema/number.ts +14 -3
  50. package/src/schema/object.ts +50 -7
  51. package/src/schema/oneOf.ts +3 -2
  52. package/src/schema/richtext.ts +63 -3
  53. package/src/schema/string.ts +14 -3
  54. package/src/schema/union.ts +3 -2
  55. package/src/schema/validation/ValidationError.ts +16 -0
  56. package/src/schema/validation/ValidationFix.ts +6 -0
  57. package/src/schema/validation.test.ts +226 -0
  58. package/src/selector/SelectorProxy.ts +1 -1
  59. package/dist/createClass-012eebbf.esm.js +0 -109
  60. package/dist/createClass-a436dbfe.cjs.dev.js +0 -116
  61. package/dist/createClass-de7426aa.cjs.prod.js +0 -116
  62. package/dist/index-9663f28a.cjs.dev.js +0 -1037
  63. package/dist/index-b2270f8f.cjs.prod.js +0 -1037
  64. package/dist/ops-6fae92a1.esm.js +0 -12
  65. package/dist/ops-87cdbafc.cjs.dev.js +0 -14
  66. package/dist/ops-ae4d1bc2.cjs.prod.js +0 -14
@@ -0,0 +1,456 @@
1
+ 'use strict';
2
+
3
+ var SelectorProxy = require('./SelectorProxy-2af1b2b8.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(SelectorProxy._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__*/SelectorProxy._createClass(function ParserError(message, span) {
122
+ SelectorProxy._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 SelectorProxy.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 SelectorProxy.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 SelectorProxy.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 SelectorProxy.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 SelectorProxy.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 SelectorProxy.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 SelectorProxy.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 = SelectorProxy._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
+ var EvalError = /*#__PURE__*/function () {
266
+ function EvalError(message, expr) {
267
+ SelectorProxy._classCallCheck(this, EvalError);
268
+ this.message = message;
269
+ this.expr = expr;
270
+ }
271
+ SelectorProxy._createClass(EvalError, [{
272
+ key: "toString",
273
+ value: function toString() {
274
+ return "".concat(this.message, " in: ").concat(this.expr.transpile());
275
+ }
276
+ }]);
277
+ return EvalError;
278
+ }();
279
+ var MAX_STACK_SIZE = 100; // an arbitrary semi-large number
280
+ function evaluateSync(expr, getSource, stack) {
281
+ // TODO: amount of evaluates should be limited?
282
+ if (stack.length > MAX_STACK_SIZE) {
283
+ throw new EvalError("Stack overflow. Final frames: ".concat(stack.slice(-10).map(function (frame, i) {
284
+ return frame.map(function (s, j) {
285
+ return "@[".concat(i, ",").concat(j, "]: ").concat(JSON.stringify(s));
286
+ }).join(", ");
287
+ }).join(" -> ")), expr);
288
+ }
289
+ if (expr instanceof SelectorProxy.Call) {
290
+ if (expr.children[0] instanceof SelectorProxy.Sym) {
291
+ if (expr.children[0].value === "val") {
292
+ if (expr.isAnon) {
293
+ throw new EvalError("cannot call 'val' as anonymous function", expr);
294
+ }
295
+ if (expr.children[1] instanceof SelectorProxy.StringLiteral) {
296
+ var _path = expr.children[1].value;
297
+ return SelectorProxy.newSelectorProxy(getSource(_path), _path);
298
+ } else {
299
+ throw new EvalError("argument of 'val' must be a string literal", expr);
300
+ }
301
+ } else if (expr.children[0].value === "json") {
302
+ if (expr.children.length !== 2) {
303
+ throw new EvalError("must call 'json' with exactly one argument", expr);
304
+ }
305
+ var value = evaluateSync(expr.children[1], getSource, stack);
306
+ var valObj = value[SelectorProxy.SourceOrExpr];
307
+ var valPath = value[SelectorProxy.Path];
308
+ if (typeof valObj !== "string") {
309
+ throw new EvalError("cannot parse JSON: ".concat(JSON.stringify(valObj), ", expected string"), expr.children[1]);
310
+ }
311
+ try {
312
+ var serialized = JSON.parse(valObj);
313
+ if (SelectorProxy.isSerializedVal(serialized)) {
314
+ return SelectorProxy.newSelectorProxy(serialized.val, serialized.valPath);
315
+ }
316
+ var parsedValue = SelectorProxy.newSelectorProxy(JSON.parse(valObj), valPath);
317
+ return parsedValue;
318
+ } catch (e) {
319
+ if (e instanceof SyntaxError) {
320
+ throw new EvalError("cannot parse JSON: ".concat(valObj, ", ").concat(e.message, " - value: ").concat(JSON.stringify(value)), expr.children[1]);
321
+ }
322
+ throw e;
323
+ }
324
+ } else if (expr.children[0].value === "stringify") {
325
+ // TODO: remove stringify
326
+ if (expr.children.length !== 2) {
327
+ throw new EvalError("must call 'stringify' with exactly one argument", expr);
328
+ }
329
+ var res = evaluateSync(expr.children[1], getSource, stack);
330
+ return SelectorProxy.newSelectorProxy(JSON.stringify(res[SelectorProxy.SourceOrExpr]));
331
+ }
332
+ }
333
+ var prop = evaluateSync(expr.children[0], getSource, stack)[SelectorProxy.SourceOrExpr];
334
+ if (expr.children.length === 1) {
335
+ // TODO: return if literal only?
336
+ return SelectorProxy.newSelectorProxy(prop);
337
+ }
338
+ var obj = evaluateSync(expr.children[1], getSource, stack);
339
+ if (typeof prop !== "string" && typeof prop !== "number") {
340
+ throw new EvalError("cannot access ".concat(JSON.stringify(obj), " with property ").concat(JSON.stringify(prop), ": is not a string or number"), expr);
341
+ }
342
+ if (prop in obj) {
343
+ if (expr.isAnon) {
344
+ // anon functions:
345
+ var maybeFunction = obj[prop];
346
+ if (typeof maybeFunction !== "function") {
347
+ throw new EvalError("cannot access property ".concat(JSON.stringify(prop), " of ").concat(JSON.stringify(obj), ": required higher ordered function got ").concat(SelectorProxy._typeof(obj[prop])), expr);
348
+ }
349
+ if (expr.children[0] instanceof SelectorProxy.Sym) {
350
+ return maybeFunction(function () {
351
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
352
+ args[_key] = arguments[_key];
353
+ }
354
+ return evaluateSync(expr.children[2], getSource, stack.concat([args]));
355
+ });
356
+ } else {
357
+ throw new EvalError("cannot call an expression that is not a symbol, got: '".concat(expr.children[0].type, "'"), expr);
358
+ }
359
+ } else {
360
+ // non-anon functions:
361
+ if (expr.children[0] instanceof SelectorProxy.Sym) {
362
+ if (expr.children[0].value === "val") {
363
+ if (expr.children[1] instanceof SelectorProxy.StringLiteral) {
364
+ var _path2 = expr.children[1].value;
365
+ return SelectorProxy.newSelectorProxy(getSource(_path2), _path2);
366
+ } else {
367
+ throw new EvalError("argument of 'val' must be a string literal", expr);
368
+ }
369
+ }
370
+ }
371
+ var args = expr.children.slice(2);
372
+ if (args.length > 0) {
373
+ var _maybeFunction = obj[prop];
374
+ if (typeof _maybeFunction !== "function") {
375
+ throw new EvalError("cannot access property ".concat(JSON.stringify(prop), " of ").concat(JSON.stringify(obj), ": required function got ").concat(SelectorProxy._typeof(obj[prop])), expr);
376
+ }
377
+ return _maybeFunction.apply(void 0, SelectorProxy._toConsumableArray(args.map(function (arg) {
378
+ return evaluateSync(arg, getSource, stack);
379
+ })));
380
+ }
381
+ var maybeValue = obj[prop];
382
+ if (typeof maybeValue === "function") {
383
+ throw new EvalError("cannot access property ".concat(JSON.stringify(prop), " of ").concat(JSON.stringify(obj), ": required value got ").concat(SelectorProxy._typeof(obj[prop])), expr);
384
+ }
385
+ return maybeValue;
386
+ }
387
+ }
388
+ } else if (expr instanceof SelectorProxy.Sym) {
389
+ if (expr.value.startsWith("@")) {
390
+ var _stack$Number;
391
+ var _expr$value$slice$spl = expr.value.slice(2, -1).split(","),
392
+ _expr$value$slice$spl2 = SelectorProxy._slicedToArray(_expr$value$slice$spl, 3),
393
+ i = _expr$value$slice$spl2[0],
394
+ j = _expr$value$slice$spl2[1],
395
+ rest = _expr$value$slice$spl2[2];
396
+ if (rest) {
397
+ throw new EvalError("cannot access stack: too many indices", expr);
398
+ }
399
+ var stackValue = (_stack$Number = stack[Number(i)]) === null || _stack$Number === void 0 ? void 0 : _stack$Number[Number(j)];
400
+ if (stackValue === undefined) {
401
+ throw new EvalError("cannot access stack: out of bounds", expr);
402
+ }
403
+ return stackValue;
404
+ } else if (expr.value === "()") {
405
+ return SelectorProxy.newSelectorProxy(null);
406
+ }
407
+ return SelectorProxy.newSelectorProxy(expr.value);
408
+ } else if (expr instanceof SelectorProxy.StringLiteral) {
409
+ return SelectorProxy.newSelectorProxy(expr.value);
410
+ } else if (expr instanceof SelectorProxy.StringTemplate) {
411
+ return SelectorProxy.newSelectorProxy(expr.children.map(function (child) {
412
+ if (child instanceof SelectorProxy.Sym && child.value === "()") {
413
+ return "null";
414
+ }
415
+ var evalRes = evaluateSync(child, getSource, stack);
416
+ if (child.type === "StringLiteral" || child.type === "StringTemplate") {
417
+ return evalRes[SelectorProxy.SourceOrExpr];
418
+ }
419
+ if (SelectorProxy.Path in evalRes) {
420
+ // a selector, so serialize to Val
421
+ return JSON.stringify({
422
+ val: evalRes[SelectorProxy.SourceOrExpr],
423
+ valPath: evalRes[SelectorProxy.Path]
424
+ });
425
+ }
426
+ return JSON.stringify(evalRes[SelectorProxy.SourceOrExpr]);
427
+ }).join(""));
428
+ }
429
+ throw new EvalError("could not evaluate", expr);
430
+ }
431
+ function evaluate(expr, source, stack) {
432
+ try {
433
+ return result.ok(evaluateSync(expr, source, stack));
434
+ } catch (err) {
435
+ if (err instanceof EvalError) {
436
+ return result.err(err);
437
+ }
438
+ throw err;
439
+ }
440
+ }
441
+
442
+ var index = /*#__PURE__*/Object.freeze({
443
+ __proto__: null,
444
+ parse: parse,
445
+ Call: SelectorProxy.Call,
446
+ Expr: SelectorProxy.Expr,
447
+ NilSym: SelectorProxy.NilSym,
448
+ StringLiteral: SelectorProxy.StringLiteral,
449
+ StringTemplate: SelectorProxy.StringTemplate,
450
+ Sym: SelectorProxy.Sym,
451
+ evaluate: evaluate
452
+ });
453
+
454
+ exports.evaluate = evaluate;
455
+ exports.index = index;
456
+ exports.parse = parse;