mol_tree2 1.0.1395 → 1.0.1397
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/node.d.ts +57 -1
- package/node.d.ts.map +1 -1
- package/node.deps.json +1 -1
- package/node.js +66 -2
- package/node.js.map +1 -1
- package/node.mjs +66 -2
- package/node.test.js +264 -9
- package/node.test.js.map +1 -1
- package/package.json +1 -1
- package/web.d.ts +57 -1
- package/web.d.ts.map +1 -1
- package/web.deps.json +1 -1
- package/web.js +66 -2
- package/web.js.map +1 -1
- package/web.mjs +66 -2
- package/web.test.js +79 -0
- package/web.test.js.map +1 -1
package/node.mjs
CHANGED
|
@@ -44,6 +44,11 @@ var $;
|
|
|
44
44
|
var $;
|
|
45
45
|
(function ($) {
|
|
46
46
|
const instances = new WeakSet();
|
|
47
|
+
/**
|
|
48
|
+
* Proxy that delegates all to lazy returned target.
|
|
49
|
+
*
|
|
50
|
+
* $mol_delegate( Array.prototype , ()=> fetch_array() )
|
|
51
|
+
*/
|
|
47
52
|
function $mol_delegate(proto, target) {
|
|
48
53
|
const proxy = new Proxy(proto, {
|
|
49
54
|
get: (_, field) => {
|
|
@@ -147,7 +152,7 @@ var $;
|
|
|
147
152
|
var $;
|
|
148
153
|
(function ($) {
|
|
149
154
|
function $mol_fail_hidden(error) {
|
|
150
|
-
throw error;
|
|
155
|
+
throw error; /// Use 'Never Pause Here' breakpoint in DevTools or simply blackbox this script
|
|
151
156
|
}
|
|
152
157
|
$.$mol_fail_hidden = $mol_fail_hidden;
|
|
153
158
|
})($ || ($ = {}));
|
|
@@ -239,6 +244,9 @@ var $;
|
|
|
239
244
|
[Symbol.dispose]() {
|
|
240
245
|
this.destructor();
|
|
241
246
|
}
|
|
247
|
+
//[ Symbol.toPrimitive ]( hint: string ) {
|
|
248
|
+
// return hint === 'number' ? this.valueOf() : this.toString()
|
|
249
|
+
//}
|
|
242
250
|
toString() {
|
|
243
251
|
return this[Symbol.toStringTag] || this.constructor.name + '<>';
|
|
244
252
|
}
|
|
@@ -250,6 +258,7 @@ var $;
|
|
|
250
258
|
"use strict";
|
|
251
259
|
var $;
|
|
252
260
|
(function ($) {
|
|
261
|
+
/** Position in any resource. */
|
|
253
262
|
class $mol_span extends $mol_object2 {
|
|
254
263
|
uri;
|
|
255
264
|
source;
|
|
@@ -265,13 +274,17 @@ var $;
|
|
|
265
274
|
this.length = length;
|
|
266
275
|
this[Symbol.toStringTag] = this.uri + ('#' + this.row + ':' + this.col + '/' + this.length);
|
|
267
276
|
}
|
|
277
|
+
/** Span for begin of unknown resource */
|
|
268
278
|
static unknown = $mol_span.begin('?');
|
|
279
|
+
/** Makes new span for begin of resource. */
|
|
269
280
|
static begin(uri, source = '') {
|
|
270
281
|
return new $mol_span(uri, source, 1, 1, 0);
|
|
271
282
|
}
|
|
283
|
+
/** Makes new span for end of resource. */
|
|
272
284
|
static end(uri, source) {
|
|
273
285
|
return new $mol_span(uri, source, 1, source.length + 1, 0);
|
|
274
286
|
}
|
|
287
|
+
/** Makes new span for entire resource. */
|
|
275
288
|
static entire(uri, source) {
|
|
276
289
|
return new $mol_span(uri, source, 1, 1, source.length);
|
|
277
290
|
}
|
|
@@ -286,15 +299,19 @@ var $;
|
|
|
286
299
|
length: this.length
|
|
287
300
|
};
|
|
288
301
|
}
|
|
302
|
+
/** Makes new error for this span. */
|
|
289
303
|
error(message, Class = Error) {
|
|
290
304
|
return new Class(`${message} (${this})`);
|
|
291
305
|
}
|
|
306
|
+
/** Makes new span for same uri. */
|
|
292
307
|
span(row, col, length) {
|
|
293
308
|
return new $mol_span(this.uri, this.source, row, col, length);
|
|
294
309
|
}
|
|
310
|
+
/** Makes new span after end of this. */
|
|
295
311
|
after(length = 0) {
|
|
296
312
|
return new $mol_span(this.uri, this.source, this.row, this.col + this.length, length);
|
|
297
313
|
}
|
|
314
|
+
/** Makes new span between begin and end. */
|
|
298
315
|
slice(begin, end = -1) {
|
|
299
316
|
let len = this.length;
|
|
300
317
|
if (begin < 0)
|
|
@@ -317,6 +334,7 @@ var $;
|
|
|
317
334
|
"use strict";
|
|
318
335
|
var $;
|
|
319
336
|
(function ($) {
|
|
337
|
+
/** Syntax error with cordinates and source line snippet. */
|
|
320
338
|
class $mol_error_syntax extends SyntaxError {
|
|
321
339
|
reason;
|
|
322
340
|
line;
|
|
@@ -335,6 +353,7 @@ var $;
|
|
|
335
353
|
"use strict";
|
|
336
354
|
var $;
|
|
337
355
|
(function ($) {
|
|
356
|
+
/** Parses tree format from string. */
|
|
338
357
|
function $mol_tree2_from_string(str, uri = '?') {
|
|
339
358
|
const span = $mol_span.entire(uri, str);
|
|
340
359
|
var root = $mol_tree2.list([], span);
|
|
@@ -344,6 +363,7 @@ var $;
|
|
|
344
363
|
var indent = 0;
|
|
345
364
|
var line_start = pos;
|
|
346
365
|
row++;
|
|
366
|
+
// read indent
|
|
347
367
|
while (str.length > pos && str[pos] == '\t') {
|
|
348
368
|
indent++;
|
|
349
369
|
pos++;
|
|
@@ -352,8 +372,10 @@ var $;
|
|
|
352
372
|
min_indent = indent;
|
|
353
373
|
}
|
|
354
374
|
indent -= min_indent;
|
|
375
|
+
// invalid tab size
|
|
355
376
|
if (indent < 0 || indent >= stack.length) {
|
|
356
377
|
const sp = span.span(row, 1, pos - line_start);
|
|
378
|
+
// skip error line
|
|
357
379
|
while (str.length > pos && str[pos] != '\n') {
|
|
358
380
|
pos++;
|
|
359
381
|
}
|
|
@@ -368,7 +390,9 @@ var $;
|
|
|
368
390
|
}
|
|
369
391
|
stack.length = indent + 1;
|
|
370
392
|
var parent = stack[indent];
|
|
393
|
+
// parse types
|
|
371
394
|
while (str.length > pos && str[pos] != '\\' && str[pos] != '\n') {
|
|
395
|
+
// type can not contain space and tab
|
|
372
396
|
var error_start = pos;
|
|
373
397
|
while (str.length > pos && (str[pos] == ' ' || str[pos] == '\t')) {
|
|
374
398
|
pos++;
|
|
@@ -380,6 +404,7 @@ var $;
|
|
|
380
404
|
const sp = span.span(row, error_start - line_start + 1, pos - error_start);
|
|
381
405
|
this.$mol_fail(new this.$mol_error_syntax(`Wrong nodes separator`, str.substring(line_start, line_end), sp));
|
|
382
406
|
}
|
|
407
|
+
// read type
|
|
383
408
|
var type_start = pos;
|
|
384
409
|
while (str.length > pos &&
|
|
385
410
|
str[pos] != '\\' &&
|
|
@@ -394,10 +419,12 @@ var $;
|
|
|
394
419
|
parent_kids.push(next);
|
|
395
420
|
parent = next;
|
|
396
421
|
}
|
|
422
|
+
// read one space if exists
|
|
397
423
|
if (str.length > pos && str[pos] == ' ') {
|
|
398
424
|
pos++;
|
|
399
425
|
}
|
|
400
426
|
}
|
|
427
|
+
// read data
|
|
401
428
|
if (str.length > pos && str[pos] == '\\') {
|
|
402
429
|
var data_start = pos;
|
|
403
430
|
while (str.length > pos && str[pos] != '\n') {
|
|
@@ -408,6 +435,7 @@ var $;
|
|
|
408
435
|
parent_kids.push(next);
|
|
409
436
|
parent = next;
|
|
410
437
|
}
|
|
438
|
+
// now must be end of text
|
|
411
439
|
if (str.length === pos && stack.length > 0) {
|
|
412
440
|
const sp = span.span(row, pos - line_start + 1, 1);
|
|
413
441
|
this.$mol_fail(new this.$mol_error_syntax(`Unexpected EOF, LF required`, str.substring(line_start, str.length), sp));
|
|
@@ -424,6 +452,7 @@ var $;
|
|
|
424
452
|
"use strict";
|
|
425
453
|
var $;
|
|
426
454
|
(function ($) {
|
|
455
|
+
/** Serializes tree to string in tree format. */
|
|
427
456
|
function $mol_tree2_to_string(tree) {
|
|
428
457
|
let output = [];
|
|
429
458
|
function dump(tree, prefix = '') {
|
|
@@ -467,12 +496,25 @@ var $;
|
|
|
467
496
|
"use strict";
|
|
468
497
|
var $;
|
|
469
498
|
(function ($) {
|
|
499
|
+
/**
|
|
500
|
+
* Abstract Syntax Tree with human readable serialization.
|
|
501
|
+
* Avoid direct instantiation. Use static factories instead.
|
|
502
|
+
* @see https://github.com/nin-jin/tree.d
|
|
503
|
+
*/
|
|
470
504
|
class $mol_tree2 extends Object {
|
|
471
505
|
type;
|
|
472
506
|
value;
|
|
473
507
|
kids;
|
|
474
508
|
span;
|
|
475
|
-
constructor(
|
|
509
|
+
constructor(
|
|
510
|
+
/** Type of structural node, `value` should be empty */
|
|
511
|
+
type,
|
|
512
|
+
/** Content of data node, `type` should be empty */
|
|
513
|
+
value,
|
|
514
|
+
/** Child nodes */
|
|
515
|
+
kids,
|
|
516
|
+
/** Position in most far source resource */
|
|
517
|
+
span) {
|
|
476
518
|
super();
|
|
477
519
|
this.type = type;
|
|
478
520
|
this.value = value;
|
|
@@ -480,12 +522,15 @@ var $;
|
|
|
480
522
|
this.span = span;
|
|
481
523
|
this[Symbol.toStringTag] = type || '\\' + value;
|
|
482
524
|
}
|
|
525
|
+
/** Makes collection node. */
|
|
483
526
|
static list(kids, span = $mol_span.unknown) {
|
|
484
527
|
return new $mol_tree2('', '', kids, span);
|
|
485
528
|
}
|
|
529
|
+
/** Makes new derived collection node. */
|
|
486
530
|
list(kids) {
|
|
487
531
|
return $mol_tree2.list(kids, this.span);
|
|
488
532
|
}
|
|
533
|
+
/** Makes data node for any string. */
|
|
489
534
|
static data(value, kids = [], span = $mol_span.unknown) {
|
|
490
535
|
const chunks = value.split('\n');
|
|
491
536
|
if (chunks.length > 1) {
|
|
@@ -499,21 +544,26 @@ var $;
|
|
|
499
544
|
}
|
|
500
545
|
return new $mol_tree2('', value, kids, span);
|
|
501
546
|
}
|
|
547
|
+
/** Makes new derived data node. */
|
|
502
548
|
data(value, kids = []) {
|
|
503
549
|
return $mol_tree2.data(value, kids, this.span);
|
|
504
550
|
}
|
|
551
|
+
/** Makes struct node. */
|
|
505
552
|
static struct(type, kids = [], span = $mol_span.unknown) {
|
|
506
553
|
if (/[ \n\t\\]/.test(type)) {
|
|
507
554
|
$$.$mol_fail(span.error(`Wrong type ${JSON.stringify(type)}`));
|
|
508
555
|
}
|
|
509
556
|
return new $mol_tree2(type, '', kids, span);
|
|
510
557
|
}
|
|
558
|
+
/** Makes new derived structural node. */
|
|
511
559
|
struct(type, kids = []) {
|
|
512
560
|
return $mol_tree2.struct(type, kids, this.span);
|
|
513
561
|
}
|
|
562
|
+
/** Makes new derived node with different kids id defined. */
|
|
514
563
|
clone(kids, span = this.span) {
|
|
515
564
|
return new $mol_tree2(this.type, this.value, kids, span);
|
|
516
565
|
}
|
|
566
|
+
/** Returns multiline text content. */
|
|
517
567
|
text() {
|
|
518
568
|
var values = [];
|
|
519
569
|
for (var kid of this.kids) {
|
|
@@ -523,15 +573,20 @@ var $;
|
|
|
523
573
|
}
|
|
524
574
|
return this.value + values.join('\n');
|
|
525
575
|
}
|
|
576
|
+
/** Parses tree format. */
|
|
577
|
+
/** @deprecated Use $mol_tree2_from_string */
|
|
526
578
|
static fromString(str, uri = 'unknown') {
|
|
527
579
|
return $$.$mol_tree2_from_string(str, uri);
|
|
528
580
|
}
|
|
581
|
+
/** Serializes to tree format. */
|
|
529
582
|
toString() {
|
|
530
583
|
return $$.$mol_tree2_to_string(this);
|
|
531
584
|
}
|
|
585
|
+
/** Makes new tree with node overrided by path. */
|
|
532
586
|
insert(value, ...path) {
|
|
533
587
|
return this.update($mol_maybe(value), ...path)[0];
|
|
534
588
|
}
|
|
589
|
+
/** Makes new tree with node overrided by path. */
|
|
535
590
|
update(value, ...path) {
|
|
536
591
|
if (path.length === 0)
|
|
537
592
|
return value;
|
|
@@ -564,6 +619,7 @@ var $;
|
|
|
564
619
|
return [this.clone(kids)];
|
|
565
620
|
}
|
|
566
621
|
}
|
|
622
|
+
/** Query nodes by path. */
|
|
567
623
|
select(...path) {
|
|
568
624
|
let next = [this];
|
|
569
625
|
for (const type of path) {
|
|
@@ -590,6 +646,7 @@ var $;
|
|
|
590
646
|
}
|
|
591
647
|
return this.list(next);
|
|
592
648
|
}
|
|
649
|
+
/** Filter kids by path or value. */
|
|
593
650
|
filter(path, value) {
|
|
594
651
|
const sub = this.kids.filter(item => {
|
|
595
652
|
var found = item.select(...path);
|
|
@@ -617,9 +674,11 @@ var $;
|
|
|
617
674
|
$mol_fail_hidden(error);
|
|
618
675
|
}
|
|
619
676
|
}
|
|
677
|
+
/** Transform tree through context with transformers */
|
|
620
678
|
hack(belt, context = {}) {
|
|
621
679
|
return [].concat(...this.kids.map(child => child.hack_self(belt, context)));
|
|
622
680
|
}
|
|
681
|
+
/** Makes Error with node coordinates. */
|
|
623
682
|
error(message, Class = Error) {
|
|
624
683
|
return this.span.error(`${message}\n${this.clone([])}`, Class);
|
|
625
684
|
}
|
|
@@ -916,14 +975,18 @@ var $;
|
|
|
916
975
|
];
|
|
917
976
|
},
|
|
918
977
|
'': (input, belt) => {
|
|
978
|
+
// string
|
|
919
979
|
if (!input.type)
|
|
920
980
|
return [
|
|
921
981
|
input.data(JSON.stringify(input.text())),
|
|
922
982
|
];
|
|
983
|
+
// variable
|
|
923
984
|
if (/^[\w$#][\w0-9$]*$/i.test(input.type))
|
|
924
985
|
return [
|
|
925
986
|
input.data(input.type),
|
|
987
|
+
// ... input.hack( context ),
|
|
926
988
|
];
|
|
989
|
+
// number
|
|
927
990
|
if ($mol_tree2_js_is_number(input.type))
|
|
928
991
|
return [
|
|
929
992
|
input.data(input.type)
|
|
@@ -1248,6 +1311,7 @@ var $;
|
|
|
1248
1311
|
"use strict";
|
|
1249
1312
|
var $;
|
|
1250
1313
|
(function ($) {
|
|
1314
|
+
/** Makes JSON from json.tree. */
|
|
1251
1315
|
function $mol_tree2_to_json(tree) {
|
|
1252
1316
|
if (!tree.type) {
|
|
1253
1317
|
if (tree.kids.every(kid => !kid.type))
|