@bablr/boot 0.6.3 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/builders.js DELETED
@@ -1,475 +0,0 @@
1
- const sym = require('@bablr/boot-helpers/symbols');
2
- const { freeze, getPrototypeOf } = Object;
3
- const { isArray } = Array;
4
- const {
5
- DoctypeTag,
6
- OpenNodeTag,
7
- CloseNodeTag,
8
- OpenFragmentTag,
9
- CloseFragmentTag,
10
- ReferenceTag,
11
- GapTag,
12
- LiteralTag,
13
- } = require('@bablr/boot-helpers/symbols');
14
-
15
- const spreads = new WeakMap();
16
-
17
- const spread = (arg) => {
18
- const wrapper = { value: arg };
19
- spreads.set(wrapper, true);
20
- return wrapper;
21
- };
22
-
23
- const interpolateArray = (values, buildSeparator) => {
24
- const children = [];
25
- for (const value of values) {
26
- if (spreads.has(value)) {
27
- let first = true;
28
-
29
- for (const element of value.value) {
30
- if (!first && buildSeparator) {
31
- children.push(buildSeparator());
32
- }
33
-
34
- children.push(element);
35
-
36
- first = false;
37
- }
38
- } else {
39
- children.push(value);
40
- }
41
- }
42
- return children;
43
- };
44
-
45
- const buildTag = (term) => {
46
- switch (term?.type) {
47
- case LiteralTag:
48
- return term;
49
-
50
- // case 'Escape':
51
- // return buildEscapeNode;
52
-
53
- default:
54
- throw new Error('Invalid tag of type ' + term.type);
55
- }
56
- };
57
-
58
- const interpolateString = (value) => {
59
- const children = [];
60
- if (isArray(value)) {
61
- for (const element of value) {
62
- children.push(buildTag(element));
63
- }
64
- } else {
65
- // we can't safely interpolate strings here, though I wish we could
66
- children.push(buildTag(value));
67
- }
68
-
69
- return buildNode('String', 'Content', children);
70
- };
71
-
72
- const buildDoctypeTag = (attributes) => {
73
- return freeze({
74
- type: DoctypeTag,
75
- value: freeze({ doctype: 'cstml', version: 0, attributes }),
76
- });
77
- };
78
-
79
- const buildReferenceTag = (name, isArray) => {
80
- return freeze({ type: ReferenceTag, value: freeze({ name, isArray }) });
81
- };
82
-
83
- const buildGapTag = () => {
84
- return freeze({ type: GapTag, value: undefined });
85
- };
86
-
87
- const buildNodeOpenTag = (flags, language, type, attributes = {}) => {
88
- return freeze({ type: OpenNodeTag, value: freeze({ flags, language, type, attributes }) });
89
- };
90
-
91
- const buildNodeCloseTag = (type) => {
92
- return freeze({ type: CloseNodeTag, value: freeze({ type }) });
93
- };
94
-
95
- const buildFragmentOpenTag = (flags = {}) => {
96
- return freeze({
97
- type: OpenFragmentTag,
98
- value: freeze({
99
- flags: freeze(flags),
100
- }),
101
- });
102
- };
103
-
104
- const buildFragmentCloseTag = (type = null, language = null) => {
105
- return freeze({ type: CloseFragmentTag, value: freeze({ language, type }) });
106
- };
107
-
108
- const buildLiteralTag = (value) => {
109
- return freeze({ type: LiteralTag, value });
110
- };
111
-
112
- const buildProperty = (key, value) => {
113
- return node('Instruction', 'Property', [ref`key`, ref`mapOperator`, buildSpace(), ref`value`], {
114
- key: buildIdentifier(key),
115
- mapOperator: s_node('Instruction', 'Punctuator', ':'),
116
- value: buildExpression(value),
117
- });
118
- };
119
-
120
- const buildString = (value) => {
121
- const Tags = [];
122
- let literal = '';
123
- for (const chr of value) {
124
- if (chr === "'") {
125
- if (literal)
126
- Tags.push(
127
- freeze({
128
- type: LiteralTag,
129
- value: literal,
130
- }),
131
- );
132
- Tags.push(
133
- e_node(
134
- 'String',
135
- 'Escape',
136
- [ref`escape`, ref`escapee`],
137
- {
138
- escape: s_node('String', 'Punctuator', '\\'),
139
- escapee: s_node('String', LiteralTag, "'"),
140
- },
141
- { cooked: chr },
142
- ),
143
- );
144
- } else if (chr === '\\') {
145
- if (literal)
146
- Tags.push({
147
- type: LiteralTag,
148
- value: literal,
149
- });
150
- Tags.push(
151
- e_node(
152
- 'String',
153
- 'Escape',
154
- [ref`escape`, ref`escapee`],
155
- {
156
- escape: s_node('String', 'Punctuator', '\\'),
157
- escapee: s_node('String', LiteralTag, '\\'),
158
- },
159
- { cooked: chr },
160
- ),
161
- );
162
- } else {
163
- literal += chr;
164
- }
165
- }
166
- if (literal)
167
- Tags.push(
168
- freeze({
169
- type: LiteralTag,
170
- value: literal,
171
- }),
172
- );
173
- return node(
174
- 'String',
175
- 'String',
176
- [ref`open`, ref`content`, ref`close`],
177
- {
178
- open: s_node('String', 'Punctuator', "'"),
179
- content: interpolateString(Tags),
180
- close: s_node('String', 'Punctuator', "'"),
181
- },
182
- {},
183
- );
184
- };
185
-
186
- const buildBoolean = (value) => {
187
- return value
188
- ? node(
189
- 'Instruction',
190
- 'Boolean',
191
- [ref`value`],
192
- {
193
- value: s_node('Instruction', 'Keyword', 'true'),
194
- },
195
- {},
196
- )
197
- : node(
198
- 'Instruction',
199
- 'Boolean',
200
- [ref`value`],
201
- {
202
- value: s_node('Instruction', 'Keyword', 'false'),
203
- },
204
- {},
205
- );
206
- };
207
-
208
- const buildNullTag = () => {
209
- return node(
210
- 'Instruction',
211
- 'Null',
212
- [ref`value`],
213
- {
214
- value: s_node('Instruction', 'Keyword', 'null'),
215
- },
216
- {},
217
- );
218
- };
219
-
220
- const buildArrayTag = (elements) => {
221
- return node(
222
- 'Instruction',
223
- 'Array',
224
- [ref`open`, ref`elements[]`, ref`close`],
225
- {
226
- open: s_node('Instruction', 'Punctuator', '['),
227
- elements: [...interpolateArray(spread(elements, buildSpace))],
228
- close: s_node('Instruction', 'Punctuator', ']'),
229
- },
230
- {},
231
- );
232
- };
233
-
234
- const buildTuple = (elements) => {
235
- return node(
236
- 'Instruction',
237
- 'Tuple',
238
- [ref`open`, ref`values[]`, ref`close`],
239
- {
240
- open: s_node('Instruction', 'Punctuator', '('),
241
- values: [...interpolateArray(spread(elements, buildSpace))],
242
- close: s_node('Instruction', 'Punctuator', ')'),
243
- },
244
- {},
245
- );
246
- };
247
-
248
- const buildObject = (properties) => {
249
- return node(
250
- 'Instruction',
251
- 'Object',
252
- [ref`open`, ref`properties[]`, ref`close`],
253
- {
254
- open: s_node('Instruction', 'Punctuator', '{'),
255
- properties: [
256
- ...interpolateArray(
257
- spread(Object.entries(properties).map(([key, value]) => buildProperty(key, value))),
258
- ),
259
- ],
260
- close: s_node('Instruction', 'Punctuator', '}'),
261
- },
262
- {},
263
- );
264
- };
265
-
266
- const buildSpace = () => {
267
- return t_node('Comment', null, [t_node('Space', 'Space', lit` `)]);
268
- };
269
-
270
- const buildIdentifier = (name) => {
271
- return node('Instruction', 'Identifier', [buildLiteralTag(name)]);
272
- };
273
-
274
- const buildAttribute = (key, value) => {
275
- return buildNode('CSTML', 'Attribute', [ref`key`, ref`mapOperator`, ref`value`], {
276
- key: buildIdentifier(key),
277
- mapOperator: s_node('CSTML', 'Punctuator', '='),
278
- value: buildExpression(value),
279
- });
280
- };
281
-
282
- const buildExpression = (expr) => {
283
- if (expr == null) return buildNullTag();
284
-
285
- switch (typeof expr) {
286
- case 'boolean':
287
- return buildBoolean(expr);
288
- case 'string':
289
- return buildString(expr);
290
- case 'object': {
291
- switch (getPrototypeOf(expr)) {
292
- case Array.prototype:
293
- return buildArrayTag(expr);
294
- case Object.prototype:
295
- if (expr.type && expr.language && expr.children && expr.properties) {
296
- return expr;
297
- }
298
- return buildObject(expr);
299
- default:
300
- throw new Error();
301
- }
302
- }
303
- default:
304
- throw new Error();
305
- }
306
- };
307
-
308
- const nodeFlags = freeze({ syntactic: false, escape: false, trivia: false });
309
-
310
- const buildNode = (language, type, children = [], properties = {}, attributes = {}) => {
311
- const openTag = buildNodeOpenTag(nodeFlags, language, type, attributes);
312
- const closeTag = buildNodeCloseTag(type);
313
- return freeze({
314
- flags: nodeFlags,
315
- language,
316
- type,
317
- children: freeze([openTag, ...children, closeTag]),
318
- properties: freeze(properties),
319
- attributes: freeze(attributes),
320
- });
321
- };
322
-
323
- const syntacticFlags = freeze({ syntactic: true, escape: false, trivia: false });
324
-
325
- const buildSyntacticNode = (language, type, value, attributes = {}) => {
326
- const openTag = buildNodeOpenTag(syntacticFlags, language, type, attributes);
327
- const closeTag = buildNodeCloseTag(type);
328
- return freeze({
329
- flags: syntacticFlags,
330
- language,
331
- type,
332
- children: [openTag, buildLiteralTag(value), closeTag],
333
- properties: freeze({}),
334
- attributes: freeze(attributes),
335
- });
336
- };
337
-
338
- const escapeFlags = freeze({ syntactic: false, escape: true, trivia: false });
339
-
340
- const buildEscapeNode = (language, type, children = [], properties = {}, attributes = {}) => {
341
- const openTag = buildNodeOpenTag(escapeFlags, language, type, attributes);
342
- const closeTag = buildNodeCloseTag(type);
343
- return freeze({
344
- flags: escapeFlags,
345
- language,
346
- type,
347
- children: freeze([openTag, ...children, closeTag]),
348
- properties: freeze(properties),
349
- attributes: freeze(attributes),
350
- });
351
- };
352
-
353
- const syntacticEscapeFlags = freeze({ syntactic: true, escape: true, trivia: false });
354
-
355
- const buildSyntacticEscapeNode = (
356
- language,
357
- type,
358
- children = [],
359
- properties = {},
360
- attributes = {},
361
- ) => {
362
- const openTag = buildNodeOpenTag(syntacticEscapeFlags, language, type, attributes);
363
- const closeTag = buildNodeCloseTag(type);
364
- return freeze({
365
- flags: syntacticEscapeFlags,
366
- language,
367
- type,
368
- children: freeze([openTag, ...children, closeTag]),
369
- properties: freeze(properties),
370
- attributes: freeze(attributes),
371
- });
372
- };
373
-
374
- const triviaFlags = freeze({ syntactic: false, escape: false, trivia: true });
375
-
376
- const buildTriviaNode = (language, type, children = [], properties = {}, attributes = {}) => {
377
- const openTag = buildNodeOpenTag(triviaFlags, language, type, attributes);
378
- const closeTag = buildNodeCloseTag(type);
379
- return freeze({
380
- flags: triviaFlags,
381
- language,
382
- type,
383
- children: freeze([openTag, ...children, closeTag]),
384
- properties: freeze(properties),
385
- attributes: freeze(attributes),
386
- });
387
- };
388
-
389
- const buildGapNode = (attributes = []) => {
390
- return freeze({
391
- flags: nodeFlags,
392
- language: null,
393
- type: sym.gap,
394
- children: freeze([buildGapTag()]),
395
- properties: freeze({}),
396
- attributes: freeze(attributes),
397
- });
398
- };
399
-
400
- const stripArray = (val) => {
401
- if (isArray(val)) {
402
- if (val.length > 1) {
403
- throw new Error();
404
- }
405
- return val[0];
406
- } else {
407
- return val;
408
- }
409
- };
410
-
411
- const ref = (path) => {
412
- if (isArray(path)) {
413
- const pathIsArray = path[0].endsWith('[]');
414
- const name = pathIsArray ? path[0].slice(0, -2) : path[0];
415
- return buildReferenceTag(name, pathIsArray);
416
- } else {
417
- const { name, pathIsArray } = path;
418
- return buildReferenceTag(name, pathIsArray);
419
- }
420
- };
421
-
422
- const lit = (str) => buildLiteralTag(stripArray(str));
423
-
424
- const gap = buildGapTag;
425
- const arr = buildArrayTag;
426
- const nodeOpen = buildNodeOpenTag;
427
- const nodeClose = buildNodeCloseTag;
428
- const node = buildNode;
429
- const g_node = buildGapNode;
430
- const s_node = buildSyntacticNode;
431
- const e_node = buildEscapeNode;
432
- const s_e_node = buildSyntacticEscapeNode;
433
- const t_node = buildTriviaNode;
434
-
435
- module.exports = {
436
- buildProperty,
437
- buildString,
438
- buildBoolean,
439
- buildNullTag,
440
- buildArrayTag,
441
- buildTuple,
442
- buildObject,
443
- buildExpression,
444
- buildSpace,
445
- buildIdentifier,
446
- buildAttribute,
447
- buildDoctypeTag,
448
- buildReferenceTag,
449
- buildGapTag,
450
- buildNodeOpenTag,
451
- buildNodeCloseTag,
452
- buildFragmentOpenTag,
453
- buildFragmentCloseTag,
454
- buildLiteralTag,
455
- buildNode,
456
- buildSyntacticNode,
457
- buildEscapeNode,
458
- buildSyntacticEscapeNode,
459
- buildTriviaNode,
460
- buildGapNode,
461
- nodeFlags,
462
-
463
- ref,
464
- lit,
465
- gap,
466
- arr,
467
- nodeOpen,
468
- nodeClose,
469
- node,
470
- g_node,
471
- s_node,
472
- e_node,
473
- s_e_node,
474
- t_node,
475
- };
package/lib/esm-entry.mjs DELETED
@@ -1,3 +0,0 @@
1
- import boot from '../dist/esm.bundle.mjs';
2
-
3
- export const { i, re, spam, cst: cstml } = boot;