@valbuild/core 0.12.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/CHANGELOG.md +0 -0
- package/README.md +537 -0
- package/dist/createClass-012eebbf.esm.js +109 -0
- package/dist/createClass-a436dbfe.cjs.dev.js +116 -0
- package/dist/createClass-de7426aa.cjs.prod.js +116 -0
- package/dist/declarations/src/Json.d.ts +6 -0
- package/dist/declarations/src/expr/eval.d.ts +19 -0
- package/dist/declarations/src/expr/expr.d.ts +32 -0
- package/dist/declarations/src/expr/index.d.ts +3 -0
- package/dist/declarations/src/expr/parser.d.ts +8 -0
- package/dist/declarations/src/expr/tokenizer.d.ts +7 -0
- package/dist/declarations/src/fetchVal.d.ts +5 -0
- package/dist/declarations/src/fp/array.d.ts +8 -0
- package/dist/declarations/src/fp/index.d.ts +3 -0
- package/dist/declarations/src/fp/result.d.ts +67 -0
- package/dist/declarations/src/fp/util.d.ts +12 -0
- package/dist/declarations/src/index.d.ts +32 -0
- package/dist/declarations/src/initSchema.d.ts +57 -0
- package/dist/declarations/src/initVal.d.ts +39 -0
- package/dist/declarations/src/module.d.ts +38 -0
- package/dist/declarations/src/patch/deref.d.ts +13 -0
- package/dist/declarations/src/patch/index.d.ts +6 -0
- package/dist/declarations/src/patch/json.d.ts +11 -0
- package/dist/declarations/src/patch/operation.d.ts +59 -0
- package/dist/declarations/src/patch/ops.d.ts +40 -0
- package/dist/declarations/src/patch/parse.d.ts +19 -0
- package/dist/declarations/src/patch/patch.d.ts +6 -0
- package/dist/declarations/src/patch/util.d.ts +6 -0
- package/dist/declarations/src/schema/array.d.ts +18 -0
- package/dist/declarations/src/schema/boolean.d.ts +15 -0
- package/dist/declarations/src/schema/i18n.d.ts +21 -0
- package/dist/declarations/src/schema/image.d.ts +32 -0
- package/dist/declarations/src/schema/index.d.ts +23 -0
- package/dist/declarations/src/schema/literal.d.ts +17 -0
- package/dist/declarations/src/schema/number.d.ts +22 -0
- package/dist/declarations/src/schema/object.d.ts +25 -0
- package/dist/declarations/src/schema/oneOf.d.ts +22 -0
- package/dist/declarations/src/schema/richtext.d.ts +76 -0
- package/dist/declarations/src/schema/string.d.ts +22 -0
- package/dist/declarations/src/schema/union.d.ts +36 -0
- package/dist/declarations/src/selector/SelectorProxy.d.ts +7 -0
- package/dist/declarations/src/selector/array.d.ts +17 -0
- package/dist/declarations/src/selector/boolean.d.ts +2 -0
- package/dist/declarations/src/selector/file.d.ts +9 -0
- package/dist/declarations/src/selector/i18n.d.ts +11 -0
- package/dist/declarations/src/selector/index.d.ts +81 -0
- package/dist/declarations/src/selector/number.d.ts +2 -0
- package/dist/declarations/src/selector/object.d.ts +10 -0
- package/dist/declarations/src/selector/primitive.d.ts +9 -0
- package/dist/declarations/src/selector/remote.d.ts +7 -0
- package/dist/declarations/src/selector/string.d.ts +2 -0
- package/dist/declarations/src/source/file.d.ts +23 -0
- package/dist/declarations/src/source/i18n.d.ts +29 -0
- package/dist/declarations/src/source/index.d.ts +33 -0
- package/dist/declarations/src/source/remote.d.ts +29 -0
- package/dist/declarations/src/val/array.d.ts +9 -0
- package/dist/declarations/src/val/index.d.ts +53 -0
- package/dist/declarations/src/val/object.d.ts +9 -0
- package/dist/declarations/src/val/primitive.d.ts +7 -0
- package/dist/index-2f1631cf.cjs.prod.js +1031 -0
- package/dist/index-55916dcd.esm.js +1000 -0
- package/dist/index-b49a2c60.cjs.dev.js +1031 -0
- package/dist/ops-6fae92a1.esm.js +12 -0
- package/dist/ops-87cdbafc.cjs.dev.js +14 -0
- package/dist/ops-ae4d1bc2.cjs.prod.js +14 -0
- package/dist/result-26f67b40.cjs.prod.js +299 -0
- package/dist/result-48320acd.cjs.dev.js +299 -0
- package/dist/result-b96df128.esm.js +279 -0
- package/dist/util-030d8a1f.cjs.prod.js +29 -0
- package/dist/util-18613e99.esm.js +26 -0
- package/dist/util-b213092b.cjs.dev.js +29 -0
- package/dist/valbuild-core.cjs.d.ts +2 -0
- package/dist/valbuild-core.cjs.d.ts.map +1 -0
- package/dist/valbuild-core.cjs.dev.js +1276 -0
- package/dist/valbuild-core.cjs.js +7 -0
- package/dist/valbuild-core.cjs.prod.js +1276 -0
- package/dist/valbuild-core.esm.js +1265 -0
- package/expr/dist/valbuild-core-expr.cjs.d.ts +2 -0
- package/expr/dist/valbuild-core-expr.cjs.d.ts.map +1 -0
- package/expr/dist/valbuild-core-expr.cjs.dev.js +18 -0
- package/expr/dist/valbuild-core-expr.cjs.js +7 -0
- package/expr/dist/valbuild-core-expr.cjs.prod.js +18 -0
- package/expr/dist/valbuild-core-expr.esm.js +3 -0
- package/expr/package.json +4 -0
- package/fp/dist/valbuild-core-fp.cjs.d.ts +2 -0
- package/fp/dist/valbuild-core-fp.cjs.d.ts.map +1 -0
- package/fp/dist/valbuild-core-fp.cjs.dev.js +13 -0
- package/fp/dist/valbuild-core-fp.cjs.js +7 -0
- package/fp/dist/valbuild-core-fp.cjs.prod.js +13 -0
- package/fp/dist/valbuild-core-fp.esm.js +2 -0
- package/fp/package.json +4 -0
- package/package.json +43 -0
- package/patch/dist/valbuild-core-patch.cjs.d.ts +2 -0
- package/patch/dist/valbuild-core-patch.cjs.d.ts.map +1 -0
- package/patch/dist/valbuild-core-patch.cjs.dev.js +443 -0
- package/patch/dist/valbuild-core-patch.cjs.js +7 -0
- package/patch/dist/valbuild-core-patch.cjs.prod.js +443 -0
- package/patch/dist/valbuild-core-patch.esm.js +431 -0
- package/patch/package.json +4 -0
- package/tsconfig.json +7 -0
@@ -0,0 +1,1031 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
var createClass = require('./createClass-a436dbfe.cjs.dev.js');
|
4
|
+
var result = require('./result-48320acd.cjs.dev.js');
|
5
|
+
|
6
|
+
function _defineProperty(obj, key, value) {
|
7
|
+
key = createClass._toPropertyKey(key);
|
8
|
+
if (key in obj) {
|
9
|
+
Object.defineProperty(obj, key, {
|
10
|
+
value: value,
|
11
|
+
enumerable: true,
|
12
|
+
configurable: true,
|
13
|
+
writable: true
|
14
|
+
});
|
15
|
+
} else {
|
16
|
+
obj[key] = value;
|
17
|
+
}
|
18
|
+
return obj;
|
19
|
+
}
|
20
|
+
|
21
|
+
function ownKeys(object, enumerableOnly) {
|
22
|
+
var keys = Object.keys(object);
|
23
|
+
if (Object.getOwnPropertySymbols) {
|
24
|
+
var symbols = Object.getOwnPropertySymbols(object);
|
25
|
+
enumerableOnly && (symbols = symbols.filter(function (sym) {
|
26
|
+
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
27
|
+
})), keys.push.apply(keys, symbols);
|
28
|
+
}
|
29
|
+
return keys;
|
30
|
+
}
|
31
|
+
function _objectSpread2(target) {
|
32
|
+
for (var i = 1; i < arguments.length; i++) {
|
33
|
+
var source = null != arguments[i] ? arguments[i] : {};
|
34
|
+
i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {
|
35
|
+
_defineProperty(target, key, source[key]);
|
36
|
+
}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {
|
37
|
+
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
38
|
+
});
|
39
|
+
}
|
40
|
+
return target;
|
41
|
+
}
|
42
|
+
|
43
|
+
function _setPrototypeOf(o, p) {
|
44
|
+
_setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) {
|
45
|
+
o.__proto__ = p;
|
46
|
+
return o;
|
47
|
+
};
|
48
|
+
return _setPrototypeOf(o, p);
|
49
|
+
}
|
50
|
+
|
51
|
+
function _inherits(subClass, superClass) {
|
52
|
+
if (typeof superClass !== "function" && superClass !== null) {
|
53
|
+
throw new TypeError("Super expression must either be null or a function");
|
54
|
+
}
|
55
|
+
subClass.prototype = Object.create(superClass && superClass.prototype, {
|
56
|
+
constructor: {
|
57
|
+
value: subClass,
|
58
|
+
writable: true,
|
59
|
+
configurable: true
|
60
|
+
}
|
61
|
+
});
|
62
|
+
Object.defineProperty(subClass, "prototype", {
|
63
|
+
writable: false
|
64
|
+
});
|
65
|
+
if (superClass) _setPrototypeOf(subClass, superClass);
|
66
|
+
}
|
67
|
+
|
68
|
+
function _getPrototypeOf(o) {
|
69
|
+
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) {
|
70
|
+
return o.__proto__ || Object.getPrototypeOf(o);
|
71
|
+
};
|
72
|
+
return _getPrototypeOf(o);
|
73
|
+
}
|
74
|
+
|
75
|
+
function _isNativeReflectConstruct() {
|
76
|
+
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
|
77
|
+
if (Reflect.construct.sham) return false;
|
78
|
+
if (typeof Proxy === "function") return true;
|
79
|
+
try {
|
80
|
+
Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
|
81
|
+
return true;
|
82
|
+
} catch (e) {
|
83
|
+
return false;
|
84
|
+
}
|
85
|
+
}
|
86
|
+
|
87
|
+
function _assertThisInitialized(self) {
|
88
|
+
if (self === void 0) {
|
89
|
+
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
90
|
+
}
|
91
|
+
return self;
|
92
|
+
}
|
93
|
+
|
94
|
+
function _possibleConstructorReturn(self, call) {
|
95
|
+
if (call && (typeof call === "object" || typeof call === "function")) {
|
96
|
+
return call;
|
97
|
+
} else if (call !== void 0) {
|
98
|
+
throw new TypeError("Derived constructors may only return object or undefined");
|
99
|
+
}
|
100
|
+
return _assertThisInitialized(self);
|
101
|
+
}
|
102
|
+
|
103
|
+
function _createSuper(Derived) {
|
104
|
+
var hasNativeReflectConstruct = _isNativeReflectConstruct();
|
105
|
+
return function _createSuperInternal() {
|
106
|
+
var Super = _getPrototypeOf(Derived),
|
107
|
+
result;
|
108
|
+
if (hasNativeReflectConstruct) {
|
109
|
+
var NewTarget = _getPrototypeOf(this).constructor;
|
110
|
+
result = Reflect.construct(Super, arguments, NewTarget);
|
111
|
+
} else {
|
112
|
+
result = Super.apply(this, arguments);
|
113
|
+
}
|
114
|
+
return _possibleConstructorReturn(this, result);
|
115
|
+
};
|
116
|
+
}
|
117
|
+
|
118
|
+
var Schema = /*#__PURE__*/function () {
|
119
|
+
function Schema() {
|
120
|
+
createClass._classCallCheck(this, Schema);
|
121
|
+
}
|
122
|
+
createClass._createClass(Schema, [{
|
123
|
+
key: "remote",
|
124
|
+
value: function remote() {
|
125
|
+
// TODO: Schema<never, "Cannot create remote schema from non-remote source.">
|
126
|
+
throw new Error("You need Val Ultra to use .remote()");
|
127
|
+
}
|
128
|
+
}]);
|
129
|
+
return Schema;
|
130
|
+
}();
|
131
|
+
|
132
|
+
// TODO: SourceError<"Could not determine type of Schema">
|
133
|
+
|
134
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
135
|
+
|
136
|
+
/**
|
137
|
+
* Selectors can be used to select parts of a Val module.
|
138
|
+
* Unlike queries, joins, aggregates etc is and will not be supported.
|
139
|
+
*
|
140
|
+
* They are designed to be be used as if they were "normal" JSON data,
|
141
|
+
* though some concessions had to be made because of TypeScript limitations.
|
142
|
+
*
|
143
|
+
* Selectors works equally on source content, defined in code, and remote content.
|
144
|
+
*
|
145
|
+
* @example
|
146
|
+
* // Select the title of a document
|
147
|
+
* const titles = useVal(docsVal.map((doc) => doc.title));
|
148
|
+
*
|
149
|
+
* @example
|
150
|
+
* // Match on a union type
|
151
|
+
* const titles = useVal(docsVal.map((doc) => doc.fold("type")({
|
152
|
+
* newsletter: (newsletter) => newsletter.title,
|
153
|
+
* email: (email) => email.subject,
|
154
|
+
* }));
|
155
|
+
*
|
156
|
+
*/
|
157
|
+
|
158
|
+
/**
|
159
|
+
* @internal
|
160
|
+
*/
|
161
|
+
var GetSchema = Symbol("GetSchema");
|
162
|
+
/**
|
163
|
+
/**
|
164
|
+
* @internal
|
165
|
+
*/
|
166
|
+
var Path = Symbol("Path");
|
167
|
+
/**
|
168
|
+
* @internal
|
169
|
+
*/
|
170
|
+
var SourceOrExpr = Symbol("SourceOrExpr");
|
171
|
+
/**
|
172
|
+
* @internal
|
173
|
+
*/
|
174
|
+
var ValError = Symbol("ValError");
|
175
|
+
var GenericSelector = /*#__PURE__*/function () {
|
176
|
+
function GenericSelector(valOrExpr, path, schema, error) {
|
177
|
+
createClass._classCallCheck(this, GenericSelector);
|
178
|
+
this[Path] = path;
|
179
|
+
this[SourceOrExpr] = valOrExpr;
|
180
|
+
this[ValError] = error;
|
181
|
+
this[GetSchema] = schema;
|
182
|
+
}
|
183
|
+
createClass._createClass(GenericSelector, [{
|
184
|
+
key: "assert",
|
185
|
+
value: function assert(schema, other) {
|
186
|
+
throw new Error("Not implemented");
|
187
|
+
}
|
188
|
+
}]);
|
189
|
+
return GenericSelector;
|
190
|
+
}();
|
191
|
+
|
192
|
+
/**
|
193
|
+
* Use this type to convert types that accepts both Source and Selectors
|
194
|
+
*
|
195
|
+
* An example would be where literals are supported like in most higher order functions (e.g. map in array)
|
196
|
+
**/
|
197
|
+
|
198
|
+
function getSchema(selector) {
|
199
|
+
return selector[GetSchema];
|
200
|
+
}
|
201
|
+
|
202
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
203
|
+
|
204
|
+
var Expr = /*#__PURE__*/createClass._createClass(function Expr(span) {
|
205
|
+
createClass._classCallCheck(this, Expr);
|
206
|
+
this.span = span;
|
207
|
+
});
|
208
|
+
var StringLiteral = /*#__PURE__*/function (_Expr) {
|
209
|
+
_inherits(StringLiteral, _Expr);
|
210
|
+
var _super = _createSuper(StringLiteral);
|
211
|
+
function StringLiteral(value, span) {
|
212
|
+
var _this;
|
213
|
+
createClass._classCallCheck(this, StringLiteral);
|
214
|
+
_this = _super.call(this, span);
|
215
|
+
_defineProperty(_assertThisInitialized(_this), "type", "StringLiteral");
|
216
|
+
_this.value = value;
|
217
|
+
return _this;
|
218
|
+
}
|
219
|
+
createClass._createClass(StringLiteral, [{
|
220
|
+
key: "transpile",
|
221
|
+
value: function transpile() {
|
222
|
+
return "'".concat(this.value, "'");
|
223
|
+
}
|
224
|
+
}]);
|
225
|
+
return StringLiteral;
|
226
|
+
}(Expr);
|
227
|
+
var Sym = /*#__PURE__*/function (_Expr2) {
|
228
|
+
_inherits(Sym, _Expr2);
|
229
|
+
var _super2 = _createSuper(Sym);
|
230
|
+
function Sym(value, span) {
|
231
|
+
var _this2;
|
232
|
+
createClass._classCallCheck(this, Sym);
|
233
|
+
_this2 = _super2.call(this, span);
|
234
|
+
_defineProperty(_assertThisInitialized(_this2), "type", "Sym");
|
235
|
+
_this2.value = value;
|
236
|
+
return _this2;
|
237
|
+
}
|
238
|
+
createClass._createClass(Sym, [{
|
239
|
+
key: "transpile",
|
240
|
+
value: function transpile() {
|
241
|
+
return this.value;
|
242
|
+
}
|
243
|
+
}]);
|
244
|
+
return Sym;
|
245
|
+
}(Expr);
|
246
|
+
var NilSym = new Sym("()");
|
247
|
+
var StringTemplate = /*#__PURE__*/function (_Expr3) {
|
248
|
+
_inherits(StringTemplate, _Expr3);
|
249
|
+
var _super3 = _createSuper(StringTemplate);
|
250
|
+
function StringTemplate(children, span) {
|
251
|
+
var _this3;
|
252
|
+
createClass._classCallCheck(this, StringTemplate);
|
253
|
+
_this3 = _super3.call(this, span);
|
254
|
+
_defineProperty(_assertThisInitialized(_this3), "type", "StringTemplate");
|
255
|
+
_this3.children = children;
|
256
|
+
return _this3;
|
257
|
+
}
|
258
|
+
createClass._createClass(StringTemplate, [{
|
259
|
+
key: "transpile",
|
260
|
+
value: function transpile() {
|
261
|
+
return "'".concat(this.children.map(function (child) {
|
262
|
+
if (child instanceof StringLiteral) {
|
263
|
+
return child.value;
|
264
|
+
} else {
|
265
|
+
return "${".concat(child.transpile(), "}");
|
266
|
+
}
|
267
|
+
}).join(""), "'");
|
268
|
+
}
|
269
|
+
}]);
|
270
|
+
return StringTemplate;
|
271
|
+
}(Expr);
|
272
|
+
var Call = /*#__PURE__*/function (_Expr4) {
|
273
|
+
_inherits(Call, _Expr4);
|
274
|
+
var _super4 = _createSuper(Call);
|
275
|
+
function Call(children, isAnon, span) {
|
276
|
+
var _this4;
|
277
|
+
createClass._classCallCheck(this, Call);
|
278
|
+
_this4 = _super4.call(this, span);
|
279
|
+
_defineProperty(_assertThisInitialized(_this4), "type", "Call");
|
280
|
+
_this4.children = children;
|
281
|
+
_this4.isAnon = isAnon;
|
282
|
+
return _this4;
|
283
|
+
}
|
284
|
+
createClass._createClass(Call, [{
|
285
|
+
key: "transpile",
|
286
|
+
value: function transpile() {
|
287
|
+
if (this.isAnon) {
|
288
|
+
return "!(".concat(this.children.map(function (child) {
|
289
|
+
return child.transpile();
|
290
|
+
}).join(" "), ")");
|
291
|
+
}
|
292
|
+
return "(".concat(this.children.map(function (child) {
|
293
|
+
return child.transpile();
|
294
|
+
}).join(" "), ")");
|
295
|
+
}
|
296
|
+
}]);
|
297
|
+
return Call;
|
298
|
+
}(Expr);
|
299
|
+
|
300
|
+
/* Branded extension types: file, remote, i18n */
|
301
|
+
var VAL_EXTENSION = "_type";
|
302
|
+
|
303
|
+
/**
|
304
|
+
* A phantom type parameter is one that doesn't show up at runtime, but is checked statically (and only) at compile time.
|
305
|
+
*
|
306
|
+
* An example where this is useful is remote types, where the type of the remote source is known at compile time,
|
307
|
+
* but the value is not there before it is fetched.
|
308
|
+
*
|
309
|
+
* @example
|
310
|
+
* type Example<T> = string & PhantomType<T>;
|
311
|
+
*
|
312
|
+
**/
|
313
|
+
|
314
|
+
var FILE_REF_PROP = "_ref";
|
315
|
+
|
316
|
+
/**
|
317
|
+
* A file source represents the path to a (local) file.
|
318
|
+
*
|
319
|
+
* It will be resolved into a Asset object.
|
320
|
+
*
|
321
|
+
*/
|
322
|
+
|
323
|
+
function file(ref, metadata) {
|
324
|
+
var _ref;
|
325
|
+
return _ref = {}, _defineProperty(_ref, FILE_REF_PROP, ref), _defineProperty(_ref, VAL_EXTENSION, "file"), _defineProperty(_ref, "metadata", metadata), _ref;
|
326
|
+
}
|
327
|
+
function isFile(obj) {
|
328
|
+
return createClass._typeof(obj) === "object" && obj !== null && VAL_EXTENSION in obj && obj[VAL_EXTENSION] === "file" && FILE_REF_PROP in obj && typeof obj[FILE_REF_PROP] === "string";
|
329
|
+
}
|
330
|
+
|
331
|
+
var ImageSchema = /*#__PURE__*/function (_Schema) {
|
332
|
+
_inherits(ImageSchema, _Schema);
|
333
|
+
var _super = _createSuper(ImageSchema);
|
334
|
+
function ImageSchema(options) {
|
335
|
+
var _this;
|
336
|
+
var opt = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
337
|
+
createClass._classCallCheck(this, ImageSchema);
|
338
|
+
_this = _super.call(this);
|
339
|
+
_this.options = options;
|
340
|
+
_this.opt = opt;
|
341
|
+
return _this;
|
342
|
+
}
|
343
|
+
createClass._createClass(ImageSchema, [{
|
344
|
+
key: "validate",
|
345
|
+
value: function validate(src) {
|
346
|
+
throw new Error("Method not implemented.");
|
347
|
+
}
|
348
|
+
}, {
|
349
|
+
key: "match",
|
350
|
+
value: function match(src) {
|
351
|
+
// TODO:
|
352
|
+
return true;
|
353
|
+
}
|
354
|
+
}, {
|
355
|
+
key: "optional",
|
356
|
+
value: function optional() {
|
357
|
+
return new ImageSchema(this.options, true);
|
358
|
+
}
|
359
|
+
}, {
|
360
|
+
key: "serialize",
|
361
|
+
value: function serialize() {
|
362
|
+
return {
|
363
|
+
type: "image",
|
364
|
+
options: this.options,
|
365
|
+
opt: this.opt
|
366
|
+
};
|
367
|
+
}
|
368
|
+
}]);
|
369
|
+
return ImageSchema;
|
370
|
+
}(Schema);
|
371
|
+
var image = function image(options) {
|
372
|
+
return new ImageSchema(options);
|
373
|
+
};
|
374
|
+
var convertImageSource = function convertImageSource(src) {
|
375
|
+
var _src$metadata;
|
376
|
+
// TODO: /public should be configurable
|
377
|
+
return {
|
378
|
+
url: src[FILE_REF_PROP].slice("/public".length) + "?sha256=".concat((_src$metadata = src.metadata) === null || _src$metadata === void 0 ? void 0 : _src$metadata.sha256),
|
379
|
+
metadata: src.metadata
|
380
|
+
};
|
381
|
+
};
|
382
|
+
|
383
|
+
function isSerializedVal(val) {
|
384
|
+
return createClass._typeof(val) === "object" && val !== null && val !== undefined && ("val" in val || "valPath" in val);
|
385
|
+
}
|
386
|
+
|
387
|
+
/**
|
388
|
+
* The path of the source value.
|
389
|
+
*
|
390
|
+
* @example
|
391
|
+
* '/app/blogs.0.text' // the text property of the first element of the /app/blogs module
|
392
|
+
*/
|
393
|
+
|
394
|
+
/**
|
395
|
+
* The path inside the module.
|
396
|
+
*
|
397
|
+
* @example
|
398
|
+
* '0."text"' // the text property of the first element of the module
|
399
|
+
*/
|
400
|
+
|
401
|
+
/**
|
402
|
+
* The id of the module.
|
403
|
+
*
|
404
|
+
* @example
|
405
|
+
* '/app/blogs' // the /app/blogs module
|
406
|
+
*/
|
407
|
+
|
408
|
+
function getValPath(valOrSelector) {
|
409
|
+
return valOrSelector[Path];
|
410
|
+
}
|
411
|
+
|
412
|
+
function hasOwn(obj, prop) {
|
413
|
+
return Object.prototype.hasOwnProperty.call(obj, prop);
|
414
|
+
}
|
415
|
+
function _andThen(f, source, path) {
|
416
|
+
if (source) {
|
417
|
+
return newSelectorProxy(f(newSelectorProxy(source, path)));
|
418
|
+
}
|
419
|
+
return newSelectorProxy(source, path);
|
420
|
+
}
|
421
|
+
function isSelector(source) {
|
422
|
+
return createClass._typeof(source) === "object" && source !== null && (SourceOrExpr in source || Path in source);
|
423
|
+
}
|
424
|
+
function newSelectorProxy(source, path, moduleSchema) {
|
425
|
+
var _ref;
|
426
|
+
if (createClass._typeof(source) === "object") {
|
427
|
+
if (isSelector(source)) {
|
428
|
+
return source;
|
429
|
+
} else if (isSerializedVal(source)) {
|
430
|
+
return newSelectorProxy(source.val, source.valPath);
|
431
|
+
}
|
432
|
+
}
|
433
|
+
if (source && source[FILE_REF_PROP] && source[VAL_EXTENSION] === "file") {
|
434
|
+
var fileRef = source[FILE_REF_PROP];
|
435
|
+
if (typeof fileRef !== "string") {
|
436
|
+
throw Error("Invalid file ref: " + fileRef);
|
437
|
+
}
|
438
|
+
return newSelectorProxy(convertImageSource(source), path, moduleSchema);
|
439
|
+
}
|
440
|
+
switch (createClass._typeof(source)) {
|
441
|
+
case "function":
|
442
|
+
case "symbol":
|
443
|
+
throw Error("Invalid selector type: ".concat(createClass._typeof(source), ": ").concat(source));
|
444
|
+
case "object":
|
445
|
+
// Handles both objects and arrays!
|
446
|
+
if (source !== null) {
|
447
|
+
return new Proxy(source, {
|
448
|
+
// TODO: see proxy docs if we want more traps
|
449
|
+
has: function has(target, prop) {
|
450
|
+
if (prop === SourceOrExpr) {
|
451
|
+
return true;
|
452
|
+
}
|
453
|
+
if (prop === Path) {
|
454
|
+
return true;
|
455
|
+
}
|
456
|
+
if (prop === "andThen") {
|
457
|
+
return true;
|
458
|
+
}
|
459
|
+
if (prop === GetSchema) {
|
460
|
+
return true;
|
461
|
+
}
|
462
|
+
return prop in target;
|
463
|
+
},
|
464
|
+
get: function get(target, prop) {
|
465
|
+
if (prop === SourceOrExpr) {
|
466
|
+
return source;
|
467
|
+
}
|
468
|
+
if (prop === Path) {
|
469
|
+
return path;
|
470
|
+
}
|
471
|
+
if (prop === GetSchema) {
|
472
|
+
return moduleSchema;
|
473
|
+
}
|
474
|
+
if (prop === "andThen") {
|
475
|
+
return function (f) {
|
476
|
+
return _andThen(f, source, path);
|
477
|
+
};
|
478
|
+
}
|
479
|
+
if (Array.isArray(target)) {
|
480
|
+
if (prop === "filter") {
|
481
|
+
return function (f) {
|
482
|
+
var filtered = target.map(function (a, i) {
|
483
|
+
return newSelectorProxy(a, createValPathOfItem(path, i), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.item);
|
484
|
+
}).filter(function (a) {
|
485
|
+
if (f && f instanceof Schema) {
|
486
|
+
return f.match(unValify(a));
|
487
|
+
} else {
|
488
|
+
return unValify(f(a));
|
489
|
+
}
|
490
|
+
});
|
491
|
+
return newSelectorProxy(filtered, path, moduleSchema);
|
492
|
+
};
|
493
|
+
} else if (prop === "map") {
|
494
|
+
return function (f) {
|
495
|
+
var filtered = target.map(function (a, i) {
|
496
|
+
var valueOrSelector = f(newSelectorProxy(a, createValPathOfItem(path, i), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.item), newSelectorProxy(i));
|
497
|
+
if (isSelector(valueOrSelector)) {
|
498
|
+
return valueOrSelector;
|
499
|
+
}
|
500
|
+
return newSelectorProxy(valueOrSelector);
|
501
|
+
});
|
502
|
+
return newSelectorProxy(filtered, path, moduleSchema);
|
503
|
+
};
|
504
|
+
}
|
505
|
+
}
|
506
|
+
if (Array.isArray(target) && prop === "length") {
|
507
|
+
return newSelectorProxy(target.length);
|
508
|
+
}
|
509
|
+
var reflectedValue = Reflect.get(target, prop);
|
510
|
+
if (hasOwn(source, prop)) {
|
511
|
+
if (!Number.isNaN(Number(prop))) {
|
512
|
+
return newSelectorProxy(reflectedValue, createValPathOfItem(path, Number(prop)), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.item);
|
513
|
+
}
|
514
|
+
return newSelectorProxy(reflectedValue, createValPathOfItem(path, prop), moduleSchema === null || moduleSchema === void 0 ? void 0 : moduleSchema.items[prop]);
|
515
|
+
}
|
516
|
+
return reflectedValue;
|
517
|
+
}
|
518
|
+
});
|
519
|
+
}
|
520
|
+
// intentional fallthrough
|
521
|
+
// eslint-disable-next-line no-fallthrough
|
522
|
+
default:
|
523
|
+
return _ref = {
|
524
|
+
eq: function eq(other) {
|
525
|
+
var otherValue = other;
|
526
|
+
if (isSelector(other)) {
|
527
|
+
otherValue = other[SourceOrExpr];
|
528
|
+
if (otherValue instanceof Expr) {
|
529
|
+
throw Error("TODO: Cannot evaluate equality with an Expr");
|
530
|
+
}
|
531
|
+
}
|
532
|
+
return newSelectorProxy(source === otherValue, undefined);
|
533
|
+
},
|
534
|
+
andThen: function andThen(f) {
|
535
|
+
return _andThen(f, source === undefined ? null : source, path);
|
536
|
+
}
|
537
|
+
}, _defineProperty(_ref, SourceOrExpr, source === undefined ? null : source), _defineProperty(_ref, Path, path), _ref;
|
538
|
+
}
|
539
|
+
}
|
540
|
+
function createValPathOfItem(arrayPath, prop) {
|
541
|
+
if (createClass._typeof(prop) === "symbol") {
|
542
|
+
throw Error("Cannot create val path of array item with symbol prop: ".concat(prop.toString()));
|
543
|
+
}
|
544
|
+
return arrayPath && "".concat(arrayPath, ".").concat(JSON.stringify(prop));
|
545
|
+
}
|
546
|
+
|
547
|
+
// TODO: could we do .val on the objects instead?
|
548
|
+
function unValify(valueOrSelector) {
|
549
|
+
if (createClass._typeof(valueOrSelector) === "object" && (SourceOrExpr in valueOrSelector || Path in valueOrSelector)) {
|
550
|
+
var selectorValue = valueOrSelector[SourceOrExpr];
|
551
|
+
return selectorValue;
|
552
|
+
}
|
553
|
+
return valueOrSelector;
|
554
|
+
}
|
555
|
+
|
556
|
+
var WHITE_SPACE = ["\n", "\r", "\t", " "];
|
557
|
+
function tokenize(input) {
|
558
|
+
var tokens = [];
|
559
|
+
var cursor = 0;
|
560
|
+
while (cursor < input.length) {
|
561
|
+
var _char = input[cursor];
|
562
|
+
var peek = input[cursor + 1];
|
563
|
+
// TODO: remove this not used any more
|
564
|
+
if (_char === "!" && peek === "(") {
|
565
|
+
tokens.push({
|
566
|
+
type: "!(",
|
567
|
+
span: [cursor, cursor + 1]
|
568
|
+
});
|
569
|
+
cursor += 2;
|
570
|
+
} else if (_char === "(") {
|
571
|
+
tokens.push({
|
572
|
+
type: "(",
|
573
|
+
span: [cursor, cursor]
|
574
|
+
});
|
575
|
+
cursor++;
|
576
|
+
} else if (_char === ")") {
|
577
|
+
tokens.push({
|
578
|
+
type: ")",
|
579
|
+
span: [cursor, cursor]
|
580
|
+
});
|
581
|
+
cursor++;
|
582
|
+
} else if (_char === "'" || _char === "}") {
|
583
|
+
var _start = cursor;
|
584
|
+
var value = "";
|
585
|
+
var unescapedValue = "";
|
586
|
+
var escaped = false;
|
587
|
+
if (_char === "}") {
|
588
|
+
tokens.push({
|
589
|
+
type: "}",
|
590
|
+
span: [cursor, cursor]
|
591
|
+
});
|
592
|
+
} else if (_char === "'") {
|
593
|
+
tokens.push({
|
594
|
+
type: "'",
|
595
|
+
span: [cursor, cursor]
|
596
|
+
});
|
597
|
+
}
|
598
|
+
while (cursor < input.length) {
|
599
|
+
if (_char === "\\") {
|
600
|
+
escaped = !escaped;
|
601
|
+
} else {
|
602
|
+
escaped = false;
|
603
|
+
}
|
604
|
+
if (peek === "'" && !escaped) {
|
605
|
+
cursor += 2;
|
606
|
+
break;
|
607
|
+
} else if (_char === "$" && peek === "{") {
|
608
|
+
cursor += 2;
|
609
|
+
break;
|
610
|
+
}
|
611
|
+
cursor++;
|
612
|
+
_char = input[cursor];
|
613
|
+
peek = input[cursor + 1];
|
614
|
+
if (!(_char === "$" && peek === "{") && cursor < input.length) {
|
615
|
+
if (!(_char === "\\" && !escaped // counter-intuitive, but escape just became false if this was a backslash we want to escape
|
616
|
+
)) {
|
617
|
+
value += _char;
|
618
|
+
}
|
619
|
+
unescapedValue += _char;
|
620
|
+
}
|
621
|
+
}
|
622
|
+
var cursorOffset = peek === "'" && !escaped ? 2 : _char === "$" && peek === "{" ? 3 : 1;
|
623
|
+
if (value) {
|
624
|
+
tokens.push(_objectSpread2({
|
625
|
+
type: "string",
|
626
|
+
span: [_start + 1, cursor - cursorOffset],
|
627
|
+
value: value
|
628
|
+
}, unescapedValue !== value && {
|
629
|
+
unescapedValue: unescapedValue
|
630
|
+
}));
|
631
|
+
}
|
632
|
+
if (peek === "'" && !escaped) {
|
633
|
+
tokens.push({
|
634
|
+
type: "'",
|
635
|
+
span: [cursor - 1, cursor - 1]
|
636
|
+
});
|
637
|
+
} else if (_char === "$" && peek === "{") {
|
638
|
+
tokens.push({
|
639
|
+
type: "${",
|
640
|
+
span: [cursor - cursorOffset + 1, cursor - 1]
|
641
|
+
});
|
642
|
+
}
|
643
|
+
} else if (WHITE_SPACE.includes(_char)) {
|
644
|
+
var _start2 = cursor;
|
645
|
+
while (WHITE_SPACE.includes(input[cursor]) && cursor < input.length) {
|
646
|
+
cursor++;
|
647
|
+
}
|
648
|
+
tokens.push({
|
649
|
+
type: "ws",
|
650
|
+
span: [_start2, cursor - 1]
|
651
|
+
});
|
652
|
+
} else {
|
653
|
+
var _value = "";
|
654
|
+
var _start3 = cursor;
|
655
|
+
do {
|
656
|
+
_char = input[cursor];
|
657
|
+
peek = input[cursor + 1];
|
658
|
+
_value += _char;
|
659
|
+
cursor++;
|
660
|
+
} while (!WHITE_SPACE.includes(peek) && peek !== ")" && peek !== "'" && cursor < input.length);
|
661
|
+
tokens.push({
|
662
|
+
type: "token",
|
663
|
+
span: [_start3, cursor - 1],
|
664
|
+
value: _value
|
665
|
+
});
|
666
|
+
}
|
667
|
+
}
|
668
|
+
return [tokens, cursor];
|
669
|
+
}
|
670
|
+
|
671
|
+
var ParserError = /*#__PURE__*/createClass._createClass(function ParserError(message, span) {
|
672
|
+
createClass._classCallCheck(this, ParserError);
|
673
|
+
this.message = message;
|
674
|
+
this.span = span;
|
675
|
+
});
|
676
|
+
function parseTokens(inputTokens) {
|
677
|
+
var tokens = inputTokens.slice();
|
678
|
+
function slurpCall(first, isAnon) {
|
679
|
+
var _tokens$, _tokens$2, _tokens$3, _tokens$7, _tokens$8, _args$slice$0$span;
|
680
|
+
// peek
|
681
|
+
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) === ")") {
|
682
|
+
slurpWs();
|
683
|
+
tokens.shift();
|
684
|
+
return result.ok(new Sym("()", [first.span[0], first.span[1] + 1]));
|
685
|
+
}
|
686
|
+
var args = [];
|
687
|
+
var completed = false;
|
688
|
+
while (!completed) {
|
689
|
+
var _res = slurp();
|
690
|
+
if (result.isOk(_res)) {
|
691
|
+
var _tokens$4, _tokens$5, _tokens$6;
|
692
|
+
args.push(_res.value);
|
693
|
+
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) === ")";
|
694
|
+
} else {
|
695
|
+
return _res;
|
696
|
+
}
|
697
|
+
}
|
698
|
+
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) === ")") {
|
699
|
+
tokens.shift();
|
700
|
+
}
|
701
|
+
var last = tokens.shift();
|
702
|
+
if ((last === null || last === void 0 ? void 0 : last.type) !== ")") {
|
703
|
+
return result.err(new ParserError("unbalanced parens: missing a ')'", [first.span[0], first.span[1] + 1]));
|
704
|
+
}
|
705
|
+
return result.ok(new 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]));
|
706
|
+
}
|
707
|
+
function slurpWs() {
|
708
|
+
while (((_tokens$9 = tokens[0]) === null || _tokens$9 === void 0 ? void 0 : _tokens$9.type) === "ws") {
|
709
|
+
var _tokens$9;
|
710
|
+
tokens.shift();
|
711
|
+
}
|
712
|
+
}
|
713
|
+
function slurpTemplate(first) {
|
714
|
+
var children = [];
|
715
|
+
while (tokens.length > 0) {
|
716
|
+
var _tokens$10;
|
717
|
+
if (((_tokens$10 = tokens[0]) === null || _tokens$10 === void 0 ? void 0 : _tokens$10.type) === "'") {
|
718
|
+
break;
|
719
|
+
}
|
720
|
+
var nextToken = tokens.shift();
|
721
|
+
if ((nextToken === null || nextToken === void 0 ? void 0 : nextToken.type) === "${") {
|
722
|
+
var _res2 = slurp();
|
723
|
+
if (result.isOk(_res2)) {
|
724
|
+
children.push(_res2.value);
|
725
|
+
var _last = tokens.shift();
|
726
|
+
if (!_last) {
|
727
|
+
var _children$slice$0$spa;
|
728
|
+
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]]));
|
729
|
+
} else if (_last.type !== "}") {
|
730
|
+
return result.err(new ParserError("unbalanced string template: expected '}'", _last.span));
|
731
|
+
}
|
732
|
+
} else {
|
733
|
+
return _res2;
|
734
|
+
}
|
735
|
+
} else if ((nextToken === null || nextToken === void 0 ? void 0 : nextToken.type) === "string") {
|
736
|
+
children.push(new StringLiteral(nextToken.unescapedValue || nextToken.value || "", nextToken.span));
|
737
|
+
}
|
738
|
+
}
|
739
|
+
var last = tokens.shift();
|
740
|
+
if (!last) {
|
741
|
+
var _children$slice$0$spa2;
|
742
|
+
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]]));
|
743
|
+
} else if (last.type !== "'") {
|
744
|
+
return result.err(new ParserError("unbalanced string template: expected '''", last.span));
|
745
|
+
}
|
746
|
+
return result.ok(new StringTemplate(children, [first.span[0], last.span[1]]));
|
747
|
+
}
|
748
|
+
function slurpString(first) {
|
749
|
+
var _tokens$11, _tokens$12, _tokens$13;
|
750
|
+
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) === "'") {
|
751
|
+
var stringToken = tokens.shift();
|
752
|
+
var last = tokens.shift();
|
753
|
+
if (!last || !stringToken) {
|
754
|
+
throw Error("Unexpected error: stringToken or last is undefined");
|
755
|
+
}
|
756
|
+
return result.ok(new StringLiteral(stringToken.unescapedValue || stringToken.value || "", [first.span[0], last.span[1]]));
|
757
|
+
} else if (((_tokens$13 = tokens[0]) === null || _tokens$13 === void 0 ? void 0 : _tokens$13.type) === "'") {
|
758
|
+
var _last2 = tokens.shift();
|
759
|
+
if (!_last2) {
|
760
|
+
throw Error("Unexpected error: last is undefined");
|
761
|
+
}
|
762
|
+
return result.ok(new StringLiteral("", [first.span[0], _last2.span[1]]));
|
763
|
+
} else {
|
764
|
+
return slurpTemplate(first);
|
765
|
+
}
|
766
|
+
}
|
767
|
+
function slurp() {
|
768
|
+
slurpWs();
|
769
|
+
var first = tokens.shift();
|
770
|
+
if (!first) {
|
771
|
+
return result.err(new ParserError("expected '(', '!(', string or literal", [0, 0]));
|
772
|
+
}
|
773
|
+
if (first.type === "(" || first.type === "!(") {
|
774
|
+
return slurpCall(first, first.type === "!(");
|
775
|
+
} else if (first.type === "'") {
|
776
|
+
return slurpString(first);
|
777
|
+
} else if (first.type === "token") {
|
778
|
+
var _first$value, _first$value2, _first$value3, _first$value4, _first$value5, _first$value6;
|
779
|
+
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(")")) {
|
780
|
+
return result.err(new ParserError("unexpected token: '(' and ')' are not allowed in tokens", first.span));
|
781
|
+
}
|
782
|
+
if ((_first$value3 = first.value) !== null && _first$value3 !== void 0 && _first$value3.includes("'")) {
|
783
|
+
return result.err(new ParserError('unexpected token: "\'" is not allowed in tokens', first.span));
|
784
|
+
}
|
785
|
+
if ((_first$value4 = first.value) !== null && _first$value4 !== void 0 && _first$value4.includes(".")) {
|
786
|
+
return result.err(new ParserError('unexpected token: "." is not allowed in tokens', first.span));
|
787
|
+
}
|
788
|
+
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("}")) {
|
789
|
+
return result.err(new ParserError("unexpected token: '{' and '}' are not allowed in tokens", first.span));
|
790
|
+
}
|
791
|
+
return result.ok(new Sym(first.value || "", first.span));
|
792
|
+
} else {
|
793
|
+
return result.err(new ParserError("expected '(', '!(' or literal or token".concat(first.value || first.type ? ", got: '".concat(first.value || first.type, "'") : ""), first.span));
|
794
|
+
}
|
795
|
+
}
|
796
|
+
var res = slurp();
|
797
|
+
slurpWs();
|
798
|
+
if (result.isErr(res)) {
|
799
|
+
return res;
|
800
|
+
}
|
801
|
+
if (tokens.length > 0) {
|
802
|
+
return result.err(new ParserError("expected end of input, superfluous tokens", [tokens[0].span[0], tokens.slice(-1)[0].span[1]]));
|
803
|
+
}
|
804
|
+
return res;
|
805
|
+
}
|
806
|
+
function parse(input) {
|
807
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
808
|
+
var _tokenize = tokenize(input),
|
809
|
+
_tokenize2 = createClass._slicedToArray(_tokenize, 2),
|
810
|
+
tokens = _tokenize2[0];
|
811
|
+
_tokenize2[1]; // TODO: we can use cursor to improve error messages / spans
|
812
|
+
return parseTokens(tokens);
|
813
|
+
}
|
814
|
+
|
815
|
+
var EvalError = /*#__PURE__*/function () {
|
816
|
+
function EvalError(message, expr) {
|
817
|
+
createClass._classCallCheck(this, EvalError);
|
818
|
+
this.message = message;
|
819
|
+
this.expr = expr;
|
820
|
+
}
|
821
|
+
createClass._createClass(EvalError, [{
|
822
|
+
key: "toString",
|
823
|
+
value: function toString() {
|
824
|
+
return "".concat(this.message, " in: ").concat(this.expr.transpile());
|
825
|
+
}
|
826
|
+
}]);
|
827
|
+
return EvalError;
|
828
|
+
}();
|
829
|
+
var MAX_STACK_SIZE = 100; // an arbitrary semi-large number
|
830
|
+
function evaluateSync(expr, getSource, stack) {
|
831
|
+
// TODO: amount of evaluates should be limited?
|
832
|
+
if (stack.length > MAX_STACK_SIZE) {
|
833
|
+
throw new EvalError("Stack overflow. Final frames: ".concat(stack.slice(-10).map(function (frame, i) {
|
834
|
+
return frame.map(function (s, j) {
|
835
|
+
return "@[".concat(i, ",").concat(j, "]: ").concat(JSON.stringify(s));
|
836
|
+
}).join(", ");
|
837
|
+
}).join(" -> ")), expr);
|
838
|
+
}
|
839
|
+
if (expr instanceof Call) {
|
840
|
+
if (expr.children[0] instanceof Sym) {
|
841
|
+
if (expr.children[0].value === "val") {
|
842
|
+
if (expr.isAnon) {
|
843
|
+
throw new EvalError("cannot call 'val' as anonymous function", expr);
|
844
|
+
}
|
845
|
+
if (expr.children[1] instanceof StringLiteral) {
|
846
|
+
return getSource(expr.children[1].value);
|
847
|
+
} else {
|
848
|
+
throw new EvalError("argument of 'val' must be a string literal", expr);
|
849
|
+
}
|
850
|
+
} else if (expr.children[0].value === "json") {
|
851
|
+
if (expr.children.length !== 2) {
|
852
|
+
throw new EvalError("must call 'json' with exactly one argument", expr);
|
853
|
+
}
|
854
|
+
var value = evaluateSync(expr.children[1], getSource, stack);
|
855
|
+
var valObj = value[SourceOrExpr];
|
856
|
+
var valPath = value[Path];
|
857
|
+
if (typeof valObj !== "string") {
|
858
|
+
throw new EvalError("cannot parse JSON: ".concat(JSON.stringify(valObj), ", expected string"), expr.children[1]);
|
859
|
+
}
|
860
|
+
try {
|
861
|
+
var serialized = JSON.parse(valObj);
|
862
|
+
if (isSerializedVal(serialized)) {
|
863
|
+
return newSelectorProxy(serialized.val, serialized.valPath);
|
864
|
+
}
|
865
|
+
var parsedValue = newSelectorProxy(JSON.parse(valObj), valPath);
|
866
|
+
return parsedValue;
|
867
|
+
} catch (e) {
|
868
|
+
if (e instanceof SyntaxError) {
|
869
|
+
throw new EvalError("cannot parse JSON: ".concat(valObj, ", ").concat(e.message, " - value: ").concat(JSON.stringify(value)), expr.children[1]);
|
870
|
+
}
|
871
|
+
throw e;
|
872
|
+
}
|
873
|
+
} else if (expr.children[0].value === "stringify") {
|
874
|
+
// TODO: remove stringify
|
875
|
+
if (expr.children.length !== 2) {
|
876
|
+
throw new EvalError("must call 'stringify' with exactly one argument", expr);
|
877
|
+
}
|
878
|
+
var res = evaluateSync(expr.children[1], getSource, stack);
|
879
|
+
return newSelectorProxy(JSON.stringify(res[SourceOrExpr]));
|
880
|
+
}
|
881
|
+
}
|
882
|
+
var prop = evaluateSync(expr.children[0], getSource, stack)[SourceOrExpr];
|
883
|
+
if (expr.children.length === 1) {
|
884
|
+
// TODO: return if literal only?
|
885
|
+
return newSelectorProxy(prop);
|
886
|
+
}
|
887
|
+
var obj = evaluateSync(expr.children[1], getSource, stack);
|
888
|
+
if (typeof prop !== "string" && typeof prop !== "number") {
|
889
|
+
throw new EvalError("cannot access ".concat(JSON.stringify(obj), " with property ").concat(JSON.stringify(prop), ": is not a string or number"), expr);
|
890
|
+
}
|
891
|
+
if (prop in obj) {
|
892
|
+
if (expr.isAnon) {
|
893
|
+
// anon functions:
|
894
|
+
var maybeFunction = obj[prop];
|
895
|
+
if (typeof maybeFunction !== "function") {
|
896
|
+
throw new EvalError("cannot access property ".concat(JSON.stringify(prop), " of ").concat(JSON.stringify(obj), ": required higher ordered function got ").concat(createClass._typeof(obj[prop])), expr);
|
897
|
+
}
|
898
|
+
if (expr.children[0] instanceof Sym) {
|
899
|
+
return maybeFunction(function () {
|
900
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
901
|
+
args[_key] = arguments[_key];
|
902
|
+
}
|
903
|
+
return evaluateSync(expr.children[2], getSource, stack.concat([args]));
|
904
|
+
});
|
905
|
+
} else {
|
906
|
+
throw new EvalError("cannot call an expression that is not a symbol, got: '".concat(expr.children[0].type, "'"), expr);
|
907
|
+
}
|
908
|
+
} else {
|
909
|
+
// non-anon functions:
|
910
|
+
if (expr.children[0] instanceof Sym) {
|
911
|
+
if (expr.children[0].value === "val") {
|
912
|
+
if (expr.children[1] instanceof StringLiteral) {
|
913
|
+
return getSource(expr.children[1].value);
|
914
|
+
} else {
|
915
|
+
throw new EvalError("argument of 'val' must be a string literal", expr);
|
916
|
+
}
|
917
|
+
}
|
918
|
+
}
|
919
|
+
var args = expr.children.slice(2);
|
920
|
+
if (args.length > 0) {
|
921
|
+
var _maybeFunction = obj[prop];
|
922
|
+
if (typeof _maybeFunction !== "function") {
|
923
|
+
throw new EvalError("cannot access property ".concat(JSON.stringify(prop), " of ").concat(JSON.stringify(obj), ": required function got ").concat(createClass._typeof(obj[prop])), expr);
|
924
|
+
}
|
925
|
+
return _maybeFunction.apply(void 0, createClass._toConsumableArray(args.map(function (arg) {
|
926
|
+
return evaluateSync(arg, getSource, stack);
|
927
|
+
})));
|
928
|
+
}
|
929
|
+
var maybeValue = obj[prop];
|
930
|
+
if (typeof maybeValue === "function") {
|
931
|
+
throw new EvalError("cannot access property ".concat(JSON.stringify(prop), " of ").concat(JSON.stringify(obj), ": required value got ").concat(createClass._typeof(obj[prop])), expr);
|
932
|
+
}
|
933
|
+
return maybeValue;
|
934
|
+
}
|
935
|
+
}
|
936
|
+
} else if (expr instanceof Sym) {
|
937
|
+
if (expr.value.startsWith("@")) {
|
938
|
+
var _stack$Number;
|
939
|
+
var _expr$value$slice$spl = expr.value.slice(2, -1).split(","),
|
940
|
+
_expr$value$slice$spl2 = createClass._slicedToArray(_expr$value$slice$spl, 3),
|
941
|
+
i = _expr$value$slice$spl2[0],
|
942
|
+
j = _expr$value$slice$spl2[1],
|
943
|
+
rest = _expr$value$slice$spl2[2];
|
944
|
+
if (rest) {
|
945
|
+
throw new EvalError("cannot access stack: too many indices", expr);
|
946
|
+
}
|
947
|
+
var stackValue = (_stack$Number = stack[Number(i)]) === null || _stack$Number === void 0 ? void 0 : _stack$Number[Number(j)];
|
948
|
+
if (stackValue === undefined) {
|
949
|
+
throw new EvalError("cannot access stack: out of bounds", expr);
|
950
|
+
}
|
951
|
+
return stackValue;
|
952
|
+
} else if (expr.value === "()") {
|
953
|
+
return newSelectorProxy(null);
|
954
|
+
}
|
955
|
+
return newSelectorProxy(expr.value);
|
956
|
+
} else if (expr instanceof StringLiteral) {
|
957
|
+
return newSelectorProxy(expr.value);
|
958
|
+
} else if (expr instanceof StringTemplate) {
|
959
|
+
return newSelectorProxy(expr.children.map(function (child) {
|
960
|
+
if (child instanceof Sym && child.value === "()") {
|
961
|
+
return "null";
|
962
|
+
}
|
963
|
+
var evalRes = evaluateSync(child, getSource, stack);
|
964
|
+
if (child.type === "StringLiteral" || child.type === "StringTemplate") {
|
965
|
+
return evalRes[SourceOrExpr];
|
966
|
+
}
|
967
|
+
if (Path in evalRes) {
|
968
|
+
// a selector, so serialize to Val
|
969
|
+
return JSON.stringify({
|
970
|
+
val: evalRes[SourceOrExpr],
|
971
|
+
valPath: evalRes[Path]
|
972
|
+
});
|
973
|
+
}
|
974
|
+
return JSON.stringify(evalRes[SourceOrExpr]);
|
975
|
+
}).join(""));
|
976
|
+
}
|
977
|
+
throw new EvalError("could not evaluate", expr);
|
978
|
+
}
|
979
|
+
function evaluate(expr, source, stack) {
|
980
|
+
try {
|
981
|
+
return result.ok(evaluateSync(expr, source, stack));
|
982
|
+
} catch (err) {
|
983
|
+
if (err instanceof EvalError) {
|
984
|
+
return result.err(err);
|
985
|
+
}
|
986
|
+
throw err;
|
987
|
+
}
|
988
|
+
}
|
989
|
+
|
990
|
+
var index = /*#__PURE__*/Object.freeze({
|
991
|
+
__proto__: null,
|
992
|
+
parse: parse,
|
993
|
+
Call: Call,
|
994
|
+
Expr: Expr,
|
995
|
+
NilSym: NilSym,
|
996
|
+
StringLiteral: StringLiteral,
|
997
|
+
StringTemplate: StringTemplate,
|
998
|
+
Sym: Sym,
|
999
|
+
evaluate: evaluate
|
1000
|
+
});
|
1001
|
+
|
1002
|
+
exports.Call = Call;
|
1003
|
+
exports.Expr = Expr;
|
1004
|
+
exports.FILE_REF_PROP = FILE_REF_PROP;
|
1005
|
+
exports.GenericSelector = GenericSelector;
|
1006
|
+
exports.ImageSchema = ImageSchema;
|
1007
|
+
exports.NilSym = NilSym;
|
1008
|
+
exports.Path = Path;
|
1009
|
+
exports.Schema = Schema;
|
1010
|
+
exports.SourceOrExpr = SourceOrExpr;
|
1011
|
+
exports.StringLiteral = StringLiteral;
|
1012
|
+
exports.StringTemplate = StringTemplate;
|
1013
|
+
exports.Sym = Sym;
|
1014
|
+
exports.VAL_EXTENSION = VAL_EXTENSION;
|
1015
|
+
exports._createSuper = _createSuper;
|
1016
|
+
exports._defineProperty = _defineProperty;
|
1017
|
+
exports._inherits = _inherits;
|
1018
|
+
exports._objectSpread2 = _objectSpread2;
|
1019
|
+
exports.convertImageSource = convertImageSource;
|
1020
|
+
exports.createValPathOfItem = createValPathOfItem;
|
1021
|
+
exports.evaluate = evaluate;
|
1022
|
+
exports.file = file;
|
1023
|
+
exports.getSchema = getSchema;
|
1024
|
+
exports.getValPath = getValPath;
|
1025
|
+
exports.image = image;
|
1026
|
+
exports.index = index;
|
1027
|
+
exports.isFile = isFile;
|
1028
|
+
exports.isSelector = isSelector;
|
1029
|
+
exports.isSerializedVal = isSerializedVal;
|
1030
|
+
exports.newSelectorProxy = newSelectorProxy;
|
1031
|
+
exports.parse = parse;
|