@lezer/common 0.15.4 → 0.15.8

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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @lezer/common
2
2
 
3
- [ [**WEBSITE**](http://lezer.codemirror.net) | [**ISSUES**](https://github.com/lezer-parser/lezer/issues) | [**FORUM**](https://discuss.codemirror.net/c/lezer) | [**CHANGELOG**](https://github.com/lezer-parser/lezer-tree/blob/master/CHANGELOG.md) ]
3
+ [ [**WEBSITE**](http://lezer.codemirror.net) | [**ISSUES**](https://github.com/lezer-parser/lezer/issues) | [**FORUM**](https://discuss.codemirror.net/c/lezer) | [**CHANGELOG**](https://github.com/lezer-parser/common/blob/master/CHANGELOG.md) ]
4
4
 
5
5
  [Lezer](https://lezer.codemirror.net/) is an incremental parser system
6
6
  intended for use in an editor or similar system.
package/dist/index.cjs CHANGED
@@ -364,7 +364,7 @@ class Tree {
364
364
  /// [`NodeType.none`](#common.NodeType^none).
365
365
  balance(config = {}) {
366
366
  return this.children.length <= 8 /* BranchFactor */ ? this :
367
- balanceRange(this.type, 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)));
367
+ 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)));
368
368
  }
369
369
  /// Build a tree from a postfix-ordered buffer of node information,
370
370
  /// or a cursor over such a buffer.
@@ -464,6 +464,22 @@ function checkSide(side, pos, from, to) {
464
464
  case 4 /* DontCare */: return true;
465
465
  }
466
466
  }
