mol_regexp 0.0.685 → 0.0.686

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
@@ -572,7 +572,8 @@ var $;
572
572
  return function $mol_log3_logger(event) {
573
573
  if (!event.time)
574
574
  event = { time: new Date().toISOString(), ...event };
575
- const tree = this.$mol_tree.fromJSON(event).clone({ type });
575
+ let tree = this.$mol_tree2_from_json(event);
576
+ tree = tree.struct(type, tree.kids);
576
577
  let str = color(tree.toString());
577
578
  this.console[level](str);
578
579
  const self = this;
@@ -1505,248 +1506,219 @@ var $;
1505
1506
  "use strict";
1506
1507
  var $;
1507
1508
  (function ($) {
1508
- $.$mol_tree_convert = Symbol('$mol_tree_convert');
1509
- class $mol_tree extends $mol_object2 {
1510
- type;
1511
- data;
1512
- sub;
1513
- baseUri;
1509
+ class $mol_span extends $mol_object2 {
1510
+ uri;
1511
+ source;
1514
1512
  row;
1515
1513
  col;
1516
1514
  length;
1517
- constructor(config = {}) {
1515
+ constructor(uri, source, row, col, length) {
1518
1516
  super();
1519
- this.type = config.type || '';
1520
- if (config.value !== undefined) {
1521
- var sub = $mol_tree.values(config.value);
1522
- if (config.type || sub.length > 1) {
1523
- this.sub = [...sub, ...(config.sub || [])];
1524
- this.data = config.data || '';
1525
- }
1526
- else {
1527
- this.data = sub[0].data;
1528
- this.sub = config.sub || [];
1529
- }
1530
- }
1531
- else {
1532
- this.data = config.data || '';
1533
- this.sub = config.sub || [];
1534
- }
1535
- this.baseUri = config.baseUri || '';
1536
- this.row = config.row || 0;
1537
- this.col = config.col || 0;
1538
- this.length = config.length || 0;
1539
- }
1540
- static values(str, baseUri) {
1541
- return str.split('\n').map((data, index) => new $mol_tree({
1542
- data: data,
1543
- baseUri: baseUri,
1544
- row: index + 1,
1545
- length: data.length,
1546
- }));
1517
+ this.uri = uri;
1518
+ this.source = source;
1519
+ this.row = row;
1520
+ this.col = col;
1521
+ this.length = length;
1522
+ this[Symbol.toStringTag] = `${this.uri}#${this.row}:${this.col}/${this.length}`;
1547
1523
  }
1548
- clone(config = {}) {
1549
- return new $mol_tree({
1550
- type: ('type' in config) ? config.type : this.type,
1551
- data: ('data' in config) ? config.data : this.data,
1552
- sub: ('sub' in config) ? config.sub : this.sub,
1553
- baseUri: ('baseUri' in config) ? config.baseUri : this.baseUri,
1554
- row: ('row' in config) ? config.row : this.row,
1555
- col: ('col' in config) ? config.col : this.col,
1556
- length: ('length' in config) ? config.length : this.length,
1557
- value: config.value
1558
- });
1524
+ static unknown = $mol_span.begin('?');
1525
+ static begin(uri, source = '') {
1526
+ return new $mol_span(uri, source, 1, 1, 0);
1559
1527
  }
1560
- make(config) {
1561
- return new $mol_tree({
1562
- baseUri: this.baseUri,
1528
+ static end(uri, source) {
1529
+ return new $mol_span(uri, source, 1, source.length + 1, length);
1530
+ }
1531
+ static entire(uri, source) {
1532
+ return new $mol_span(uri, source, 1, 1, source.length);
1533
+ }
1534
+ toString() {
1535
+ return this[Symbol.toStringTag];
1536
+ }
1537
+ toJSON() {
1538
+ return {
1539
+ uri: this.uri,
1563
1540
  row: this.row,
1564
1541
  col: this.col,
1565
- length: this.length,
1566
- ...config,
1567
- });
1542
+ length: this.length
1543
+ };
1568
1544
  }
1569
- make_data(value, sub) {
1570
- return this.make({ value, sub });
1571
- }
1572
- make_struct(type, sub) {
1573
- return this.make({ type, sub });
1574
- }
1575
- static fromString(str, baseUri) {
1576
- var root = new $mol_tree({ baseUri: baseUri });
1577
- var stack = [root];
1578
- var row = 0;
1579
- var prefix = str.replace(/^\n?(\t*)[\s\S]*/, '$1');
1580
- var lines = str.replace(new RegExp('^\\t{0,' + prefix.length + '}', 'mg'), '').split('\n');
1581
- lines.forEach(line => {
1582
- ++row;
1583
- var chunks = /^(\t*)((?:[^\n\t\\ ]+ *)*)(\\[^\n]*)?(.*?)(?:$|\n)/m.exec(line);
1584
- if (!chunks || chunks[4])
1585
- return this.$.$mol_fail(new Error(`Syntax error at ${baseUri}:${row}\n${line}`));
1586
- var indent = chunks[1];
1587
- var path = chunks[2];
1588
- var data = chunks[3];
1589
- var deep = indent.length;
1590
- var types = path ? path.replace(/ $/, '').split(/ +/) : [];
1591
- if (stack.length <= deep)
1592
- return this.$.$mol_fail(new Error(`Too many tabs at ${baseUri}:${row}\n${line}`));
1593
- stack.length = deep + 1;
1594
- var parent = stack[deep];
1595
- let col = deep;
1596
- types.forEach(type => {
1597
- if (!type)
1598
- return this.$.$mol_fail(new Error(`Unexpected space symbol ${baseUri}:${row}\n${line}`));
1599
- var next = new $mol_tree({ type, baseUri, row, col, length: type.length });
1600
- const parent_sub = parent.sub;
1601
- parent_sub.push(next);
1602
- parent = next;
1603
- col += type.length + 1;
1604
- });
1605
- if (data) {
1606
- var next = new $mol_tree({ data: data.substring(1), baseUri, row, col, length: data.length });
1607
- const parent_sub = parent.sub;
1608
- parent_sub.push(next);
1609
- parent = next;
1610
- }
1611
- stack.push(parent);
1612
- });
1613
- return root;
1614
- }
1615
- static fromJSON(json, baseUri = '') {
1616
- switch (true) {
1617
- case typeof json === 'boolean':
1618
- case typeof json === 'number':
1619
- case json === null:
1620
- return new $mol_tree({
1621
- type: String(json),
1622
- baseUri: baseUri
1623
- });
1624
- case typeof json === 'string':
1625
- return new $mol_tree({
1626
- value: json,
1627
- baseUri: baseUri
1628
- });
1629
- case Array.isArray(json):
1630
- return new $mol_tree({
1631
- type: "/",
1632
- sub: json.map(json => $mol_tree.fromJSON(json, baseUri))
1633
- });
1634
- case json instanceof Date:
1635
- return new $mol_tree({
1636
- value: json.toISOString(),
1637
- baseUri: baseUri
1638
- });
1639
- default:
1640
- if (typeof json[$.$mol_tree_convert] === 'function') {
1641
- return json[$.$mol_tree_convert]();
1642
- }
1643
- if (typeof json.toJSON === 'function') {
1644
- return $mol_tree.fromJSON(json.toJSON());
1645
- }
1646
- if (json instanceof Error) {
1647
- const { name, message, stack } = json;
1648
- json = { ...json, name, message, stack };
1649
- }
1650
- var sub = [];
1651
- for (var key in json) {
1652
- if (json[key] === undefined)
1653
- continue;
1654
- const subsub = $mol_tree.fromJSON(json[key], baseUri);
1655
- if (/^[^\n\t\\ ]+$/.test(key)) {
1656
- var child = new $mol_tree({
1657
- type: key,
1658
- baseUri: baseUri,
1659
- sub: [subsub],
1660
- });
1661
- }
1662
- else {
1663
- var child = new $mol_tree({
1664
- value: key,
1665
- baseUri: baseUri,
1666
- sub: [subsub],
1667
- });
1668
- }
1669
- sub.push(child);
1670
- }
1671
- return new $mol_tree({
1672
- type: "*",
1673
- sub: sub,
1674
- baseUri: baseUri
1675
- });
1676
- }
1545
+ error(message, Class = Error) {
1546
+ return new Class(`${message}${this}`);
1547
+ }
1548
+ span(row, col, length) {
1549
+ return new $mol_span(this.uri, this.source, row, col, length);
1550
+ }
1551
+ after(length = 0) {
1552
+ return new $mol_span(this.uri, this.source, this.row, this.col + this.length, length);
1553
+ }
1554
+ slice(begin, end = -1) {
1555
+ let len = this.length;
1556
+ if (begin < 0)
1557
+ begin += len;
1558
+ if (end < 0)
1559
+ end += len;
1560
+ if (begin < 0 || begin > len)
1561
+ this.$.$mol_fail(`Begin value '${begin}' out of range ${this}`);
1562
+ if (end < 0 || end > len)
1563
+ this.$.$mol_fail(`End value '${end}' out of range ${this}`);
1564
+ if (end < begin)
1565
+ this.$.$mol_fail(`End value '${end}' can't be less than begin value ${this}`);
1566
+ return this.span(this.row, this.col + begin, end - begin);
1677
1567
  }
1678
- get uri() {
1679
- return this.baseUri + '#' + this.row + ':' + this.col;
1568
+ }
1569
+ $.$mol_span = $mol_span;
1570
+ })($ || ($ = {}));
1571
+ //mol/span/span.ts
1572
+ ;
1573
+ "use strict";
1574
+ var $;
1575
+ (function ($_1) {
1576
+ $mol_test({
1577
+ 'span for same uri'($) {
1578
+ const span = new $mol_span('test.ts', '', 1, 3, 4);
1579
+ const child = span.span(4, 5, 8);
1580
+ $mol_assert_equal(child.uri, 'test.ts');
1581
+ $mol_assert_equal(child.row, 4);
1582
+ $mol_assert_equal(child.col, 5);
1583
+ $mol_assert_equal(child.length, 8);
1584
+ },
1585
+ 'span after of given position'($) {
1586
+ const span = new $mol_span('test.ts', '', 1, 3, 4);
1587
+ const child = span.after(11);
1588
+ $mol_assert_equal(child.uri, 'test.ts');
1589
+ $mol_assert_equal(child.row, 1);
1590
+ $mol_assert_equal(child.col, 7);
1591
+ $mol_assert_equal(child.length, 11);
1592
+ },
1593
+ 'slice span - regular'($) {
1594
+ const span = new $mol_span('test.ts', '', 1, 3, 5);
1595
+ const child = span.slice(1, 4);
1596
+ $mol_assert_equal(child.row, 1);
1597
+ $mol_assert_equal(child.col, 4);
1598
+ $mol_assert_equal(child.length, 3);
1599
+ const child2 = span.slice(2, 2);
1600
+ $mol_assert_equal(child2.col, 5);
1601
+ $mol_assert_equal(child2.length, 0);
1602
+ },
1603
+ 'slice span - negative'($) {
1604
+ const span = new $mol_span('test.ts', '', 1, 3, 5);
1605
+ const child = span.slice(-3, -1);
1606
+ $mol_assert_equal(child.row, 1);
1607
+ $mol_assert_equal(child.col, 5);
1608
+ $mol_assert_equal(child.length, 2);
1609
+ },
1610
+ 'slice span - out of range'($) {
1611
+ const span = new $mol_span('test.ts', '', 1, 3, 5);
1612
+ $mol_assert_fail(() => span.slice(-1, 3));
1613
+ $mol_assert_fail(() => span.slice(1, 6));
1614
+ $mol_assert_fail(() => span.slice(1, 10));
1615
+ },
1616
+ 'error handling'($) {
1617
+ const span = new $mol_span('test.ts', '', 1, 3, 4);
1618
+ const error = span.error('Some error\n');
1619
+ $mol_assert_equal(error.message, 'Some error\ntest.ts#1:3/4');
1680
1620
  }
1681
- toString(prefix = '') {
1682
- var output = '';
1683
- if (this.type.length) {
1621
+ });
1622
+ })($ || ($ = {}));
1623
+ //mol/span/span.test.ts
1624
+ ;
1625
+ "use strict";
1626
+ var $;
1627
+ (function ($) {
1628
+ function $mol_tree2_to_string(tree) {
1629
+ let output = [];
1630
+ function dump(tree, prefix = '') {
1631
+ if (tree.type.length) {
1684
1632
  if (!prefix.length) {
1685
1633
  prefix = "\t";
1686
1634
  }
1687
- output += this.type;
1688
- if (this.sub.length == 1) {
1689
- return output + ' ' + this.sub[0].toString(prefix);
1635
+ output.push(tree.type);
1636
+ if (tree.kids.length == 1) {
1637
+ output.push(' ');
1638
+ dump(tree.kids[0], prefix);
1639
+ return;
1690
1640
  }
1691
- output += "\n";
1641
+ output.push("\n");
1692
1642
  }
1693
- else if (this.data.length || prefix.length) {
1694
- output += "\\" + this.data + "\n";
1643
+ else if (tree.value.length || prefix.length) {
1644
+ output.push("\\" + tree.value + "\n");
1695
1645
  }
1696
- for (var child of this.sub) {
1697
- output += prefix;
1698
- output += child.toString(prefix + "\t");
1646
+ for (const kid of tree.kids) {
1647
+ output.push(prefix);
1648
+ dump(kid, prefix + "\t");
1699
1649
  }
1700
- return output;
1701
1650
  }
1702
- toJSON() {
1703
- if (!this.type)
1704
- return this.value;
1705
- if (this.type === 'true')
1706
- return true;
1707
- if (this.type === 'false')
1708
- return false;
1709
- if (this.type === 'null')
1710
- return null;
1711
- if (this.type === '*') {
1712
- var obj = {};
1713
- for (var child of this.sub) {
1714
- if (child.type === '-')
1715
- continue;
1716
- var key = child.type || child.clone({ sub: child.sub.slice(0, child.sub.length - 1) }).value;
1717
- var val = child.sub[child.sub.length - 1].toJSON();
1718
- if (val !== undefined)
1719
- obj[key] = val;
1720
- }
1721
- return obj;
1722
- }
1723
- if (this.type === '/') {
1724
- var res = [];
1725
- this.sub.forEach(child => {
1726
- if (child.type === '-')
1727
- return;
1728
- var val = child.toJSON();
1729
- if (val !== undefined)
1730
- res.push(val);
1651
+ dump(tree);
1652
+ return output.join('');
1653
+ }
1654
+ $.$mol_tree2_to_string = $mol_tree2_to_string;
1655
+ })($ || ($ = {}));
1656
+ //mol/tree2/to/string/string.ts
1657
+ ;
1658
+ "use strict";
1659
+ var $;
1660
+ (function ($) {
1661
+ class $mol_tree2 extends Object {
1662
+ type;
1663
+ value;
1664
+ kids;
1665
+ span;
1666
+ constructor(type, value, kids, span) {
1667
+ super();
1668
+ this.type = type;
1669
+ this.value = value;
1670
+ this.kids = kids;
1671
+ this.span = span;
1672
+ this[Symbol.toStringTag] = type || '\\' + value;
1673
+ }
1674
+ static list(kids, span = $mol_span.unknown) {
1675
+ return new $mol_tree2('', '', kids, span);
1676
+ }
1677
+ list(kids) {
1678
+ return $mol_tree2.list(kids, this.span);
1679
+ }
1680
+ static data(value, kids = [], span = $mol_span.unknown) {
1681
+ const chunks = value.split('\n');
1682
+ if (chunks.length > 1) {
1683
+ let kid_span = span.span(span.row, span.col, 0);
1684
+ const data = chunks.map(chunk => {
1685
+ kid_span = kid_span.after(chunk.length);
1686
+ return new $mol_tree2('', chunk, [], kid_span);
1731
1687
  });
1732
- return res;
1688
+ kids = [...data, ...kids];
1689
+ value = '';
1733
1690
  }
1734
- if (this.type === 'time') {
1735
- return new Date(this.value);
1691
+ return new $mol_tree2('', value, kids, span);
1692
+ }
1693
+ data(value, kids = []) {
1694
+ return $mol_tree2.data(value, kids, this.span);
1695
+ }
1696
+ static struct(type, kids = [], span = $mol_span.unknown) {
1697
+ if (/[ \n\t\\]/.test(type)) {
1698
+ $$.$mol_fail(span.error(`Wrong type ${JSON.stringify(type)}`));
1736
1699
  }
1737
- const numb = Number(this.type);
1738
- if (!Number.isNaN(numb) || this.type === 'NaN')
1739
- return numb;
1740
- throw new Error(`Unknown type (${this.type}) at ${this.uri}`);
1700
+ return new $mol_tree2(type, '', kids, span);
1701
+ }
1702
+ struct(type, kids = []) {
1703
+ return $mol_tree2.struct(type, kids, this.span);
1704
+ }
1705
+ clone(kids, span = this.span) {
1706
+ return new $mol_tree2(this.type, this.value, kids, span);
1741
1707
  }
1742
- get value() {
1708
+ text() {
1743
1709
  var values = [];
1744
- for (var child of this.sub) {
1745
- if (child.type)
1710
+ for (var kid of this.kids) {
1711
+ if (kid.type)
1746
1712
  continue;
1747
- values.push(child.value);
1713
+ values.push(kid.value);
1748
1714
  }
1749
- return this.data + values.join("\n");
1715
+ return this.value + values.join('\n');
1716
+ }
1717
+ static fromString(str, uri = 'unknown') {
1718
+ return $$.$mol_tree2_from_string(str, uri);
1719
+ }
1720
+ toString() {
1721
+ return $$.$mol_tree2_to_string(this);
1750
1722
  }
1751
1723
  insert(value, ...path) {
1752
1724
  if (path.length === 0)
@@ -1754,143 +1726,375 @@ var $;
1754
1726
  const type = path[0];
1755
1727
  if (typeof type === 'string') {
1756
1728
  let replaced = false;
1757
- const sub = this.sub.map((item, index) => {
1729
+ const sub = this.kids.map((item, index) => {
1758
1730
  if (item.type !== type)
1759
1731
  return item;
1760
1732
  replaced = true;
1761
1733
  return item.insert(value, ...path.slice(1));
1762
- });
1763
- if (!replaced)
1764
- sub.push(new $mol_tree({ type }).insert(value, ...path.slice(1)));
1765
- return this.clone({ sub });
1734
+ }).filter(Boolean);
1735
+ if (!replaced && value) {
1736
+ sub.push(this.struct(type, []).insert(value, ...path.slice(1)));
1737
+ }
1738
+ return this.clone(sub);
1766
1739
  }
1767
1740
  else if (typeof type === 'number') {
1768
- const sub = this.sub.slice();
1769
- sub[type] = (sub[type] || new $mol_tree).insert(value, ...path.slice(1));
1770
- return this.clone({ sub });
1741
+ const sub = this.kids.slice();
1742
+ sub[type] = (sub[type] || this.list([]))
1743
+ .insert(value, ...path.slice(1));
1744
+ return this.clone(sub.filter(Boolean));
1771
1745
  }
1772
1746
  else {
1773
- return this.clone({ sub: ((this.sub.length === 0) ? [new $mol_tree()] : this.sub).map(item => item.insert(value, ...path.slice(1))) });
1747
+ const kids = ((this.kids.length === 0) ? [this.list([])] : this.kids)
1748
+ .map(item => item.insert(value, ...path.slice(1)))
1749
+ .filter(Boolean);
1750
+ return this.clone(kids);
1774
1751
  }
1775
1752
  }
1776
1753
  select(...path) {
1777
- var next = [this];
1778
- for (var type of path) {
1754
+ let next = [this];
1755
+ for (const type of path) {
1779
1756
  if (!next.length)
1780
1757
  break;
1781
- var prev = next;
1758
+ const prev = next;
1782
1759
  next = [];
1783
1760
  for (var item of prev) {
1784
1761
  switch (typeof (type)) {
1785
1762
  case 'string':
1786
- for (var child of item.sub) {
1787
- if (!type || (child.type == type)) {
1763
+ for (var child of item.kids) {
1764
+ if (child.type == type) {
1788
1765
  next.push(child);
1789
1766
  }
1790
1767
  }
1791
1768
  break;
1792
1769
  case 'number':
1793
- if (type < item.sub.length)
1794
- next.push(item.sub[type]);
1770
+ if (type < item.kids.length)
1771
+ next.push(item.kids[type]);
1795
1772
  break;
1796
- default: next.push(...item.sub);
1773
+ default: next.push(...item.kids);
1797
1774
  }
1798
1775
  }
1799
1776
  }
1800
- return new $mol_tree({ sub: next });
1777
+ return this.list(next);
1801
1778
  }
1802
1779
  filter(path, value) {
1803
- var sub = this.sub.filter(function (item) {
1780
+ const sub = this.kids.filter(item => {
1804
1781
  var found = item.select(...path);
1805
- if (value == null) {
1806
- return Boolean(found.sub.length);
1782
+ if (value === undefined) {
1783
+ return Boolean(found.kids.length);
1807
1784
  }
1808
1785
  else {
1809
- return found.sub.some(child => child.value == value);
1786
+ return found.kids.some(child => child.value == value);
1810
1787
  }
1811
1788
  });
1812
- return new $mol_tree({ sub: sub });
1813
- }
1814
- transform(visit, stack = []) {
1815
- const sub_stack = [this, ...stack];
1816
- return visit(sub_stack, () => this.sub.map(node => node.transform(visit, sub_stack)).filter(n => n));
1817
- }
1818
- hack(context) {
1819
- const sub = [].concat(...this.sub.map(child => {
1820
- const handle = context[child.type] || context[''];
1821
- if (!handle)
1822
- $mol_fail(child.error('Handler not defined'));
1823
- return handle(child, context);
1789
+ return this.clone(sub);
1790
+ }
1791
+ hack(belt, context = {}) {
1792
+ return [].concat(...this.kids.map(child => {
1793
+ let handle = belt[child.type] || belt[''];
1794
+ if (!handle || handle === Object.prototype[child.type]) {
1795
+ handle = (input, belt, context) => [
1796
+ input.clone(input.hack(belt, context), context.span)
1797
+ ];
1798
+ }
1799
+ try {
1800
+ return handle(child, belt, context);
1801
+ }
1802
+ catch (error) {
1803
+ error.message += `\n${child.clone([])}${child.span}`;
1804
+ $mol_fail_hidden(error);
1805
+ }
1824
1806
  }));
1825
- return this.clone({ sub });
1826
1807
  }
1827
- error(message) {
1828
- return new Error(`${message}:\n${this} ${this.baseUri}:${this.row}:${this.col}`);
1808
+ error(message, Class = Error) {
1809
+ return this.span.error(`${message}\n${this.clone([])}`, Class);
1810
+ }
1811
+ }
1812
+ $.$mol_tree2 = $mol_tree2;
1813
+ class $mol_tree2_empty extends $mol_tree2 {
1814
+ constructor() {
1815
+ super('', '', [], $mol_span.unknown);
1829
1816
  }
1830
1817
  }
1831
- $.$mol_tree = $mol_tree;
1818
+ $.$mol_tree2_empty = $mol_tree2_empty;
1832
1819
  })($ || ($ = {}));
1833
- //mol/tree/tree.ts
1820
+ //mol/tree2/tree2.ts
1834
1821
  ;
1835
1822
  "use strict";
1836
1823
  var $;
1837
1824
  (function ($_1) {
1838
1825
  $mol_test({
1839
- 'tree parsing'() {
1840
- $mol_assert_equal($mol_tree.fromString("foo\nbar\n").sub.length, 2);
1841
- $mol_assert_equal($mol_tree.fromString("foo\nbar\n").sub[1].type, "bar");
1842
- $mol_assert_equal($mol_tree.fromString("foo\n\n\n").sub.length, 1);
1843
- $mol_assert_equal($mol_tree.fromString("=foo\n\\bar\n").sub.length, 2);
1844
- $mol_assert_equal($mol_tree.fromString("=foo\n\\bar\n").sub[1].data, "bar");
1845
- $mol_assert_equal($mol_tree.fromString("foo bar \\pol").sub[0].sub[0].sub[0].data, "pol");
1846
- $mol_assert_equal($mol_tree.fromString("foo bar\n\t\\pol\n\t\\men").sub[0].sub[0].sub[1].data, "men");
1847
- $mol_assert_equal($mol_tree.fromString('foo bar \\text\n').toString(), 'foo bar \\text\n');
1848
- },
1849
- 'inserting'() {
1850
- $mol_assert_equal($mol_tree.fromString('a b c d').insert(new $mol_tree, 'a', 'b', 'c').toString(), 'a b \\\n');
1851
- $mol_assert_equal($mol_tree.fromString('a b').insert(new $mol_tree, 'a', 'b', 'c', 'd').toString(), 'a b c \\\n');
1852
- $mol_assert_equal($mol_tree.fromString('a b c d').insert(new $mol_tree, 0, 0, 0).toString(), 'a b \\\n');
1853
- $mol_assert_equal($mol_tree.fromString('a b').insert(new $mol_tree, 0, 0, 0, 0).toString(), 'a b \\\n\t\\\n');
1854
- $mol_assert_equal($mol_tree.fromString('a b c d').insert(new $mol_tree, null, null, null).toString(), 'a b \\\n');
1855
- $mol_assert_equal($mol_tree.fromString('a b').insert(new $mol_tree, null, null, null, null).toString(), 'a b \\\n\t\\\n');
1856
- },
1857
- 'fromJSON'() {
1858
- $mol_assert_equal($mol_tree.fromJSON([]).toString(), '/\n');
1859
- $mol_assert_equal($mol_tree.fromJSON([false, true]).toString(), '/\n\tfalse\n\ttrue\n');
1860
- $mol_assert_equal($mol_tree.fromJSON([0, 1, 2.3]).toString(), '/\n\t0\n\t1\n\t2.3\n');
1861
- $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');
1862
- $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');
1863
- },
1864
- 'toJSON'() {
1865
- $mol_assert_equal(JSON.stringify($mol_tree.fromString('/\n').sub[0]), '[]');
1866
- $mol_assert_equal(JSON.stringify($mol_tree.fromString('/\n\tfalse\n\ttrue\n').sub[0]), '[false,true]');
1867
- $mol_assert_equal(JSON.stringify($mol_tree.fromString('/\n\t0\n\t1\n\t2.3\n').sub[0]), '[0,1,2.3]');
1868
- $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"]');
1869
- $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"}');
1870
- },
1871
- 'hack'() {
1872
- const res = $mol_tree.fromString(`foo bar xxx`).hack({
1873
- '': (tree, context) => [tree.hack(context)],
1874
- 'bar': (tree, context) => [tree.hack(context).clone({ type: '777' })],
1826
+ 'inserting'($) {
1827
+ $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
1828
+ .insert($mol_tree2.struct('x'), 'a', 'b', 'c')
1829
+ .toString(), 'a b x\n');
1830
+ $mol_assert_equal($.$mol_tree2_from_string('a b\n')
1831
+ .insert($mol_tree2.struct('x'), 'a', 'b', 'c', 'd')
1832
+ .toString(), 'a b c x\n');
1833
+ $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
1834
+ .insert($mol_tree2.struct('x'), 0, 0, 0)
1835
+ .toString(), 'a b x\n');
1836
+ $mol_assert_equal($.$mol_tree2_from_string('a b\n')
1837
+ .insert($mol_tree2.struct('x'), 0, 0, 0, 0)
1838
+ .toString(), 'a b \\\n\tx\n');
1839
+ $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
1840
+ .insert($mol_tree2.struct('x'), null, null, null)
1841
+ .toString(), 'a b x\n');
1842
+ $mol_assert_equal($.$mol_tree2_from_string('a b\n')
1843
+ .insert($mol_tree2.struct('x'), null, null, null, null)
1844
+ .toString(), 'a b \\\n\tx\n');
1845
+ },
1846
+ 'deleting'($) {
1847
+ $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
1848
+ .insert(null, 'a', 'b', 'c')
1849
+ .toString(), 'a b\n');
1850
+ $mol_assert_equal($.$mol_tree2_from_string('a b c d\n')
1851
+ .insert(null, 0, 0, 0)
1852
+ .toString(), 'a b\n');
1853
+ },
1854
+ 'hack'($) {
1855
+ const res = $.$mol_tree2_from_string(`foo bar xxx\n`)
1856
+ .hack({
1857
+ 'bar': (input, belt) => [input.struct('777', input.hack(belt))],
1875
1858
  });
1876
- $mol_assert_equal(res.toString(), new $mol_tree({ type: 'foo 777 xxx' }).toString());
1859
+ $mol_assert_equal(res.toString(), 'foo 777 xxx\n');
1877
1860
  },
1878
- 'errors handling'($) {
1861
+ });
1862
+ })($ || ($ = {}));
1863
+ //mol/tree2/tree2.test.ts
1864
+ ;
1865
+ "use strict";
1866
+ var $;
1867
+ (function ($) {
1868
+ class $mol_error_syntax extends SyntaxError {
1869
+ reason;
1870
+ line;
1871
+ span;
1872
+ constructor(reason, line, span) {
1873
+ super(`${reason}\n${span}\n${line.substring(0, span.col - 1).replace(/\S/g, ' ')}${''.padEnd(span.length, '!')}\n${line}`);
1874
+ this.reason = reason;
1875
+ this.line = line;
1876
+ this.span = span;
1877
+ }
1878
+ }
1879
+ $.$mol_error_syntax = $mol_error_syntax;
1880
+ })($ || ($ = {}));
1881
+ //mol/error/syntax/syntax.ts
1882
+ ;
1883
+ "use strict";
1884
+ var $;
1885
+ (function ($) {
1886
+ function $mol_tree2_from_string(str, uri = '?') {
1887
+ const span = $mol_span.entire(uri, str);
1888
+ var root = $mol_tree2.list([], span);
1889
+ var stack = [root];
1890
+ var pos = 0, row = 0, min_indent = 0;
1891
+ while (str.length > pos) {
1892
+ var indent = 0;
1893
+ var line_start = pos;
1894
+ row++;
1895
+ while (str.length > pos && str[pos] == '\t') {
1896
+ indent++;
1897
+ pos++;
1898
+ }
1899
+ if (!root.kids.length) {
1900
+ min_indent = indent;
1901
+ }
1902
+ indent -= min_indent;
1903
+ if (indent < 0 || indent >= stack.length) {
1904
+ const sp = span.span(row, 1, pos - line_start);
1905
+ while (str.length > pos && str[pos] != '\n') {
1906
+ pos++;
1907
+ }
1908
+ if (indent < 0) {
1909
+ if (str.length > pos) {
1910
+ this.$mol_fail(new this.$mol_error_syntax(`Too few tabs`, str.substring(line_start, pos), sp));
1911
+ }
1912
+ }
1913
+ else {
1914
+ this.$mol_fail(new this.$mol_error_syntax(`Too many tabs`, str.substring(line_start, pos), sp));
1915
+ }
1916
+ }
1917
+ stack.length = indent + 1;
1918
+ var parent = stack[indent];
1919
+ while (str.length > pos && str[pos] != '\\' && str[pos] != '\n') {
1920
+ var error_start = pos;
1921
+ while (str.length > pos && (str[pos] == ' ' || str[pos] == '\t')) {
1922
+ pos++;
1923
+ }
1924
+ if (pos > error_start) {
1925
+ let line_end = str.indexOf('\n', pos);
1926
+ if (line_end === -1)
1927
+ line_end = str.length;
1928
+ const sp = span.span(row, error_start - line_start, pos - error_start + 1);
1929
+ this.$mol_fail(new this.$mol_error_syntax(`Wrong nodes separator`, str.substring(line_start, line_end), sp));
1930
+ }
1931
+ var type_start = pos;
1932
+ while (str.length > pos &&
1933
+ str[pos] != '\\' &&
1934
+ str[pos] != ' ' &&
1935
+ str[pos] != '\t' &&
1936
+ str[pos] != '\n') {
1937
+ pos++;
1938
+ }
1939
+ if (pos > type_start) {
1940
+ let next = new $mol_tree2(str.slice(type_start, pos), '', [], span.span(row, type_start - line_start + 1, pos - type_start));
1941
+ const parent_kids = parent.kids;
1942
+ parent_kids.push(next);
1943
+ parent = next;
1944
+ }
1945
+ if (str.length > pos && str[pos] == ' ') {
1946
+ pos++;
1947
+ }
1948
+ }
1949
+ if (str.length > pos && str[pos] == '\\') {
1950
+ var data_start = pos;
1951
+ while (str.length > pos && str[pos] != '\n') {
1952
+ pos++;
1953
+ }
1954
+ let next = new $mol_tree2('', str.slice(data_start + 1, pos), [], span.span(row, data_start - line_start + 2, pos - data_start - 1));
1955
+ const parent_kids = parent.kids;
1956
+ parent_kids.push(next);
1957
+ parent = next;
1958
+ }
1959
+ if (str.length === pos && stack.length > 0) {
1960
+ const sp = span.span(row, pos - line_start + 1, 1);
1961
+ this.$mol_fail(new this.$mol_error_syntax(`Undexpected EOF, LF required`, str.substring(line_start, str.length), sp));
1962
+ }
1963
+ stack.push(parent);
1964
+ pos++;
1965
+ }
1966
+ return root;
1967
+ }
1968
+ $.$mol_tree2_from_string = $mol_tree2_from_string;
1969
+ })($ || ($ = {}));
1970
+ //mol/tree2/from/string/string.ts
1971
+ ;
1972
+ "use strict";
1973
+ var $;
1974
+ (function ($_1) {
1975
+ $mol_test({
1976
+ 'tree parsing'($) {
1977
+ $mol_assert_equal($.$mol_tree2_from_string("foo\nbar\n").kids.length, 2);
1978
+ $mol_assert_equal($.$mol_tree2_from_string("foo\nbar\n").kids[1].type, "bar");
1979
+ $mol_assert_equal($.$mol_tree2_from_string("foo\n\n\n").kids.length, 1);
1980
+ $mol_assert_equal($.$mol_tree2_from_string("=foo\n\\bar\n").kids.length, 2);
1981
+ $mol_assert_equal($.$mol_tree2_from_string("=foo\n\\bar\n").kids[1].value, "bar");
1982
+ $mol_assert_equal($.$mol_tree2_from_string("foo bar \\pol\n").kids[0].kids[0].kids[0].value, "pol");
1983
+ $mol_assert_equal($.$mol_tree2_from_string("foo bar\n\t\\pol\n\t\\men\n").kids[0].kids[0].kids[1].value, "men");
1984
+ $mol_assert_equal($.$mol_tree2_from_string('foo bar \\text\n').toString(), 'foo bar \\text\n');
1985
+ },
1986
+ 'Too many tabs'($) {
1987
+ const tree = `
1988
+ foo
1989
+ bar
1990
+ `;
1991
+ $mol_assert_fail(() => {
1992
+ $.$mol_tree2_from_string(tree, 'test');
1993
+ }, 'Too many tabs\ntest#3:1/6\n!!!!!!\n\t\t\t\t\t\tbar');
1994
+ },
1995
+ 'Too few tabs'($) {
1996
+ const tree = `
1997
+ foo
1998
+ bar
1999
+ `;
2000
+ $mol_assert_fail(() => {
2001
+ $.$mol_tree2_from_string(tree, 'test');
2002
+ }, 'Too few tabs\ntest#3:1/4\n!!!!\n\t\t\t\tbar');
2003
+ },
2004
+ 'Wrong nodes separator'($) {
2005
+ const tree = `foo bar\n`;
2006
+ $mol_assert_fail(() => {
2007
+ $.$mol_tree2_from_string(tree, 'test');
2008
+ }, 'Wrong nodes separator\ntest#1:4/2\n !!\nfoo bar');
2009
+ },
2010
+ 'Undexpected EOF, LF required'($) {
2011
+ const tree = ` foo`;
2012
+ $mol_assert_fail(() => {
2013
+ $.$mol_tree2_from_string(tree, 'test');
2014
+ }, 'Undexpected EOF, LF required\ntest#1:5/1\n !\n foo');
2015
+ },
2016
+ 'Errors skip and collect'($) {
2017
+ const tree = `foo bar`;
1879
2018
  const errors = [];
1880
- class Tree extends $mol_tree {
1881
- static $ = $.$mol_ambient({
1882
- $mol_fail: error => errors.push(error.message)
1883
- });
2019
+ const $$ = $.$mol_ambient({
2020
+ $mol_fail: (error) => {
2021
+ errors.push(error.message);
2022
+ return null;
2023
+ }
2024
+ });
2025
+ const res = $$.$mol_tree2_from_string(tree, 'test');
2026
+ $mol_assert_like(errors, [
2027
+ 'Wrong nodes separator\ntest#1:4/2\n !!\nfoo bar',
2028
+ 'Undexpected EOF, LF required\ntest#1:9/1\n !\nfoo bar',
2029
+ ]);
2030
+ $mol_assert_equal(res.toString(), 'foo bar\n');
2031
+ },
2032
+ });
2033
+ })($ || ($ = {}));
2034
+ //mol/tree2/from/string/string.test.ts
2035
+ ;
2036
+ "use strict";
2037
+ var $;
2038
+ (function ($) {
2039
+ function $mol_tree2_from_json(json, span = $mol_span.unknown) {
2040
+ if (typeof json === 'boolean' || typeof json === 'number' || json === null) {
2041
+ return new $mol_tree2(String(json), '', [], span);
2042
+ }
2043
+ if (typeof json === 'string') {
2044
+ return $mol_tree2.data(json, [], span);
2045
+ }
2046
+ if (Array.isArray(json)) {
2047
+ const sub = json.map(json => $mol_tree2_from_json(json, span));
2048
+ return new $mol_tree2('/', '', sub, span);
2049
+ }
2050
+ if (ArrayBuffer.isView(json)) {
2051
+ const buf = new Uint8Array(json.buffer, json.byteOffset, json.byteLength);
2052
+ return $mol_tree2.data(String.fromCharCode(...buf), [], span);
2053
+ }
2054
+ if (json instanceof Date) {
2055
+ return new $mol_tree2('', json.toISOString(), [], span);
2056
+ }
2057
+ if (typeof json.toJSON === 'function') {
2058
+ return $mol_tree2_from_json(json.toJSON());
2059
+ }
2060
+ if (json instanceof Error) {
2061
+ const { name, message, stack } = json;
2062
+ json = { ...json, name, message, stack };
2063
+ }
2064
+ const sub = [];
2065
+ for (var key in json) {
2066
+ const val = json[key];
2067
+ if (val === undefined)
2068
+ continue;
2069
+ const subsub = $mol_tree2_from_json(val, span);
2070
+ if (/^[^\n\t\\ ]+$/.test(key)) {
2071
+ sub.push(new $mol_tree2(key, '', [subsub], span));
2072
+ }
2073
+ else {
2074
+ sub.push($mol_tree2.data(key, [subsub], span));
1884
2075
  }
1885
- Tree.fromString(`
1886
- \t \tfoo
1887
- bar \\data
1888
- `, 'test');
1889
- $mol_assert_like(errors, ['Syntax error at test:2\n \tfoo']);
2076
+ }
2077
+ return new $mol_tree2('*', '', sub, span);
2078
+ }
2079
+ $.$mol_tree2_from_json = $mol_tree2_from_json;
2080
+ })($ || ($ = {}));
2081
+ //mol/tree2/from/json/json.ts
2082
+ ;
2083
+ "use strict";
2084
+ var $;
2085
+ (function ($) {
2086
+ $mol_test({
2087
+ 'fromJSON'() {
2088
+ $mol_assert_equal($mol_tree2_from_json([]).toString(), '/\n');
2089
+ $mol_assert_equal($mol_tree2_from_json([false, true]).toString(), '/\n\tfalse\n\ttrue\n');
2090
+ $mol_assert_equal($mol_tree2_from_json([0, 1, 2.3]).toString(), '/\n\t0\n\t1\n\t2.3\n');
2091
+ $mol_assert_equal($mol_tree2_from_json(new Uint16Array([1, 10, 256])).toString(), '\\\x01\x00\n\\\x00\x00\x01\n');
2092
+ $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');
2093
+ $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');
1890
2094
  },
1891
2095
  });
1892
2096
  })($ || ($ = {}));
1893
- //mol/tree/tree.test.ts
2097
+ //mol/tree2/from/json/json.test.ts
1894
2098
  ;
1895
2099
  "use strict";
1896
2100
  var $;