@lezer/common 1.0.0 → 1.0.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
@@ -116,8 +116,8 @@ class NodeType {
116
116
  /// Define a node type.
117
117
  static define(spec) {
118
118
  let props = spec.props && spec.props.length ? Object.create(null) : noProps;
119
- let flags = (spec.top ? 1 /* Top */ : 0) | (spec.skipped ? 2 /* Skipped */ : 0) |
120
- (spec.error ? 4 /* Error */ : 0) | (spec.name == null ? 8 /* Anonymous */ : 0);
119
+ let flags = (spec.top ? 1 /* NodeFlag.Top */ : 0) | (spec.skipped ? 2 /* NodeFlag.Skipped */ : 0) |
120
+ (spec.error ? 4 /* NodeFlag.Error */ : 0) | (spec.name == null ? 8 /* NodeFlag.Anonymous */ : 0);
121
121
  let type = new NodeType(spec.name || "", props, spec.id, flags);
122
122
  if (spec.props)
123
123
  for (let src of spec.props) {
@@ -135,14 +135,14 @@ class NodeType {
135
135
  /// the prop isn't present on this node.
136
136
  prop(prop) { return this.props[prop.id]; }
137
137
  /// True when this is the top node of a grammar.
138
- get isTop() { return (this.flags & 1 /* Top */) > 0; }
138
+ get isTop() { return (this.flags & 1 /* NodeFlag.Top */) > 0; }
139
139
  /// True when this node is produced by a skip rule.
140
- get isSkipped() { return (this.flags & 2 /* Skipped */) > 0; }
140
+ get isSkipped() { return (this.flags & 2 /* NodeFlag.Skipped */) > 0; }
141
141
  /// Indicates whether this is an error node.
142
- get isError() { return (this.flags & 4 /* Error */) > 0; }
142
+ get isError() { return (this.flags & 4 /* NodeFlag.Error */) > 0; }
143
143
  /// When true, this node type doesn't correspond to a user-declared
144
144
  /// named node, for example because it is used to cache repetition.
145
- get isAnonymous() { return (this.flags & 8 /* Anonymous */) > 0; }
145
+ get isAnonymous() { return (this.flags & 8 /* NodeFlag.Anonymous */) > 0; }
146
146
  /// Returns true when this node's name or one of its
147
147
  /// [groups](#common.NodeProp^group) matches the given string.
148
148
  is(name) {
@@ -175,7 +175,7 @@ class NodeType {
175
175
  }
176
176
  }
177
177
  /// An empty dummy node type to use when no actual type is available.
178
- NodeType.none = new NodeType("", Object.create(null), 0, 8 /* Anonymous */);
178
+ NodeType.none = new NodeType("", Object.create(null), 0, 8 /* NodeFlag.Anonymous */);
179
179
  /// A node set holds a collection of node types. It is used to
180
180
  /// compactly represent trees by storing their type ids, rather than a
181
181
  /// full pointer to the type object, in a numeric array. Each parser
@@ -321,6 +321,10 @@ class Tree {
321
321
  /// position. If 1, it'll move into nodes that start at the
322
322
  /// position. With 0, it'll only enter nodes that cover the position
323
323
  /// from both sides.
324
+ ///
325
+ /// Note that this will not enter
326
+ /// [overlays](#common.MountedTree.overlay), and you often want
327
+ /// [`resolveInner`](#common.Tree.resolveInner) instead.
324
328
  resolve(pos, side = 0) {
325
329
  let node = resolveNode(CachedNode.get(this) || this.topNode, pos, side, false);
326
330
  CachedNode.set(this, node);
@@ -380,7 +384,7 @@ class Tree {
380
384
  /// which may have children grouped into subtrees with type
381
385
  /// [`NodeType.none`](#common.NodeType^none).
382
386
  balance(config = {}) {
383
- return this.children.length <= 8 /* BranchFactor */ ? this :
387
+ return this.children.length <= 8 /* Balance.BranchFactor */ ? this :
384
388
  balanceRange(NodeType.none, this.children, this.positions, 0, this.children.length, 0, this.length, (children, positions, length) => new Tree(this.type, children, positions, length, this.propValues), config.makeTree || ((children, positions, length) => new Tree(NodeType.none, children, positions, length)));
385
389
  }
386
390
  /// Build a tree from a postfix-ordered buffer of node information,
@@ -459,26 +463,27 @@ class TreeBuffer {
459
463
  return pick;
460
464
  }
461
465
  /// @internal
462
- slice(startI, endI, from, to) {
466
+ slice(startI, endI, from) {
463
467
  let b = this.buffer;
464
- let copy = new Uint16Array(endI - startI);
468
+ let copy = new Uint16Array(endI - startI), len = 0;
465
469
  for (let i = startI, j = 0; i < endI;) {
466
470
  copy[j++] = b[i++];
467
471
  copy[j++] = b[i++] - from;
468
- copy[j++] = b[i++] - from;
472
+ let to = copy[j++] = b[i++] - from;
469
473
  copy[j++] = b[i++] - startI;
474
+ len = Math.max(len, to);
470
475
  }
471
- return new TreeBuffer(copy, to - from, this.set);
476
+ return new TreeBuffer(copy, len, this.set);
472
477
  }
473
478
  }
474
479
  function checkSide(side, pos, from, to) {
475
480
  switch (side) {
476
- case -2 /* Before */: return from < pos;
477
- case -1 /* AtOrBefore */: return to >= pos && from < pos;
478
- case 0 /* Around */: return from < pos && to > pos;
479
- case 1 /* AtOrAfter */: return from <= pos && to > pos;
480
- case 2 /* After */: return to > pos;
481
- case 4 /* DontCare */: return true;
481
+ case -2 /* Side.Before */: return from < pos;
482
+ case -1 /* Side.AtOrBefore */: return to >= pos && from < pos;
483
+ case 0 /* Side.Around */: return from < pos && to > pos;
484
+ case 1 /* Side.AtOrAfter */: return from <= pos && to > pos;
485
+ case 2 /* Side.After */: return to > pos;
486
+ case 4 /* Side.DontCare */: return true;
482
487
  }
483
488
  }
484
489
  function enterUnfinishedNodesBefore(node, pos) {
@@ -568,10 +573,10 @@ class TreeNode {
568
573
  return null;
569
574
  }
570
575
  }
571
- get firstChild() { return this.nextChild(0, 1, 0, 4 /* DontCare */); }
572
- get lastChild() { return this.nextChild(this._tree.children.length - 1, -1, 0, 4 /* DontCare */); }
573
- childAfter(pos) { return this.nextChild(0, 1, pos, 2 /* After */); }
574
- childBefore(pos) { return this.nextChild(this._tree.children.length - 1, -1, pos, -2 /* Before */); }
576
+ get firstChild() { return this.nextChild(0, 1, 0, 4 /* Side.DontCare */); }
577
+ get lastChild() { return this.nextChild(this._tree.children.length - 1, -1, 0, 4 /* Side.DontCare */); }
578
+ childAfter(pos) { return this.nextChild(0, 1, pos, 2 /* Side.After */); }
579
+ childBefore(pos) { return this.nextChild(this._tree.children.length - 1, -1, pos, -2 /* Side.Before */); }
575
580
  enter(pos, side, mode = 0) {
576
581
  let mounted;
577
582
  if (!(mode & exports.IterMode.IgnoreOverlays) && (mounted = this._tree.prop(NodeProp.mounted)) && mounted.overlay) {
@@ -594,10 +599,10 @@ class TreeNode {
594
599
  return this._parent ? this._parent.nextSignificantParent() : null;
595
600
  }
596
601
  get nextSibling() {
597
- return this._parent && this.index >= 0 ? this._parent.nextChild(this.index + 1, 1, 0, 4 /* DontCare */) : null;
602
+ return this._parent && this.index >= 0 ? this._parent.nextChild(this.index + 1, 1, 0, 4 /* Side.DontCare */) : null;
598
603
  }
599
604
  get prevSibling() {
600
- return this._parent && this.index >= 0 ? this._parent.nextChild(this.index - 1, -1, 0, 4 /* DontCare */) : null;
605
+ return this._parent && this.index >= 0 ? this._parent.nextChild(this.index - 1, -1, 0, 4 /* Side.DontCare */) : null;
601
606
  }
602
607
  cursor(mode = 0) { return new TreeCursor(this, mode); }
603
608
  get tree() { return this._tree; }
@@ -659,24 +664,24 @@ class BufferContext {
659
664
  }
660
665
  }
661
666
  class BufferNode {
667
+ get name() { return this.type.name; }
668
+ get from() { return this.context.start + this.context.buffer.buffer[this.index + 1]; }
669
+ get to() { return this.context.start + this.context.buffer.buffer[this.index + 2]; }
662
670
  constructor(context, _parent, index) {
663
671
  this.context = context;
664
672
  this._parent = _parent;
665
673
  this.index = index;
666
674
  this.type = context.buffer.set.types[context.buffer.buffer[index]];
667
675
  }
668
- get name() { return this.type.name; }
669
- get from() { return this.context.start + this.context.buffer.buffer[this.index + 1]; }
670
- get to() { return this.context.start + this.context.buffer.buffer[this.index + 2]; }
671
676
  child(dir, pos, side) {
672
677
  let { buffer } = this.context;
673
678
  let index = buffer.findChild(this.index + 4, buffer.buffer[this.index + 3], dir, pos - this.context.start, side);
674
679
  return index < 0 ? null : new BufferNode(this.context, this, index);
675
680
  }
676
- get firstChild() { return this.child(1, 0, 4 /* DontCare */); }
677
- get lastChild() { return this.child(-1, 0, 4 /* DontCare */); }
678
- childAfter(pos) { return this.child(1, pos, 2 /* After */); }
679
- childBefore(pos) { return this.child(-1, pos, -2 /* Before */); }
681
+ get firstChild() { return this.child(1, 0, 4 /* Side.DontCare */); }
682
+ get lastChild() { return this.child(-1, 0, 4 /* Side.DontCare */); }
683
+ childAfter(pos) { return this.child(1, pos, 2 /* Side.After */); }
684
+ childBefore(pos) { return this.child(-1, pos, -2 /* Side.Before */); }
680
685
  enter(pos, side, mode = 0) {
681
686
  if (mode & exports.IterMode.ExcludeBuffers)
682
687
  return null;
@@ -688,7 +693,7 @@ class BufferNode {
688
693
  return this._parent || this.context.parent.nextSignificantParent();
689
694
  }
690
695
  externalSibling(dir) {
691
- return this._parent ? null : this.context.parent.nextChild(this.context.index + dir, dir, 0, 4 /* DontCare */);
696
+ return this._parent ? null : this.context.parent.nextChild(this.context.index + dir, dir, 0, 4 /* Side.DontCare */);
692
697
  }
693
698
  get nextSibling() {
694
699
  let { buffer } = this.context;
@@ -702,7 +707,7 @@ class BufferNode {
702
707
  let parentStart = this._parent ? this._parent.index + 4 : 0;
703
708
  if (this.index == parentStart)
704
709
  return this.externalSibling(-1);
705
- return new BufferNode(this.context, this._parent, buffer.findChild(parentStart, this.index, -1, 0, 4 /* DontCare */));
710
+ return new BufferNode(this.context, this._parent, buffer.findChild(parentStart, this.index, -1, 0, 4 /* Side.DontCare */));
706
711
  }
707
712
  cursor(mode = 0) { return new TreeCursor(this, mode); }
708
713
  get tree() { return null; }
@@ -711,8 +716,8 @@ class BufferNode {
711
716
  let { buffer } = this.context;
712
717
  let startI = this.index + 4, endI = buffer.buffer[this.index + 3];
713
718
  if (endI > startI) {
714
- let from = buffer.buffer[this.index + 1], to = buffer.buffer[this.index + 2];
715
- children.push(buffer.slice(startI, endI, from, to));
719
+ let from = buffer.buffer[this.index + 1];
720
+ children.push(buffer.slice(startI, endI, from));
716
721
  positions.push(0);
717
722
  }
718
723
  return new Tree(this.type, children, positions, this.to - this.from);
@@ -739,6 +744,8 @@ class BufferNode {
739
744
  /// A tree cursor object focuses on a given node in a syntax tree, and
740
745
  /// allows you to move to adjacent nodes.
741
746
  class TreeCursor {
747
+ /// Shorthand for `.type.name`.
748
+ get name() { return this.type.name; }
742
749
  /// @internal
743
750
  constructor(node,
744
751
  /// @internal
@@ -762,8 +769,6 @@ class TreeCursor {
762
769
  this.yieldBuf(node.index);
763
770
  }
764
771
  }
765
- /// Shorthand for `.type.name`.
766
- get name() { return this.type.name; }
767
772
  yieldNode(node) {
768
773
  if (!node)
769
774
  return false;
@@ -808,13 +813,13 @@ class TreeCursor {
808
813
  }
809
814
  /// Move the cursor to this node's first child. When this returns
810
815
  /// false, the node has no child, and the cursor has not been moved.
811
- firstChild() { return this.enterChild(1, 0, 4 /* DontCare */); }
816
+ firstChild() { return this.enterChild(1, 0, 4 /* Side.DontCare */); }
812
817
  /// Move the cursor to this node's last child.
813
- lastChild() { return this.enterChild(-1, 0, 4 /* DontCare */); }
818
+ lastChild() { return this.enterChild(-1, 0, 4 /* Side.DontCare */); }
814
819
  /// Move the cursor to the first child that ends after `pos`.
815
- childAfter(pos) { return this.enterChild(1, pos, 2 /* After */); }
820
+ childAfter(pos) { return this.enterChild(1, pos, 2 /* Side.After */); }
816
821
  /// Move to the last child that starts before `pos`.
817
- childBefore(pos) { return this.enterChild(-1, pos, -2 /* Before */); }
822
+ childBefore(pos) { return this.enterChild(-1, pos, -2 /* Side.Before */); }
818
823
  /// Move the cursor to the child around `pos`. If side is -1 the
819
824
  /// child may end at that position, when 1 it may start there. This
820
825
  /// will also enter [overlaid](#common.MountedTree.overlay)
@@ -840,19 +845,19 @@ class TreeCursor {
840
845
  if (!this.buffer)
841
846
  return !this._tree._parent ? false
842
847
  : this.yield(this._tree.index < 0 ? null
843
- : this._tree._parent.nextChild(this._tree.index + dir, dir, 0, 4 /* DontCare */, this.mode));
848
+ : this._tree._parent.nextChild(this._tree.index + dir, dir, 0, 4 /* Side.DontCare */, this.mode));
844
849
  let { buffer } = this.buffer, d = this.stack.length - 1;
845
850
  if (dir < 0) {
846
851
  let parentStart = d < 0 ? 0 : this.stack[d] + 4;
847
852
  if (this.index != parentStart)
848
- return this.yieldBuf(buffer.findChild(parentStart, this.index, -1, 0, 4 /* DontCare */));
853
+ return this.yieldBuf(buffer.findChild(parentStart, this.index, -1, 0, 4 /* Side.DontCare */));
849
854
  }
850
855
  else {
851
856
  let after = buffer.buffer[this.index + 3];
852
857
  if (after < (d < 0 ? buffer.buffer.length : buffer.buffer[this.stack[d] + 3]))
853
858
  return this.yieldBuf(after);
854
859
  }
855
- return d < 0 ? this.yield(this.buffer.parent.nextChild(this.buffer.index + dir, dir, 0, 4 /* DontCare */, this.mode)) : false;
860
+ return d < 0 ? this.yield(this.buffer.parent.nextChild(this.buffer.index + dir, dir, 0, 4 /* Side.DontCare */, this.mode)) : false;
856
861
  }
857
862
  /// Move to this node's next sibling, if any.
858
863
  nextSibling() { return this.sibling(1); }
@@ -889,7 +894,7 @@ class TreeCursor {
889
894
  return true;
890
895
  }
891
896
  move(dir, enter) {
892
- if (enter && this.enterChild(dir, 0, 4 /* DontCare */))
897
+ if (enter && this.enterChild(dir, 0, 4 /* Side.DontCare */))
893
898
  return true;
894
899
  for (;;) {
895
900
  if (this.sibling(dir))
@@ -899,7 +904,7 @@ class TreeCursor {
899
904
  }
900
905
  }
901
906
  /// Move to the next node in a
902
- /// [pre-order](https://en.wikipedia.org/wiki/Tree_traversal#Pre-order_(NLR))
907
+ /// [pre-order](https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR)
903
908
  /// traversal, going from a node to its first child or, if the
904
909
  /// current node is empty or `enter` is false, its next sibling or
905
910
  /// the next sibling of the first parent node that has one.
@@ -1015,17 +1020,17 @@ function buildTree(data) {
1015
1020
  let lookAheadAtStart = lookAhead;
1016
1021
  while (size < 0) {
1017
1022
  cursor.next();
1018
- if (size == -1 /* Reuse */) {
1023
+ if (size == -1 /* SpecialRecord.Reuse */) {
1019
1024
  let node = reused[id];
1020
1025
  children.push(node);
1021
1026
  positions.push(start - parentStart);
1022
1027
  return;
1023
1028
  }
1024
- else if (size == -3 /* ContextChange */) { // Context change
1029
+ else if (size == -3 /* SpecialRecord.ContextChange */) { // Context change
1025
1030
  contextHash = id;
1026
1031
  return;
1027
1032
  }
1028
- else if (size == -4 /* LookAhead */) {
1033
+ else if (size == -4 /* SpecialRecord.LookAhead */) {
1029
1034
  lookAhead = id;
1030
1035
  return;
1031
1036
  }
@@ -1142,7 +1147,7 @@ function buildTree(data) {
1142
1147
  fork.next();
1143
1148
  while (fork.pos > startPos) {
1144
1149
  if (fork.size < 0) {
1145
- if (fork.size == -3 /* ContextChange */)
1150
+ if (fork.size == -3 /* SpecialRecord.ContextChange */)
1146
1151
  localSkipped += 4;
1147
1152
  else
1148
1153
  break scan;
@@ -1178,10 +1183,10 @@ function buildTree(data) {
1178
1183
  buffer[--index] = start - bufferStart;
1179
1184
  buffer[--index] = id;
1180
1185
  }
1181
- else if (size == -3 /* ContextChange */) {
1186
+ else if (size == -3 /* SpecialRecord.ContextChange */) {
1182
1187
  contextHash = id;
1183
1188
  }
1184
- else if (size == -4 /* LookAhead */) {
1189
+ else if (size == -4 /* SpecialRecord.LookAhead */) {
1185
1190
  lookAhead = id;
1186
1191
  }
1187
1192
  return index;
@@ -1228,7 +1233,7 @@ mkTree) {
1228
1233
  let total = 0;
1229
1234
  for (let i = from; i < to; i++)
1230
1235
  total += nodeSize(balanceType, children[i]);
1231
- let maxChild = Math.ceil((total * 1.5) / 8 /* BranchFactor */);
1236
+ let maxChild = Math.ceil((total * 1.5) / 8 /* Balance.BranchFactor */);
1232
1237
  let localChildren = [], localPositions = [];
1233
1238
  function divide(children, positions, from, to, offset) {
1234
1239
  for (let i = from; i < to;) {
@@ -1331,16 +1336,16 @@ class TreeFragment {
1331
1336
  this.to = to;
1332
1337
  this.tree = tree;
1333
1338
  this.offset = offset;
1334
- this.open = (openStart ? 1 /* Start */ : 0) | (openEnd ? 2 /* End */ : 0);
1339
+ this.open = (openStart ? 1 /* Open.Start */ : 0) | (openEnd ? 2 /* Open.End */ : 0);
1335
1340
  }
1336
1341
  /// Whether the start of the fragment represents the start of a
1337
1342
  /// parse, or the end of a change. (In the second case, it may not
1338
1343
  /// be safe to reuse some nodes at the start, depending on the
1339
1344
  /// parsing algorithm.)
1340
- get openStart() { return (this.open & 1 /* Start */) > 0; }
1345
+ get openStart() { return (this.open & 1 /* Open.Start */) > 0; }
1341
1346
  /// Whether the end of the fragment represents the end of a
1342
1347
  /// full-document parse, or the start of a change.
1343
- get openEnd() { return (this.open & 2 /* End */) > 0; }
1348
+ get openEnd() { return (this.open & 2 /* Open.End */) > 0; }
1344
1349
  /// Create a set of fragments from a freshly parsed tree, or update
1345
1350
  /// an existing set of fragments by replacing the ones that overlap
1346
1351
  /// with a tree with content from the new tree. When `partial` is
@@ -1534,7 +1539,7 @@ class MixedParse {
1534
1539
  enter = false;
1535
1540
  }
1536
1541
  else if (covered && (isCovered = checkCover(covered.ranges, cursor.from, cursor.to))) {
1537
- enter = isCovered != 2 /* Full */;
1542
+ enter = isCovered != 2 /* Cover.Full */;
1538
1543
  }
1539
1544
  else if (!cursor.type.isAnonymous && cursor.from < cursor.to && (nest = this.nest(cursor, this.input))) {
1540
1545
  if (!cursor.tree)
@@ -1589,16 +1594,16 @@ function checkCover(covered, from, to) {
1589
1594
  if (range.from >= to)
1590
1595
  break;
1591
1596
  if (range.to > from)
1592
- return range.from <= from && range.to >= to ? 2 /* Full */ : 1 /* Partial */;
1597
+ return range.from <= from && range.to >= to ? 2 /* Cover.Full */ : 1 /* Cover.Partial */;
1593
1598
  }
1594
- return 0 /* None */;
1599
+ return 0 /* Cover.None */;
1595
1600
  }
1596
1601
  // Take a piece of buffer and convert it into a stand-alone
1597
1602
  // TreeBuffer.
1598
1603
  function sliceBuf(buf, startI, endI, nodes, positions, off) {
1599
1604
  if (startI < endI) {
1600
- let from = buf.buffer[startI + 1], to = buf.buffer[endI - 2];
1601
- nodes.push(buf.slice(startI, endI, from, to));
1605
+ let from = buf.buffer[startI + 1];
1606
+ nodes.push(buf.slice(startI, endI, from));
1602
1607
  positions.push(from - off);
1603
1608
  }
1604
1609
  }
@@ -1803,14 +1808,14 @@ function enterFragments(mounts, ranges) {
1803
1808
  for (let i = 0, pos = from;; i++) {
1804
1809
  let last = i == changes.length, end = last ? to : changes[i].from;
1805
1810
  if (end > pos)
1806
- result.push(new TreeFragment(pos, end, mount.tree, -startPos, frag.from >= pos, frag.to <= end));
1811
+ result.push(new TreeFragment(pos, end, mount.tree, -startPos, frag.from >= pos || frag.openStart, frag.to <= end || frag.openEnd));
1807
1812
  if (last)
1808
1813
  break;
1809
1814
  pos = changes[i].to;
1810
1815
  }
1811
1816
  }
1812
1817
  else {
1813
- result.push(new TreeFragment(from, to, mount.tree, -startPos, frag.from >= startPos, frag.to <= endPos));
1818
+ result.push(new TreeFragment(from, to, mount.tree, -startPos, frag.from >= startPos || frag.openStart, frag.to <= endPos || frag.openEnd));
1814
1819
  }
1815
1820
  }
1816
1821
  return result;