467
+ function enterUnfinishedNodesBefore(node, pos) {
468
+ let scan = node.childBefore(pos);
469
+ while (scan) {
470
+ let last = scan.lastChild;
471
+ if (!last || last.to != scan.to)
472
+ break;
473
+ if (last.type.isError && last.from == last.to) {
474
+ node = scan;
475
+ scan = last.prevSibling;
476
+ }
477
+ else {
478
+ scan = last;
479
+ }
480
+ }
481
+ return node;
482
+ }
467
483
  class TreeNode {
468
484
  constructor(node, _from,
469
485
  // Index in parent node, set to -1 if the node is not a direct child of _parent.node (overlay)
@@ -547,6 +563,7 @@ class TreeNode {
547
563
  resolve(pos, side = 0) {
548
564
  return this.cursor.moveTo(pos, side).node;
549
565
  }
566
+ enterUnfinishedNodesBefore(pos) { return enterUnfinishedNodesBefore(this, pos); }
550
567
  getChild(type, before = null, after = null) {
551
568
  let r = getChildren(this, type, before, after);
552
569
  return r.length ? r[0] : null;
@@ -644,6 +661,7 @@ class BufferNode {
644
661
  resolve(pos, side = 0) {
645
662
  return this.cursor.moveTo(pos, side).node;
646
663
  }
664
+ enterUnfinishedNodesBefore(pos) { return enterUnfinishedNodesBefore(this, pos); }
647
665
  /// @internal
648
666
  toString() { return this.context.buffer.childString(this.index); }
649
667
  getChild(type, before = null, after = null) {
@@ -741,7 +759,7 @@ class TreeCursor {
741
759
  return this.yield(this._tree.enter(pos, side, overlays, buffers));
742
760
  return buffers ? this.enterChild(1, pos, side) : false;
743
761
  }
744
- /// Move the node's parent node, if this isn't the top node.
762
+ /// Move to the node's parent node, if this isn't the top node.
745
763
  parent() {
746
764
  if (!this.buffer)
747
765
  return this.yieldNode((this.mode & 1 /* Full */) ? this._tree._parent : this._tree.parent);
@@ -1063,15 +1081,21 @@ function nodeSize(balanceType, node) {
1063
1081
  return 1;
1064
1082
  let size = nodeSizeCache.get(node);
1065
1083
  if (size == null) {
1066
- size = node.children.reduce((s, ch) => s + nodeSize(balanceType, ch), 1);
1084
+ size = 1;
1085
+ for (let child of node.children) {
1086
+ if (child.type != balanceType || !(child instanceof Tree)) {
1087
+ size = 1;
1088
+ break;
1089
+ }
1090
+ size += nodeSize(balanceType, child);
1091
+ }
1067
1092
  nodeSizeCache.set(node, size);
1068
1093
  }
1069
1094
  return size;
1070
1095
  }
1071
1096
  function balanceRange(
1072
- // The type to tag the resulting tree with. Will also be used for
1073
- // internal nodes when it is an anonymous type
1074
- type,
1097
+ // The type the balanced tree's inner nodes.
1098
+ balanceType,
1075
1099
  // The direct children and their positions
1076
1100
  children, positions,
1077
1101
  // The index range in children/positions to use
@@ -1086,22 +1110,22 @@ mkTop,
1086
1110
  mkTree) {
1087
1111
  let total = 0;
1088
1112
  for (let i = from; i < to; i++)
1089
- total += nodeSize(type, children[i]);
1113
+ total += nodeSize(balanceType, children[i]);
1090
1114
  let maxChild = Math.ceil((total * 1.5) / 8 /* BranchFactor */);
1091
1115
  let localChildren = [], localPositions = [];
1092
1116
  function divide(children, positions, from, to, offset) {
1093
1117
  for (let i = from; i < to;) {
1094
- let groupFrom = i, groupStart = positions[i], groupSize = nodeSize(type, children[i]);
1118
+ let groupFrom = i, groupStart = positions[i], groupSize = nodeSize(balanceType, children[i]);
1095
1119
  i++;
1096
1120
  for (; i < to; i++) {
1097
- let nextSize = nodeSize(type, children[i]);
1121
+ let nextSize = nodeSize(balanceType, children[i]);
1098
1122
  if (groupSize + nextSize >= maxChild)
1099
1123
  break;
1100
1124
  groupSize += nextSize;
1101
1125
  }
1102
1126
  if (i == groupFrom + 1) {
1103
1127
  if (groupSize > maxChild) {
1104
- let only = children[groupFrom];
1128
+ let only = children[groupFrom]; // Only trees can have a size > 1
1105
1129
  divide(only.children, only.positions, 0, only.children.length, positions[groupFrom] + offset);
1106
1130
  continue;
1107
1131
  }
@@ -1109,7 +1133,7 @@ mkTree) {
1109
1133
  }
1110
1134
  else {
1111
1135
  let length = positions[i - 1] + children[i - 1].length - groupStart;
1112
- localChildren.push(balanceRange(type, children, positions, groupFrom, i, groupStart, length, null, mkTree));
1136
+ localChildren.push(balanceRange(balanceType, children, positions, groupFrom, i, groupStart, length, null, mkTree));
1113
1137
  }
1114
1138
  localPositions.push(groupStart + offset - start);
1115
1139
  }
@@ -1324,7 +1348,8 @@ class MixedParse {
1324
1348
  let fragmentCursor = new FragmentCursor(this.fragments);
1325
1349
  let overlay = null;
1326
1350
  let covered = null;
1327
- scan: for (let cursor = this.baseTree.fullCursor(), nest, isCovered;;) {
1351
+ let cursor = new TreeCursor(new TreeNode(this.baseTree, this.ranges[0].from, 0, null), 1 /* Full */);
1352
+ scan: for (let nest, isCovered;;) {
1328
1353
  let enter = true, range;
1329
1354
  if (fragmentCursor.hasNode(cursor)) {
1330
1355
  if (overlay) {
@@ -1464,7 +1489,7 @@ class StructureCursor {
1464
1489
  }
1465
1490
  hasNode(cursor) {
1466
1491
  this.moveTo(cursor.from);
1467
- if (!this.done && this.cursor.from + this.offset == cursor.from) {
1492
+ if (!this.done && this.cursor.from + this.offset == cursor.from && this.cursor.tree) {
1468
1493
  for (let tree = this.cursor.tree;;) {
1469
1494
  if (tree == cursor.tree)
1470
1495
  return true;
package/dist/index.js CHANGED
@@ -360,7 +360,7 @@ class Tree {
360
360
  /// [`NodeType.none`](#common.NodeType^none).
361
361
  balance(config = {}) {
362
362
  return this.children.length <= 8 /* BranchFactor */ ? this :
363
- balanceRange(this.type, 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)));
363
+ 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)));
364
364
  }
365
365
  /// Build a tree from a postfix-ordered buffer of node information,
366
366
  /// or a cursor over such a buffer.
@@ -460,6 +460,22 @@ function checkSide(side, pos, from, to) {
460
460
  case 4 /* DontCare */: return true;
461
461
  }
462
462
  }
463
+ function enterUnfinishedNodesBefore(node, pos) {
464
+ let scan = node.childBefore(pos);
465
+ while (scan) {
466
+ let last = scan.lastChild;
467
+ if (!last || last.to != scan.to)
468
+ break;
469
+ if (last.type.isError && last.from == last.to) {
470
+ node = scan;
471
+ scan = last.prevSibling;
472
+ }
473
+ else {
474
+ scan = last;
475
+ }
476
+ }
477
+ return node;
478
+ }
463
479
  class TreeNode {
464
480
  constructor(node, _from,
465
481
  // Index in parent node, set to -1 if the node is not a direct child of _parent.node (overlay)
@@ -543,6 +559,7 @@ class TreeNode {
543
559
  resolve(pos, side = 0) {
544
560
  return this.cursor.moveTo(pos, side).node;
545
561
  }
562
+ enterUnfinishedNodesBefore(pos) { return enterUnfinishedNodesBefore(this, pos); }
546
563
  getChild(type, before = null, after = null) {
547
564
  let r = getChildren(this, type, before, after);
548
565
  return r.length ? r[0] : null;
@@ -640,6 +657,7 @@ class BufferNode {
640
657
  resolve(pos, side = 0) {
641
658
  return this.cursor.moveTo(pos, side).node;
642
659
  }
660
+ enterUnfinishedNodesBefore(pos) { return enterUnfinishedNodesBefore(this, pos); }
643
661
  /// @internal
644
662
  toString() { return this.context.buffer.childString(this.index); }
645
663
  getChild(type, before = null, after = null) {
@@ -737,7 +755,7 @@ class TreeCursor {
737
755
  return this.yield(this._tree.enter(pos, side, overlays, buffers));
738
756
  return buffers ? this.enterChild(1, pos, side) : false;
739
757
  }
740
- /// Move the node's parent node, if this isn't the top node.
758
+ /// Move to the node's parent node, if this isn't the top node.
741
759
  parent() {
742
760
  if (!this.buffer)
743
761
  return this.yieldNode((this.mode & 1 /* Full */) ? this._tree._parent : this._tree.parent);
@@ -1059,15 +1077,21 @@ function nodeSize(balanceType, node) {
1059
1077
  return 1;
1060
1078
  let size = nodeSizeCache.get(node);
1061
1079
  if (size == null) {
1062
- size = node.children.reduce((s, ch) => s + nodeSize(balanceType, ch), 1);
1080
+ size = 1;
1081
+ for (let child of node.children) {
1082
+ if (child.type != balanceType || !(child instanceof Tree)) {
1083
+ size = 1;
1084
+ break;
1085
+ }
1086
+ size += nodeSize(balanceType, child);
1087
+ }
1063
1088
  nodeSizeCache.set(node, size);
1064
1089
  }
1065
1090
  return size;
1066
1091
  }
1067
1092
  function balanceRange(
1068
- // The type to tag the resulting tree with. Will also be used for
1069
- // internal nodes when it is an anonymous type
1070
- type,
1093
+ // The type the balanced tree's inner nodes.
1094
+ balanceType,
1071
1095
  // The direct children and their positions
1072
1096
  children, positions,
1073
1097
  // The index range in children/positions to use
@@ -1082,22 +1106,22 @@ mkTop,
1082
1106
  mkTree) {
1083
1107
  let total = 0;
1084
1108
  for (let i = from; i < to; i++)
1085
- total += nodeSize(type, children[i]);
1109
+ total += nodeSize(balanceType, children[i]);
1086
1110
  let maxChild = Math.ceil((total * 1.5) / 8 /* BranchFactor */);
1087
1111
  let localChildren = [], localPositions = [];
1088
1112
  function divide(children, positions, from, to, offset) {
1089
1113
  for (let i = from; i < to;) {
1090
- let groupFrom = i, groupStart = positions[i], groupSize = nodeSize(type, children[i]);
1114
+ let groupFrom = i, groupStart = positions[i], groupSize = nodeSize(balanceType, children[i]);
1091
1115
  i++;
1092
1116
  for (; i < to; i++) {
1093
- let nextSize = nodeSize(type, children[i]);
1117
+ let nextSize = nodeSize(balanceType, children[i]);
1094
1118
  if (groupSize + nextSize >= maxChild)
1095
1119
  break;
1096
1120
  groupSize += nextSize;
1097
1121
  }
1098
1122
  if (i == groupFrom + 1) {
1099
1123
  if (groupSize > maxChild) {
1100
- let only = children[groupFrom];
1124
+ let only = children[groupFrom]; // Only trees can have a size > 1
1101
1125
  divide(only.children, only.positions, 0, only.children.length, positions[groupFrom] + offset);
1102
1126
  continue;
1103
1127
  }
@@ -1105,7 +1129,7 @@ mkTree) {
1105
1129
  }
1106
1130
  else {
1107
1131
  let length = positions[i - 1] + children[i - 1].length - groupStart;
1108
- localChildren.push(balanceRange(type, children, positions, groupFrom, i, groupStart, length, null, mkTree));
1132
+ localChildren.push(balanceRange(balanceType, children, positions, groupFrom, i, groupStart, length, null, mkTree));
1109
1133
  }
1110
1134
  localPositions.push(groupStart + offset - start);
1111
1135
  }
@@ -1320,7 +1344,8 @@ class MixedParse {
1320
1344
  let fragmentCursor = new FragmentCursor(this.fragments);
1321
1345
  let overlay = null;
1322
1346
  let covered = null;
1323
- scan: for (let cursor = this.baseTree.fullCursor(), nest, isCovered;;) {
1347
+ let cursor = new TreeCursor(new TreeNode(this.baseTree, this.ranges[0].from, 0, null), 1 /* Full */);
1348
+ scan: for (let nest, isCovered;;) {
1324
1349
  let enter = true, range;
1325
1350
  if (fragmentCursor.hasNode(cursor)) {
1326
1351
  if (overlay) {
@@ -1460,7 +1485,7 @@ class StructureCursor {
1460
1485
  }
1461
1486
  hasNode(cursor) {
1462
1487
  this.moveTo(cursor.from);
1463
- if (!this.done && this.cursor.from + this.offset == cursor.from) {
1488
+ if (!this.done && this.cursor.from + this.offset == cursor.from && this.cursor.tree) {
1464
1489
  for (let tree = this.cursor.tree;;) {
1465
1490
  if (tree == cursor.tree)
1466
1491
  return true;
package/dist/tree.d.ts CHANGED
@@ -128,11 +128,86 @@ export interface SyntaxNode {
128
128
  prevSibling: SyntaxNode | null;
129
129
  cursor: TreeCursor;
130
130
  resolve(pos: number, side?: -1 | 0 | 1): SyntaxNode;
131
+ enterUnfinishedNodesBefore(pos: number): SyntaxNode;
131
132
  tree: Tree | null;
132
133
  toTree(): Tree;
133
134
  getChild(type: string | number, before?: string | number | null, after?: string | number | null): SyntaxNode | null;
134
135
  getChildren(type: string | number, before?: string | number | null, after?: string | number | null): SyntaxNode[];
135
136
  }
137
+ declare const enum Side {
138
+ Before = -2,
139
+ AtOrBefore = -1,
140
+ Around = 0,
141
+ AtOrAfter = 1,
142
+ After = 2,
143
+ DontCare = 4
144
+ }
145
+ export declare const enum Mode {
146
+ Full = 1,
147
+ NoEnterBuffer = 2
148
+ }
149
+ export declare class TreeNode implements SyntaxNode {
150
+ readonly node: Tree;
151
+ readonly _from: number;
152
+ readonly index: number;
153
+ readonly _parent: TreeNode | null;
154
+ constructor(node: Tree, _from: number, index: number, _parent: TreeNode | null);
155
+ get type(): NodeType;
156
+ get name(): string;
157
+ get from(): number;
158
+ get to(): number;
159
+ nextChild(i: number, dir: 1 | -1, pos: number, side: Side, mode?: Mode): TreeNode | BufferNode | null;
160
+ get firstChild(): TreeNode | BufferNode;
161
+ get lastChild(): TreeNode | BufferNode;
162
+ childAfter(pos: number): TreeNode | BufferNode;
163
+ childBefore(pos: number): TreeNode | BufferNode;
164
+ enter(pos: number, side: -1 | 0 | 1, overlays?: boolean, buffers?: boolean): TreeNode | BufferNode;
165
+ nextSignificantParent(): TreeNode;
166
+ get parent(): TreeNode;
167
+ get nextSibling(): TreeNode | BufferNode;
168
+ get prevSibling(): TreeNode | BufferNode;
169
+ get cursor(): TreeCursor;
170
+ get tree(): Tree;
171
+ toTree(): Tree;
172
+ resolve(pos: number, side?: -1 | 0 | 1): SyntaxNode;
173
+ enterUnfinishedNodesBefore(pos: number): SyntaxNode;
174
+ getChild(type: string | number, before?: string | number | null, after?: string | number | null): SyntaxNode;
175
+ getChildren(type: string | number, before?: string | number | null, after?: string | number | null): SyntaxNode[];
176
+ }
177
+ declare class BufferContext {
178
+ readonly parent: TreeNode;
179
+ readonly buffer: TreeBuffer;
180
+ readonly index: number;
181
+ readonly start: number;
182
+ constructor(parent: TreeNode, buffer: TreeBuffer, index: number, start: number);
183
+ }
184
+ declare class BufferNode implements SyntaxNode {
185
+ readonly context: BufferContext;
186
+ readonly _parent: BufferNode | null;
187
+ readonly index: number;
188
+ type: NodeType;
189
+ get name(): string;
190
+ get from(): number;
191
+ get to(): number;
192
+ constructor(context: BufferContext, _parent: BufferNode | null, index: number);
193
+ child(dir: 1 | -1, pos: number, side: Side): BufferNode | null;
194
+ get firstChild(): BufferNode;
195
+ get lastChild(): BufferNode;
196
+ childAfter(pos: number): BufferNode;
197
+ childBefore(pos: number): BufferNode;
198
+ enter(pos: number, side: -1 | 0 | 1, overlays?: boolean, buffers?: boolean): BufferNode;
199
+ get parent(): TreeNode | BufferNode;
200
+ externalSibling(dir: 1 | -1): TreeNode | BufferNode;
201
+ get nextSibling(): SyntaxNode | null;
202
+ get prevSibling(): SyntaxNode | null;
203
+ get cursor(): TreeCursor;
204
+ get tree(): any;
205
+ toTree(): Tree;
206
+ resolve(pos: number, side?: -1 | 0 | 1): SyntaxNode;
207
+ enterUnfinishedNodesBefore(pos: number): SyntaxNode;
208
+ getChild(type: string | number, before?: string | number | null, after?: string | number | null): SyntaxNode;
209
+ getChildren(type: string | number, before?: string | number | null, after?: string | number | null): SyntaxNode[];
210
+ }
136
211
  export declare class TreeCursor {
137
212
  type: NodeType;
138
213
  get name(): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lezer/common",
3
- "version": "0.15.4",
3
+ "version": "0.15.8",
4
4
  "description": "Syntax tree data structure and parser interfaces for the lezer parser",
5
5
  "main": "dist/index.cjs",
6
6
  "type": "module",