@lezer/common 1.2.0 → 1.2.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
@@ -692,7 +692,7 @@ class BaseNode {
692
692
  return resolveNode(this, pos, side, true);
693
693
  }
694
694
  matchContext(context) {
695
- return matchNodeContext(this, context);
695
+ return matchNodeContext(this.parent, context);
696
696
  }
697
697
  enterUnfinishedNodesBefore(pos) {
698
698
  let scan = this.childBefore(pos), node = this;
@@ -802,9 +802,11 @@ function getChildren(node, type, before, after) {
802
802
  if (!cur.firstChild())
803
803
  return result;
804
804
  if (before != null)
805
- while (!cur.type.is(before))
805
+ for (let found = false; !found;) {
806
+ found = cur.type.is(before);
806
807
  if (!cur.nextSibling())
807
808
  return result;
809
+ }
808
810
  for (;;) {
809
811
  if (after != null && cur.type.is(after))
810
812
  return result;
@@ -815,7 +817,7 @@ function getChildren(node, type, before, after) {
815
817
  }
816
818
  }
817
819
  function matchNodeContext(node, context, i = context.length - 1) {
818
- for (let p = node.parent; i >= 0; p = p.parent) {
820
+ for (let p = node; i >= 0; p = p.parent) {
819
821
  if (!p)
820
822
  return false;
821
823
  if (!p.type.isAnonymous) {
@@ -1244,11 +1246,11 @@ class TreeCursor {
1244
1246
  */
1245
1247
  matchContext(context) {
1246
1248
  if (!this.buffer)
1247
- return matchNodeContext(this.node, context);
1249
+ return matchNodeContext(this.node.parent, context);
1248
1250
  let { buffer } = this.buffer, { types } = buffer.set;
1249
1251
  for (let i = context.length - 1, d = this.stack.length - 1; i >= 0; d--) {
1250
1252
  if (d < 0)
1251
- return matchNodeContext(this.node, context, i);
1253
+ return matchNodeContext(this._tree, context, i);
1252
1254
  let type = types[buffer.buffer[this.stack[d]]];
1253
1255
  if (!type.isAnonymous) {
1254
1256
  if (context[i] && context[i] != type.name)
@@ -1270,7 +1272,7 @@ function buildTree(data) {
1270
1272
  let contextHash = 0, lookAhead = 0;
1271
1273
  function takeNode(parentStart, minPos, children, positions, inRepeat, depth) {
1272
1274
  let { id, start, end, size } = cursor;
1273
- let lookAheadAtStart = lookAhead;
1275
+ let lookAheadAtStart = lookAhead, contextAtStart = contextHash;
1274
1276
  while (size < 0) {
1275
1277
  cursor.next();
1276
1278
  if (size == -1 /* SpecialRecord.Reuse */) {
@@ -1311,7 +1313,7 @@ function buildTree(data) {
1311
1313
  while (cursor.pos > endPos) {
1312
1314
  if (localInRepeat >= 0 && cursor.id == localInRepeat && cursor.size >= 0) {
1313
1315
  if (cursor.end <= lastEnd - maxBufferLength) {
1314
- makeRepeatLeaf(localChildren, localPositions, start, lastGroup, cursor.end, lastEnd, localInRepeat, lookAheadAtStart);
1316
+ makeRepeatLeaf(localChildren, localPositions, start, lastGroup, cursor.end, lastEnd, localInRepeat, lookAheadAtStart, contextAtStart);
1315
1317
  lastGroup = localChildren.length;
1316
1318
  lastEnd = cursor.end;
1317
1319
  }
@@ -1325,15 +1327,15 @@ function buildTree(data) {
1325
1327
  }
1326
1328
  }
1327
1329
  if (localInRepeat >= 0 && lastGroup > 0 && lastGroup < localChildren.length)
1328
- makeRepeatLeaf(localChildren, localPositions, start, lastGroup, start, lastEnd, localInRepeat, lookAheadAtStart);
1330
+ makeRepeatLeaf(localChildren, localPositions, start, lastGroup, start, lastEnd, localInRepeat, lookAheadAtStart, contextAtStart);
1329
1331
  localChildren.reverse();
1330
1332
  localPositions.reverse();
1331
1333
  if (localInRepeat > -1 && lastGroup > 0) {
1332
- let make = makeBalanced(type);
1334
+ let make = makeBalanced(type, contextAtStart);
1333
1335
  node = balanceRange(type, localChildren, localPositions, 0, localChildren.length, 0, end - start, make, make);
1334
1336
  }
1335
1337
  else {
1336
- node = makeTree(type, localChildren, localPositions, end - start, lookAheadAtStart - end);
1338
+ node = makeTree(type, localChildren, localPositions, end - start, lookAheadAtStart - end, contextAtStart);
1337
1339
  }
1338
1340
  }
1339
1341
  children.push(node);
@@ -1371,7 +1373,7 @@ function buildTree(data) {
1371
1373
  positions.push(start - parentStart);
1372
1374
  }
1373
1375
  }
1374
- function makeBalanced(type) {
1376
+ function makeBalanced(type, contextHash) {
1375
1377
  return (children, positions, length) => {
1376
1378
  let lookAhead = 0, lastI = children.length - 1, last, lookAheadProp;
1377
1379
  if (lastI >= 0 && (last = children[lastI]) instanceof Tree) {
@@ -1380,19 +1382,19 @@ function buildTree(data) {
1380
1382
  if (lookAheadProp = last.prop(NodeProp.lookAhead))
1381
1383
  lookAhead = positions[lastI] + last.length + lookAheadProp;
1382
1384
  }
1383
- return makeTree(type, children, positions, length, lookAhead);
1385
+ return makeTree(type, children, positions, length, lookAhead, contextHash);
1384
1386
  };
1385
1387
  }
1386
- function makeRepeatLeaf(children, positions, base, i, from, to, type, lookAhead) {
1388
+ function makeRepeatLeaf(children, positions, base, i, from, to, type, lookAhead, contextHash) {
1387
1389
  let localChildren = [], localPositions = [];
1388
1390
  while (children.length > i) {
1389
1391
  localChildren.push(children.pop());
1390
1392
  localPositions.push(positions.pop() + base - from);
1391
1393
  }
1392
- children.push(makeTree(nodeSet.types[type], localChildren, localPositions, to - from, lookAhead - to));
1394
+ children.push(makeTree(nodeSet.types[type], localChildren, localPositions, to - from, lookAhead - to, contextHash));
1393
1395
  positions.push(from - base);
1394
1396
  }
1395
- function makeTree(type, children, positions, length, lookAhead = 0, props) {
1397
+ function makeTree(type, children, positions, length, lookAhead, contextHash, props) {
1396
1398
  if (contextHash) {
1397
1399
  let pair = [NodeProp.contextHash, contextHash];
1398
1400
  props = props ? [pair].concat(props) : [pair];
@@ -1899,8 +1901,13 @@ class MixedParse {
1899
1901
  else if (overlay && (range = overlay.predicate(cursor))) {
1900
1902
  if (range === true)
1901
1903
  range = new Range(cursor.from, cursor.to);
1902
- if (range.from < range.to)
1903
- overlay.ranges.push(range);
1904
+ if (range.from < range.to) {
1905
+ let last = overlay.ranges.length - 1;
1906
+ if (last >= 0 && overlay.ranges[last].to == range.from)
1907
+ overlay.ranges[last] = { from: overlay.ranges[last].from, to: range.to };
1908
+ else
1909
+ overlay.ranges.push(range);
1910
+ }
1904
1911
  }
1905
1912
  if (enter && cursor.firstChild()) {
1906
1913
  if (overlay)
package/dist/index.js CHANGED
@@ -690,7 +690,7 @@ class BaseNode {
690
690
  return resolveNode(this, pos, side, true);
691
691
  }
692
692
  matchContext(context) {
693
- return matchNodeContext(this, context);
693
+ return matchNodeContext(this.parent, context);
694
694
  }
695
695
  enterUnfinishedNodesBefore(pos) {
696
696
  let scan = this.childBefore(pos), node = this;
@@ -800,9 +800,11 @@ function getChildren(node, type, before, after) {
800
800
  if (!cur.firstChild())
801
801
  return result;
802
802
  if (before != null)
803
- while (!cur.type.is(before))
803
+ for (let found = false; !found;) {
804
+ found = cur.type.is(before);
804
805
  if (!cur.nextSibling())
805
806
  return result;
807
+ }
806
808
  for (;;) {
807
809
  if (after != null && cur.type.is(after))
808
810
  return result;
@@ -813,7 +815,7 @@ function getChildren(node, type, before, after) {
813
815
  }
814
816
  }
815
817
  function matchNodeContext(node, context, i = context.length - 1) {
816
- for (let p = node.parent; i >= 0; p = p.parent) {
818
+ for (let p = node; i >= 0; p = p.parent) {
817
819
  if (!p)
818
820
  return false;
819
821
  if (!p.type.isAnonymous) {
@@ -1242,11 +1244,11 @@ class TreeCursor {
1242
1244
  */
1243
1245
  matchContext(context) {
1244
1246
  if (!this.buffer)
1245
- return matchNodeContext(this.node, context);
1247
+ return matchNodeContext(this.node.parent, context);
1246
1248
  let { buffer } = this.buffer, { types } = buffer.set;
1247
1249
  for (let i = context.length - 1, d = this.stack.length - 1; i >= 0; d--) {
1248
1250
  if (d < 0)
1249
- return matchNodeContext(this.node, context, i);
1251
+ return matchNodeContext(this._tree, context, i);
1250
1252
  let type = types[buffer.buffer[this.stack[d]]];
1251
1253
  if (!type.isAnonymous) {
1252
1254
  if (context[i] && context[i] != type.name)
@@ -1268,7 +1270,7 @@ function buildTree(data) {
1268
1270
  let contextHash = 0, lookAhead = 0;
1269
1271
  function takeNode(parentStart, minPos, children, positions, inRepeat, depth) {
1270
1272
  let { id, start, end, size } = cursor;
1271
- let lookAheadAtStart = lookAhead;
1273
+ let lookAheadAtStart = lookAhead, contextAtStart = contextHash;
1272
1274
  while (size < 0) {
1273
1275
  cursor.next();
1274
1276
  if (size == -1 /* SpecialRecord.Reuse */) {
@@ -1309,7 +1311,7 @@ function buildTree(data) {
1309
1311
  while (cursor.pos > endPos) {
1310
1312
  if (localInRepeat >= 0 && cursor.id == localInRepeat && cursor.size >= 0) {
1311
1313
  if (cursor.end <= lastEnd - maxBufferLength) {
1312
- makeRepeatLeaf(localChildren, localPositions, start, lastGroup, cursor.end, lastEnd, localInRepeat, lookAheadAtStart);
1314
+ makeRepeatLeaf(localChildren, localPositions, start, lastGroup, cursor.end, lastEnd, localInRepeat, lookAheadAtStart, contextAtStart);
1313
1315
  lastGroup = localChildren.length;
1314
1316
  lastEnd = cursor.end;
1315
1317
  }
@@ -1323,15 +1325,15 @@ function buildTree(data) {
1323
1325
  }
1324
1326
  }
1325
1327
  if (localInRepeat >= 0 && lastGroup > 0 && lastGroup < localChildren.length)
1326
- makeRepeatLeaf(localChildren, localPositions, start, lastGroup, start, lastEnd, localInRepeat, lookAheadAtStart);
1328
+ makeRepeatLeaf(localChildren, localPositions, start, lastGroup, start, lastEnd, localInRepeat, lookAheadAtStart, contextAtStart);
1327
1329
  localChildren.reverse();
1328
1330
  localPositions.reverse();
1329
1331
  if (localInRepeat > -1 && lastGroup > 0) {
1330
- let make = makeBalanced(type);
1332
+ let make = makeBalanced(type, contextAtStart);
1331
1333
  node = balanceRange(type, localChildren, localPositions, 0, localChildren.length, 0, end - start, make, make);
1332
1334
  }
1333
1335
  else {
1334
- node = makeTree(type, localChildren, localPositions, end - start, lookAheadAtStart - end);
1336
+ node = makeTree(type, localChildren, localPositions, end - start, lookAheadAtStart - end, contextAtStart);
1335
1337
  }
1336
1338
  }
1337
1339
  children.push(node);
@@ -1369,7 +1371,7 @@ function buildTree(data) {
1369
1371
  positions.push(start - parentStart);
1370
1372
  }
1371
1373
  }
1372
- function makeBalanced(type) {
1374
+ function makeBalanced(type, contextHash) {
1373
1375
  return (children, positions, length) => {
1374
1376
  let lookAhead = 0, lastI = children.length - 1, last, lookAheadProp;
1375
1377
  if (lastI >= 0 && (last = children[lastI]) instanceof Tree) {
@@ -1378,19 +1380,19 @@ function buildTree(data) {
1378
1380
  if (lookAheadProp = last.prop(NodeProp.lookAhead))
1379
1381
  lookAhead = positions[lastI] + last.length + lookAheadProp;
1380
1382
  }
1381
- return makeTree(type, children, positions, length, lookAhead);
1383
+ return makeTree(type, children, positions, length, lookAhead, contextHash);
1382
1384
  };
1383
1385
  }
1384
- function makeRepeatLeaf(children, positions, base, i, from, to, type, lookAhead) {
1386
+ function makeRepeatLeaf(children, positions, base, i, from, to, type, lookAhead, contextHash) {
1385
1387
  let localChildren = [], localPositions = [];
1386
1388
  while (children.length > i) {
1387
1389
  localChildren.push(children.pop());
1388
1390
  localPositions.push(positions.pop() + base - from);
1389
1391
  }
1390
- children.push(makeTree(nodeSet.types[type], localChildren, localPositions, to - from, lookAhead - to));
1392
+ children.push(makeTree(nodeSet.types[type], localChildren, localPositions, to - from, lookAhead - to, contextHash));
1391
1393
  positions.push(from - base);
1392
1394
  }
1393
- function makeTree(type, children, positions, length, lookAhead = 0, props) {
1395
+ function makeTree(type, children, positions, length, lookAhead, contextHash, props) {
1394
1396
  if (contextHash) {
1395
1397
  let pair = [NodeProp.contextHash, contextHash];
1396
1398
  props = props ? [pair].concat(props) : [pair];
@@ -1897,8 +1899,13 @@ class MixedParse {
1897
1899
  else if (overlay && (range = overlay.predicate(cursor))) {
1898
1900
  if (range === true)
1899
1901
  range = new Range(cursor.from, cursor.to);
1900
- if (range.from < range.to)
1901
- overlay.ranges.push(range);
1902
+ if (range.from < range.to) {
1903
+ let last = overlay.ranges.length - 1;
1904
+ if (last >= 0 && overlay.ranges[last].to == range.from)
1905
+ overlay.ranges[last] = { from: overlay.ranges[last].from, to: range.to };
1906
+ else
1907
+ overlay.ranges.push(range);
1908
+ }
1902
1909
  }
1903
1910
  if (enter && cursor.firstChild()) {
1904
1911
  if (overlay)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lezer/common",
3
- "version": "1.2.0",
3
+ "version": "1.2.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",
@@ -16,7 +16,8 @@
16
16
  "ist": "^1.1.1",
17
17
  "@marijn/buildtool": "^0.1.5",
18
18
  "@types/mocha": "^5.2.6",
19
- "mocha": "^10.2.0"
19
+ "mocha": "^10.2.0",
20
+ "ts-node": "^10.9.2"
20
21
  },
21
22
  "files": ["dist"],
22
23
  "repository": {