@lezer/common 1.1.0 → 1.1.2

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/dist/index.cjs CHANGED
@@ -885,8 +885,6 @@ class BufferNode extends BaseNode {
885
885
  function iterStack(heads) {
886
886
  if (!heads.length)
887
887
  return null;
888
- if (heads.length == 1)
889
- return heads[0];
890
888
  let pick = 0, picked = heads[0];
891
889
  for (let i = 1; i < heads.length; i++) {
892
890
  let node = heads[i];
@@ -922,7 +920,7 @@ function stackIterator(tree, pos, side) {
922
920
  let mount = MountedTree.get(scan.tree);
923
921
  // Relevant overlay branching off
924
922
  if (mount && mount.overlay && mount.overlay[0].from <= pos && mount.overlay[mount.overlay.length - 1].to >= pos) {
925
- let root = new TreeNode(mount.tree, mount.overlay[0].from + scan.from, 0, null);
923
+ let root = new TreeNode(mount.tree, mount.overlay[0].from + scan.from, -1, scan);
926
924
  (layers || (layers = [inner])).push(resolveNode(root, pos, side, false));
927
925
  }
928
926
  }
@@ -986,6 +984,9 @@ class TreeCursor {
986
984
  this.to = start + buffer.buffer[index + 2];
987
985
  return true;
988
986
  }
987
+ /**
988
+ @internal
989
+ */
989
990
  yield(node) {
990
991
  if (!node)
991
992
  return false;
@@ -1251,7 +1252,7 @@ function buildTree(data) {
1251
1252
  let cursor = Array.isArray(buffer) ? new FlatBufferCursor(buffer, buffer.length) : buffer;
1252
1253
  let types = nodeSet.types;
1253
1254
  let contextHash = 0, lookAhead = 0;
1254
- function takeNode(parentStart, minPos, children, positions, inRepeat) {
1255
+ function takeNode(parentStart, minPos, children, positions, inRepeat, depth) {
1255
1256
  let { id, start, end, size } = cursor;
1256
1257
  let lookAheadAtStart = lookAhead;
1257
1258
  while (size < 0) {
@@ -1300,8 +1301,11 @@ function buildTree(data) {
1300
1301
  }
1301
1302
  cursor.next();
1302
1303
  }
1304
+ else if (depth > 2500 /* CutOff.Depth */) {
1305
+ takeFlatNode(start, endPos, localChildren, localPositions);
1306
+ }
1303
1307
  else {
1304
- takeNode(start, endPos, localChildren, localPositions, localInRepeat);
1308
+ takeNode(start, endPos, localChildren, localPositions, localInRepeat, depth + 1);
1305
1309
  }
1306
1310
  }
1307
1311
  if (localInRepeat >= 0 && lastGroup > 0 && lastGroup < localChildren.length)
@@ -1319,6 +1323,38 @@ function buildTree(data) {
1319
1323
  children.push(node);
1320
1324
  positions.push(startPos);
1321
1325
  }
1326
+ function takeFlatNode(parentStart, minPos, children, positions) {
1327
+ let nodes = []; // Temporary, inverted array of leaf nodes found, with absolute positions
1328
+ let nodeCount = 0, stopAt = -1;
1329
+ while (cursor.pos > minPos) {
1330
+ let { id, start, end, size } = cursor;
1331
+ if (size > 4) { // Not a leaf
1332
+ cursor.next();
1333
+ }
1334
+ else if (stopAt > -1 && start < stopAt) {
1335
+ break;
1336
+ }
1337
+ else {
1338
+ if (stopAt < 0)
1339
+ stopAt = end - maxBufferLength;
1340
+ nodes.push(id, start, end);
1341
+ nodeCount++;
1342
+ cursor.next();
1343
+ }
1344
+ }
1345
+ if (nodeCount) {
1346
+ let buffer = new Uint16Array(nodeCount * 4);
1347
+ let start = nodes[nodes.length - 2];
1348
+ for (let i = nodes.length - 3, j = 0; i >= 0; i -= 3) {
1349
+ buffer[j++] = nodes[i];
1350
+ buffer[j++] = nodes[i + 1] - start;
1351
+ buffer[j++] = nodes[i + 2] - start;
1352
+ buffer[j++] = j;
1353
+ }
1354
+ children.push(new TreeBuffer(buffer, nodes[2] - start, nodeSet));
1355
+ positions.push(start - parentStart);
1356
+ }
1357
+ }
1322
1358
  function makeBalanced(type) {
1323
1359
  return (children, positions, length) => {
1324
1360
  let lookAhead = 0, lastI = children.length - 1, last, lookAheadProp;
@@ -1429,7 +1465,7 @@ function buildTree(data) {
1429
1465
  }
1430
1466
  let children = [], positions = [];
1431
1467
  while (cursor.pos > 0)
1432
- takeNode(data.start || 0, data.bufferStart || 0, children, positions, -1);
1468
+ takeNode(data.start || 0, data.bufferStart || 0, children, positions, -1, 0);
1433
1469
  let length = (_a = data.length) !== null && _a !== void 0 ? _a : (children.length ? positions[0] + children[0].length : 0);
1434
1470
  return new Tree(types[data.topID], children.reverse(), positions.reverse(), length);
1435
1471
  }
@@ -1710,16 +1746,18 @@ function parseMixed(nest) {
1710
1746
  return (parse, input, fragments, ranges) => new MixedParse(parse, nest, input, fragments, ranges);
1711
1747
  }
1712
1748
  class InnerParse {
1713
- constructor(parser, parse, overlay, target, ranges) {
1749
+ constructor(parser, parse, overlay, target, from) {
1714
1750
  this.parser = parser;
1715
1751
  this.parse = parse;
1716
1752
  this.overlay = overlay;
1717
1753
  this.target = target;
1718
- this.ranges = ranges;
1719
- if (!ranges.length || ranges.some(r => r.from >= r.to))
1720
- throw new RangeError("Invalid inner parse ranges given: " + JSON.stringify(ranges));
1754
+ this.from = from;
1721
1755
  }
1722
1756
  }
1757
+ function checkRanges(ranges) {
1758
+ if (!ranges.length || ranges.some(r => r.from >= r.to))
1759
+ throw new RangeError("Invalid inner parse ranges given: " + JSON.stringify(ranges));
1760
+ }
1723
1761
  class ActiveOverlay {
1724
1762
  constructor(parser, predicate, mounts, index, start, target, prev) {
1725
1763
  this.parser = parser;
@@ -1782,7 +1820,7 @@ class MixedParse {
1782
1820
  return 0;
1783
1821
  let pos = this.input.length;
1784
1822
  for (let i = this.innerDone; i < this.inner.length; i++) {
1785
- if (this.inner[i].ranges[0].from < pos)
1823
+ if (this.inner[i].from < pos)
1786
1824
  pos = Math.min(pos, this.inner[i].parse.parsedPos);
1787
1825
  }
1788
1826
  return pos;
@@ -1800,9 +1838,12 @@ class MixedParse {
1800
1838
  let overlay = null;
1801
1839
  let covered = null;
1802
1840
  let cursor = new TreeCursor(new TreeNode(this.baseTree, this.ranges[0].from, 0, null), exports.IterMode.IncludeAnonymous | exports.IterMode.IgnoreMounts);
1803
- scan: for (let nest, isCovered; this.stoppedAt == null || cursor.from < this.stoppedAt;) {
1841
+ scan: for (let nest, isCovered;;) {
1804
1842
  let enter = true, range;
1805
- if (fragmentCursor.hasNode(cursor)) {
1843
+ if (this.stoppedAt != null && cursor.from >= this.stoppedAt) {
1844
+ enter = false;
1845
+ }
1846
+ else if (fragmentCursor.hasNode(cursor)) {
1806
1847
  if (overlay) {
1807
1848
  let match = overlay.mounts.find(m => m.frag.from <= cursor.from && m.frag.to >= cursor.to && m.mount.overlay);
1808
1849
  if (match)
@@ -1817,7 +1858,8 @@ class MixedParse {
1817
1858
  else if (covered && (isCovered = checkCover(covered.ranges, cursor.from, cursor.to))) {
1818
1859
  enter = isCovered != 2 /* Cover.Full */;
1819
1860
  }
1820
- else if (!cursor.type.isAnonymous && cursor.from < cursor.to && (nest = this.nest(cursor, this.input))) {
1861
+ else if (!cursor.type.isAnonymous && (nest = this.nest(cursor, this.input)) &&
1862
+ (cursor.from < cursor.to || !nest.overlay)) {
1821
1863
  if (!cursor.tree)
1822
1864
  materialize(cursor);
1823
1865
  let oldMounts = fragmentCursor.findMounts(cursor.from, nest.parser);
@@ -1825,9 +1867,13 @@ class MixedParse {
1825
1867
  overlay = new ActiveOverlay(nest.parser, nest.overlay, oldMounts, this.inner.length, cursor.from, cursor.tree, overlay);
1826
1868
  }
1827
1869
  else {
1828
- let ranges = punchRanges(this.ranges, nest.overlay || [new Range(cursor.from, cursor.to)]);
1870
+ let ranges = punchRanges(this.ranges, nest.overlay ||
1871
+ (cursor.from < cursor.to ? [new Range(cursor.from, cursor.to)] : []));
1829
1872
  if (ranges.length)
1830
- this.inner.push(new InnerParse(nest.parser, nest.parser.startParse(this.input, enterFragments(oldMounts, ranges), ranges), nest.overlay ? nest.overlay.map(r => new Range(r.from - cursor.from, r.to - cursor.from)) : null, cursor.tree, ranges));
1873
+ checkRanges(ranges);
1874
+ if (ranges.length || !nest.overlay)
1875
+ this.inner.push(new InnerParse(nest.parser, ranges.length ? nest.parser.startParse(this.input, enterFragments(oldMounts, ranges), ranges)
1876
+ : nest.parser.startParse(""), nest.overlay ? nest.overlay.map(r => new Range(r.from - cursor.from, r.to - cursor.from)) : null, cursor.tree, ranges.length ? ranges[0].from : cursor.from));
1831
1877
  if (!nest.overlay)
1832
1878
  enter = false;
1833
1879
  else if (ranges.length)
@@ -1854,8 +1900,10 @@ class MixedParse {
1854
1900
  break scan;
1855
1901
  if (overlay && !--overlay.depth) {
1856
1902
  let ranges = punchRanges(this.ranges, overlay.ranges);
1857
- if (ranges.length)
1858
- this.inner.splice(overlay.index, 0, new InnerParse(overlay.parser, overlay.parser.startParse(this.input, enterFragments(overlay.mounts, ranges), ranges), overlay.ranges.map(r => new Range(r.from - overlay.start, r.to - overlay.start)), overlay.target, ranges));
1903
+ if (ranges.length) {
1904
+ checkRanges(ranges);
1905
+ this.inner.splice(overlay.index, 0, new InnerParse(overlay.parser, overlay.parser.startParse(this.input, enterFragments(overlay.mounts, ranges), ranges), overlay.ranges.map(r => new Range(r.from - overlay.start, r.to - overlay.start)), overlay.target, ranges[0].from));
1906
+ }
1859
1907
  overlay = overlay.prev;
1860
1908
  }
1861
1909
  if (covered && !--covered.depth)
@@ -1889,39 +1937,38 @@ function sliceBuf(buf, startI, endI, nodes, positions, off) {
1889
1937
  // parse that was ran via the mix parser, and thus aren't shared with
1890
1938
  // any other code, making violations of the immutability safe.
1891
1939
  function materialize(cursor) {
1892
- let { node } = cursor, depth = 0;
1940
+ let { node } = cursor, stack = [];
1941
+ let buffer = node.context.buffer;
1893
1942
  // Scan up to the nearest tree
1894
1943
  do {
1944
+ stack.push(cursor.index);
1895
1945
  cursor.parent();
1896
- depth++;
1897
1946
  } while (!cursor.tree);
1898
1947
  // Find the index of the buffer in that tree
1899
- let i = 0, base = cursor.tree, off = 0;
1900
- for (;; i++) {
1901
- off = base.positions[i] + cursor.from;
1902
- if (off <= node.from && off + base.children[i].length >= node.to)
1903
- break;
1904
- }
1905
- let buf = base.children[i], b = buf.buffer;
1948
+ let base = cursor.tree, i = base.children.indexOf(buffer);
1949
+ let buf = base.children[i], b = buf.buffer, newStack = [i];
1906
1950
  // Split a level in the buffer, putting the nodes before and after
1907
1951
  // the child that contains `node` into new buffers.
1908
- function split(startI, endI, type, innerOffset, length) {
1909
- let i = startI;
1910
- while (b[i + 2] + off <= node.from)
1911
- i = b[i + 3];
1952
+ function split(startI, endI, type, innerOffset, length, stackPos) {
1953
+ let targetI = stack[stackPos];
1912
1954
  let children = [], positions = [];
1913
- sliceBuf(buf, startI, i, children, positions, innerOffset);
1914
- let from = b[i + 1], to = b[i + 2];
1915
- let isTarget = from + off == node.from && to + off == node.to && b[i] == node.type.id;
1916
- children.push(isTarget ? node.toTree() : split(i + 4, b[i + 3], buf.set.types[b[i]], from, to - from));
1955
+ sliceBuf(buf, startI, targetI, children, positions, innerOffset);
1956
+ let from = b[targetI + 1], to = b[targetI + 2];
1957
+ newStack.push(children.length);
1958
+ let child = stackPos
1959
+ ? split(targetI + 4, b[targetI + 3], buf.set.types[b[targetI]], from, to - from, stackPos - 1)
1960
+ : node.toTree();
1961
+ children.push(child);
1917
1962
  positions.push(from - innerOffset);
1918
- sliceBuf(buf, b[i + 3], endI, children, positions, innerOffset);
1963
+ sliceBuf(buf, b[targetI + 3], endI, children, positions, innerOffset);
1919
1964
  return new Tree(type, children, positions, length);
1920
1965
  }
1921
- base.children[i] = split(0, b.length, NodeType.none, 0, buf.length);
1966
+ base.children[i] = split(0, b.length, NodeType.none, 0, buf.length, stack.length - 1);
1922
1967
  // Move the cursor back to the target node
1923
- for (let d = 0; d <= depth; d++)
1924
- cursor.childAfter(node.from);
1968
+ for (let index of newStack) {
1969
+ let tree = cursor.tree.children[index], pos = cursor.tree.positions[index];
1970
+ cursor.yield(new TreeNode(tree, pos + cursor.from, index, cursor._tree));
1971
+ }
1925
1972
  }
1926
1973
  class StructureCursor {
1927
1974
  constructor(root, offset) {
package/dist/index.d.cts CHANGED
@@ -964,7 +964,6 @@ declare class TreeCursor implements SyntaxNodeRef {
964
964
  private bufferNode;
965
965
  private yieldNode;
966
966
  private yieldBuf;
967
- private yield;
968
967
  /**
969
968
  Move the cursor to this node's first child. When this returns
970
969
  false, the node has no child, and the cursor has not been moved.
package/dist/index.d.ts CHANGED
@@ -964,7 +964,6 @@ declare class TreeCursor implements SyntaxNodeRef {
964
964
  private bufferNode;
965
965
  private yieldNode;
966
966
  private yieldBuf;
967
- private yield;
968
967
  /**
969
968
  Move the cursor to this node's first child. When this returns
970
969
  false, the node has no child, and the cursor has not been moved.
package/dist/index.js CHANGED
@@ -883,8 +883,6 @@ class BufferNode extends BaseNode {
883
883
  function iterStack(heads) {
884
884
  if (!heads.length)
885
885
  return null;
886
- if (heads.length == 1)
887
- return heads[0];
888
886
  let pick = 0, picked = heads[0];
889
887
  for (let i = 1; i < heads.length; i++) {
890
888
  let node = heads[i];
@@ -920,7 +918,7 @@ function stackIterator(tree, pos, side) {
920
918
  let mount = MountedTree.get(scan.tree);
921
919
  // Relevant overlay branching off
922
920
  if (mount && mount.overlay && mount.overlay[0].from <= pos && mount.overlay[mount.overlay.length - 1].to >= pos) {
923
- let root = new TreeNode(mount.tree, mount.overlay[0].from + scan.from, 0, null);
921
+ let root = new TreeNode(mount.tree, mount.overlay[0].from + scan.from, -1, scan);
924
922
  (layers || (layers = [inner])).push(resolveNode(root, pos, side, false));
925
923
  }
926
924
  }
@@ -984,6 +982,9 @@ class TreeCursor {
984
982
  this.to = start + buffer.buffer[index + 2];
985
983
  return true;
986
984
  }
985
+ /**
986
+ @internal
987
+ */
987
988
  yield(node) {
988
989
  if (!node)
989
990
  return false;
@@ -1249,7 +1250,7 @@ function buildTree(data) {
1249
1250
  let cursor = Array.isArray(buffer) ? new FlatBufferCursor(buffer, buffer.length) : buffer;
1250
1251
  let types = nodeSet.types;
1251
1252
  let contextHash = 0, lookAhead = 0;
1252
- function takeNode(parentStart, minPos, children, positions, inRepeat) {
1253
+ function takeNode(parentStart, minPos, children, positions, inRepeat, depth) {
1253
1254
  let { id, start, end, size } = cursor;
1254
1255
  let lookAheadAtStart = lookAhead;
1255
1256
  while (size < 0) {
@@ -1298,8 +1299,11 @@ function buildTree(data) {
1298
1299
  }
1299
1300
  cursor.next();
1300
1301
  }
1302
+ else if (depth > 2500 /* CutOff.Depth */) {
1303
+ takeFlatNode(start, endPos, localChildren, localPositions);
1304
+ }
1301
1305
  else {
1302
- takeNode(start, endPos, localChildren, localPositions, localInRepeat);
1306
+ takeNode(start, endPos, localChildren, localPositions, localInRepeat, depth + 1);
1303
1307
  }
1304
1308
  }
1305
1309
  if (localInRepeat >= 0 && lastGroup > 0 && lastGroup < localChildren.length)
@@ -1317,6 +1321,38 @@ function buildTree(data) {
1317
1321
  children.push(node);
1318
1322
  positions.push(startPos);
1319
1323
  }
1324
+ function takeFlatNode(parentStart, minPos, children, positions) {
1325
+ let nodes = []; // Temporary, inverted array of leaf nodes found, with absolute positions
1326
+ let nodeCount = 0, stopAt = -1;
1327
+ while (cursor.pos > minPos) {
1328
+ let { id, start, end, size } = cursor;
1329
+ if (size > 4) { // Not a leaf
1330
+ cursor.next();
1331
+ }
1332
+ else if (stopAt > -1 && start < stopAt) {
1333
+ break;
1334
+ }
1335
+ else {
1336
+ if (stopAt < 0)
1337
+ stopAt = end - maxBufferLength;
1338
+ nodes.push(id, start, end);
1339
+ nodeCount++;
1340
+ cursor.next();
1341
+ }
1342
+ }
1343
+ if (nodeCount) {
1344
+ let buffer = new Uint16Array(nodeCount * 4);
1345
+ let start = nodes[nodes.length - 2];
1346
+ for (let i = nodes.length - 3, j = 0; i >= 0; i -= 3) {
1347
+ buffer[j++] = nodes[i];
1348
+ buffer[j++] = nodes[i + 1] - start;
1349
+ buffer[j++] = nodes[i + 2] - start;
1350
+ buffer[j++] = j;
1351
+ }
1352
+ children.push(new TreeBuffer(buffer, nodes[2] - start, nodeSet));
1353
+ positions.push(start - parentStart);
1354
+ }
1355
+ }
1320
1356
  function makeBalanced(type) {
1321
1357
  return (children, positions, length) => {
1322
1358
  let lookAhead = 0, lastI = children.length - 1, last, lookAheadProp;
@@ -1427,7 +1463,7 @@ function buildTree(data) {
1427
1463
  }
1428
1464
  let children = [], positions = [];
1429
1465
  while (cursor.pos > 0)
1430
- takeNode(data.start || 0, data.bufferStart || 0, children, positions, -1);
1466
+ takeNode(data.start || 0, data.bufferStart || 0, children, positions, -1, 0);
1431
1467
  let length = (_a = data.length) !== null && _a !== void 0 ? _a : (children.length ? positions[0] + children[0].length : 0);
1432
1468
  return new Tree(types[data.topID], children.reverse(), positions.reverse(), length);
1433
1469
  }
@@ -1708,16 +1744,18 @@ function parseMixed(nest) {
1708
1744
  return (parse, input, fragments, ranges) => new MixedParse(parse, nest, input, fragments, ranges);
1709
1745
  }
1710
1746
  class InnerParse {
1711
- constructor(parser, parse, overlay, target, ranges) {
1747
+ constructor(parser, parse, overlay, target, from) {
1712
1748
  this.parser = parser;
1713
1749
  this.parse = parse;
1714
1750
  this.overlay = overlay;
1715
1751
  this.target = target;
1716
- this.ranges = ranges;
1717
- if (!ranges.length || ranges.some(r => r.from >= r.to))
1718
- throw new RangeError("Invalid inner parse ranges given: " + JSON.stringify(ranges));
1752
+ this.from = from;
1719
1753
  }
1720
1754
  }
1755
+ function checkRanges(ranges) {
1756
+ if (!ranges.length || ranges.some(r => r.from >= r.to))
1757
+ throw new RangeError("Invalid inner parse ranges given: " + JSON.stringify(ranges));
1758
+ }
1721
1759
  class ActiveOverlay {
1722
1760
  constructor(parser, predicate, mounts, index, start, target, prev) {
1723
1761
  this.parser = parser;
@@ -1780,7 +1818,7 @@ class MixedParse {
1780
1818
  return 0;
1781
1819
  let pos = this.input.length;
1782
1820
  for (let i = this.innerDone; i < this.inner.length; i++) {
1783
- if (this.inner[i].ranges[0].from < pos)
1821
+ if (this.inner[i].from < pos)
1784
1822
  pos = Math.min(pos, this.inner[i].parse.parsedPos);
1785
1823
  }
1786
1824
  return pos;
@@ -1798,9 +1836,12 @@ class MixedParse {
1798
1836
  let overlay = null;
1799
1837
  let covered = null;
1800
1838
  let cursor = new TreeCursor(new TreeNode(this.baseTree, this.ranges[0].from, 0, null), IterMode.IncludeAnonymous | IterMode.IgnoreMounts);
1801
- scan: for (let nest, isCovered; this.stoppedAt == null || cursor.from < this.stoppedAt;) {
1839
+ scan: for (let nest, isCovered;;) {
1802
1840
  let enter = true, range;
1803
- if (fragmentCursor.hasNode(cursor)) {
1841
+ if (this.stoppedAt != null && cursor.from >= this.stoppedAt) {
1842
+ enter = false;
1843
+ }
1844
+ else if (fragmentCursor.hasNode(cursor)) {
1804
1845
  if (overlay) {
1805
1846
  let match = overlay.mounts.find(m => m.frag.from <= cursor.from && m.frag.to >= cursor.to && m.mount.overlay);
1806
1847
  if (match)
@@ -1815,7 +1856,8 @@ class MixedParse {
1815
1856
  else if (covered && (isCovered = checkCover(covered.ranges, cursor.from, cursor.to))) {
1816
1857
  enter = isCovered != 2 /* Cover.Full */;
1817
1858
  }
1818
- else if (!cursor.type.isAnonymous && cursor.from < cursor.to && (nest = this.nest(cursor, this.input))) {
1859
+ else if (!cursor.type.isAnonymous && (nest = this.nest(cursor, this.input)) &&
1860
+ (cursor.from < cursor.to || !nest.overlay)) {
1819
1861
  if (!cursor.tree)
1820
1862
  materialize(cursor);
1821
1863
  let oldMounts = fragmentCursor.findMounts(cursor.from, nest.parser);
@@ -1823,9 +1865,13 @@ class MixedParse {
1823
1865
  overlay = new ActiveOverlay(nest.parser, nest.overlay, oldMounts, this.inner.length, cursor.from, cursor.tree, overlay);
1824
1866
  }
1825
1867
  else {
1826
- let ranges = punchRanges(this.ranges, nest.overlay || [new Range(cursor.from, cursor.to)]);
1868
+ let ranges = punchRanges(this.ranges, nest.overlay ||
1869
+ (cursor.from < cursor.to ? [new Range(cursor.from, cursor.to)] : []));
1827
1870
  if (ranges.length)
1828
- this.inner.push(new InnerParse(nest.parser, nest.parser.startParse(this.input, enterFragments(oldMounts, ranges), ranges), nest.overlay ? nest.overlay.map(r => new Range(r.from - cursor.from, r.to - cursor.from)) : null, cursor.tree, ranges));
1871
+ checkRanges(ranges);
1872
+ if (ranges.length || !nest.overlay)
1873
+ this.inner.push(new InnerParse(nest.parser, ranges.length ? nest.parser.startParse(this.input, enterFragments(oldMounts, ranges), ranges)
1874
+ : nest.parser.startParse(""), nest.overlay ? nest.overlay.map(r => new Range(r.from - cursor.from, r.to - cursor.from)) : null, cursor.tree, ranges.length ? ranges[0].from : cursor.from));
1829
1875
  if (!nest.overlay)
1830
1876
  enter = false;
1831
1877
  else if (ranges.length)
@@ -1852,8 +1898,10 @@ class MixedParse {
1852
1898
  break scan;
1853
1899
  if (overlay && !--overlay.depth) {
1854
1900
  let ranges = punchRanges(this.ranges, overlay.ranges);
1855
- if (ranges.length)
1856
- this.inner.splice(overlay.index, 0, new InnerParse(overlay.parser, overlay.parser.startParse(this.input, enterFragments(overlay.mounts, ranges), ranges), overlay.ranges.map(r => new Range(r.from - overlay.start, r.to - overlay.start)), overlay.target, ranges));
1901
+ if (ranges.length) {
1902
+ checkRanges(ranges);
1903
+ this.inner.splice(overlay.index, 0, new InnerParse(overlay.parser, overlay.parser.startParse(this.input, enterFragments(overlay.mounts, ranges), ranges), overlay.ranges.map(r => new Range(r.from - overlay.start, r.to - overlay.start)), overlay.target, ranges[0].from));
1904
+ }
1857
1905
  overlay = overlay.prev;
1858
1906
  }
1859
1907
  if (covered && !--covered.depth)
@@ -1887,39 +1935,38 @@ function sliceBuf(buf, startI, endI, nodes, positions, off) {
1887
1935
  // parse that was ran via the mix parser, and thus aren't shared with
1888
1936
  // any other code, making violations of the immutability safe.
1889
1937
  function materialize(cursor) {
1890
- let { node } = cursor, depth = 0;
1938
+ let { node } = cursor, stack = [];
1939
+ let buffer = node.context.buffer;
1891
1940
  // Scan up to the nearest tree
1892
1941
  do {
1942
+ stack.push(cursor.index);
1893
1943
  cursor.parent();
1894
- depth++;
1895
1944
  } while (!cursor.tree);
1896
1945
  // Find the index of the buffer in that tree
1897
- let i = 0, base = cursor.tree, off = 0;
1898
- for (;; i++) {
1899
- off = base.positions[i] + cursor.from;
1900
- if (off <= node.from && off + base.children[i].length >= node.to)
1901
- break;
1902
- }
1903
- let buf = base.children[i], b = buf.buffer;
1946
+ let base = cursor.tree, i = base.children.indexOf(buffer);
1947
+ let buf = base.children[i], b = buf.buffer, newStack = [i];
1904
1948
  // Split a level in the buffer, putting the nodes before and after
1905
1949
  // the child that contains `node` into new buffers.
1906
- function split(startI, endI, type, innerOffset, length) {
1907
- let i = startI;
1908
- while (b[i + 2] + off <= node.from)
1909
- i = b[i + 3];
1950
+ function split(startI, endI, type, innerOffset, length, stackPos) {
1951
+ let targetI = stack[stackPos];
1910
1952
  let children = [], positions = [];
1911
- sliceBuf(buf, startI, i, children, positions, innerOffset);
1912
- let from = b[i + 1], to = b[i + 2];
1913
- let isTarget = from + off == node.from && to + off == node.to && b[i] == node.type.id;
1914
- children.push(isTarget ? node.toTree() : split(i + 4, b[i + 3], buf.set.types[b[i]], from, to - from));
1953
+ sliceBuf(buf, startI, targetI, children, positions, innerOffset);
1954
+ let from = b[targetI + 1], to = b[targetI + 2];
1955
+ newStack.push(children.length);
1956
+ let child = stackPos
1957
+ ? split(targetI + 4, b[targetI + 3], buf.set.types[b[targetI]], from, to - from, stackPos - 1)
1958
+ : node.toTree();
1959
+ children.push(child);
1915
1960
  positions.push(from - innerOffset);
1916
- sliceBuf(buf, b[i + 3], endI, children, positions, innerOffset);
1961
+ sliceBuf(buf, b[targetI + 3], endI, children, positions, innerOffset);
1917
1962
  return new Tree(type, children, positions, length);
1918
1963
  }
1919
- base.children[i] = split(0, b.length, NodeType.none, 0, buf.length);
1964
+ base.children[i] = split(0, b.length, NodeType.none, 0, buf.length, stack.length - 1);
1920
1965
  // Move the cursor back to the target node
1921
- for (let d = 0; d <= depth; d++)
1922
- cursor.childAfter(node.from);
1966
+ for (let index of newStack) {
1967
+ let tree = cursor.tree.children[index], pos = cursor.tree.positions[index];
1968
+ cursor.yield(new TreeNode(tree, pos + cursor.from, index, cursor._tree));
1969
+ }
1923
1970
  }
1924
1971
  class StructureCursor {
1925
1972
  constructor(root, offset) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lezer/common",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "Syntax tree data structure and parser interfaces for the lezer parser",
5
5
  "main": "dist/index.cjs",
6
6
  "type": "module",