mol_mutable 0.0.60 → 0.0.62

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.test.js CHANGED
@@ -307,7 +307,8 @@ var $;
307
307
  return function $mol_log3_logger(event) {
308
308
  if (!event.time)
309
309
  event = { time: new Date().toISOString(), ...event };
310
- const tree = this.$mol_tree.fromJSON(event).clone({ type });
310
+ let tree = this.$mol_tree2_from_json(event);
311
+ tree = tree.struct(type, tree.kids);
311
312
  let str = color(tree.toString());
312
313
  this.console[level](str);
313
314
  const self = this;
@@ -1240,248 +1241,219 @@ var $;
1240
1241
  "use strict";
1241
1242
  var $;
1242
1243
  (function ($) {
1243
- $.$mol_tree_convert = Symbol('$mol_tree_convert');
1244
- class $mol_tree extends $mol_object2 {
1245
- type;
1246
- data;
1247
- sub;
1248
- baseUri;
1244
+ class $mol_span extends $mol_object2 {
1245
+ uri;
1246
+ source;
1249
1247
  row;
1250
1248
  col;
1251
1249
  length;
1252
- constructor(config = {}) {
1250
+ constructor(uri, source, row, col, length) {
1253
1251
  super();
1254
- this.type = config.type || '';
1255
- if (config.value !== undefined) {
1256
- var sub = $mol_tree.values(config.value);
1257
- if (config.type || sub.length > 1) {
1258
- this.sub = [...sub, ...(config.sub || [])];
1259
- this.data = config.data || '';
1260
- }
1261
- else {
1262
- this.data = sub[0].data;
1263
- this.sub = config.sub || [];
1264
- }
1265
- }
1266
- else {
1267
- this.data = config.data || '';
1268
- this.sub = config.sub || [];
1269
- }
1270
- this.baseUri = config.baseUri || '';
1271
- this.row = config.row || 0;
1272
- this.col = config.col || 0;
1273
- this.length = config.length || 0;
1274
- }
1275
- static values(str, baseUri) {
1276
- return str.split('\n').map((data, index) => new $mol_tree({
1277
- data: data,
1278
- baseUri: baseUri,
1279
- row: index + 1,
1280
- length: data.length,
1281
- }));
1252
+ this.uri = uri;
1253
+ this.source = source;
1254
+ this.row = row;
1255
+ this.col = col;
1256
+ this.length = length;
1257
+ this[Symbol.toStringTag] = `${this.uri}#${this.row}:${this.col}/${this.length}`;
1282
1258
  }
1283
- clone(config = {}) {
1284
- return new $mol_tree({
1285
- type: ('type' in config) ? config.type : this.type,
1286
- data: ('data' in config) ? config.data : this.data,
1287
- sub: ('sub' in config) ? config.sub : this.sub,
1288
- baseUri: ('baseUri' in config) ? config.baseUri : this.baseUri,
1289
- row: ('row' in config) ? config.row : this.row,
1290
- col: ('col' in config) ? config.col : this.col,
1291
- length: ('length' in config) ? config.length : this.length,
1292
- value: config.value
1293
- });
1259
+ static unknown = $mol_span.begin('?');
1260
+ static begin(uri, source = '') {
1261
+ return new $mol_span(uri, source, 1, 1, 0);
1262
+ }
1263
+ static end(uri, source) {
1264
+ return new $mol_span(uri, source, 1, source.length + 1, length);
1294
1265
  }
1295
- make(config) {
1296
- return new $mol_tree({
1297
- baseUri: this.baseUri,
1266
+ static entire(uri, source) {
1267
+ return new $mol_span(uri, source, 1, 1, source.length);
1268
+ }
1269
+ toString() {
1270
+ return this[Symbol.toStringTag];
1271
+ }
1272
+ toJSON() {
1273
+ return {
1274
+ uri: this.uri,
1298
1275
  row: this.row,
1299
1276
  col: this.col,
1300
- length: this.length,
1301
- ...config,
1302
- });
1277
+ length: this.length
1278
+ };
1303
1279
  }
1304
- make_data(value, sub) {
1305
- return this.make({ value, sub });
1306
- }
1307
- make_struct(type, sub) {
1308
- return this.make({ type, sub });
1309
- }
1310
- static fromString(str, baseUri) {
1311
- var root = new $mol_tree({ baseUri: baseUri });
1312
- var stack = [root];
1313
- var row = 0;
1314
- var prefix = str.replace(/^\n?(\t*)[\s\S]*/, '$1');
1315
- var lines = str.replace(new RegExp('^\\t{0,' + prefix.length + '}', 'mg'), '').split('\n');
1316
- lines.forEach(line => {
1317
- ++row;
1318
- var chunks = /^(\t*)((?:[^\n\t\\ ]+ *)*)(\\[^\n]*)?(.*?)(?:$|\n)/m.exec(line);
1319
- if (!chunks || chunks[4])
1320
- return this.$.$mol_fail(new Error(`Syntax error at ${baseUri}:${row}\n${line}`));
1321
- var indent = chunks[1];
1322
- var path = chunks[2];
1323
- var data = chunks[3];
1324
- var deep = indent.length;
1325
- var types = path ? path.replace(/ $/, '').split(/ +/) : [];
1326
- if (stack.length <= deep)
1327
- return this.$.$mol_fail(new Error(`Too many tabs at ${baseUri}:${row}\n${line}`));
1328
- stack.length = deep + 1;
1329
- var parent = stack[deep];
1330
- let col = deep;
1331
- types.forEach(type => {
1332
- if (!type)
1333
- return this.$.$mol_fail(new Error(`Unexpected space symbol ${baseUri}:${row}\n${line}`));
1334
- var next = new $mol_tree({ type, baseUri, row, col, length: type.length });
1335
- const parent_sub = parent.sub;
1336
- parent_sub.push(next);
1337
- parent = next;
1338
- col += type.length + 1;
1339
- });
1340
- if (data) {
1341
- var next = new $mol_tree({ data: data.substring(1), baseUri, row, col, length: data.length });
1342
- const parent_sub = parent.sub;
1343
- parent_sub.push(next);
1344
- parent = next;
1345
- }
1346
- stack.push(parent);
1347
- });
1348
- return root;
1349
- }
1350
- static fromJSON(json, baseUri = '') {
1351
- switch (true) {
1352
- case typeof json === 'boolean':
1353
- case typeof json === 'number':
1354
- case json === null:
1355
- return new $mol_tree({
1356
- type: String(json),
1357
- baseUri: baseUri
1358
- });
1359
- case typeof json === 'string':
1360
- return new $mol_tree({
1361
- value: json,
1362
- baseUri: baseUri
1363
- });
1364
- case Array.isArray(json):
1365
- return new $mol_tree({
1366
- type: "/",
1367
- sub: json.map(json => $mol_tree.fromJSON(json, baseUri))
1368
- });
1369
- case json instanceof Date:
1370
- return new $mol_tree({
1371
- value: json.toISOString(),
1372
- baseUri: baseUri
1373
- });
1374
- default:
1375
- if (typeof json[$.$mol_tree_convert] === 'function') {
1376
- return json[$.$mol_tree_convert]();
1377
- }
1378
- if (typeof json.toJSON === 'function') {
1379
- return $mol_tree.fromJSON(json.toJSON());
1380
- }
1381
- if (json instanceof Error) {
1382
- const { name, message, stack } = json;
1383
- json = { ...json, name, message, stack };
1384
- }
1385
- var sub = [];
1386
- for (var key in json) {
1387
- if (json[key] === undefined)
1388
- continue;
1389
- const subsub = $mol_tree.fromJSON(json[key], baseUri);
1390
- if (/^[^\n\t\\ ]+$/.test(key)) {
1391
- var child = new $mol_tree({
1392
- type: key,
1393
- baseUri: baseUri,
1394
- sub: [subsub],
1395
- });
1396
- }
1397
- else {
1398
- var child = new $mol_tree({
1399
- value: key,
1400
- baseUri: baseUri,
1401
- sub: [subsub],
1402
- });
1403
- }
1404
- sub.push(child);
1405
- }
1406
- return new $mol_tree({
1407
- type: "*",
1408
- sub: sub,
1409
- baseUri: baseUri
1410
- });
1411
- }
1280
+ error(message, Class = Error) {
1281
+ return new Class(`${message}${this}`);
1282
+ }
1283
+ span(row, col, length) {
1284
+ return new $mol_span(this.uri, this.source, row, col, length);
1412
1285
  }
1413
- get uri() {
1414
- return this.baseUri + '#' + this.row + ':' + this.col;
1286
+ after(length = 0) {
1287
+ return new $mol_span(this.uri, this.source, this.row, this.col + this.length, length);
1288
+ }
1289
+ slice(begin, end = -1) {
1290
+ let len = this.length;
1291
+ if (begin < 0)
1292
+ begin += len;
1293
+ if (end < 0)
1294
+ end += len;
1295
+ if (begin < 0 || begin > len)
1296
+ this.$.$mol_fail(`Begin value '${begin}' out of range ${this}`);
1297
+ if (end < 0 || end > len)
1298
+ this.$.$mol_fail(`End value '${end}' out of range ${this}`);
1299
+ if (end < begin)
1300
+ this.$.$mol_fail(`End value '${end}' can't be less than begin value ${this}`);
1301
+ return this.span(this.row, this.col + begin, end - begin);
1302
+ }
1303
+ }
1304
+ $.$mol_span = $mol_span;
1305
+ })($ || ($ = {}));
1306
+ //mol/span/span.ts
1307
+ ;
1308
+ "use strict";
1309
+ var $;
1310
+ (function ($_1) {
1311
+ $mol_test({
1312
+ 'span for same uri'($) {
1313
+ const span = new $mol_span('test.ts', '', 1, 3, 4);
1314
+ const child = span.span(4, 5, 8);
1315
+ $mol_assert_equal(child.uri, 'test.ts');
1316
+ $mol_assert_equal(child.row, 4);
1317
+ $mol_assert_equal(child.col, 5);
1318
+ $mol_assert_equal(child.length, 8);
1319
+ },
1320
+ 'span after of given position'($) {
1321
+ const span = new $mol_span('test.ts', '', 1, 3, 4);
1322
+ const child = span.after(11);
1323
+ $mol_assert_equal(child.uri, 'test.ts');
1324
+ $mol_assert_equal(child.row, 1);
1325
+ $mol_assert_equal(child.col, 7);
1326
+ $mol_assert_equal(child.length, 11);
1327
+ },
1328
+ 'slice span - regular'($) {
1329
+ const span = new $mol_span('test.ts', '', 1, 3, 5);
1330
+ const child = span.slice(1, 4);
1331
+ $mol_assert_equal(child.row, 1);
1332
+ $mol_assert_equal(child.col, 4);
1333
+ $mol_assert_equal(child.length, 3);
1334
+ const child2 = span.slice(2, 2);
1335
+ $mol_assert_equal(child2.col, 5);
1336
+ $mol_assert_equal(child2.length, 0);
1337
+ },
1338
+ 'slice span - negative'($) {
1339
+ const span = new $mol_span('test.ts', '', 1, 3, 5);
1340
+ const child = span.slice(-3, -1);
1341
+ $mol_assert_equal(child.row, 1);
1342
+ $mol_assert_equal(child.col, 5);
1343
+ $mol_assert_equal(child.length, 2);
1344
+ },
1345
+ 'slice span - out of range'($) {
1346
+ const span = new $mol_span('test.ts', '', 1, 3, 5);
1347
+ $mol_assert_fail(() => span.slice(-1, 3));
1348
+ $mol_assert_fail(() => span.slice(1, 6));
1349
+ $mol_assert_fail(() => span.slice(1, 10));
1350
+ },
1351
+ 'error handling'($) {
1352
+ const span = new $mol_span('test.ts', '', 1, 3, 4);
1353
+ const error = span.error('Some error\n');
1354
+ $mol_assert_equal(error.message, 'Some error\ntest.ts#1:3/4');
1415
1355
  }
1416
- toString(prefix = '') {
1417
- var output = '';
1418
- if (this.type.length) {
1356
+ });
1357
+ })($ || ($ = {}));
1358
+ //mol/span/span.test.ts
1359
+ ;
1360
+ "use strict";
1361
+ var $;
1362
+ (function ($) {
1363
+ function $mol_tree2_to_string(tree) {
1364
+ let output = [];
1365
+ function dump(tree, prefix = '') {
1366
+ if (tree.type.length) {
1419
1367
  if (!prefix.length) {
1420
1368
  prefix = "\t";
1421
1369
  }
1422
- output += this.type;
1423
- if (this.sub.length == 1) {
1424
- return output + ' ' + this.sub[0].toString(prefix);
1370
+ output.push(tree.type);
1371
+ if (tree.kids.length == 1) {
1372
+ output.push(' ');
1373
+ dump(tree.kids[0], prefix);
1374
+ return;
1425
1375
  }
1426
- output += "\n";
1376
+ output.push("\n");
1427
1377
  }
1428
- else if (this.data.length || prefix.length) {
1429
- output += "\\" + this.data + "\n";
1378
+ else if (tree.value.length || prefix.length) {
1379
+ output.push("\\" + tree.value + "\n");
1430
1380
  }
1431
- for (var child of this.sub) {
1432
- output += prefix;
1433
- output += child.toString(prefix + "\t");
1381
+ for (const kid of tree.kids) {
1382
+ output.push(prefix);
1383
+ dump(kid, prefix + "\t");
1434
1384
  }
1435
- return output;
1436
1385
  }
1437
- toJSON() {
1438
- if (!this.type)
1439
- return this.value;
1440
- if (this.type === 'true')
1441
- return true;
1442
- if (this.type === 'false')
1443
- return false;
1444
- if (this.type === 'null')
1445
- return null;
1446
- if (this.type === '*') {
1447
- var obj = {};
1448
- for (var child of this.sub) {
1449
- if (child.type === '-')
1450
- continue;
1451
- var key = child.type || child.clone({ sub: child.sub.slice(0, child.sub.length - 1) }).value;
1452
- var val = child.sub[child.sub.length - 1].toJSON();
1453
- if (val !== undefined)
1454
- obj[key] = val;
1455
- }
1456
- return obj;
1457
- }
1458
- if (this.type === '/') {
1459
- var res = [];
1460
- this.sub.forEach(child => {
1461
- if (child.type === '-')
1462
- return;
1463
- var val = child.toJSON();
1464
- if (val !== undefined)
1465
- res.push(val);
1386
+ dump(tree);
1387
+ return output.join('');
1388
+ }
1389
+ $.$mol_tree2_to_string = $mol_tree2_to_string;
1390
+ })($ || ($ = {}));
1391
+ //mol/tree2/to/string/string.ts
1392
+ ;
1393
+ "use strict";
1394
+ var $;
1395
+ (function ($) {
1396
+ class $mol_tree2 extends Object {
1397
+ type;
1398
+ value;
1399
+ kids;
1400
+ span;
1401
+ constructor(type, value, kids, span) {
1402
+ super();
1403
+ this.type = type;
1404
+ this.value = value;
1405
+ this.kids = kids;
1406
+ this.span = span;
1407
+ this[Symbol.toStringTag] = type || '\\' + value;
1408
+ }
1409
+ static list(kids, span = $mol_span.unknown) {
1410
+ return new $mol_tree2('', '', kids, span);
1411
+ }
1412
+ list(kids) {
1413
+ return $mol_tree2.list(kids, this.span);
1414
+ }
1415
+ static data(value, kids = [], span = $mol_span.unknown) {
1416
+ const chunks = value.split('\n');
1417
+ if (chunks.length > 1) {
1418
+ let kid_span = span.span(span.row, span.col, 0);
1419
+ const data = chunks.map(chunk => {
1420
+ kid_span = kid_span.after(chunk.length);
1421
+ return new $mol_tree2('', chunk, [], kid_span);
1466
1422
  });
1467
- return res;
1423
+ kids = [...data, ...kids];
1424
+ value = '';
1468
1425
  }
1469
- if (this.type === 'time') {
1470
- return new Date(this.value);
1426
+ return new $mol_tree2('', value, kids, span);
1427
+ }
1428
+ data(value, kids = []) {
1429
+ return $mol_tree2.data(value, kids, this.span);
1430
+ }
1431
+ static struct(type, kids = [], span = $mol_span.unknown) {
1432
+ if (/[ \n\t\\]/.test(type)) {
1433
+ $$.$mol_fail(span.error(`Wrong type ${JSON.stringify(type)}`));
1471
1434
  }
1472
- const numb = Number(this.type);
1473
- if (!Number.isNaN(numb) || this.type === 'NaN')
1474
- return numb;
1475
- throw new Error(`Unknown type (${this.type}) at ${this.uri}`);
1435
+ return new $mol_tree2(type, '', kids, span);
1436
+ }
1437
+ struct(type, kids = []) {
1438
+ return $mol_tree2.struct(type, kids, this.span);
1439
+ }
1440
+ clone(kids, span = this.span) {
1441
+ return new $mol_tree2(this.type, this.value, kids, span);
1476
1442
  }
1477
- get value() {
1443
+ text() {
1478
1444
  var values = [];
1479
- for (var child of this.sub) {
1480
- if (child.type)
1445
+ for (var kid of this.kids) {
1446
+ if (kid.type)
1481
1447
  continue;
1482
- values.push(child.value);
1448
+ values.push(kid.value);
1483
1449
  }
1484
- return this.data + values.join("\n");
1450
+ return this.value + values.join('\n');
1451
+ }
1452
+ static fromString(str, uri = 'unknown') {
1453
+ return $$.$mol_tree2_from_string(str, uri);
1454
+ }
1455
+ toString() {
1456
+ return $$.$mol_tree2_to_string(this);
1485
1457
  }
1486
1458
  insert(value, ...path) {
1487
1459
  if (path.length === 0)
@@ -1489,143 +1461,375 @@ var $;
1489
1461
  const type = path[0];
1490
1462
  if (typeof type === 'string') {
1491
1463
  let replaced = false;
1492
- const sub = this.sub.map((item, index) => {
1464
+ const sub = this.kids.map((item, index) => {
1493
1465
  if (item.type !== type)
1494
1466
  return item;
1495
1467
  replaced = true;
1496
1468
  return item.insert(value, ...path.slice(1));
1497
- });
1498
- if (!replaced)
1499
- sub.push(new $mol_tree({ type }).insert(value, ...path.slice(1)));
1500
- return this.clone({ sub });
1469
+ }).filter(Boolean);
1470
+ if (!replaced && value) {
1471
+ sub.push(this.struct(type, []).insert(value, ...path.slice(1)));
1472
+ }
1473
+ return this.clone(sub);
1501
1474
  }
1502
1475
  else if (typeof type === 'number') {
1503
- const sub = this.sub.slice();
1504
- sub[type] = (sub[type] || new $mol_tree).insert(value, ...path.slice(1));
1505
- return this.clone({ sub });
1476
+ const sub = this.kids.slice();
1477
+ sub[type] = (sub[type] || this.list([]))
1478
+ .insert(value, ...path.slice(1));
1479
+ return this.clone(sub.filter(Boolean));
1506
1480
  }
1507
1481
  else {
1508
- return this.clone({ sub: ((this.sub.length === 0) ? [new $mol_tree()] : this.sub).map(item => item.insert(value, ...path.slice(1))) });
1482
+ const kids = ((this.kids.length === 0) ? [this.list([])] : this.kids)
1483
+ .map(item => item.insert(value, ...path.slice(1)))
1484
+ .filter(Boolean);
1485
+ return this.clone(kids);
1509
1486
  }
1510
1487
  }
1511
1488
  select(...path) {
1512
- var next = [this];
1513
- for (var type of path) {
1489
+ let next = [this];
1490
+ for (const type of path) {
1514
1491
  if (!next.length)
1515
1492
  break;
1516
- var prev = next;
1493
+ const prev = next;
1517
1494
  next = [];
1518
1495
  for (var item of prev) {
1519
1496
  switch (typeof (type)) {
1520
1497
  case 'string':
1521
- for (var child of item.sub) {
1522
- if (!type || (child.type == type)) {
1498
+ for (var child of item.kids) {
1499
+ if (child.type == type) {
1523
1500
  next.push(child);
1524
1501
  }
1525
1502
  }
1526
1503
  break;
1527
1504
  case 'number':
1528
- if (type < item.sub.length)
1529
- next.push(item.sub[type]);
1505
+ if (type < item.kids.length)
1506
+ next.push(item.kids[type]);
1530
1507
  break;
1531
- default: next.push(...item.sub);
1508
+ default: next.push(...item.kids);
1532
1509
  }
1533
1510
  }
1534
1511
  }
1535
- return new $mol_tree({ sub: next });
1512
+ return this.list(next);
1536
1513
  }
1537
1514
  filter(path, value) {
1538
- var sub = this.sub.filter(function (item) {
1515
+ const sub = this.kids.filter(item => {
1539
1516
  var found = item.select(...path);
1540
- if (value == null) {
1541
- return Boolean(found.sub.length);
1517
+ if (value === undefined) {
1518
+ return Boolean(found.kids.length);
1542
1519
  }
1543
1520
  else {
1544
- return found.sub.some(child => child.value == value);
1521
+ return found.kids.some(child => child.value == value);
1545
1522
  }
1546
1523
  });
1547
- return new $mol_tree({ sub: sub });
1548
- }
1549
- transform(visit, stack = []) {
1550
- const sub_stack = [this, ...stack];
1551
- return visit(sub_stack, () => this.sub.map(node => node.transform(visit, sub_stack)).filter(n => n));
1552
- }
1553
- hack(context) {
1554
- const sub = [].concat(...this.sub.map(child => {
1555
- const handle = context[child.type] || context[''];
1556
- if (!handle)
1557
- $mol_fail(child.error('Handler not defined'));
1558
- return handle(child, context);
1524
+ return this.clone(sub);
1525
+ }
1526
+ hack(belt, context = {}) {
1527
+ return [].concat(...this.kids.map(child => {
1528
+ let handle = belt[child.type] || belt[''];
1529
+ if (!handle || handle === Object.prototype[child.type]) {
1530
+ handle = (input, belt, context) => [
1531
+ input.clone(input.hack(belt, context), context.span)
1532
+ ];
1533
+ }
1534
+ try {
1535
+ return handle(child, belt, context);
1536
+ }
1537
+ catch (error) {
1538
+ error.message += `\n${child.clone([])}${child.span}`;
1539
+ $mol_fail_hidden(error);
1540
+ }
1559
1541
  }));
1560
- return this.clone({ sub });
1561
1542
  }
1562
- error(message) {
1563
- return new Error(`${message}:\n${this} ${this.baseUri}:${this.row}:${this.col}`);
1543
+ error(message, Class = Error) {
1544
+ return this.span.error(`${message}\n${this.clone([])}`, Class);
1545
+ }
1546
+ }
1547
+ $.$mol_tree2 = $mol_tree2;
1548
+ class $mol_tree2_empty extends $mol_tree2 {
1549
+ constructor() {
1550
+ super('', '', [], $mol_span.unknown);
1564
1551
  }
1565
1552
  }
1566
- $.$mol_tree = $mol_tree;
1553
+ $.$mol_tree2_empty = $mol_tree2_empty;
1567
1554
  })($ || ($ = {}));
1568
- //mol/tree/tree.ts
1555
+ //mol/tree2/tree2.ts
1569
1556
  ;
1570
1557
  "use strict";
1571
1558
  var $;
1572
1559
  (function ($_1) {
1573
1560
  $mol_test({
1574
- 'tree parsing'() {
1575
- $mol_assert_equal($mol_tree.fromString("foo\nbar\n").sub.length, 2);
1576
- $mol_assert_equal($mol_tree.fromString("foo\nbar\n").sub[1].type, "bar");
1577
- $mol_assert_equal($mol_tree.fromString("foo\n\n\n").sub.length, 1);
1578
- $mol_assert_equal($mol_tree.fromString("=foo\n\\bar\n").sub.length, 2);
1579
- $mol_assert_equal($mol_tree.fromString("=foo\n\\bar\n").sub[1].data, "bar");
1580
- $mol_assert_equal($mol_tree.fromString("foo bar \\pol").sub[0].sub[0].sub[0].data, "pol");
1581
- $mol_assert_equal($mol_tree.fromString("foo bar\n\t\\pol\n\t\\men").sub[0].sub[0].sub[1].data, "men");
1582
- $mol_assert_equal($mol_tree.fromString('foo bar \\text\n').toString(), 'foo bar \\text\n');
1583
- },
1584
- 'inserting'() {
1585
- $mol_assert_equal($mol_tree.fromString('a b c d').insert(new $mol_tree, 'a', 'b', 'c').toString(), 'a b \\\n');
1586
- $mol_assert_equal($mol_tree.fromString('a b').insert(new $mol_tree, 'a', 'b', 'c', 'd').toString(), 'a b c \\\n');
1587
- $mol_assert_equal($mol_tree.fromString('a b c d').insert(new $mol_tree, 0, 0, 0).toString(), 'a b \\\n');
1588
- $mol_assert_equal($mol_tree.fromString('a b').insert(new $mol_tree, 0, 0, 0, 0).toString(), 'a b \\\n\t\\\n');
1589
- $mol_assert_equal($mol_tree.fromString('a b c d').insert(new $mol_tree, null, null, null).toString(), 'a b \\\n');
1590
- $mol_assert_equal($mol_tree.fromString('a b').insert(new $mol_tree, null, null, null, null).toString(), 'a b \\\n\t\\\n');
1561
+ 'inserting'($) {
1562
+ $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
1563
+ .insert($mol_tree2.struct('x'), 'a', 'b', 'c')
1564
+ .toString(), 'a b x\n');
1565
+ $mol_assert_equal($.$mol_tree2_from_string('a b\n')
1566
+ .insert($mol_tree2.struct('x'), 'a', 'b', 'c', 'd')
1567
+ .toString(), 'a b c x\n');
1568
+ $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
1569
+ .insert($mol_tree2.struct('x'), 0, 0, 0)
1570
+ .toString(), 'a b x\n');
1571
+ $mol_assert_equal($.$mol_tree2_from_string('a b\n')
1572
+ .insert($mol_tree2.struct('x'), 0, 0, 0, 0)
1573
+ .toString(), 'a b \\\n\tx\n');
1574
+ $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
1575
+ .insert($mol_tree2.struct('x'), null, null, null)
1576
+ .toString(), 'a b x\n');
1577
+ $mol_assert_equal($.$mol_tree2_from_string('a b\n')
1578
+ .insert($mol_tree2.struct('x'), null, null, null, null)
1579
+ .toString(), 'a b \\\n\tx\n');
1591
1580
  },
1592
- 'fromJSON'() {
1593
- $mol_assert_equal($mol_tree.fromJSON([]).toString(), '/\n');
1594
- $mol_assert_equal($mol_tree.fromJSON([false, true]).toString(), '/\n\tfalse\n\ttrue\n');
1595
- $mol_assert_equal($mol_tree.fromJSON([0, 1, 2.3]).toString(), '/\n\t0\n\t1\n\t2.3\n');
1596
- $mol_assert_equal($mol_tree.fromJSON(['', 'foo', 'bar\nbaz']).toString(), '/\n\t\\\n\t\\foo\n\t\\\n\t\t\\bar\n\t\t\\baz\n');
1597
- $mol_assert_equal($mol_tree.fromJSON({ 'foo': false, 'bar\nbaz': 'lol' }).toString(), '*\n\tfoo false\n\t\\\n\t\t\\bar\n\t\t\\baz\n\t\t\\lol\n');
1598
- },
1599
- 'toJSON'() {
1600
- $mol_assert_equal(JSON.stringify($mol_tree.fromString('/\n').sub[0]), '[]');
1601
- $mol_assert_equal(JSON.stringify($mol_tree.fromString('/\n\tfalse\n\ttrue\n').sub[0]), '[false,true]');
1602
- $mol_assert_equal(JSON.stringify($mol_tree.fromString('/\n\t0\n\t1\n\t2.3\n').sub[0]), '[0,1,2.3]');
1603
- $mol_assert_equal(JSON.stringify($mol_tree.fromString('/\n\t\\\n\t\\foo\n\t\\\n\t\t\\bar\n\t\t\\baz\n').sub[0]), '["","foo","bar\\nbaz"]');
1604
- $mol_assert_equal(JSON.stringify($mol_tree.fromString('*\n\tfoo false\n\t\\\n\t\t\\bar\n\t\t\\baz\n\t\t\\lol\n').sub[0]), '{"foo":false,"bar\\nbaz":"lol"}');
1605
- },
1606
- 'hack'() {
1607
- const res = $mol_tree.fromString(`foo bar xxx`).hack({
1608
- '': (tree, context) => [tree.hack(context)],
1609
- 'bar': (tree, context) => [tree.hack(context).clone({ type: '777' })],
1581
+ 'deleting'($) {
1582
+ $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
1583
+ .insert(null, 'a', 'b', 'c')
1584
+ .toString(), 'a b\n');
1585
+ $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
1586
+ .insert(null, 0, 0, 0)
1587
+ .toString(), 'a b\n');
1588
+ },
1589
+ 'hack'($) {
1590
+ const res = $.$mol_tree2_from_string(`foo bar xxx\n`)
1591
+ .hack({
1592
+ 'bar': (input, belt) => [input.struct('777', input.hack(belt))],
1610
1593
  });
1611
- $mol_assert_equal(res.toString(), new $mol_tree({ type: 'foo 777 xxx' }).toString());
1594
+ $mol_assert_equal(res.toString(), 'foo 777 xxx\n');
1595
+ },
1596
+ });
1597
+ })($ || ($ = {}));
1598
+ //mol/tree2/tree2.test.ts
1599
+ ;
1600
+ "use strict";
1601
+ var $;
1602
+ (function ($) {
1603
+ class $mol_error_syntax extends SyntaxError {
1604
+ reason;
1605
+ line;
1606
+ span;
1607
+ constructor(reason, line, span) {
1608
+ super(`${reason}\n${span}\n${line.substring(0, span.col - 1).replace(/\S/g, ' ')}${''.padEnd(span.length, '!')}\n${line}`);
1609
+ this.reason = reason;
1610
+ this.line = line;
1611
+ this.span = span;
1612
+ }
1613
+ }
1614
+ $.$mol_error_syntax = $mol_error_syntax;
1615
+ })($ || ($ = {}));
1616
+ //mol/error/syntax/syntax.ts
1617
+ ;
1618
+ "use strict";
1619
+ var $;
1620
+ (function ($) {
1621
+ function $mol_tree2_from_string(str, uri = '?') {
1622
+ const span = $mol_span.entire(uri, str);
1623
+ var root = $mol_tree2.list([], span);
1624
+ var stack = [root];
1625
+ var pos = 0, row = 0, min_indent = 0;
1626
+ while (str.length > pos) {
1627
+ var indent = 0;
1628
+ var line_start = pos;
1629
+ row++;
1630
+ while (str.length > pos && str[pos] == '\t') {
1631
+ indent++;
1632
+ pos++;
1633
+ }
1634
+ if (!root.kids.length) {
1635
+ min_indent = indent;
1636
+ }
1637
+ indent -= min_indent;
1638
+ if (indent < 0 || indent >= stack.length) {
1639
+ const sp = span.span(row, 1, pos - line_start);
1640
+ while (str.length > pos && str[pos] != '\n') {
1641
+ pos++;
1642
+ }
1643
+ if (indent < 0) {
1644
+ if (str.length > pos) {
1645
+ this.$mol_fail(new this.$mol_error_syntax(`Too few tabs`, str.substring(line_start, pos), sp));
1646
+ }
1647
+ }
1648
+ else {
1649
+ this.$mol_fail(new this.$mol_error_syntax(`Too many tabs`, str.substring(line_start, pos), sp));
1650
+ }
1651
+ }
1652
+ stack.length = indent + 1;
1653
+ var parent = stack[indent];
1654
+ while (str.length > pos && str[pos] != '\\' && str[pos] != '\n') {
1655
+ var error_start = pos;
1656
+ while (str.length > pos && (str[pos] == ' ' || str[pos] == '\t')) {
1657
+ pos++;
1658
+ }
1659
+ if (pos > error_start) {
1660
+ let line_end = str.indexOf('\n', pos);
1661
+ if (line_end === -1)
1662
+ line_end = str.length;
1663
+ const sp = span.span(row, error_start - line_start, pos - error_start + 1);
1664
+ this.$mol_fail(new this.$mol_error_syntax(`Wrong nodes separator`, str.substring(line_start, line_end), sp));
1665
+ }
1666
+ var type_start = pos;
1667
+ while (str.length > pos &&
1668
+ str[pos] != '\\' &&
1669
+ str[pos] != ' ' &&
1670
+ str[pos] != '\t' &&
1671
+ str[pos] != '\n') {
1672
+ pos++;
1673
+ }
1674
+ if (pos > type_start) {
1675
+ let next = new $mol_tree2(str.slice(type_start, pos), '', [], span.span(row, type_start - line_start + 1, pos - type_start));
1676
+ const parent_kids = parent.kids;
1677
+ parent_kids.push(next);
1678
+ parent = next;
1679
+ }
1680
+ if (str.length > pos && str[pos] == ' ') {
1681
+ pos++;
1682
+ }
1683
+ }
1684
+ if (str.length > pos && str[pos] == '\\') {
1685
+ var data_start = pos;
1686
+ while (str.length > pos && str[pos] != '\n') {
1687
+ pos++;
1688
+ }
1689
+ let next = new $mol_tree2('', str.slice(data_start + 1, pos), [], span.span(row, data_start - line_start + 2, pos - data_start - 1));
1690
+ const parent_kids = parent.kids;
1691
+ parent_kids.push(next);
1692
+ parent = next;
1693
+ }
1694
+ if (str.length === pos && stack.length > 0) {
1695
+ const sp = span.span(row, pos - line_start + 1, 1);
1696
+ this.$mol_fail(new this.$mol_error_syntax(`Undexpected EOF, LF required`, str.substring(line_start, str.length), sp));
1697
+ }
1698
+ stack.push(parent);
1699
+ pos++;
1700
+ }
1701
+ return root;
1702
+ }
1703
+ $.$mol_tree2_from_string = $mol_tree2_from_string;
1704
+ })($ || ($ = {}));
1705
+ //mol/tree2/from/string/string.ts
1706
+ ;
1707
+ "use strict";
1708
+ var $;
1709
+ (function ($_1) {
1710
+ $mol_test({
1711
+ 'tree parsing'($) {
1712
+ $mol_assert_equal($.$mol_tree2_from_string("foo\nbar\n").kids.length, 2);
1713
+ $mol_assert_equal($.$mol_tree2_from_string("foo\nbar\n").kids[1].type, "bar");
1714
+ $mol_assert_equal($.$mol_tree2_from_string("foo\n\n\n").kids.length, 1);
1715
+ $mol_assert_equal($.$mol_tree2_from_string("=foo\n\\bar\n").kids.length, 2);
1716
+ $mol_assert_equal($.$mol_tree2_from_string("=foo\n\\bar\n").kids[1].value, "bar");
1717
+ $mol_assert_equal($.$mol_tree2_from_string("foo bar \\pol\n").kids[0].kids[0].kids[0].value, "pol");
1718
+ $mol_assert_equal($.$mol_tree2_from_string("foo bar\n\t\\pol\n\t\\men\n").kids[0].kids[0].kids[1].value, "men");
1719
+ $mol_assert_equal($.$mol_tree2_from_string('foo bar \\text\n').toString(), 'foo bar \\text\n');
1720
+ },
1721
+ 'Too many tabs'($) {
1722
+ const tree = `
1723
+ foo
1724
+ bar
1725
+ `;
1726
+ $mol_assert_fail(() => {
1727
+ $.$mol_tree2_from_string(tree, 'test');
1728
+ }, 'Too many tabs\ntest#3:1/6\n!!!!!!\n\t\t\t\t\t\tbar');
1729
+ },
1730
+ 'Too few tabs'($) {
1731
+ const tree = `
1732
+ foo
1733
+ bar
1734
+ `;
1735
+ $mol_assert_fail(() => {
1736
+ $.$mol_tree2_from_string(tree, 'test');
1737
+ }, 'Too few tabs\ntest#3:1/4\n!!!!\n\t\t\t\tbar');
1738
+ },
1739
+ 'Wrong nodes separator'($) {
1740
+ const tree = `foo bar\n`;
1741
+ $mol_assert_fail(() => {
1742
+ $.$mol_tree2_from_string(tree, 'test');
1743
+ }, 'Wrong nodes separator\ntest#1:4/2\n !!\nfoo bar');
1744
+ },
1745
+ 'Undexpected EOF, LF required'($) {
1746
+ const tree = ` foo`;
1747
+ $mol_assert_fail(() => {
1748
+ $.$mol_tree2_from_string(tree, 'test');
1749
+ }, 'Undexpected EOF, LF required\ntest#1:5/1\n !\n foo');
1612
1750
  },
1613
- 'errors handling'($) {
1751
+ 'Errors skip and collect'($) {
1752
+ const tree = `foo bar`;
1614
1753
  const errors = [];
1615
- class Tree extends $mol_tree {
1616
- static $ = $.$mol_ambient({
1617
- $mol_fail: error => errors.push(error.message)
1618
- });
1754
+ const $$ = $.$mol_ambient({
1755
+ $mol_fail: (error) => {
1756
+ errors.push(error.message);
1757
+ return null;
1758
+ }
1759
+ });
1760
+ const res = $$.$mol_tree2_from_string(tree, 'test');
1761
+ $mol_assert_like(errors, [
1762
+ 'Wrong nodes separator\ntest#1:4/2\n !!\nfoo bar',
1763
+ 'Undexpected EOF, LF required\ntest#1:9/1\n !\nfoo bar',
1764
+ ]);
1765
+ $mol_assert_equal(res.toString(), 'foo bar\n');
1766
+ },
1767
+ });
1768
+ })($ || ($ = {}));
1769
+ //mol/tree2/from/string/string.test.ts
1770
+ ;
1771
+ "use strict";
1772
+ var $;
1773
+ (function ($) {
1774
+ function $mol_tree2_from_json(json, span = $mol_span.unknown) {
1775
+ if (typeof json === 'boolean' || typeof json === 'number' || json === null) {
1776
+ return new $mol_tree2(String(json), '', [], span);
1777
+ }
1778
+ if (typeof json === 'string') {
1779
+ return $mol_tree2.data(json, [], span);
1780
+ }
1781
+ if (Array.isArray(json)) {
1782
+ const sub = json.map(json => $mol_tree2_from_json(json, span));
1783
+ return new $mol_tree2('/', '', sub, span);
1784
+ }
1785
+ if (ArrayBuffer.isView(json)) {
1786
+ const buf = new Uint8Array(json.buffer, json.byteOffset, json.byteLength);
1787
+ return $mol_tree2.data(String.fromCharCode(...buf), [], span);
1788
+ }
1789
+ if (json instanceof Date) {
1790
+ return new $mol_tree2('', json.toISOString(), [], span);
1791
+ }
1792
+ if (typeof json.toJSON === 'function') {
1793
+ return $mol_tree2_from_json(json.toJSON());
1794
+ }
1795
+ if (json instanceof Error) {
1796
+ const { name, message, stack } = json;
1797
+ json = { ...json, name, message, stack };
1798
+ }
1799
+ const sub = [];
1800
+ for (var key in json) {
1801
+ const val = json[key];
1802
+ if (val === undefined)
1803
+ continue;
1804
+ const subsub = $mol_tree2_from_json(val, span);
1805
+ if (/^[^\n\t\\ ]+$/.test(key)) {
1806
+ sub.push(new $mol_tree2(key, '', [subsub], span));
1807
+ }
1808
+ else {
1809
+ sub.push($mol_tree2.data(key, [subsub], span));
1619
1810
  }
1620
- Tree.fromString(`
1621
- \t \tfoo
1622
- bar \\data
1623
- `, 'test');
1624
- $mol_assert_like(errors, ['Syntax error at test:2\n \tfoo']);
1811
+ }
1812
+ return new $mol_tree2('*', '', sub, span);
1813
+ }
1814
+ $.$mol_tree2_from_json = $mol_tree2_from_json;
1815
+ })($ || ($ = {}));
1816
+ //mol/tree2/from/json/json.ts
1817
+ ;
1818
+ "use strict";
1819
+ var $;
1820
+ (function ($) {
1821
+ $mol_test({
1822
+ 'fromJSON'() {
1823
+ $mol_assert_equal($mol_tree2_from_json([]).toString(), '/\n');
1824
+ $mol_assert_equal($mol_tree2_from_json([false, true]).toString(), '/\n\tfalse\n\ttrue\n');
1825
+ $mol_assert_equal($mol_tree2_from_json([0, 1, 2.3]).toString(), '/\n\t0\n\t1\n\t2.3\n');
1826
+ $mol_assert_equal($mol_tree2_from_json(new Uint16Array([1, 10, 256])).toString(), '\\\x01\x00\n\\\x00\x00\x01\n');
1827
+ $mol_assert_equal($mol_tree2_from_json(['', 'foo', 'bar\nbaz']).toString(), '/\n\t\\\n\t\\foo\n\t\\\n\t\t\\bar\n\t\t\\baz\n');
1828
+ $mol_assert_equal($mol_tree2_from_json({ 'foo': false, 'bar\nbaz': 'lol' }).toString(), '*\n\tfoo false\n\t\\\n\t\t\\bar\n\t\t\\baz\n\t\t\\lol\n');
1625
1829
  },
1626
1830
  });
1627
1831
  })($ || ($ = {}));
1628
- //mol/tree/tree.test.ts
1832
+ //mol/tree2/from/json/json.test.ts
1629
1833
  ;
1630
1834
  "use strict";
1631
1835
  var $;