lezer-tree
Advanced tools
Comparing version 0.10.0 to 0.11.0
@@ -0,1 +1,7 @@ | ||
## 0.11.0 (2020-09-26) | ||
### Breaking changes | ||
Adjust to new output format of repeat rules. | ||
## 0.10.0 (2020-08-07) | ||
@@ -2,0 +8,0 @@ |
@@ -679,4 +679,8 @@ /// The default maximum length of a `TreeBuffer` node. | ||
let types = group.types; | ||
function takeNode(parentStart, minPos, children, positions, tagBuffer) { | ||
let { id, start, end, size } = cursor, buffer; | ||
function takeNode(parentStart, minPos, children, positions, inRepeat) { | ||
let { id, start, end, size } = cursor; | ||
while (id == inRepeat) { | ||
cursor.next(); | ||
({ id, start, end, size } = cursor); | ||
} | ||
let startPos = start - parentStart; | ||
@@ -689,4 +693,4 @@ if (size < 0) { // Reused node | ||
} | ||
let type = types[id], node; | ||
if (end - start <= maxBufferLength && (buffer = findBufferSize(cursor.pos - minPos))) { | ||
let type = types[id], node, buffer; | ||
if (end - start <= maxBufferLength && (buffer = findBufferSize(cursor.pos - minPos, inRepeat))) { | ||
// Small enough for a buffer, and no reused nodes inside | ||
@@ -696,4 +700,4 @@ let data = new Uint16Array(buffer.size - buffer.skip); | ||
while (cursor.pos > endPos) | ||
index = copyToBuffer(buffer.start, data, index); | ||
node = new TreeBuffer(data, end - buffer.start, group, tagBuffer); | ||
index = copyToBuffer(buffer.start, data, index, inRepeat); | ||
node = new TreeBuffer(data, end - buffer.start, group, inRepeat < 0 ? NodeType.none : types[inRepeat]); | ||
startPos = buffer.start - parentStart; | ||
@@ -705,19 +709,8 @@ } | ||
let localChildren = [], localPositions = []; | ||
// Check if this is a repeat wrapper. Store the id of the inner | ||
// repeat node in the variable if it is | ||
let repeating = id >= group.types.length ? id - (group.types.length - minRepeatType) : -1; | ||
if (repeating > -1) { | ||
type = types[repeating]; | ||
while (cursor.pos > endPos) { | ||
let isRepeat = cursor.id == repeating; // This starts with an inner repeated node | ||
takeNode(start, endPos, localChildren, localPositions, isRepeat ? type : NodeType.none); | ||
} | ||
} | ||
else { | ||
while (cursor.pos > endPos) | ||
takeNode(start, endPos, localChildren, localPositions, NodeType.none); | ||
} | ||
let localInRepeat = id >= minRepeatType ? id : -1; | ||
while (cursor.pos > endPos) | ||
takeNode(start, endPos, localChildren, localPositions, localInRepeat); | ||
localChildren.reverse(); | ||
localPositions.reverse(); | ||
if (repeating > -1 && localChildren.length > BalanceBranchFactor) | ||
if (localInRepeat > -1 && localChildren.length > BalanceBranchFactor) | ||
node = balanceRange(type, type, localChildren, localPositions, 0, localChildren.length, 0, maxBufferLength, end - start); | ||
@@ -730,11 +723,25 @@ else | ||
} | ||
function findBufferSize(maxSize) { | ||
function findBufferSize(maxSize, inRepeat) { | ||
// Scan through the buffer to find previous siblings that fit | ||
// together in a TreeBuffer, and don't contain any reused nodes | ||
// (which can't be stored in a buffer) | ||
// If `type` is > -1, only include siblings with that same type | ||
// (used to group repeat content into a buffer) | ||
// (which can't be stored in a buffer). | ||
// If `inRepeat` is > -1, ignore node boundaries of that type for | ||
// nesting, but make sure the end falls either at the start | ||
// (`maxSize`) or before such a node. | ||
let fork = cursor.fork(); | ||
let size = 0, start = 0, skip = 0, minStart = fork.end - maxBufferLength; | ||
let result = { size: 0, start: 0, skip: 0 }; | ||
scan: for (let minPos = fork.pos - maxSize; fork.pos > minPos;) { | ||
// Pretend nested repeat nodes of the same type don't exist | ||
if (fork.id == inRepeat) { | ||
// Except that we store the current state as a valid return | ||
// value. | ||
result.size = size; | ||
result.start = start; | ||
result.skip = skip; | ||
skip += 4; | ||
size += 4; | ||
fork.next(); | ||
continue; | ||
} | ||
let nodeSize = fork.size, startPos = fork.pos - nodeSize; | ||
@@ -757,7 +764,14 @@ if (nodeSize < 0 || startPos < minPos || fork.start < minStart) | ||
} | ||
return size > 4 ? { size, start, skip } : null; | ||
if (inRepeat < 0 || size == maxSize) { | ||
result.size = size; | ||
result.start = start; | ||
result.skip = skip; | ||
} | ||
return result.size > 4 ? result : undefined; | ||
} | ||
function copyToBuffer(bufferStart, buffer, index) { | ||
function copyToBuffer(bufferStart, buffer, index, inRepeat) { | ||
let { id, start, end, size } = cursor; | ||
cursor.next(); | ||
if (id == inRepeat) | ||
return index; | ||
let startIndex = index; | ||
@@ -767,3 +781,3 @@ if (size > 4) { | ||
while (cursor.pos > endPos) | ||
index = copyToBuffer(bufferStart, buffer, index); | ||
index = copyToBuffer(bufferStart, buffer, index, inRepeat); | ||
} | ||
@@ -780,3 +794,3 @@ if (id < minRepeatType) { // Don't copy repeat nodes into buffers | ||
while (cursor.pos > 0) | ||
takeNode(0, 0, children, positions, NodeType.none); | ||
takeNode(0, 0, children, positions, -1); | ||
let length = children.length ? positions[0] + children[0].length : 0; | ||
@@ -783,0 +797,0 @@ return new Tree(group.types[topID], children.reverse(), positions.reverse(), length); |
{ | ||
"name": "lezer-tree", | ||
"version": "0.10.0", | ||
"version": "0.11.0", | ||
"description": "Syntax tree data structure for the lezer parser", | ||
@@ -5,0 +5,0 @@ "main": "dist/tree.cjs", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
119537
1928