@lezer/common 0.15.7 → 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/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.
@@ -759,7 +759,7 @@ class TreeCursor {
759
759
  return this.yield(this._tree.enter(pos, side, overlays, buffers));
760
760
  return buffers ? this.enterChild(1, pos, side) : false;
761
761
  }
762
- /// 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.
763
763
  parent() {
764
764
  if (!this.buffer)
765
765
  return this.yieldNode((this.mode & 1 /* Full */) ? this._tree._parent : this._tree.parent);
@@ -1081,15 +1081,21 @@ function nodeSize(balanceType, node) {
1081
1081
  return 1;
1082
1082
  let size = nodeSizeCache.get(node);
1083
1083
  if (size == null) {
1084
- 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
+ }
1085
1092
  nodeSizeCache.set(node, size);
1086
1093
  }
1087
1094
  return size;
1088
1095
  }
1089
1096
  function balanceRange(
1090
- // The type to tag the resulting tree with. Will also be used for
1091
- // internal nodes when it is an anonymous type
1092
- type,
1097
+ // The type the balanced tree's inner nodes.
1098
+ balanceType,
1093
1099
  // The direct children and their positions
1094
1100
  children, positions,
1095
1101
  // The index range in children/positions to use
@@ -1104,22 +1110,22 @@ mkTop,
1104
1110
  mkTree) {
1105
1111
  let total = 0;
1106
1112
  for (let i = from; i < to; i++)
1107
- total += nodeSize(type, children[i]);
1113
+ total += nodeSize(balanceType, children[i]);
1108
1114
  let maxChild = Math.ceil((total * 1.5) / 8 /* BranchFactor */);
1109
1115
  let localChildren = [], localPositions = [];
1110
1116
  function divide(children, positions, from, to, offset) {
1111
1117
  for (let i = from; i < to;) {
1112
- let groupFrom = i, groupStart = positions[i], groupSize = nodeSize(type, children[i]);
1118
+ let groupFrom = i, groupStart = positions[i], groupSize = nodeSize(balanceType, children[i]);
1113
1119
  i++;
1114
1120
  for (; i < to; i++) {
1115
- let nextSize = nodeSize(type, children[i]);
1121
+ let nextSize = nodeSize(balanceType, children[i]);
1116
1122
  if (groupSize + nextSize >= maxChild)
1117
1123
  break;
1118
1124
  groupSize += nextSize;
1119
1125
  }
1120
1126
  if (i == groupFrom + 1) {
1121
1127
  if (groupSize > maxChild) {
1122
- let only = children[groupFrom];
1128
+ let only = children[groupFrom]; // Only trees can have a size > 1
1123
1129
  divide(only.children, only.positions, 0, only.children.length, positions[groupFrom] + offset);
1124
1130
  continue;
1125
1131
  }
@@ -1127,7 +1133,7 @@ mkTree) {
1127
1133
  }
1128
1134
  else {
1129
1135
  let length = positions[i - 1] + children[i - 1].length - groupStart;
1130
- 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));
1131
1137
  }
1132
1138
  localPositions.push(groupStart + offset - start);
1133
1139
  }
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.
@@ -755,7 +755,7 @@ class TreeCursor {
755
755
  return this.yield(this._tree.enter(pos, side, overlays, buffers));
756
756
  return buffers ? this.enterChild(1, pos, side) : false;
757
757
  }
758
- /// 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.
759
759
  parent() {
760
760
  if (!this.buffer)
761
761
  return this.yieldNode((this.mode & 1 /* Full */) ? this._tree._parent : this._tree.parent);
@@ -1077,15 +1077,21 @@ function nodeSize(balanceType, node) {
1077
1077
  return 1;
1078
1078
  let size = nodeSizeCache.get(node);
1079
1079
  if (size == null) {
1080
- 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
+ }
1081
1088
  nodeSizeCache.set(node, size);
1082
1089
  }
1083
1090
  return size;
1084
1091
  }
1085
1092
  function balanceRange(
1086
- // The type to tag the resulting tree with. Will also be used for
1087
- // internal nodes when it is an anonymous type
1088
- type,
1093
+ // The type the balanced tree's inner nodes.
1094
+ balanceType,
1089
1095
  // The direct children and their positions
1090
1096
  children, positions,
1091
1097
  // The index range in children/positions to use
@@ -1100,22 +1106,22 @@ mkTop,
1100
1106
  mkTree) {
1101
1107
  let total = 0;
1102
1108
  for (let i = from; i < to; i++)
1103
- total += nodeSize(type, children[i]);
1109
+ total += nodeSize(balanceType, children[i]);
1104
1110
  let maxChild = Math.ceil((total * 1.5) / 8 /* BranchFactor */);
1105
1111
  let localChildren = [], localPositions = [];
1106
1112
  function divide(children, positions, from, to, offset) {
1107
1113
  for (let i = from; i < to;) {
1108
- let groupFrom = i, groupStart = positions[i], groupSize = nodeSize(type, children[i]);
1114
+ let groupFrom = i, groupStart = positions[i], groupSize = nodeSize(balanceType, children[i]);
1109
1115
  i++;
1110
1116
  for (; i < to; i++) {
1111
- let nextSize = nodeSize(type, children[i]);
1117
+ let nextSize = nodeSize(balanceType, children[i]);
1112
1118
  if (groupSize + nextSize >= maxChild)
1113
1119
  break;
1114
1120
  groupSize += nextSize;
1115
1121
  }
1116
1122
  if (i == groupFrom + 1) {
1117
1123
  if (groupSize > maxChild) {
1118
- let only = children[groupFrom];
1124
+ let only = children[groupFrom]; // Only trees can have a size > 1
1119
1125
  divide(only.children, only.positions, 0, only.children.length, positions[groupFrom] + offset);
1120
1126
  continue;
1121
1127
  }
@@ -1123,7 +1129,7 @@ mkTree) {
1123
1129
  }
1124
1130
  else {
1125
1131
  let length = positions[i - 1] + children[i - 1].length - groupStart;
1126
- 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));
1127
1133
  }
1128
1134
  localPositions.push(groupStart + offset - start);
1129
1135
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lezer/common",
3
- "version": "0.15.7",
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",