@lezer/common 0.15.9 → 0.15.12
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 +51 -20
- package/dist/index.js +51 -20
- package/dist/tree.d.ts +3 -0
- package/package.json +1 -1
- package/dist/composite.d.ts +0 -42
- package/dist/frame.d.ts +0 -25
- package/dist/scaffold.d.ts +0 -25
- package/dist/tree.cjs +0 -1019
- package/dist/tree.cjs.map +0 -1
- package/dist/tree.es.js +0 -1007
- package/dist/tree.es.js.map +0 -1
package/dist/tree.es.js
DELETED
|
@@ -1,1007 +0,0 @@
|
|
|
1
|
-
/// The default maximum length of a `TreeBuffer` node.
|
|
2
|
-
const DefaultBufferLength = 1024;
|
|
3
|
-
let nextPropID = 0;
|
|
4
|
-
const CachedNode = new WeakMap();
|
|
5
|
-
/// Each [node type](#tree.NodeType) can have metadata associated with
|
|
6
|
-
/// it in props. Instances of this class represent prop names.
|
|
7
|
-
class NodeProp {
|
|
8
|
-
/// Create a new node prop type. You can optionally pass a
|
|
9
|
-
/// `deserialize` function.
|
|
10
|
-
constructor({ deserialize } = {}) {
|
|
11
|
-
this.id = nextPropID++;
|
|
12
|
-
this.deserialize = deserialize || (() => {
|
|
13
|
-
throw new Error("This node type doesn't define a deserialize function");
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
/// Create a string-valued node prop whose deserialize function is
|
|
17
|
-
/// the identity function.
|
|
18
|
-
static string() { return new NodeProp({ deserialize: str => str }); }
|
|
19
|
-
/// Create a number-valued node prop whose deserialize function is
|
|
20
|
-
/// just `Number`.
|
|
21
|
-
static number() { return new NodeProp({ deserialize: Number }); }
|
|
22
|
-
/// Creates a boolean-valued node prop whose deserialize function
|
|
23
|
-
/// returns true for any input.
|
|
24
|
-
static flag() { return new NodeProp({ deserialize: () => true }); }
|
|
25
|
-
/// Store a value for this prop in the given object. This can be
|
|
26
|
-
/// useful when building up a prop object to pass to the
|
|
27
|
-
/// [`NodeType`](#tree.NodeType) constructor. Returns its first
|
|
28
|
-
/// argument.
|
|
29
|
-
set(propObj, value) {
|
|
30
|
-
propObj[this.id] = value;
|
|
31
|
-
return propObj;
|
|
32
|
-
}
|
|
33
|
-
/// This is meant to be used with
|
|
34
|
-
/// [`NodeSet.extend`](#tree.NodeSet.extend) or
|
|
35
|
-
/// [`Parser.withProps`](#lezer.Parser.withProps) to compute prop
|
|
36
|
-
/// values for each node type in the set. Takes a [match
|
|
37
|
-
/// object](#tree.NodeType^match) or function that returns undefined
|
|
38
|
-
/// if the node type doesn't get this prop, and the prop's value if
|
|
39
|
-
/// it does.
|
|
40
|
-
add(match) {
|
|
41
|
-
if (typeof match != "function")
|
|
42
|
-
match = NodeType.match(match);
|
|
43
|
-
return (type) => {
|
|
44
|
-
let result = match(type);
|
|
45
|
-
return result === undefined ? null : [this, result];
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
/// Prop that is used to describe matching delimiters. For opening
|
|
50
|
-
/// delimiters, this holds an array of node names (written as a
|
|
51
|
-
/// space-separated string when declaring this prop in a grammar)
|
|
52
|
-
/// for the node types of closing delimiters that match it.
|
|
53
|
-
NodeProp.closedBy = new NodeProp({ deserialize: str => str.split(" ") });
|
|
54
|
-
/// The inverse of [`closedBy`](#tree.NodeProp^closedBy). This is
|
|
55
|
-
/// attached to closing delimiters, holding an array of node names
|
|
56
|
-
/// of types of matching opening delimiters.
|
|
57
|
-
NodeProp.openedBy = new NodeProp({ deserialize: str => str.split(" ") });
|
|
58
|
-
/// Used to assign node types to groups (for example, all node
|
|
59
|
-
/// types that represent an expression could be tagged with an
|
|
60
|
-
/// `"Expression"` group).
|
|
61
|
-
NodeProp.group = new NodeProp({ deserialize: str => str.split(" ") });
|
|
62
|
-
const noProps = Object.create(null);
|
|
63
|
-
/// Each node in a syntax tree has a node type associated with it.
|
|
64
|
-
class NodeType {
|
|
65
|
-
/// @internal
|
|
66
|
-
constructor(
|
|
67
|
-
/// The name of the node type. Not necessarily unique, but if the
|
|
68
|
-
/// grammar was written properly, different node types with the
|
|
69
|
-
/// same name within a node set should play the same semantic
|
|
70
|
-
/// role.
|
|
71
|
-
name,
|
|
72
|
-
/// @internal
|
|
73
|
-
props,
|
|
74
|
-
/// The id of this node in its set. Corresponds to the term ids
|
|
75
|
-
/// used in the parser.
|
|
76
|
-
id,
|
|
77
|
-
/// @internal
|
|
78
|
-
flags = 0) {
|
|
79
|
-
this.name = name;
|
|
80
|
-
this.props = props;
|
|
81
|
-
this.id = id;
|
|
82
|
-
this.flags = flags;
|
|
83
|
-
}
|
|
84
|
-
static define(spec) {
|
|
85
|
-
let props = spec.props && spec.props.length ? Object.create(null) : noProps;
|
|
86
|
-
let flags = (spec.top ? 1 /* Top */ : 0) | (spec.skipped ? 2 /* Skipped */ : 0) |
|
|
87
|
-
(spec.error ? 4 /* Error */ : 0) | (spec.name == null ? 8 /* Anonymous */ : 0);
|
|
88
|
-
let type = new NodeType(spec.name || "", props, spec.id, flags);
|
|
89
|
-
if (spec.props)
|
|
90
|
-
for (let src of spec.props) {
|
|
91
|
-
if (!Array.isArray(src))
|
|
92
|
-
src = src(type);
|
|
93
|
-
if (src)
|
|
94
|
-
src[0].set(props, src[1]);
|
|
95
|
-
}
|
|
96
|
-
return type;
|
|
97
|
-
}
|
|
98
|
-
/// Retrieves a node prop for this type. Will return `undefined` if
|
|
99
|
-
/// the prop isn't present on this node.
|
|
100
|
-
prop(prop) { return this.props[prop.id]; }
|
|
101
|
-
/// True when this is the top node of a grammar.
|
|
102
|
-
get isTop() { return (this.flags & 1 /* Top */) > 0; }
|
|
103
|
-
/// True when this node is produced by a skip rule.
|
|
104
|
-
get isSkipped() { return (this.flags & 2 /* Skipped */) > 0; }
|
|
105
|
-
/// Indicates whether this is an error node.
|
|
106
|
-
get isError() { return (this.flags & 4 /* Error */) > 0; }
|
|
107
|
-
/// When true, this node type doesn't correspond to a user-declared
|
|
108
|
-
/// named node, for example because it is used to cache repetition.
|
|
109
|
-
get isAnonymous() { return (this.flags & 8 /* Anonymous */) > 0; }
|
|
110
|
-
/// Returns true when this node's name or one of its
|
|
111
|
-
/// [groups](#tree.NodeProp^group) matches the given string.
|
|
112
|
-
is(name) {
|
|
113
|
-
if (typeof name == 'string') {
|
|
114
|
-
if (this.name == name)
|
|
115
|
-
return true;
|
|
116
|
-
let group = this.prop(NodeProp.group);
|
|
117
|
-
return group ? group.indexOf(name) > -1 : false;
|
|
118
|
-
}
|
|
119
|
-
return this.id == name;
|
|
120
|
-
}
|
|
121
|
-
/// Create a function from node types to arbitrary values by
|
|
122
|
-
/// specifying an object whose property names are node or
|
|
123
|
-
/// [group](#tree.NodeProp^group) names. Often useful with
|
|
124
|
-
/// [`NodeProp.add`](#tree.NodeProp.add). You can put multiple
|
|
125
|
-
/// names, separated by spaces, in a single property name to map
|
|
126
|
-
/// multiple node names to a single value.
|
|
127
|
-
static match(map) {
|
|
128
|
-
let direct = Object.create(null);
|
|
129
|
-
for (let prop in map)
|
|
130
|
-
for (let name of prop.split(" "))
|
|
131
|
-
direct[name] = map[prop];
|
|
132
|
-
return (node) => {
|
|
133
|
-
for (let groups = node.prop(NodeProp.group), i = -1; i < (groups ? groups.length : 0); i++) {
|
|
134
|
-
let found = direct[i < 0 ? node.name : groups[i]];
|
|
135
|
-
if (found)
|
|
136
|
-
return found;
|
|
137
|
-
}
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
/// An empty dummy node type to use when no actual type is available.
|
|
142
|
-
NodeType.none = new NodeType("", Object.create(null), 0, 8 /* Anonymous */);
|
|
143
|
-
/// A node set holds a collection of node types. It is used to
|
|
144
|
-
/// compactly represent trees by storing their type ids, rather than a
|
|
145
|
-
/// full pointer to the type object, in a number array. Each parser
|
|
146
|
-
/// [has](#lezer.Parser.nodeSet) a node set, and [tree
|
|
147
|
-
/// buffers](#tree.TreeBuffer) can only store collections of nodes
|
|
148
|
-
/// from the same set. A set can have a maximum of 2**16 (65536)
|
|
149
|
-
/// node types in it, so that the ids fit into 16-bit typed array
|
|
150
|
-
/// slots.
|
|
151
|
-
class NodeSet {
|
|
152
|
-
/// Create a set with the given types. The `id` property of each
|
|
153
|
-
/// type should correspond to its position within the array.
|
|
154
|
-
constructor(
|
|
155
|
-
/// The node types in this set, by id.
|
|
156
|
-
types) {
|
|
157
|
-
this.types = types;
|
|
158
|
-
for (let i = 0; i < types.length; i++)
|
|
159
|
-
if (types[i].id != i)
|
|
160
|
-
throw new RangeError("Node type ids should correspond to array positions when creating a node set");
|
|
161
|
-
}
|
|
162
|
-
/// Create a copy of this set with some node properties added. The
|
|
163
|
-
/// arguments to this method should be created with
|
|
164
|
-
/// [`NodeProp.add`](#tree.NodeProp.add).
|
|
165
|
-
extend(...props) {
|
|
166
|
-
let newTypes = [];
|
|
167
|
-
for (let type of this.types) {
|
|
168
|
-
let newProps = null;
|
|
169
|
-
for (let source of props) {
|
|
170
|
-
let add = source(type);
|
|
171
|
-
if (add) {
|
|
172
|
-
if (!newProps)
|
|
173
|
-
newProps = Object.assign({}, type.props);
|
|
174
|
-
add[0].set(newProps, add[1]);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
newTypes.push(newProps ? new NodeType(type.name, newProps, type.id, type.flags) : type);
|
|
178
|
-
}
|
|
179
|
-
return new NodeSet(newTypes);
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
/// A piece of syntax tree. There are two ways to approach these
|
|
183
|
-
/// trees: the way they are actually stored in memory, and the
|
|
184
|
-
/// convenient way.
|
|
185
|
-
///
|
|
186
|
-
/// Syntax trees are stored as a tree of `Tree` and `TreeBuffer`
|
|
187
|
-
/// objects. By packing detail information into `TreeBuffer` leaf
|
|
188
|
-
/// nodes, the representation is made a lot more memory-efficient.
|
|
189
|
-
///
|
|
190
|
-
/// However, when you want to actually work with tree nodes, this
|
|
191
|
-
/// representation is very awkward, so most client code will want to
|
|
192
|
-
/// use the `TreeCursor` interface instead, which provides a view on
|
|
193
|
-
/// some part of this data structure, and can be used to move around
|
|
194
|
-
/// to adjacent nodes.
|
|
195
|
-
class Tree {
|
|
196
|
-
/// Construct a new tree. You usually want to go through
|
|
197
|
-
/// [`Tree.build`](#tree.Tree^build) instead.
|
|
198
|
-
constructor(type,
|
|
199
|
-
/// The tree's child nodes. Children small enough to fit in a
|
|
200
|
-
/// `TreeBuffer will be represented as such, other children can be
|
|
201
|
-
/// further `Tree` instances with their own internal structure.
|
|
202
|
-
children,
|
|
203
|
-
/// The positions (offsets relative to the start of this tree) of
|
|
204
|
-
/// the children.
|
|
205
|
-
positions,
|
|
206
|
-
/// The total length of this tree
|
|
207
|
-
length) {
|
|
208
|
-
this.type = type;
|
|
209
|
-
this.children = children;
|
|
210
|
-
this.positions = positions;
|
|
211
|
-
this.length = length;
|
|
212
|
-
}
|
|
213
|
-
/// @internal
|
|
214
|
-
toString() {
|
|
215
|
-
let children = this.children.map(c => c.toString()).join();
|
|
216
|
-
return !this.type.name ? children :
|
|
217
|
-
(/\W/.test(this.type.name) && !this.type.isError ? JSON.stringify(this.type.name) : this.type.name) +
|
|
218
|
-
(children.length ? "(" + children + ")" : "");
|
|
219
|
-
}
|
|
220
|
-
/// Get a [tree cursor](#tree.TreeCursor) rooted at this tree. When
|
|
221
|
-
/// `pos` is given, the cursor is [moved](#tree.TreeCursor.moveTo)
|
|
222
|
-
/// to the given position and side.
|
|
223
|
-
cursor(pos, side = 0) {
|
|
224
|
-
let scope = (pos != null && CachedNode.get(this)) || this.topNode;
|
|
225
|
-
let cursor = new TreeCursor(scope);
|
|
226
|
-
if (pos != null) {
|
|
227
|
-
cursor.moveTo(pos, side);
|
|
228
|
-
CachedNode.set(this, cursor._tree);
|
|
229
|
-
}
|
|
230
|
-
return cursor;
|
|
231
|
-
}
|
|
232
|
-
/// Get a [tree cursor](#tree.TreeCursor) that, unlike regular
|
|
233
|
-
/// cursors, doesn't skip [anonymous](#tree.NodeType.isAnonymous)
|
|
234
|
-
/// nodes.
|
|
235
|
-
fullCursor() {
|
|
236
|
-
return new TreeCursor(this.topNode, true);
|
|
237
|
-
}
|
|
238
|
-
/// Get a [syntax node](#tree.SyntaxNode) object for the top of the
|
|
239
|
-
/// tree.
|
|
240
|
-
get topNode() {
|
|
241
|
-
return new TreeNode(this, 0, 0, null);
|
|
242
|
-
}
|
|
243
|
-
/// Get the [syntax node](#tree.SyntaxNode) at the given position.
|
|
244
|
-
/// If `side` is -1, this will move into nodes that end at the
|
|
245
|
-
/// position. If 1, it'll move into nodes that start at the
|
|
246
|
-
/// position. With 0, it'll only enter nodes that cover the position
|
|
247
|
-
/// from both sides.
|
|
248
|
-
resolve(pos, side = 0) {
|
|
249
|
-
return this.cursor(pos, side).node;
|
|
250
|
-
}
|
|
251
|
-
/// Iterate over the tree and its children, calling `enter` for any
|
|
252
|
-
/// node that touches the `from`/`to` region (if given) before
|
|
253
|
-
/// running over such a node's children, and `leave` (if given) when
|
|
254
|
-
/// leaving the node. When `enter` returns `false`, the given node
|
|
255
|
-
/// will not have its children iterated over (or `leave` called).
|
|
256
|
-
iterate(spec) {
|
|
257
|
-
let { enter, leave, from = 0, to = this.length } = spec;
|
|
258
|
-
for (let c = this.cursor();;) {
|
|
259
|
-
let mustLeave = false;
|
|
260
|
-
if (c.from <= to && c.to >= from && (c.type.isAnonymous || enter(c.type, c.from, c.to) !== false)) {
|
|
261
|
-
if (c.firstChild())
|
|
262
|
-
continue;
|
|
263
|
-
if (!c.type.isAnonymous)
|
|
264
|
-
mustLeave = true;
|
|
265
|
-
}
|
|
266
|
-
for (;;) {
|
|
267
|
-
if (mustLeave && leave)
|
|
268
|
-
leave(c.type, c.from, c.to);
|
|
269
|
-
mustLeave = c.type.isAnonymous;
|
|
270
|
-
if (c.nextSibling())
|
|
271
|
-
break;
|
|
272
|
-
if (!c.parent())
|
|
273
|
-
return;
|
|
274
|
-
mustLeave = true;
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
/// Balance the direct children of this tree.
|
|
279
|
-
balance(maxBufferLength = DefaultBufferLength) {
|
|
280
|
-
return this.children.length <= BalanceBranchFactor ? this
|
|
281
|
-
: balanceRange(this.type, NodeType.none, this.children, this.positions, 0, this.children.length, 0, maxBufferLength, this.length, 0);
|
|
282
|
-
}
|
|
283
|
-
/// Build a tree from a postfix-ordered buffer of node information,
|
|
284
|
-
/// or a cursor over such a buffer.
|
|
285
|
-
static build(data) { return buildTree(data); }
|
|
286
|
-
}
|
|
287
|
-
/// The empty tree
|
|
288
|
-
Tree.empty = new Tree(NodeType.none, [], [], 0);
|
|
289
|
-
// For trees that need a context hash attached, we're using this
|
|
290
|
-
// kludge which assigns an extra property directly after
|
|
291
|
-
// initialization (creating a single new object shape).
|
|
292
|
-
function withHash(tree, hash) {
|
|
293
|
-
if (hash)
|
|
294
|
-
tree.contextHash = hash;
|
|
295
|
-
return tree;
|
|
296
|
-
}
|
|
297
|
-
/// Tree buffers contain (type, start, end, endIndex) quads for each
|
|
298
|
-
/// node. In such a buffer, nodes are stored in prefix order (parents
|
|
299
|
-
/// before children, with the endIndex of the parent indicating which
|
|
300
|
-
/// children belong to it)
|
|
301
|
-
class TreeBuffer {
|
|
302
|
-
/// Create a tree buffer @internal
|
|
303
|
-
constructor(
|
|
304
|
-
/// @internal
|
|
305
|
-
buffer,
|
|
306
|
-
/// The total length of the group of nodes in the buffer.
|
|
307
|
-
length,
|
|
308
|
-
/// @internal
|
|
309
|
-
set,
|
|
310
|
-
/// An optional repeat node type associated with the buffer.
|
|
311
|
-
type = NodeType.none) {
|
|
312
|
-
this.buffer = buffer;
|
|
313
|
-
this.length = length;
|
|
314
|
-
this.set = set;
|
|
315
|
-
this.type = type;
|
|
316
|
-
}
|
|
317
|
-
/// @internal
|
|
318
|
-
toString() {
|
|
319
|
-
let result = [];
|
|
320
|
-
for (let index = 0; index < this.buffer.length;) {
|
|
321
|
-
result.push(this.childString(index));
|
|
322
|
-
index = this.buffer[index + 3];
|
|
323
|
-
}
|
|
324
|
-
return result.join(",");
|
|
325
|
-
}
|
|
326
|
-
/// @internal
|
|
327
|
-
childString(index) {
|
|
328
|
-
let id = this.buffer[index], endIndex = this.buffer[index + 3];
|
|
329
|
-
let type = this.set.types[id], result = type.name;
|
|
330
|
-
if (/\W/.test(result) && !type.isError)
|
|
331
|
-
result = JSON.stringify(result);
|
|
332
|
-
index += 4;
|
|
333
|
-
if (endIndex == index)
|
|
334
|
-
return result;
|
|
335
|
-
let children = [];
|
|
336
|
-
while (index < endIndex) {
|
|
337
|
-
children.push(this.childString(index));
|
|
338
|
-
index = this.buffer[index + 3];
|
|
339
|
-
}
|
|
340
|
-
return result + "(" + children.join(",") + ")";
|
|
341
|
-
}
|
|
342
|
-
/// @internal
|
|
343
|
-
findChild(startIndex, endIndex, dir, after) {
|
|
344
|
-
let { buffer } = this, pick = -1;
|
|
345
|
-
for (let i = startIndex; i != endIndex; i = buffer[i + 3]) {
|
|
346
|
-
if (after != -100000000 /* None */) {
|
|
347
|
-
let start = buffer[i + 1], end = buffer[i + 2];
|
|
348
|
-
if (dir > 0) {
|
|
349
|
-
if (end > after)
|
|
350
|
-
pick = i;
|
|
351
|
-
if (end > after)
|
|
352
|
-
break;
|
|
353
|
-
}
|
|
354
|
-
else {
|
|
355
|
-
if (start < after)
|
|
356
|
-
pick = i;
|
|
357
|
-
if (end >= after)
|
|
358
|
-
break;
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
else {
|
|
362
|
-
pick = i;
|
|
363
|
-
if (dir > 0)
|
|
364
|
-
break;
|
|
365
|
-
}
|
|
366
|
-
}
|
|
367
|
-
return pick;
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
class TreeNode {
|
|
371
|
-
constructor(node, from, index, _parent) {
|
|
372
|
-
this.node = node;
|
|
373
|
-
this.from = from;
|
|
374
|
-
this.index = index;
|
|
375
|
-
this._parent = _parent;
|
|
376
|
-
}
|
|
377
|
-
get type() { return this.node.type; }
|
|
378
|
-
get name() { return this.node.type.name; }
|
|
379
|
-
get to() { return this.from + this.node.length; }
|
|
380
|
-
nextChild(i, dir, after, full = false) {
|
|
381
|
-
for (let parent = this;;) {
|
|
382
|
-
for (let { children, positions } = parent.node, e = dir > 0 ? children.length : -1; i != e; i += dir) {
|
|
383
|
-
let next = children[i], start = positions[i] + parent.from;
|
|
384
|
-
if (after != -100000000 /* None */ && (dir < 0 ? start >= after : start + next.length <= after))
|
|
385
|
-
continue;
|
|
386
|
-
if (next instanceof TreeBuffer) {
|
|
387
|
-
let index = next.findChild(0, next.buffer.length, dir, after == -100000000 /* None */ ? -100000000 /* None */ : after - start);
|
|
388
|
-
if (index > -1)
|
|
389
|
-
return new BufferNode(new BufferContext(parent, next, i, start), null, index);
|
|
390
|
-
}
|
|
391
|
-
else if (full || (!next.type.isAnonymous || hasChild(next))) {
|
|
392
|
-
let inner = new TreeNode(next, start, i, parent);
|
|
393
|
-
return full || !inner.type.isAnonymous ? inner : inner.nextChild(dir < 0 ? next.children.length - 1 : 0, dir, after);
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
if (full || !parent.type.isAnonymous)
|
|
397
|
-
return null;
|
|
398
|
-
i = parent.index + dir;
|
|
399
|
-
parent = parent._parent;
|
|
400
|
-
if (!parent)
|
|
401
|
-
return null;
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
get firstChild() { return this.nextChild(0, 1, -100000000 /* None */); }
|
|
405
|
-
get lastChild() { return this.nextChild(this.node.children.length - 1, -1, -100000000 /* None */); }
|
|
406
|
-
childAfter(pos) { return this.nextChild(0, 1, pos); }
|
|
407
|
-
childBefore(pos) { return this.nextChild(this.node.children.length - 1, -1, pos); }
|
|
408
|
-
nextSignificantParent() {
|
|
409
|
-
let val = this;
|
|
410
|
-
while (val.type.isAnonymous && val._parent)
|
|
411
|
-
val = val._parent;
|
|
412
|
-
return val;
|
|
413
|
-
}
|
|
414
|
-
get parent() {
|
|
415
|
-
return this._parent ? this._parent.nextSignificantParent() : null;
|
|
416
|
-
}
|
|
417
|
-
get nextSibling() {
|
|
418
|
-
return this._parent ? this._parent.nextChild(this.index + 1, 1, -1) : null;
|
|
419
|
-
}
|
|
420
|
-
get prevSibling() {
|
|
421
|
-
return this._parent ? this._parent.nextChild(this.index - 1, -1, -1) : null;
|
|
422
|
-
}
|
|
423
|
-
get cursor() { return new TreeCursor(this); }
|
|
424
|
-
resolve(pos, side = 0) {
|
|
425
|
-
return this.cursor.moveTo(pos, side).node;
|
|
426
|
-
}
|
|
427
|
-
getChild(type, before = null, after = null) {
|
|
428
|
-
let r = getChildren(this, type, before, after);
|
|
429
|
-
return r.length ? r[0] : null;
|
|
430
|
-
}
|
|
431
|
-
getChildren(type, before = null, after = null) {
|
|
432
|
-
return getChildren(this, type, before, after);
|
|
433
|
-
}
|
|
434
|
-
/// @internal
|
|
435
|
-
toString() { return this.node.toString(); }
|
|
436
|
-
}
|
|
437
|
-
function getChildren(node, type, before, after) {
|
|
438
|
-
let cur = node.cursor, result = [];
|
|
439
|
-
if (!cur.firstChild())
|
|
440
|
-
return result;
|
|
441
|
-
if (before != null)
|
|
442
|
-
while (!cur.type.is(before))
|
|
443
|
-
if (!cur.nextSibling())
|
|
444
|
-
return result;
|
|
445
|
-
for (;;) {
|
|
446
|
-
if (after != null && cur.type.is(after))
|
|
447
|
-
return result;
|
|
448
|
-
if (cur.type.is(type))
|
|
449
|
-
result.push(cur.node);
|
|
450
|
-
if (!cur.nextSibling())
|
|
451
|
-
return after == null ? result : [];
|
|
452
|
-
}
|
|
453
|
-
}
|
|
454
|
-
class BufferContext {
|
|
455
|
-
constructor(parent, buffer, index, start) {
|
|
456
|
-
this.parent = parent;
|
|
457
|
-
this.buffer = buffer;
|
|
458
|
-
this.index = index;
|
|
459
|
-
this.start = start;
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
class BufferNode {
|
|
463
|
-
constructor(context, _parent, index) {
|
|
464
|
-
this.context = context;
|
|
465
|
-
this._parent = _parent;
|
|
466
|
-
this.index = index;
|
|
467
|
-
this.type = context.buffer.set.types[context.buffer.buffer[index]];
|
|
468
|
-
}
|
|
469
|
-
get name() { return this.type.name; }
|
|
470
|
-
get from() { return this.context.start + this.context.buffer.buffer[this.index + 1]; }
|
|
471
|
-
get to() { return this.context.start + this.context.buffer.buffer[this.index + 2]; }
|
|
472
|
-
child(dir, after) {
|
|
473
|
-
let { buffer } = this.context;
|
|
474
|
-
let index = buffer.findChild(this.index + 4, buffer.buffer[this.index + 3], dir, after == -100000000 /* None */ ? -100000000 /* None */ : after - this.context.start);
|
|
475
|
-
return index < 0 ? null : new BufferNode(this.context, this, index);
|
|
476
|
-
}
|
|
477
|
-
get firstChild() { return this.child(1, -100000000 /* None */); }
|
|
478
|
-
get lastChild() { return this.child(-1, -100000000 /* None */); }
|
|
479
|
-
childAfter(pos) { return this.child(1, pos); }
|
|
480
|
-
childBefore(pos) { return this.child(-1, pos); }
|
|
481
|
-
get parent() {
|
|
482
|
-
return this._parent || this.context.parent.nextSignificantParent();
|
|
483
|
-
}
|
|
484
|
-
externalSibling(dir) {
|
|
485
|
-
return this._parent ? null : this.context.parent.nextChild(this.context.index + dir, dir, -1);
|
|
486
|
-
}
|
|
487
|
-
get nextSibling() {
|
|
488
|
-
let { buffer } = this.context;
|
|
489
|
-
let after = buffer.buffer[this.index + 3];
|
|
490
|
-
if (after < (this._parent ? buffer.buffer[this._parent.index + 3] : buffer.buffer.length))
|
|
491
|
-
return new BufferNode(this.context, this._parent, after);
|
|
492
|
-
return this.externalSibling(1);
|
|
493
|
-
}
|
|
494
|
-
get prevSibling() {
|
|
495
|
-
let { buffer } = this.context;
|
|
496
|
-
let parentStart = this._parent ? this._parent.index + 4 : 0;
|
|
497
|
-
if (this.index == parentStart)
|
|
498
|
-
return this.externalSibling(-1);
|
|
499
|
-
return new BufferNode(this.context, this._parent, buffer.findChild(parentStart, this.index, -1, -100000000 /* None */));
|
|
500
|
-
}
|
|
501
|
-
get cursor() { return new TreeCursor(this); }
|
|
502
|
-
resolve(pos, side = 0) {
|
|
503
|
-
return this.cursor.moveTo(pos, side).node;
|
|
504
|
-
}
|
|
505
|
-
/// @internal
|
|
506
|
-
toString() { return this.context.buffer.childString(this.index); }
|
|
507
|
-
getChild(type, before = null, after = null) {
|
|
508
|
-
let r = getChildren(this, type, before, after);
|
|
509
|
-
return r.length ? r[0] : null;
|
|
510
|
-
}
|
|
511
|
-
getChildren(type, before = null, after = null) {
|
|
512
|
-
return getChildren(this, type, before, after);
|
|
513
|
-
}
|
|
514
|
-
}
|
|
515
|
-
/// A tree cursor object focuses on a given node in a syntax tree, and
|
|
516
|
-
/// allows you to move to adjacent nodes.
|
|
517
|
-
class TreeCursor {
|
|
518
|
-
/// @internal
|
|
519
|
-
constructor(node, full = false) {
|
|
520
|
-
this.full = full;
|
|
521
|
-
this.buffer = null;
|
|
522
|
-
this.stack = [];
|
|
523
|
-
this.index = 0;
|
|
524
|
-
this.bufferNode = null;
|
|
525
|
-
if (node instanceof TreeNode) {
|
|
526
|
-
this.yieldNode(node);
|
|
527
|
-
}
|
|
528
|
-
else {
|
|
529
|
-
this._tree = node.context.parent;
|
|
530
|
-
this.buffer = node.context;
|
|
531
|
-
for (let n = node._parent; n; n = n._parent)
|
|
532
|
-
this.stack.unshift(n.index);
|
|
533
|
-
this.bufferNode = node;
|
|
534
|
-
this.yieldBuf(node.index);
|
|
535
|
-
}
|
|
536
|
-
}
|
|
537
|
-
/// Shorthand for `.type.name`.
|
|
538
|
-
get name() { return this.type.name; }
|
|
539
|
-
yieldNode(node) {
|
|
540
|
-
if (!node)
|
|
541
|
-
return false;
|
|
542
|
-
this._tree = node;
|
|
543
|
-
this.type = node.type;
|
|
544
|
-
this.from = node.from;
|
|
545
|
-
this.to = node.to;
|
|
546
|
-
return true;
|
|
547
|
-
}
|
|
548
|
-
yieldBuf(index, type) {
|
|
549
|
-
this.index = index;
|
|
550
|
-
let { start, buffer } = this.buffer;
|
|
551
|
-
this.type = type || buffer.set.types[buffer.buffer[index]];
|
|
552
|
-
this.from = start + buffer.buffer[index + 1];
|
|
553
|
-
this.to = start + buffer.buffer[index + 2];
|
|
554
|
-
return true;
|
|
555
|
-
}
|
|
556
|
-
yield(node) {
|
|
557
|
-
if (!node)
|
|
558
|
-
return false;
|
|
559
|
-
if (node instanceof TreeNode) {
|
|
560
|
-
this.buffer = null;
|
|
561
|
-
return this.yieldNode(node);
|
|
562
|
-
}
|
|
563
|
-
this.buffer = node.context;
|
|
564
|
-
return this.yieldBuf(node.index, node.type);
|
|
565
|
-
}
|
|
566
|
-
/// @internal
|
|
567
|
-
toString() {
|
|
568
|
-
return this.buffer ? this.buffer.buffer.childString(this.index) : this._tree.toString();
|
|
569
|
-
}
|
|
570
|
-
/// @internal
|
|
571
|
-
enter(dir, after) {
|
|
572
|
-
if (!this.buffer)
|
|
573
|
-
return this.yield(this._tree.nextChild(dir < 0 ? this._tree.node.children.length - 1 : 0, dir, after, this.full));
|
|
574
|
-
let { buffer } = this.buffer;
|
|
575
|
-
let index = buffer.findChild(this.index + 4, buffer.buffer[this.index + 3], dir, after == -100000000 /* None */ ? -100000000 /* None */ : after - this.buffer.start);
|
|
576
|
-
if (index < 0)
|
|
577
|
-
return false;
|
|
578
|
-
this.stack.push(this.index);
|
|
579
|
-
return this.yieldBuf(index);
|
|
580
|
-
}
|
|
581
|
-
/// Move the cursor to this node's first child. When this returns
|
|
582
|
-
/// false, the node has no child, and the cursor has not been moved.
|
|
583
|
-
firstChild() { return this.enter(1, -100000000 /* None */); }
|
|
584
|
-
/// Move the cursor to this node's last child.
|
|
585
|
-
lastChild() { return this.enter(-1, -100000000 /* None */); }
|
|
586
|
-
/// Move the cursor to the first child that starts at or after `pos`.
|
|
587
|
-
childAfter(pos) { return this.enter(1, pos); }
|
|
588
|
-
/// Move to the last child that ends at or before `pos`.
|
|
589
|
-
childBefore(pos) { return this.enter(-1, pos); }
|
|
590
|
-
/// Move the node's parent node, if this isn't the top node.
|
|
591
|
-
parent() {
|
|
592
|
-
if (!this.buffer)
|
|
593
|
-
return this.yieldNode(this.full ? this._tree._parent : this._tree.parent);
|
|
594
|
-
if (this.stack.length)
|
|
595
|
-
return this.yieldBuf(this.stack.pop());
|
|
596
|
-
let parent = this.full ? this.buffer.parent : this.buffer.parent.nextSignificantParent();
|
|
597
|
-
this.buffer = null;
|
|
598
|
-
return this.yieldNode(parent);
|
|
599
|
-
}
|
|
600
|
-
/// @internal
|
|
601
|
-
sibling(dir) {
|
|
602
|
-
if (!this.buffer)
|
|
603
|
-
return !this._tree._parent ? false
|
|
604
|
-
: this.yield(this._tree._parent.nextChild(this._tree.index + dir, dir, -100000000 /* None */, this.full));
|
|
605
|
-
let { buffer } = this.buffer, d = this.stack.length - 1;
|
|
606
|
-
if (dir < 0) {
|
|
607
|
-
let parentStart = d < 0 ? 0 : this.stack[d] + 4;
|
|
608
|
-
if (this.index != parentStart)
|
|
609
|
-
return this.yieldBuf(buffer.findChild(parentStart, this.index, -1, -100000000 /* None */));
|
|
610
|
-
}
|
|
611
|
-
else {
|
|
612
|
-
let after = buffer.buffer[this.index + 3];
|
|
613
|
-
if (after < (d < 0 ? buffer.buffer.length : buffer.buffer[this.stack[d] + 3]))
|
|
614
|
-
return this.yieldBuf(after);
|
|
615
|
-
}
|
|
616
|
-
return d < 0 ? this.yield(this.buffer.parent.nextChild(this.buffer.index + dir, dir, -100000000 /* None */, this.full)) : false;
|
|
617
|
-
}
|
|
618
|
-
/// Move to this node's next sibling, if any.
|
|
619
|
-
nextSibling() { return this.sibling(1); }
|
|
620
|
-
/// Move to this node's previous sibling, if any.
|
|
621
|
-
prevSibling() { return this.sibling(-1); }
|
|
622
|
-
atLastNode(dir) {
|
|
623
|
-
let index, parent, { buffer } = this;
|
|
624
|
-
if (buffer) {
|
|
625
|
-
if (dir > 0) {
|
|
626
|
-
if (this.index < buffer.buffer.buffer.length)
|
|
627
|
-
return false;
|
|
628
|
-
}
|
|
629
|
-
else {
|
|
630
|
-
for (let i = 0; i < this.index; i++)
|
|
631
|
-
if (buffer.buffer.buffer[i + 3] < this.index)
|
|
632
|
-
return false;
|
|
633
|
-
}
|
|
634
|
-
({ index, parent } = buffer);
|
|
635
|
-
}
|
|
636
|
-
else {
|
|
637
|
-
({ index, _parent: parent } = this._tree);
|
|
638
|
-
}
|
|
639
|
-
for (; parent; { index, _parent: parent } = parent) {
|
|
640
|
-
for (let i = index + dir, e = dir < 0 ? -1 : parent.node.children.length; i != e; i += dir) {
|
|
641
|
-
let child = parent.node.children[i];
|
|
642
|
-
if (this.full || !child.type.isAnonymous || child instanceof TreeBuffer || hasChild(child))
|
|
643
|
-
return false;
|
|
644
|
-
}
|
|
645
|
-
}
|
|
646
|
-
return true;
|
|
647
|
-
}
|
|
648
|
-
move(dir) {
|
|
649
|
-
if (this.enter(dir, -100000000 /* None */))
|
|
650
|
-
return true;
|
|
651
|
-
for (;;) {
|
|
652
|
-
if (this.sibling(dir))
|
|
653
|
-
return true;
|
|
654
|
-
if (this.atLastNode(dir) || !this.parent())
|
|
655
|
-
return false;
|
|
656
|
-
}
|
|
657
|
-
}
|
|
658
|
-
/// Move to the next node in a
|
|
659
|
-
/// [pre-order](https://en.wikipedia.org/wiki/Tree_traversal#Pre-order_(NLR))
|
|
660
|
-
/// traversal, going from a node to its first child or, if the
|
|
661
|
-
/// current node is empty, its next sibling or the next sibling of
|
|
662
|
-
/// the first parent node that has one.
|
|
663
|
-
next() { return this.move(1); }
|
|
664
|
-
/// Move to the next node in a last-to-first pre-order traveral. A
|
|
665
|
-
/// node is followed by ist last child or, if it has none, its
|
|
666
|
-
/// previous sibling or the previous sibling of the first parent
|
|
667
|
-
/// node that has one.
|
|
668
|
-
prev() { return this.move(-1); }
|
|
669
|
-
/// Move the cursor to the innermost node that covers `pos`. If
|
|
670
|
-
/// `side` is -1, it will enter nodes that end at `pos`. If it is 1,
|
|
671
|
-
/// it will enter nodes that start at `pos`.
|
|
672
|
-
moveTo(pos, side = 0) {
|
|
673
|
-
// Move up to a node that actually holds the position, if possible
|
|
674
|
-
while (this.from == this.to ||
|
|
675
|
-
(side < 1 ? this.from >= pos : this.from > pos) ||
|
|
676
|
-
(side > -1 ? this.to <= pos : this.to < pos))
|
|
677
|
-
if (!this.parent())
|
|
678
|
-
break;
|
|
679
|
-
// Then scan down into child nodes as far as possible
|
|
680
|
-
for (;;) {
|
|
681
|
-
if (side < 0 ? !this.childBefore(pos) : !this.childAfter(pos))
|
|
682
|
-
break;
|
|
683
|
-
if (this.from == this.to ||
|
|
684
|
-
(side < 1 ? this.from >= pos : this.from > pos) ||
|
|
685
|
-
(side > -1 ? this.to <= pos : this.to < pos)) {
|
|
686
|
-
this.parent();
|
|
687
|
-
break;
|
|
688
|
-
}
|
|
689
|
-
}
|
|
690
|
-
return this;
|
|
691
|
-
}
|
|
692
|
-
/// Get a [syntax node](#tree.SyntaxNode) at the cursor's current
|
|
693
|
-
/// position.
|
|
694
|
-
get node() {
|
|
695
|
-
if (!this.buffer)
|
|
696
|
-
return this._tree;
|
|
697
|
-
let cache = this.bufferNode, result = null, depth = 0;
|
|
698
|
-
if (cache && cache.context == this.buffer) {
|
|
699
|
-
scan: for (let index = this.index, d = this.stack.length; d >= 0;) {
|
|
700
|
-
for (let c = cache; c; c = c._parent)
|
|
701
|
-
if (c.index == index) {
|
|
702
|
-
if (index == this.index)
|
|
703
|
-
return c;
|
|
704
|
-
result = c;
|
|
705
|
-
depth = d + 1;
|
|
706
|
-
break scan;
|
|
707
|
-
}
|
|
708
|
-
index = this.stack[--d];
|
|
709
|
-
}
|
|
710
|
-
}
|
|
711
|
-
for (let i = depth; i < this.stack.length; i++)
|
|
712
|
-
result = new BufferNode(this.buffer, result, this.stack[i]);
|
|
713
|
-
return this.bufferNode = new BufferNode(this.buffer, result, this.index);
|
|
714
|
-
}
|
|
715
|
-
/// Get the [tree](#tree.Tree) that represents the current node, if
|
|
716
|
-
/// any. Will return null when the node is in a [tree
|
|
717
|
-
/// buffer](#tree.TreeBuffer).
|
|
718
|
-
get tree() {
|
|
719
|
-
return this.buffer ? null : this._tree.node;
|
|
720
|
-
}
|
|
721
|
-
}
|
|
722
|
-
function hasChild(tree) {
|
|
723
|
-
return tree.children.some(ch => !ch.type.isAnonymous || ch instanceof TreeBuffer || hasChild(ch));
|
|
724
|
-
}
|
|
725
|
-
class FlatBufferCursor {
|
|
726
|
-
constructor(buffer, index) {
|
|
727
|
-
this.buffer = buffer;
|
|
728
|
-
this.index = index;
|
|
729
|
-
}
|
|
730
|
-
get id() { return this.buffer[this.index - 4]; }
|
|
731
|
-
get start() { return this.buffer[this.index - 3]; }
|
|
732
|
-
get end() { return this.buffer[this.index - 2]; }
|
|
733
|
-
get size() { return this.buffer[this.index - 1]; }
|
|
734
|
-
get pos() { return this.index; }
|
|
735
|
-
next() { this.index -= 4; }
|
|
736
|
-
fork() { return new FlatBufferCursor(this.buffer, this.index); }
|
|
737
|
-
}
|
|
738
|
-
const BalanceBranchFactor = 8;
|
|
739
|
-
function buildTree(data) {
|
|
740
|
-
var _a;
|
|
741
|
-
let { buffer, nodeSet, topID = 0, maxBufferLength = DefaultBufferLength, reused = [], minRepeatType = nodeSet.types.length } = data;
|
|
742
|
-
let cursor = Array.isArray(buffer) ? new FlatBufferCursor(buffer, buffer.length) : buffer;
|
|
743
|
-
let types = nodeSet.types;
|
|
744
|
-
let contextHash = 0;
|
|
745
|
-
function takeNode(parentStart, minPos, children, positions, inRepeat) {
|
|
746
|
-
let { id, start, end, size } = cursor;
|
|
747
|
-
let startPos = start - parentStart;
|
|
748
|
-
if (size < 0) {
|
|
749
|
-
if (size == -1) { // Reused node
|
|
750
|
-
children.push(reused[id]);
|
|
751
|
-
positions.push(startPos);
|
|
752
|
-
}
|
|
753
|
-
else { // Context change
|
|
754
|
-
contextHash = id;
|
|
755
|
-
}
|
|
756
|
-
cursor.next();
|
|
757
|
-
return;
|
|
758
|
-
}
|
|
759
|
-
let type = types[id], node, buffer;
|
|
760
|
-
if (end - start <= maxBufferLength && (buffer = findBufferSize(cursor.pos - minPos, inRepeat))) {
|
|
761
|
-
// Small enough for a buffer, and no reused nodes inside
|
|
762
|
-
let data = new Uint16Array(buffer.size - buffer.skip);
|
|
763
|
-
let endPos = cursor.pos - buffer.size, index = data.length;
|
|
764
|
-
while (cursor.pos > endPos)
|
|
765
|
-
index = copyToBuffer(buffer.start, data, index, inRepeat);
|
|
766
|
-
node = new TreeBuffer(data, end - buffer.start, nodeSet, inRepeat < 0 ? NodeType.none : types[inRepeat]);
|
|
767
|
-
startPos = buffer.start - parentStart;
|
|
768
|
-
}
|
|
769
|
-
else { // Make it a node
|
|
770
|
-
let endPos = cursor.pos - size;
|
|
771
|
-
cursor.next();
|
|
772
|
-
let localChildren = [], localPositions = [];
|
|
773
|
-
let localInRepeat = id >= minRepeatType ? id : -1;
|
|
774
|
-
while (cursor.pos > endPos) {
|
|
775
|
-
if (cursor.id == localInRepeat)
|
|
776
|
-
cursor.next();
|
|
777
|
-
else
|
|
778
|
-
takeNode(start, endPos, localChildren, localPositions, localInRepeat);
|
|
779
|
-
}
|
|
780
|
-
localChildren.reverse();
|
|
781
|
-
localPositions.reverse();
|
|
782
|
-
if (localInRepeat > -1 && localChildren.length > BalanceBranchFactor)
|
|
783
|
-
node = balanceRange(type, type, localChildren, localPositions, 0, localChildren.length, 0, maxBufferLength, end - start, contextHash);
|
|
784
|
-
else
|
|
785
|
-
node = withHash(new Tree(type, localChildren, localPositions, end - start), contextHash);
|
|
786
|
-
}
|
|
787
|
-
children.push(node);
|
|
788
|
-
positions.push(startPos);
|
|
789
|
-
}
|
|
790
|
-
function findBufferSize(maxSize, inRepeat) {
|
|
791
|
-
// Scan through the buffer to find previous siblings that fit
|
|
792
|
-
// together in a TreeBuffer, and don't contain any reused nodes
|
|
793
|
-
// (which can't be stored in a buffer).
|
|
794
|
-
// If `inRepeat` is > -1, ignore node boundaries of that type for
|
|
795
|
-
// nesting, but make sure the end falls either at the start
|
|
796
|
-
// (`maxSize`) or before such a node.
|
|
797
|
-
let fork = cursor.fork();
|
|
798
|
-
let size = 0, start = 0, skip = 0, minStart = fork.end - maxBufferLength;
|
|
799
|
-
let result = { size: 0, start: 0, skip: 0 };
|
|
800
|
-
scan: for (let minPos = fork.pos - maxSize; fork.pos > minPos;) {
|
|
801
|
-
// Pretend nested repeat nodes of the same type don't exist
|
|
802
|
-
if (fork.id == inRepeat) {
|
|
803
|
-
// Except that we store the current state as a valid return
|
|
804
|
-
// value.
|
|
805
|
-
result.size = size;
|
|
806
|
-
result.start = start;
|
|
807
|
-
result.skip = skip;
|
|
808
|
-
skip += 4;
|
|
809
|
-
size += 4;
|
|
810
|
-
fork.next();
|
|
811
|
-
continue;
|
|
812
|
-
}
|
|
813
|
-
let nodeSize = fork.size, startPos = fork.pos - nodeSize;
|
|
814
|
-
if (nodeSize < 0 || startPos < minPos || fork.start < minStart)
|
|
815
|
-
break;
|
|
816
|
-
let localSkipped = fork.id >= minRepeatType ? 4 : 0;
|
|
817
|
-
let nodeStart = fork.start;
|
|
818
|
-
fork.next();
|
|
819
|
-
while (fork.pos > startPos) {
|
|
820
|
-
if (fork.size < 0)
|
|
821
|
-
break scan;
|
|
822
|
-
if (fork.id >= minRepeatType)
|
|
823
|
-
localSkipped += 4;
|
|
824
|
-
fork.next();
|
|
825
|
-
}
|
|
826
|
-
start = nodeStart;
|
|
827
|
-
size += nodeSize;
|
|
828
|
-
skip += localSkipped;
|
|
829
|
-
}
|
|
830
|
-
if (inRepeat < 0 || size == maxSize) {
|
|
831
|
-
result.size = size;
|
|
832
|
-
result.start = start;
|
|
833
|
-
result.skip = skip;
|
|
834
|
-
}
|
|
835
|
-
return result.size > 4 ? result : undefined;
|
|
836
|
-
}
|
|
837
|
-
function copyToBuffer(bufferStart, buffer, index, inRepeat) {
|
|
838
|
-
let { id, start, end, size } = cursor;
|
|
839
|
-
cursor.next();
|
|
840
|
-
if (id == inRepeat)
|
|
841
|
-
return index;
|
|
842
|
-
let startIndex = index;
|
|
843
|
-
if (size > 4) {
|
|
844
|
-
let endPos = cursor.pos - (size - 4);
|
|
845
|
-
while (cursor.pos > endPos)
|
|
846
|
-
index = copyToBuffer(bufferStart, buffer, index, inRepeat);
|
|
847
|
-
}
|
|
848
|
-
if (id < minRepeatType) { // Don't copy repeat nodes into buffers
|
|
849
|
-
buffer[--index] = startIndex;
|
|
850
|
-
buffer[--index] = end - bufferStart;
|
|
851
|
-
buffer[--index] = start - bufferStart;
|
|
852
|
-
buffer[--index] = id;
|
|
853
|
-
}
|
|
854
|
-
return index;
|
|
855
|
-
}
|
|
856
|
-
let children = [], positions = [];
|
|
857
|
-
while (cursor.pos > 0)
|
|
858
|
-
takeNode(data.start || 0, 0, children, positions, -1);
|
|
859
|
-
let length = (_a = data.length) !== null && _a !== void 0 ? _a : (children.length ? positions[0] + children[0].length : 0);
|
|
860
|
-
return new Tree(types[topID], children.reverse(), positions.reverse(), length);
|
|
861
|
-
}
|
|
862
|
-
function balanceRange(outerType, innerType, children, positions, from, to, start, maxBufferLength, length, contextHash) {
|
|
863
|
-
let localChildren = [], localPositions = [];
|
|
864
|
-
if (length <= maxBufferLength) {
|
|
865
|
-
for (let i = from; i < to; i++) {
|
|
866
|
-
localChildren.push(children[i]);
|
|
867
|
-
localPositions.push(positions[i] - start);
|
|
868
|
-
}
|
|
869
|
-
}
|
|
870
|
-
else {
|
|
871
|
-
let maxChild = Math.max(maxBufferLength, Math.ceil(length * 1.5 / BalanceBranchFactor));
|
|
872
|
-
for (let i = from; i < to;) {
|
|
873
|
-
let groupFrom = i, groupStart = positions[i];
|
|
874
|
-
i++;
|
|
875
|
-
for (; i < to; i++) {
|
|
876
|
-
let nextEnd = positions[i] + children[i].length;
|
|
877
|
-
if (nextEnd - groupStart > maxChild)
|
|
878
|
-
break;
|
|
879
|
-
}
|
|
880
|
-
if (i == groupFrom + 1) {
|
|
881
|
-
let only = children[groupFrom];
|
|
882
|
-
if (only instanceof Tree && only.type == innerType && only.length > maxChild << 1) { // Too big, collapse
|
|
883
|
-
for (let j = 0; j < only.children.length; j++) {
|
|
884
|
-
localChildren.push(only.children[j]);
|
|
885
|
-
localPositions.push(only.positions[j] + groupStart - start);
|
|
886
|
-
}
|
|
887
|
-
continue;
|
|
888
|
-
}
|
|
889
|
-
localChildren.push(only);
|
|
890
|
-
}
|
|
891
|
-
else if (i == groupFrom + 1) {
|
|
892
|
-
localChildren.push(children[groupFrom]);
|
|
893
|
-
}
|
|
894
|
-
else {
|
|
895
|
-
let inner = balanceRange(innerType, innerType, children, positions, groupFrom, i, groupStart, maxBufferLength, positions[i - 1] + children[i - 1].length - groupStart, contextHash);
|
|
896
|
-
if (innerType != NodeType.none && !containsType(inner.children, innerType))
|
|
897
|
-
inner = withHash(new Tree(NodeType.none, inner.children, inner.positions, inner.length), contextHash);
|
|
898
|
-
localChildren.push(inner);
|
|
899
|
-
}
|
|
900
|
-
localPositions.push(groupStart - start);
|
|
901
|
-
}
|
|
902
|
-
}
|
|
903
|
-
return withHash(new Tree(outerType, localChildren, localPositions, length), contextHash);
|
|
904
|
-
}
|
|
905
|
-
function containsType(nodes, type) {
|
|
906
|
-
for (let elt of nodes)
|
|
907
|
-
if (elt.type == type)
|
|
908
|
-
return true;
|
|
909
|
-
return false;
|
|
910
|
-
}
|
|
911
|
-
/// Tree fragments are used during [incremental
|
|
912
|
-
/// parsing](#lezer.ParseOptions.fragments) to track parts of old
|
|
913
|
-
/// trees that can be reused in a new parse. An array of fragments is
|
|
914
|
-
/// used to track regions of an old tree whose nodes might be reused
|
|
915
|
-
/// in new parses. Use the static
|
|
916
|
-
/// [`applyChanges`](#tree.TreeFragment^applyChanges) method to update
|
|
917
|
-
/// fragments for document changes.
|
|
918
|
-
class TreeFragment {
|
|
919
|
-
constructor(
|
|
920
|
-
/// The start of the unchanged range pointed to by this fragment.
|
|
921
|
-
/// This refers to an offset in the _updated_ document (as opposed
|
|
922
|
-
/// to the original tree).
|
|
923
|
-
from,
|
|
924
|
-
/// The end of the unchanged range.
|
|
925
|
-
to,
|
|
926
|
-
/// The tree that this fragment is based on.
|
|
927
|
-
tree,
|
|
928
|
-
/// The offset between the fragment's tree and the document that
|
|
929
|
-
/// this fragment can be used against. Add this when going from
|
|
930
|
-
/// document to tree positions, subtract it to go from tree to
|
|
931
|
-
/// document positions.
|
|
932
|
-
offset, open) {
|
|
933
|
-
this.from = from;
|
|
934
|
-
this.to = to;
|
|
935
|
-
this.tree = tree;
|
|
936
|
-
this.offset = offset;
|
|
937
|
-
this.open = open;
|
|
938
|
-
}
|
|
939
|
-
get openStart() { return (this.open & 1 /* Start */) > 0; }
|
|
940
|
-
get openEnd() { return (this.open & 2 /* End */) > 0; }
|
|
941
|
-
/// Apply a set of edits to an array of fragments, removing or
|
|
942
|
-
/// splitting fragments as necessary to remove edited ranges, and
|
|
943
|
-
/// adjusting offsets for fragments that moved.
|
|
944
|
-
static applyChanges(fragments, changes, minGap = 128) {
|
|
945
|
-
if (!changes.length)
|
|
946
|
-
return fragments;
|
|
947
|
-
let result = [];
|
|
948
|
-
let fI = 1, nextF = fragments.length ? fragments[0] : null;
|
|
949
|
-
for (let cI = 0, pos = 0, off = 0;; cI++) {
|
|
950
|
-
let nextC = cI < changes.length ? changes[cI] : null;
|
|
951
|
-
let nextPos = nextC ? nextC.fromA : 1e9;
|
|
952
|
-
if (nextPos - pos >= minGap)
|
|
953
|
-
while (nextF && nextF.from < nextPos) {
|
|
954
|
-
let cut = nextF;
|
|
955
|
-
if (pos >= cut.from || nextPos <= cut.to || off) {
|
|
956
|
-
let fFrom = Math.max(cut.from, pos) - off, fTo = Math.min(cut.to, nextPos) - off;
|
|
957
|
-
cut = fFrom >= fTo ? null :
|
|
958
|
-
new TreeFragment(fFrom, fTo, cut.tree, cut.offset + off, (cI > 0 ? 1 /* Start */ : 0) | (nextC ? 2 /* End */ : 0));
|
|
959
|
-
}
|
|
960
|
-
if (cut)
|
|
961
|
-
result.push(cut);
|
|
962
|
-
if (nextF.to > nextPos)
|
|
963
|
-
break;
|
|
964
|
-
nextF = fI < fragments.length ? fragments[fI++] : null;
|
|
965
|
-
}
|
|
966
|
-
if (!nextC)
|
|
967
|
-
break;
|
|
968
|
-
pos = nextC.toA;
|
|
969
|
-
off = nextC.toA - nextC.toB;
|
|
970
|
-
}
|
|
971
|
-
return result;
|
|
972
|
-
}
|
|
973
|
-
/// Create a set of fragments from a freshly parsed tree, or update
|
|
974
|
-
/// an existing set of fragments by replacing the ones that overlap
|
|
975
|
-
/// with a tree with content from the new tree. When `partial` is
|
|
976
|
-
/// true, the parse is treated as incomplete, and the token at its
|
|
977
|
-
/// end is not included in [`safeTo`](#tree.TreeFragment.safeTo).
|
|
978
|
-
static addTree(tree, fragments = [], partial = false) {
|
|
979
|
-
let result = [new TreeFragment(0, tree.length, tree, 0, partial ? 2 /* End */ : 0)];
|
|
980
|
-
for (let f of fragments)
|
|
981
|
-
if (f.to > tree.length)
|
|
982
|
-
result.push(f);
|
|
983
|
-
return result;
|
|
984
|
-
}
|
|
985
|
-
}
|
|
986
|
-
// Creates an `Input` that is backed by a single, flat string.
|
|
987
|
-
function stringInput(input) { return new StringInput(input); }
|
|
988
|
-
class StringInput {
|
|
989
|
-
constructor(string, length = string.length) {
|
|
990
|
-
this.string = string;
|
|
991
|
-
this.length = length;
|
|
992
|
-
}
|
|
993
|
-
get(pos) {
|
|
994
|
-
return pos < 0 || pos >= this.length ? -1 : this.string.charCodeAt(pos);
|
|
995
|
-
}
|
|
996
|
-
lineAfter(pos) {
|
|
997
|
-
if (pos < 0)
|
|
998
|
-
return "";
|
|
999
|
-
let end = this.string.indexOf("\n", pos);
|
|
1000
|
-
return this.string.slice(pos, end < 0 ? this.length : Math.min(end, this.length));
|
|
1001
|
-
}
|
|
1002
|
-
read(from, to) { return this.string.slice(from, Math.min(this.length, to)); }
|
|
1003
|
-
clip(at) { return new StringInput(this.string, at); }
|
|
1004
|
-
}
|
|
1005
|
-
|
|
1006
|
-
export { DefaultBufferLength, NodeProp, NodeSet, NodeType, Tree, TreeBuffer, TreeCursor, TreeFragment, stringInput };
|
|
1007
|
-
//# sourceMappingURL=tree.es.js.map
|