@vuu-ui/vuu-data-local 0.8.32 → 0.8.34

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.
Files changed (58) hide show
  1. package/cjs/node_modules/@lezer/common/dist/index.js +1288 -0
  2. package/cjs/node_modules/@lezer/common/dist/index.js.map +1 -0
  3. package/cjs/node_modules/@lezer/lr/dist/index.js +1669 -0
  4. package/cjs/node_modules/@lezer/lr/dist/index.js.map +1 -0
  5. package/cjs/packages/vuu-data-local/src/array-data-source/aggregate-utils.js +243 -0
  6. package/cjs/packages/vuu-data-local/src/array-data-source/aggregate-utils.js.map +1 -0
  7. package/cjs/packages/vuu-data-local/src/array-data-source/array-data-source.js +542 -0
  8. package/cjs/packages/vuu-data-local/src/array-data-source/array-data-source.js.map +1 -0
  9. package/cjs/packages/vuu-data-local/src/array-data-source/array-data-utils.js +52 -0
  10. package/cjs/packages/vuu-data-local/src/array-data-source/array-data-utils.js.map +1 -0
  11. package/cjs/packages/vuu-data-local/src/array-data-source/group-utils.js +169 -0
  12. package/cjs/packages/vuu-data-local/src/array-data-source/group-utils.js.map +1 -0
  13. package/cjs/packages/vuu-data-local/src/array-data-source/sort-utils.js +54 -0
  14. package/cjs/packages/vuu-data-local/src/array-data-source/sort-utils.js.map +1 -0
  15. package/cjs/packages/vuu-data-local/src/index.js +10 -0
  16. package/cjs/packages/vuu-data-local/src/index.js.map +1 -0
  17. package/cjs/packages/vuu-data-local/src/json-data-source/json-data-source.js +388 -0
  18. package/cjs/packages/vuu-data-local/src/json-data-source/json-data-source.js.map +1 -0
  19. package/cjs/packages/vuu-filter-parser/src/FilterParser.js +14 -0
  20. package/cjs/packages/vuu-filter-parser/src/FilterParser.js.map +1 -0
  21. package/cjs/packages/vuu-filter-parser/src/FilterTreeWalker.js +135 -0
  22. package/cjs/packages/vuu-filter-parser/src/FilterTreeWalker.js.map +1 -0
  23. package/cjs/packages/vuu-filter-parser/src/filter-evaluation-utils.js +86 -0
  24. package/cjs/packages/vuu-filter-parser/src/filter-evaluation-utils.js.map +1 -0
  25. package/cjs/packages/vuu-filter-parser/src/generated/filter-parser.js +21 -0
  26. package/cjs/packages/vuu-filter-parser/src/generated/filter-parser.js.map +1 -0
  27. package/esm/node_modules/@lezer/common/dist/index.js +1279 -0
  28. package/esm/node_modules/@lezer/common/dist/index.js.map +1 -0
  29. package/esm/node_modules/@lezer/lr/dist/index.js +1665 -0
  30. package/esm/node_modules/@lezer/lr/dist/index.js.map +1 -0
  31. package/esm/packages/vuu-data-local/src/array-data-source/aggregate-utils.js +241 -0
  32. package/esm/packages/vuu-data-local/src/array-data-source/aggregate-utils.js.map +1 -0
  33. package/esm/packages/vuu-data-local/src/array-data-source/array-data-source.js +540 -0
  34. package/esm/packages/vuu-data-local/src/array-data-source/array-data-source.js.map +1 -0
  35. package/esm/packages/vuu-data-local/src/array-data-source/array-data-utils.js +49 -0
  36. package/esm/packages/vuu-data-local/src/array-data-source/array-data-utils.js.map +1 -0
  37. package/esm/packages/vuu-data-local/src/array-data-source/group-utils.js +165 -0
  38. package/esm/packages/vuu-data-local/src/array-data-source/group-utils.js.map +1 -0
  39. package/esm/packages/vuu-data-local/src/array-data-source/sort-utils.js +52 -0
  40. package/esm/packages/vuu-data-local/src/array-data-source/sort-utils.js.map +1 -0
  41. package/esm/packages/vuu-data-local/src/index.js +3 -0
  42. package/esm/packages/vuu-data-local/src/index.js.map +1 -0
  43. package/esm/packages/vuu-data-local/src/json-data-source/json-data-source.js +386 -0
  44. package/esm/packages/vuu-data-local/src/json-data-source/json-data-source.js.map +1 -0
  45. package/esm/packages/vuu-filter-parser/src/FilterParser.js +12 -0
  46. package/esm/packages/vuu-filter-parser/src/FilterParser.js.map +1 -0
  47. package/esm/packages/vuu-filter-parser/src/FilterTreeWalker.js +133 -0
  48. package/esm/packages/vuu-filter-parser/src/FilterTreeWalker.js.map +1 -0
  49. package/esm/packages/vuu-filter-parser/src/filter-evaluation-utils.js +84 -0
  50. package/esm/packages/vuu-filter-parser/src/filter-evaluation-utils.js.map +1 -0
  51. package/esm/packages/vuu-filter-parser/src/generated/filter-parser.js +19 -0
  52. package/esm/packages/vuu-filter-parser/src/generated/filter-parser.js.map +1 -0
  53. package/package.json +10 -9
  54. package/LICENSE +0 -201
  55. package/cjs/index.js +0 -2
  56. package/cjs/index.js.map +0 -7
  57. package/esm/index.js +0 -2
  58. package/esm/index.js.map +0 -7
@@ -0,0 +1,1288 @@
1
+ 'use strict';
2
+
3
+ // FIXME profile adding a per-Tree TreeNode cache, validating it by
4
+ // parent pointer
5
+ /// The default maximum length of a `TreeBuffer` node.
6
+ const DefaultBufferLength = 1024;
7
+ let nextPropID = 0;
8
+ class Range {
9
+ constructor(from, to) {
10
+ this.from = from;
11
+ this.to = to;
12
+ }
13
+ }
14
+ /// Each [node type](#common.NodeType) or [individual tree](#common.Tree)
15
+ /// can have metadata associated with it in props. Instances of this
16
+ /// class represent prop names.
17
+ class NodeProp {
18
+ /// Create a new node prop type.
19
+ constructor(config = {}) {
20
+ this.id = nextPropID++;
21
+ this.perNode = !!config.perNode;
22
+ this.deserialize = config.deserialize || (() => {
23
+ throw new Error("This node type doesn't define a deserialize function");
24
+ });
25
+ }
26
+ /// This is meant to be used with
27
+ /// [`NodeSet.extend`](#common.NodeSet.extend) or
28
+ /// [`LRParser.configure`](#lr.ParserConfig.props) to compute
29
+ /// prop values for each node type in the set. Takes a [match
30
+ /// object](#common.NodeType^match) or function that returns undefined
31
+ /// if the node type doesn't get this prop, and the prop's value if
32
+ /// it does.
33
+ add(match) {
34
+ if (this.perNode)
35
+ throw new RangeError("Can't add per-node props to node types");
36
+ if (typeof match != "function")
37
+ match = NodeType.match(match);
38
+ return (type) => {
39
+ let result = match(type);
40
+ return result === undefined ? null : [this, result];
41
+ };
42
+ }
43
+ }
44
+ /// Prop that is used to describe matching delimiters. For opening
45
+ /// delimiters, this holds an array of node names (written as a
46
+ /// space-separated string when declaring this prop in a grammar)
47
+ /// for the node types of closing delimiters that match it.
48
+ NodeProp.closedBy = new NodeProp({ deserialize: str => str.split(" ") });
49
+ /// The inverse of [`closedBy`](#common.NodeProp^closedBy). This is
50
+ /// attached to closing delimiters, holding an array of node names
51
+ /// of types of matching opening delimiters.
52
+ NodeProp.openedBy = new NodeProp({ deserialize: str => str.split(" ") });
53
+ /// Used to assign node types to groups (for example, all node
54
+ /// types that represent an expression could be tagged with an
55
+ /// `"Expression"` group).
56
+ NodeProp.group = new NodeProp({ deserialize: str => str.split(" ") });
57
+ /// The hash of the [context](#lr.ContextTracker.constructor)
58
+ /// that the node was parsed in, if any. Used to limit reuse of
59
+ /// contextual nodes.
60
+ NodeProp.contextHash = new NodeProp({ perNode: true });
61
+ /// The distance beyond the end of the node that the tokenizer
62
+ /// looked ahead for any of the tokens inside the node. (The LR
63
+ /// parser only stores this when it is larger than 25, for
64
+ /// efficiency reasons.)
65
+ NodeProp.lookAhead = new NodeProp({ perNode: true });
66
+ /// This per-node prop is used to replace a given node, or part of a
67
+ /// node, with another tree. This is useful to include trees from
68
+ /// different languages in mixed-language parsers.
69
+ NodeProp.mounted = new NodeProp({ perNode: true });
70
+ const noProps = Object.create(null);
71
+ /// Each node in a syntax tree has a node type associated with it.
72
+ class NodeType {
73
+ /// @internal
74
+ constructor(
75
+ /// The name of the node type. Not necessarily unique, but if the
76
+ /// grammar was written properly, different node types with the
77
+ /// same name within a node set should play the same semantic
78
+ /// role.
79
+ name,
80
+ /// @internal
81
+ props,
82
+ /// The id of this node in its set. Corresponds to the term ids
83
+ /// used in the parser.
84
+ id,
85
+ /// @internal
86
+ flags = 0) {
87
+ this.name = name;
88
+ this.props = props;
89
+ this.id = id;
90
+ this.flags = flags;
91
+ }
92
+ /// Define a node type.
93
+ static define(spec) {
94
+ let props = spec.props && spec.props.length ? Object.create(null) : noProps;
95
+ let flags = (spec.top ? 1 /* NodeFlag.Top */ : 0) | (spec.skipped ? 2 /* NodeFlag.Skipped */ : 0) |
96
+ (spec.error ? 4 /* NodeFlag.Error */ : 0) | (spec.name == null ? 8 /* NodeFlag.Anonymous */ : 0);
97
+ let type = new NodeType(spec.name || "", props, spec.id, flags);
98
+ if (spec.props)
99
+ for (let src of spec.props) {
100
+ if (!Array.isArray(src))
101
+ src = src(type);
102
+ if (src) {
103
+ if (src[0].perNode)
104
+ throw new RangeError("Can't store a per-node prop on a node type");
105
+ props[src[0].id] = src[1];
106
+ }
107
+ }
108
+ return type;
109
+ }
110
+ /// Retrieves a node prop for this type. Will return `undefined` if
111
+ /// the prop isn't present on this node.
112
+ prop(prop) { return this.props[prop.id]; }
113
+ /// True when this is the top node of a grammar.
114
+ get isTop() { return (this.flags & 1 /* NodeFlag.Top */) > 0; }
115
+ /// True when this node is produced by a skip rule.
116
+ get isSkipped() { return (this.flags & 2 /* NodeFlag.Skipped */) > 0; }
117
+ /// Indicates whether this is an error node.
118
+ get isError() { return (this.flags & 4 /* NodeFlag.Error */) > 0; }
119
+ /// When true, this node type doesn't correspond to a user-declared
120
+ /// named node, for example because it is used to cache repetition.
121
+ get isAnonymous() { return (this.flags & 8 /* NodeFlag.Anonymous */) > 0; }
122
+ /// Returns true when this node's name or one of its
123
+ /// [groups](#common.NodeProp^group) matches the given string.
124
+ is(name) {
125
+ if (typeof name == 'string') {
126
+ if (this.name == name)
127
+ return true;
128
+ let group = this.prop(NodeProp.group);
129
+ return group ? group.indexOf(name) > -1 : false;
130
+ }
131
+ return this.id == name;
132
+ }
133
+ /// Create a function from node types to arbitrary values by
134
+ /// specifying an object whose property names are node or
135
+ /// [group](#common.NodeProp^group) names. Often useful with
136
+ /// [`NodeProp.add`](#common.NodeProp.add). You can put multiple
137
+ /// names, separated by spaces, in a single property name to map
138
+ /// multiple node names to a single value.
139
+ static match(map) {
140
+ let direct = Object.create(null);
141
+ for (let prop in map)
142
+ for (let name of prop.split(" "))
143
+ direct[name] = map[prop];
144
+ return (node) => {
145
+ for (let groups = node.prop(NodeProp.group), i = -1; i < (groups ? groups.length : 0); i++) {
146
+ let found = direct[i < 0 ? node.name : groups[i]];
147
+ if (found)
148
+ return found;
149
+ }
150
+ };
151
+ }
152
+ }
153
+ /// An empty dummy node type to use when no actual type is available.
154
+ NodeType.none = new NodeType("", Object.create(null), 0, 8 /* NodeFlag.Anonymous */);
155
+ /// A node set holds a collection of node types. It is used to
156
+ /// compactly represent trees by storing their type ids, rather than a
157
+ /// full pointer to the type object, in a numeric array. Each parser
158
+ /// [has](#lr.LRParser.nodeSet) a node set, and [tree
159
+ /// buffers](#common.TreeBuffer) can only store collections of nodes
160
+ /// from the same set. A set can have a maximum of 2**16 (65536) node
161
+ /// types in it, so that the ids fit into 16-bit typed array slots.
162
+ class NodeSet {
163
+ /// Create a set with the given types. The `id` property of each
164
+ /// type should correspond to its position within the array.
165
+ constructor(
166
+ /// The node types in this set, by id.
167
+ types) {
168
+ this.types = types;
169
+ for (let i = 0; i < types.length; i++)
170
+ if (types[i].id != i)
171
+ throw new RangeError("Node type ids should correspond to array positions when creating a node set");
172
+ }
173
+ /// Create a copy of this set with some node properties added. The
174
+ /// arguments to this method can be created with
175
+ /// [`NodeProp.add`](#common.NodeProp.add).
176
+ extend(...props) {
177
+ let newTypes = [];
178
+ for (let type of this.types) {
179
+ let newProps = null;
180
+ for (let source of props) {
181
+ let add = source(type);
182
+ if (add) {
183
+ if (!newProps)
184
+ newProps = Object.assign({}, type.props);
185
+ newProps[add[0].id] = add[1];
186
+ }
187
+ }
188
+ newTypes.push(newProps ? new NodeType(type.name, newProps, type.id, type.flags) : type);
189
+ }
190
+ return new NodeSet(newTypes);
191
+ }
192
+ }
193
+ const CachedNode = new WeakMap(), CachedInnerNode = new WeakMap();
194
+ /// Options that control iteration. Can be combined with the `|`
195
+ /// operator to enable multiple ones.
196
+ exports.IterMode = void 0;
197
+ (function (IterMode) {
198
+ /// When enabled, iteration will only visit [`Tree`](#common.Tree)
199
+ /// objects, not nodes packed into
200
+ /// [`TreeBuffer`](#common.TreeBuffer)s.
201
+ IterMode[IterMode["ExcludeBuffers"] = 1] = "ExcludeBuffers";
202
+ /// Enable this to make iteration include anonymous nodes (such as
203
+ /// the nodes that wrap repeated grammar constructs into a balanced
204
+ /// tree).
205
+ IterMode[IterMode["IncludeAnonymous"] = 2] = "IncludeAnonymous";
206
+ /// By default, regular [mounted](#common.NodeProp^mounted) nodes
207
+ /// replace their base node in iteration. Enable this to ignore them
208
+ /// instead.
209
+ IterMode[IterMode["IgnoreMounts"] = 4] = "IgnoreMounts";
210
+ /// This option only applies in
211
+ /// [`enter`](#common.SyntaxNode.enter)-style methods. It tells the
212
+ /// library to not enter mounted overlays if one covers the given
213
+ /// position.
214
+ IterMode[IterMode["IgnoreOverlays"] = 8] = "IgnoreOverlays";
215
+ })(exports.IterMode || (exports.IterMode = {}));
216
+ /// A piece of syntax tree. There are two ways to approach these
217
+ /// trees: the way they are actually stored in memory, and the
218
+ /// convenient way.
219
+ ///
220
+ /// Syntax trees are stored as a tree of `Tree` and `TreeBuffer`
221
+ /// objects. By packing detail information into `TreeBuffer` leaf
222
+ /// nodes, the representation is made a lot more memory-efficient.
223
+ ///
224
+ /// However, when you want to actually work with tree nodes, this
225
+ /// representation is very awkward, so most client code will want to
226
+ /// use the [`TreeCursor`](#common.TreeCursor) or
227
+ /// [`SyntaxNode`](#common.SyntaxNode) interface instead, which provides
228
+ /// a view on some part of this data structure, and can be used to
229
+ /// move around to adjacent nodes.
230
+ class Tree {
231
+ /// Construct a new tree. See also [`Tree.build`](#common.Tree^build).
232
+ constructor(
233
+ /// The type of the top node.
234
+ type,
235
+ /// This node's child nodes.
236
+ children,
237
+ /// The positions (offsets relative to the start of this tree) of
238
+ /// the children.
239
+ positions,
240
+ /// The total length of this tree
241
+ length,
242
+ /// Per-node [node props](#common.NodeProp) to associate with this node.
243
+ props) {
244
+ this.type = type;
245
+ this.children = children;
246
+ this.positions = positions;
247
+ this.length = length;
248
+ /// @internal
249
+ this.props = null;
250
+ if (props && props.length) {
251
+ this.props = Object.create(null);
252
+ for (let [prop, value] of props)
253
+ this.props[typeof prop == "number" ? prop : prop.id] = value;
254
+ }
255
+ }
256
+ /// @internal
257
+ toString() {
258
+ let mounted = this.prop(NodeProp.mounted);
259
+ if (mounted && !mounted.overlay)
260
+ return mounted.tree.toString();
261
+ let children = "";
262
+ for (let ch of this.children) {
263
+ let str = ch.toString();
264
+ if (str) {
265
+ if (children)
266
+ children += ",";
267
+ children += str;
268
+ }
269
+ }
270
+ return !this.type.name ? children :
271
+ (/\W/.test(this.type.name) && !this.type.isError ? JSON.stringify(this.type.name) : this.type.name) +
272
+ (children.length ? "(" + children + ")" : "");
273
+ }
274
+ /// Get a [tree cursor](#common.TreeCursor) positioned at the top of
275
+ /// the tree. Mode can be used to [control](#common.IterMode) which
276
+ /// nodes the cursor visits.
277
+ cursor(mode = 0) {
278
+ return new TreeCursor(this.topNode, mode);
279
+ }
280
+ /// Get a [tree cursor](#common.TreeCursor) pointing into this tree
281
+ /// at the given position and side (see
282
+ /// [`moveTo`](#common.TreeCursor.moveTo).
283
+ cursorAt(pos, side = 0, mode = 0) {
284
+ let scope = CachedNode.get(this) || this.topNode;
285
+ let cursor = new TreeCursor(scope);
286
+ cursor.moveTo(pos, side);
287
+ CachedNode.set(this, cursor._tree);
288
+ return cursor;
289
+ }
290
+ /// Get a [syntax node](#common.SyntaxNode) object for the top of the
291
+ /// tree.
292
+ get topNode() {
293
+ return new TreeNode(this, 0, 0, null);
294
+ }
295
+ /// Get the [syntax node](#common.SyntaxNode) at the given position.
296
+ /// If `side` is -1, this will move into nodes that end at the
297
+ /// position. If 1, it'll move into nodes that start at the
298
+ /// position. With 0, it'll only enter nodes that cover the position
299
+ /// from both sides.
300
+ ///
301
+ /// Note that this will not enter
302
+ /// [overlays](#common.MountedTree.overlay), and you often want
303
+ /// [`resolveInner`](#common.Tree.resolveInner) instead.
304
+ resolve(pos, side = 0) {
305
+ let node = resolveNode(CachedNode.get(this) || this.topNode, pos, side, false);
306
+ CachedNode.set(this, node);
307
+ return node;
308
+ }
309
+ /// Like [`resolve`](#common.Tree.resolve), but will enter
310
+ /// [overlaid](#common.MountedTree.overlay) nodes, producing a syntax node
311
+ /// pointing into the innermost overlaid tree at the given position
312
+ /// (with parent links going through all parent structure, including
313
+ /// the host trees).
314
+ resolveInner(pos, side = 0) {
315
+ let node = resolveNode(CachedInnerNode.get(this) || this.topNode, pos, side, true);
316
+ CachedInnerNode.set(this, node);
317
+ return node;
318
+ }
319
+ /// Iterate over the tree and its children, calling `enter` for any
320
+ /// node that touches the `from`/`to` region (if given) before
321
+ /// running over such a node's children, and `leave` (if given) when
322
+ /// leaving the node. When `enter` returns `false`, that node will
323
+ /// not have its children iterated over (or `leave` called).
324
+ iterate(spec) {
325
+ let { enter, leave, from = 0, to = this.length } = spec;
326
+ let mode = spec.mode || 0, anon = (mode & exports.IterMode.IncludeAnonymous) > 0;
327
+ for (let c = this.cursor(mode | exports.IterMode.IncludeAnonymous);;) {
328
+ let entered = false;
329
+ if (c.from <= to && c.to >= from && (!anon && c.type.isAnonymous || enter(c) !== false)) {
330
+ if (c.firstChild())
331
+ continue;
332
+ entered = true;
333
+ }
334
+ for (;;) {
335
+ if (entered && leave && (anon || !c.type.isAnonymous))
336
+ leave(c);
337
+ if (c.nextSibling())
338
+ break;
339
+ if (!c.parent())
340
+ return;
341
+ entered = true;
342
+ }
343
+ }
344
+ }
345
+ /// Get the value of the given [node prop](#common.NodeProp) for this
346
+ /// node. Works with both per-node and per-type props.
347
+ prop(prop) {
348
+ return !prop.perNode ? this.type.prop(prop) : this.props ? this.props[prop.id] : undefined;
349
+ }
350
+ /// Returns the node's [per-node props](#common.NodeProp.perNode) in a
351
+ /// format that can be passed to the [`Tree`](#common.Tree)
352
+ /// constructor.
353
+ get propValues() {
354
+ let result = [];
355
+ if (this.props)
356
+ for (let id in this.props)
357
+ result.push([+id, this.props[id]]);
358
+ return result;
359
+ }
360
+ /// Balance the direct children of this tree, producing a copy of
361
+ /// which may have children grouped into subtrees with type
362
+ /// [`NodeType.none`](#common.NodeType^none).
363
+ balance(config = {}) {
364
+ return this.children.length <= 8 /* Balance.BranchFactor */ ? this :
365
+ 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)));
366
+ }
367
+ /// Build a tree from a postfix-ordered buffer of node information,
368
+ /// or a cursor over such a buffer.
369
+ static build(data) { return buildTree(data); }
370
+ }
371
+ /// The empty tree
372
+ Tree.empty = new Tree(NodeType.none, [], [], 0);
373
+ class FlatBufferCursor {
374
+ constructor(buffer, index) {
375
+ this.buffer = buffer;
376
+ this.index = index;
377
+ }
378
+ get id() { return this.buffer[this.index - 4]; }
379
+ get start() { return this.buffer[this.index - 3]; }
380
+ get end() { return this.buffer[this.index - 2]; }
381
+ get size() { return this.buffer[this.index - 1]; }
382
+ get pos() { return this.index; }
383
+ next() { this.index -= 4; }
384
+ fork() { return new FlatBufferCursor(this.buffer, this.index); }
385
+ }
386
+ /// Tree buffers contain (type, start, end, endIndex) quads for each
387
+ /// node. In such a buffer, nodes are stored in prefix order (parents
388
+ /// before children, with the endIndex of the parent indicating which
389
+ /// children belong to it).
390
+ class TreeBuffer {
391
+ /// Create a tree buffer.
392
+ constructor(
393
+ /// The buffer's content.
394
+ buffer,
395
+ /// The total length of the group of nodes in the buffer.
396
+ length,
397
+ /// The node set used in this buffer.
398
+ set) {
399
+ this.buffer = buffer;
400
+ this.length = length;
401
+ this.set = set;
402
+ }
403
+ /// @internal
404
+ get type() { return NodeType.none; }
405
+ /// @internal
406
+ toString() {
407
+ let result = [];
408
+ for (let index = 0; index < this.buffer.length;) {
409
+ result.push(this.childString(index));
410
+ index = this.buffer[index + 3];
411
+ }
412
+ return result.join(",");
413
+ }
414
+ /// @internal
415
+ childString(index) {
416
+ let id = this.buffer[index], endIndex = this.buffer[index + 3];
417
+ let type = this.set.types[id], result = type.name;
418
+ if (/\W/.test(result) && !type.isError)
419
+ result = JSON.stringify(result);
420
+ index += 4;
421
+ if (endIndex == index)
422
+ return result;
423
+ let children = [];
424
+ while (index < endIndex) {
425
+ children.push(this.childString(index));
426
+ index = this.buffer[index + 3];
427
+ }
428
+ return result + "(" + children.join(",") + ")";
429
+ }
430
+ /// @internal
431
+ findChild(startIndex, endIndex, dir, pos, side) {
432
+ let { buffer } = this, pick = -1;
433
+ for (let i = startIndex; i != endIndex; i = buffer[i + 3]) {
434
+ if (checkSide(side, pos, buffer[i + 1], buffer[i + 2])) {
435
+ pick = i;
436
+ if (dir > 0)
437
+ break;
438
+ }
439
+ }
440
+ return pick;
441
+ }
442
+ /// @internal
443
+ slice(startI, endI, from) {
444
+ let b = this.buffer;
445
+ let copy = new Uint16Array(endI - startI), len = 0;
446
+ for (let i = startI, j = 0; i < endI;) {
447
+ copy[j++] = b[i++];
448
+ copy[j++] = b[i++] - from;
449
+ let to = copy[j++] = b[i++] - from;
450
+ copy[j++] = b[i++] - startI;
451
+ len = Math.max(len, to);
452
+ }
453
+ return new TreeBuffer(copy, len, this.set);
454
+ }
455
+ }
456
+ function checkSide(side, pos, from, to) {
457
+ switch (side) {
458
+ case -2 /* Side.Before */: return from < pos;
459
+ case -1 /* Side.AtOrBefore */: return to >= pos && from < pos;
460
+ case 0 /* Side.Around */: return from < pos && to > pos;
461
+ case 1 /* Side.AtOrAfter */: return from <= pos && to > pos;
462
+ case 2 /* Side.After */: return to > pos;
463
+ case 4 /* Side.DontCare */: return true;
464
+ }
465
+ }
466
+ function enterUnfinishedNodesBefore(node, pos) {
467
+ let scan = node.childBefore(pos);
468
+ while (scan) {
469
+ let last = scan.lastChild;
470
+ if (!last || last.to != scan.to)
471
+ break;
472
+ if (last.type.isError && last.from == last.to) {
473
+ node = scan;
474
+ scan = last.prevSibling;
475
+ }
476
+ else {
477
+ scan = last;
478
+ }
479
+ }
480
+ return node;
481
+ }
482
+ function resolveNode(node, pos, side, overlays) {
483
+ var _a;
484
+ // Move up to a node that actually holds the position, if possible
485
+ while (node.from == node.to ||
486
+ (side < 1 ? node.from >= pos : node.from > pos) ||
487
+ (side > -1 ? node.to <= pos : node.to < pos)) {
488
+ let parent = !overlays && node instanceof TreeNode && node.index < 0 ? null : node.parent;
489
+ if (!parent)
490
+ return node;
491
+ node = parent;
492
+ }
493
+ let mode = overlays ? 0 : exports.IterMode.IgnoreOverlays;
494
+ // Must go up out of overlays when those do not overlap with pos
495
+ if (overlays)
496
+ for (let scan = node, parent = scan.parent; parent; scan = parent, parent = scan.parent) {
497
+ if (scan instanceof TreeNode && scan.index < 0 && ((_a = parent.enter(pos, side, mode)) === null || _a === void 0 ? void 0 : _a.from) != scan.from)
498
+ node = parent;
499
+ }
500
+ for (;;) {
501
+ let inner = node.enter(pos, side, mode);
502
+ if (!inner)
503
+ return node;
504
+ node = inner;
505
+ }
506
+ }
507
+ class TreeNode {
508
+ constructor(_tree, from,
509
+ // Index in parent node, set to -1 if the node is not a direct child of _parent.node (overlay)
510
+ index, _parent) {
511
+ this._tree = _tree;
512
+ this.from = from;
513
+ this.index = index;
514
+ this._parent = _parent;
515
+ }
516
+ get type() { return this._tree.type; }
517
+ get name() { return this._tree.type.name; }
518
+ get to() { return this.from + this._tree.length; }
519
+ nextChild(i, dir, pos, side, mode = 0) {
520
+ for (let parent = this;;) {
521
+ for (let { children, positions } = parent._tree, e = dir > 0 ? children.length : -1; i != e; i += dir) {
522
+ let next = children[i], start = positions[i] + parent.from;
523
+ if (!checkSide(side, pos, start, start + next.length))
524
+ continue;
525
+ if (next instanceof TreeBuffer) {
526
+ if (mode & exports.IterMode.ExcludeBuffers)
527
+ continue;
528
+ let index = next.findChild(0, next.buffer.length, dir, pos - start, side);
529
+ if (index > -1)
530
+ return new BufferNode(new BufferContext(parent, next, i, start), null, index);
531
+ }
532
+ else if ((mode & exports.IterMode.IncludeAnonymous) || (!next.type.isAnonymous || hasChild(next))) {
533
+ let mounted;
534
+ if (!(mode & exports.IterMode.IgnoreMounts) &&
535
+ next.props && (mounted = next.prop(NodeProp.mounted)) && !mounted.overlay)
536
+ return new TreeNode(mounted.tree, start, i, parent);
537
+ let inner = new TreeNode(next, start, i, parent);
538
+ return (mode & exports.IterMode.IncludeAnonymous) || !inner.type.isAnonymous ? inner
539
+ : inner.nextChild(dir < 0 ? next.children.length - 1 : 0, dir, pos, side);
540
+ }
541
+ }
542
+ if ((mode & exports.IterMode.IncludeAnonymous) || !parent.type.isAnonymous)
543
+ return null;
544
+ if (parent.index >= 0)
545
+ i = parent.index + dir;
546
+ else
547
+ i = dir < 0 ? -1 : parent._parent._tree.children.length;
548
+ parent = parent._parent;
549
+ if (!parent)
550
+ return null;
551
+ }
552
+ }
553
+ get firstChild() { return this.nextChild(0, 1, 0, 4 /* Side.DontCare */); }
554
+ get lastChild() { return this.nextChild(this._tree.children.length - 1, -1, 0, 4 /* Side.DontCare */); }
555
+ childAfter(pos) { return this.nextChild(0, 1, pos, 2 /* Side.After */); }
556
+ childBefore(pos) { return this.nextChild(this._tree.children.length - 1, -1, pos, -2 /* Side.Before */); }
557
+ enter(pos, side, mode = 0) {
558
+ let mounted;
559
+ if (!(mode & exports.IterMode.IgnoreOverlays) && (mounted = this._tree.prop(NodeProp.mounted)) && mounted.overlay) {
560
+ let rPos = pos - this.from;
561
+ for (let { from, to } of mounted.overlay) {
562
+ if ((side > 0 ? from <= rPos : from < rPos) &&
563
+ (side < 0 ? to >= rPos : to > rPos))
564
+ return new TreeNode(mounted.tree, mounted.overlay[0].from + this.from, -1, this);
565
+ }
566
+ }
567
+ return this.nextChild(0, 1, pos, side, mode);
568
+ }
569
+ nextSignificantParent() {
570
+ let val = this;
571
+ while (val.type.isAnonymous && val._parent)
572
+ val = val._parent;
573
+ return val;
574
+ }
575
+ get parent() {
576
+ return this._parent ? this._parent.nextSignificantParent() : null;
577
+ }
578
+ get nextSibling() {
579
+ return this._parent && this.index >= 0 ? this._parent.nextChild(this.index + 1, 1, 0, 4 /* Side.DontCare */) : null;
580
+ }
581
+ get prevSibling() {
582
+ return this._parent && this.index >= 0 ? this._parent.nextChild(this.index - 1, -1, 0, 4 /* Side.DontCare */) : null;
583
+ }
584
+ cursor(mode = 0) { return new TreeCursor(this, mode); }
585
+ get tree() { return this._tree; }
586
+ toTree() { return this._tree; }
587
+ resolve(pos, side = 0) {
588
+ return resolveNode(this, pos, side, false);
589
+ }
590
+ resolveInner(pos, side = 0) {
591
+ return resolveNode(this, pos, side, true);
592
+ }
593
+ enterUnfinishedNodesBefore(pos) { return enterUnfinishedNodesBefore(this, pos); }
594
+ getChild(type, before = null, after = null) {
595
+ let r = getChildren(this, type, before, after);
596
+ return r.length ? r[0] : null;
597
+ }
598
+ getChildren(type, before = null, after = null) {
599
+ return getChildren(this, type, before, after);
600
+ }
601
+ /// @internal
602
+ toString() { return this._tree.toString(); }
603
+ get node() { return this; }
604
+ matchContext(context) { return matchNodeContext(this, context); }
605
+ }
606
+ function getChildren(node, type, before, after) {
607
+ let cur = node.cursor(), result = [];
608
+ if (!cur.firstChild())
609
+ return result;
610
+ if (before != null)
611
+ while (!cur.type.is(before))
612
+ if (!cur.nextSibling())
613
+ return result;
614
+ for (;;) {
615
+ if (after != null && cur.type.is(after))
616
+ return result;
617
+ if (cur.type.is(type))
618
+ result.push(cur.node);
619
+ if (!cur.nextSibling())
620
+ return after == null ? result : [];
621
+ }
622
+ }
623
+ function matchNodeContext(node, context, i = context.length - 1) {
624
+ for (let p = node.parent; i >= 0; p = p.parent) {
625
+ if (!p)
626
+ return false;
627
+ if (!p.type.isAnonymous) {
628
+ if (context[i] && context[i] != p.name)
629
+ return false;
630
+ i--;
631
+ }
632
+ }
633
+ return true;
634
+ }
635
+ class BufferContext {
636
+ constructor(parent, buffer, index, start) {
637
+ this.parent = parent;
638
+ this.buffer = buffer;
639
+ this.index = index;
640
+ this.start = start;
641
+ }
642
+ }
643
+ class BufferNode {
644
+ get name() { return this.type.name; }
645
+ get from() { return this.context.start + this.context.buffer.buffer[this.index + 1]; }
646
+ get to() { return this.context.start + this.context.buffer.buffer[this.index + 2]; }
647
+ constructor(context, _parent, index) {
648
+ this.context = context;
649
+ this._parent = _parent;
650
+ this.index = index;
651
+ this.type = context.buffer.set.types[context.buffer.buffer[index]];
652
+ }
653
+ child(dir, pos, side) {
654
+ let { buffer } = this.context;
655
+ let index = buffer.findChild(this.index + 4, buffer.buffer[this.index + 3], dir, pos - this.context.start, side);
656
+ return index < 0 ? null : new BufferNode(this.context, this, index);
657
+ }
658
+ get firstChild() { return this.child(1, 0, 4 /* Side.DontCare */); }
659
+ get lastChild() { return this.child(-1, 0, 4 /* Side.DontCare */); }
660
+ childAfter(pos) { return this.child(1, pos, 2 /* Side.After */); }
661
+ childBefore(pos) { return this.child(-1, pos, -2 /* Side.Before */); }
662
+ enter(pos, side, mode = 0) {
663
+ if (mode & exports.IterMode.ExcludeBuffers)
664
+ return null;
665
+ let { buffer } = this.context;
666
+ let index = buffer.findChild(this.index + 4, buffer.buffer[this.index + 3], side > 0 ? 1 : -1, pos - this.context.start, side);
667
+ return index < 0 ? null : new BufferNode(this.context, this, index);
668
+ }
669
+ get parent() {
670
+ return this._parent || this.context.parent.nextSignificantParent();
671
+ }
672
+ externalSibling(dir) {
673
+ return this._parent ? null : this.context.parent.nextChild(this.context.index + dir, dir, 0, 4 /* Side.DontCare */);
674
+ }
675
+ get nextSibling() {
676
+ let { buffer } = this.context;
677
+ let after = buffer.buffer[this.index + 3];
678
+ if (after < (this._parent ? buffer.buffer[this._parent.index + 3] : buffer.buffer.length))
679
+ return new BufferNode(this.context, this._parent, after);
680
+ return this.externalSibling(1);
681
+ }
682
+ get prevSibling() {
683
+ let { buffer } = this.context;
684
+ let parentStart = this._parent ? this._parent.index + 4 : 0;
685
+ if (this.index == parentStart)
686
+ return this.externalSibling(-1);
687
+ return new BufferNode(this.context, this._parent, buffer.findChild(parentStart, this.index, -1, 0, 4 /* Side.DontCare */));
688
+ }
689
+ cursor(mode = 0) { return new TreeCursor(this, mode); }
690
+ get tree() { return null; }
691
+ toTree() {
692
+ let children = [], positions = [];
693
+ let { buffer } = this.context;
694
+ let startI = this.index + 4, endI = buffer.buffer[this.index + 3];
695
+ if (endI > startI) {
696
+ let from = buffer.buffer[this.index + 1];
697
+ children.push(buffer.slice(startI, endI, from));
698
+ positions.push(0);
699
+ }
700
+ return new Tree(this.type, children, positions, this.to - this.from);
701
+ }
702
+ resolve(pos, side = 0) {
703
+ return resolveNode(this, pos, side, false);
704
+ }
705
+ resolveInner(pos, side = 0) {
706
+ return resolveNode(this, pos, side, true);
707
+ }
708
+ enterUnfinishedNodesBefore(pos) { return enterUnfinishedNodesBefore(this, pos); }
709
+ /// @internal
710
+ toString() { return this.context.buffer.childString(this.index); }
711
+ getChild(type, before = null, after = null) {
712
+ let r = getChildren(this, type, before, after);
713
+ return r.length ? r[0] : null;
714
+ }
715
+ getChildren(type, before = null, after = null) {
716
+ return getChildren(this, type, before, after);
717
+ }
718
+ get node() { return this; }
719
+ matchContext(context) { return matchNodeContext(this, context); }
720
+ }
721
+ /// A tree cursor object focuses on a given node in a syntax tree, and
722
+ /// allows you to move to adjacent nodes.
723
+ class TreeCursor {
724
+ /// Shorthand for `.type.name`.
725
+ get name() { return this.type.name; }
726
+ /// @internal
727
+ constructor(node,
728
+ /// @internal
729
+ mode = 0) {
730
+ this.mode = mode;
731
+ /// @internal
732
+ this.buffer = null;
733
+ this.stack = [];
734
+ /// @internal
735
+ this.index = 0;
736
+ this.bufferNode = null;
737
+ if (node instanceof TreeNode) {
738
+ this.yieldNode(node);
739
+ }
740
+ else {
741
+ this._tree = node.context.parent;
742
+ this.buffer = node.context;
743
+ for (let n = node._parent; n; n = n._parent)
744
+ this.stack.unshift(n.index);
745
+ this.bufferNode = node;
746
+ this.yieldBuf(node.index);
747
+ }
748
+ }
749
+ yieldNode(node) {
750
+ if (!node)
751
+ return false;
752
+ this._tree = node;
753
+ this.type = node.type;
754
+ this.from = node.from;
755
+ this.to = node.to;
756
+ return true;
757
+ }
758
+ yieldBuf(index, type) {
759
+ this.index = index;
760
+ let { start, buffer } = this.buffer;
761
+ this.type = type || buffer.set.types[buffer.buffer[index]];
762
+ this.from = start + buffer.buffer[index + 1];
763
+ this.to = start + buffer.buffer[index + 2];
764
+ return true;
765
+ }
766
+ yield(node) {
767
+ if (!node)
768
+ return false;
769
+ if (node instanceof TreeNode) {
770
+ this.buffer = null;
771
+ return this.yieldNode(node);
772
+ }
773
+ this.buffer = node.context;
774
+ return this.yieldBuf(node.index, node.type);
775
+ }
776
+ /// @internal
777
+ toString() {
778
+ return this.buffer ? this.buffer.buffer.childString(this.index) : this._tree.toString();
779
+ }
780
+ /// @internal
781
+ enterChild(dir, pos, side) {
782
+ if (!this.buffer)
783
+ return this.yield(this._tree.nextChild(dir < 0 ? this._tree._tree.children.length - 1 : 0, dir, pos, side, this.mode));
784
+ let { buffer } = this.buffer;
785
+ let index = buffer.findChild(this.index + 4, buffer.buffer[this.index + 3], dir, pos - this.buffer.start, side);
786
+ if (index < 0)
787
+ return false;
788
+ this.stack.push(this.index);
789
+ return this.yieldBuf(index);
790
+ }
791
+ /// Move the cursor to this node's first child. When this returns
792
+ /// false, the node has no child, and the cursor has not been moved.
793
+ firstChild() { return this.enterChild(1, 0, 4 /* Side.DontCare */); }
794
+ /// Move the cursor to this node's last child.
795
+ lastChild() { return this.enterChild(-1, 0, 4 /* Side.DontCare */); }
796
+ /// Move the cursor to the first child that ends after `pos`.
797
+ childAfter(pos) { return this.enterChild(1, pos, 2 /* Side.After */); }
798
+ /// Move to the last child that starts before `pos`.
799
+ childBefore(pos) { return this.enterChild(-1, pos, -2 /* Side.Before */); }
800
+ /// Move the cursor to the child around `pos`. If side is -1 the
801
+ /// child may end at that position, when 1 it may start there. This
802
+ /// will also enter [overlaid](#common.MountedTree.overlay)
803
+ /// [mounted](#common.NodeProp^mounted) trees unless `overlays` is
804
+ /// set to false.
805
+ enter(pos, side, mode = this.mode) {
806
+ if (!this.buffer)
807
+ return this.yield(this._tree.enter(pos, side, mode));
808
+ return mode & exports.IterMode.ExcludeBuffers ? false : this.enterChild(1, pos, side);
809
+ }
810
+ /// Move to the node's parent node, if this isn't the top node.
811
+ parent() {
812
+ if (!this.buffer)
813
+ return this.yieldNode((this.mode & exports.IterMode.IncludeAnonymous) ? this._tree._parent : this._tree.parent);
814
+ if (this.stack.length)
815
+ return this.yieldBuf(this.stack.pop());
816
+ let parent = (this.mode & exports.IterMode.IncludeAnonymous) ? this.buffer.parent : this.buffer.parent.nextSignificantParent();
817
+ this.buffer = null;
818
+ return this.yieldNode(parent);
819
+ }
820
+ /// @internal
821
+ sibling(dir) {
822
+ if (!this.buffer)
823
+ return !this._tree._parent ? false
824
+ : this.yield(this._tree.index < 0 ? null
825
+ : this._tree._parent.nextChild(this._tree.index + dir, dir, 0, 4 /* Side.DontCare */, this.mode));
826
+ let { buffer } = this.buffer, d = this.stack.length - 1;
827
+ if (dir < 0) {
828
+ let parentStart = d < 0 ? 0 : this.stack[d] + 4;
829
+ if (this.index != parentStart)
830
+ return this.yieldBuf(buffer.findChild(parentStart, this.index, -1, 0, 4 /* Side.DontCare */));
831
+ }
832
+ else {
833
+ let after = buffer.buffer[this.index + 3];
834
+ if (after < (d < 0 ? buffer.buffer.length : buffer.buffer[this.stack[d] + 3]))
835
+ return this.yieldBuf(after);
836
+ }
837
+ return d < 0 ? this.yield(this.buffer.parent.nextChild(this.buffer.index + dir, dir, 0, 4 /* Side.DontCare */, this.mode)) : false;
838
+ }
839
+ /// Move to this node's next sibling, if any.
840
+ nextSibling() { return this.sibling(1); }
841
+ /// Move to this node's previous sibling, if any.
842
+ prevSibling() { return this.sibling(-1); }
843
+ atLastNode(dir) {
844
+ let index, parent, { buffer } = this;
845
+ if (buffer) {
846
+ if (dir > 0) {
847
+ if (this.index < buffer.buffer.buffer.length)
848
+ return false;
849
+ }
850
+ else {
851
+ for (let i = 0; i < this.index; i++)
852
+ if (buffer.buffer.buffer[i + 3] < this.index)
853
+ return false;
854
+ }
855
+ ({ index, parent } = buffer);
856
+ }
857
+ else {
858
+ ({ index, _parent: parent } = this._tree);
859
+ }
860
+ for (; parent; { index, _parent: parent } = parent) {
861
+ if (index > -1)
862
+ for (let i = index + dir, e = dir < 0 ? -1 : parent._tree.children.length; i != e; i += dir) {
863
+ let child = parent._tree.children[i];
864
+ if ((this.mode & exports.IterMode.IncludeAnonymous) ||
865
+ child instanceof TreeBuffer ||
866
+ !child.type.isAnonymous ||
867
+ hasChild(child))
868
+ return false;
869
+ }
870
+ }
871
+ return true;
872
+ }
873
+ move(dir, enter) {
874
+ if (enter && this.enterChild(dir, 0, 4 /* Side.DontCare */))
875
+ return true;
876
+ for (;;) {
877
+ if (this.sibling(dir))
878
+ return true;
879
+ if (this.atLastNode(dir) || !this.parent())
880
+ return false;
881
+ }
882
+ }
883
+ /// Move to the next node in a
884
+ /// [pre-order](https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR)
885
+ /// traversal, going from a node to its first child or, if the
886
+ /// current node is empty or `enter` is false, its next sibling or
887
+ /// the next sibling of the first parent node that has one.
888
+ next(enter = true) { return this.move(1, enter); }
889
+ /// Move to the next node in a last-to-first pre-order traveral. A
890
+ /// node is followed by its last child or, if it has none, its
891
+ /// previous sibling or the previous sibling of the first parent
892
+ /// node that has one.
893
+ prev(enter = true) { return this.move(-1, enter); }
894
+ /// Move the cursor to the innermost node that covers `pos`. If
895
+ /// `side` is -1, it will enter nodes that end at `pos`. If it is 1,
896
+ /// it will enter nodes that start at `pos`.
897
+ moveTo(pos, side = 0) {
898
+ // Move up to a node that actually holds the position, if possible
899
+ while (this.from == this.to ||
900
+ (side < 1 ? this.from >= pos : this.from > pos) ||
901
+ (side > -1 ? this.to <= pos : this.to < pos))
902
+ if (!this.parent())
903
+ break;
904
+ // Then scan down into child nodes as far as possible
905
+ while (this.enterChild(1, pos, side)) { }
906
+ return this;
907
+ }
908
+ /// Get a [syntax node](#common.SyntaxNode) at the cursor's current
909
+ /// position.
910
+ get node() {
911
+ if (!this.buffer)
912
+ return this._tree;
913
+ let cache = this.bufferNode, result = null, depth = 0;
914
+ if (cache && cache.context == this.buffer) {
915
+ scan: for (let index = this.index, d = this.stack.length; d >= 0;) {
916
+ for (let c = cache; c; c = c._parent)
917
+ if (c.index == index) {
918
+ if (index == this.index)
919
+ return c;
920
+ result = c;
921
+ depth = d + 1;
922
+ break scan;
923
+ }
924
+ index = this.stack[--d];
925
+ }
926
+ }
927
+ for (let i = depth; i < this.stack.length; i++)
928
+ result = new BufferNode(this.buffer, result, this.stack[i]);
929
+ return this.bufferNode = new BufferNode(this.buffer, result, this.index);
930
+ }
931
+ /// Get the [tree](#common.Tree) that represents the current node, if
932
+ /// any. Will return null when the node is in a [tree
933
+ /// buffer](#common.TreeBuffer).
934
+ get tree() {
935
+ return this.buffer ? null : this._tree._tree;
936
+ }
937
+ /// Iterate over the current node and all its descendants, calling
938
+ /// `enter` when entering a node and `leave`, if given, when leaving
939
+ /// one. When `enter` returns `false`, any children of that node are
940
+ /// skipped, and `leave` isn't called for it.
941
+ iterate(enter, leave) {
942
+ for (let depth = 0;;) {
943
+ let mustLeave = false;
944
+ if (this.type.isAnonymous || enter(this) !== false) {
945
+ if (this.firstChild()) {
946
+ depth++;
947
+ continue;
948
+ }
949
+ if (!this.type.isAnonymous)
950
+ mustLeave = true;
951
+ }
952
+ for (;;) {
953
+ if (mustLeave && leave)
954
+ leave(this);
955
+ mustLeave = this.type.isAnonymous;
956
+ if (this.nextSibling())
957
+ break;
958
+ if (!depth)
959
+ return;
960
+ this.parent();
961
+ depth--;
962
+ mustLeave = true;
963
+ }
964
+ }
965
+ }
966
+ /// Test whether the current node matches a given context—a sequence
967
+ /// of direct parent node names. Empty strings in the context array
968
+ /// are treated as wildcards.
969
+ matchContext(context) {
970
+ if (!this.buffer)
971
+ return matchNodeContext(this.node, context);
972
+ let { buffer } = this.buffer, { types } = buffer.set;
973
+ for (let i = context.length - 1, d = this.stack.length - 1; i >= 0; d--) {
974
+ if (d < 0)
975
+ return matchNodeContext(this.node, context, i);
976
+ let type = types[buffer.buffer[this.stack[d]]];
977
+ if (!type.isAnonymous) {
978
+ if (context[i] && context[i] != type.name)
979
+ return false;
980
+ i--;
981
+ }
982
+ }
983
+ return true;
984
+ }
985
+ }
986
+ function hasChild(tree) {
987
+ return tree.children.some(ch => ch instanceof TreeBuffer || !ch.type.isAnonymous || hasChild(ch));
988
+ }
989
+ function buildTree(data) {
990
+ var _a;
991
+ let { buffer, nodeSet, maxBufferLength = DefaultBufferLength, reused = [], minRepeatType = nodeSet.types.length } = data;
992
+ let cursor = Array.isArray(buffer) ? new FlatBufferCursor(buffer, buffer.length) : buffer;
993
+ let types = nodeSet.types;
994
+ let contextHash = 0, lookAhead = 0;
995
+ function takeNode(parentStart, minPos, children, positions, inRepeat) {
996
+ let { id, start, end, size } = cursor;
997
+ let lookAheadAtStart = lookAhead;
998
+ while (size < 0) {
999
+ cursor.next();
1000
+ if (size == -1 /* SpecialRecord.Reuse */) {
1001
+ let node = reused[id];
1002
+ children.push(node);
1003
+ positions.push(start - parentStart);
1004
+ return;
1005
+ }
1006
+ else if (size == -3 /* SpecialRecord.ContextChange */) { // Context change
1007
+ contextHash = id;
1008
+ return;
1009
+ }
1010
+ else if (size == -4 /* SpecialRecord.LookAhead */) {
1011
+ lookAhead = id;
1012
+ return;
1013
+ }
1014
+ else {
1015
+ throw new RangeError(`Unrecognized record size: ${size}`);
1016
+ }
1017
+ }
1018
+ let type = types[id], node, buffer;
1019
+ let startPos = start - parentStart;
1020
+ if (end - start <= maxBufferLength && (buffer = findBufferSize(cursor.pos - minPos, inRepeat))) {
1021
+ // Small enough for a buffer, and no reused nodes inside
1022
+ let data = new Uint16Array(buffer.size - buffer.skip);
1023
+ let endPos = cursor.pos - buffer.size, index = data.length;
1024
+ while (cursor.pos > endPos)
1025
+ index = copyToBuffer(buffer.start, data, index);
1026
+ node = new TreeBuffer(data, end - buffer.start, nodeSet);
1027
+ startPos = buffer.start - parentStart;
1028
+ }
1029
+ else { // Make it a node
1030
+ let endPos = cursor.pos - size;
1031
+ cursor.next();
1032
+ let localChildren = [], localPositions = [];
1033
+ let localInRepeat = id >= minRepeatType ? id : -1;
1034
+ let lastGroup = 0, lastEnd = end;
1035
+ while (cursor.pos > endPos) {
1036
+ if (localInRepeat >= 0 && cursor.id == localInRepeat && cursor.size >= 0) {
1037
+ if (cursor.end <= lastEnd - maxBufferLength) {
1038
+ makeRepeatLeaf(localChildren, localPositions, start, lastGroup, cursor.end, lastEnd, localInRepeat, lookAheadAtStart);
1039
+ lastGroup = localChildren.length;
1040
+ lastEnd = cursor.end;
1041
+ }
1042
+ cursor.next();
1043
+ }
1044
+ else {
1045
+ takeNode(start, endPos, localChildren, localPositions, localInRepeat);
1046
+ }
1047
+ }
1048
+ if (localInRepeat >= 0 && lastGroup > 0 && lastGroup < localChildren.length)
1049
+ makeRepeatLeaf(localChildren, localPositions, start, lastGroup, start, lastEnd, localInRepeat, lookAheadAtStart);
1050
+ localChildren.reverse();
1051
+ localPositions.reverse();
1052
+ if (localInRepeat > -1 && lastGroup > 0) {
1053
+ let make = makeBalanced(type);
1054
+ node = balanceRange(type, localChildren, localPositions, 0, localChildren.length, 0, end - start, make, make);
1055
+ }
1056
+ else {
1057
+ node = makeTree(type, localChildren, localPositions, end - start, lookAheadAtStart - end);
1058
+ }
1059
+ }
1060
+ children.push(node);
1061
+ positions.push(startPos);
1062
+ }
1063
+ function makeBalanced(type) {
1064
+ return (children, positions, length) => {
1065
+ let lookAhead = 0, lastI = children.length - 1, last, lookAheadProp;
1066
+ if (lastI >= 0 && (last = children[lastI]) instanceof Tree) {
1067
+ if (!lastI && last.type == type && last.length == length)
1068
+ return last;
1069
+ if (lookAheadProp = last.prop(NodeProp.lookAhead))
1070
+ lookAhead = positions[lastI] + last.length + lookAheadProp;
1071
+ }
1072
+ return makeTree(type, children, positions, length, lookAhead);
1073
+ };
1074
+ }
1075
+ function makeRepeatLeaf(children, positions, base, i, from, to, type, lookAhead) {
1076
+ let localChildren = [], localPositions = [];
1077
+ while (children.length > i) {
1078
+ localChildren.push(children.pop());
1079
+ localPositions.push(positions.pop() + base - from);
1080
+ }
1081
+ children.push(makeTree(nodeSet.types[type], localChildren, localPositions, to - from, lookAhead - to));
1082
+ positions.push(from - base);
1083
+ }
1084
+ function makeTree(type, children, positions, length, lookAhead = 0, props) {
1085
+ if (contextHash) {
1086
+ let pair = [NodeProp.contextHash, contextHash];
1087
+ props = props ? [pair].concat(props) : [pair];
1088
+ }
1089
+ if (lookAhead > 25) {
1090
+ let pair = [NodeProp.lookAhead, lookAhead];
1091
+ props = props ? [pair].concat(props) : [pair];
1092
+ }
1093
+ return new Tree(type, children, positions, length, props);
1094
+ }
1095
+ function findBufferSize(maxSize, inRepeat) {
1096
+ // Scan through the buffer to find previous siblings that fit
1097
+ // together in a TreeBuffer, and don't contain any reused nodes
1098
+ // (which can't be stored in a buffer).
1099
+ // If `inRepeat` is > -1, ignore node boundaries of that type for
1100
+ // nesting, but make sure the end falls either at the start
1101
+ // (`maxSize`) or before such a node.
1102
+ let fork = cursor.fork();
1103
+ let size = 0, start = 0, skip = 0, minStart = fork.end - maxBufferLength;
1104
+ let result = { size: 0, start: 0, skip: 0 };
1105
+ scan: for (let minPos = fork.pos - maxSize; fork.pos > minPos;) {
1106
+ let nodeSize = fork.size;
1107
+ // Pretend nested repeat nodes of the same type don't exist
1108
+ if (fork.id == inRepeat && nodeSize >= 0) {
1109
+ // Except that we store the current state as a valid return
1110
+ // value.
1111
+ result.size = size;
1112
+ result.start = start;
1113
+ result.skip = skip;
1114
+ skip += 4;
1115
+ size += 4;
1116
+ fork.next();
1117
+ continue;
1118
+ }
1119
+ let startPos = fork.pos - nodeSize;
1120
+ if (nodeSize < 0 || startPos < minPos || fork.start < minStart)
1121
+ break;
1122
+ let localSkipped = fork.id >= minRepeatType ? 4 : 0;
1123
+ let nodeStart = fork.start;
1124
+ fork.next();
1125
+ while (fork.pos > startPos) {
1126
+ if (fork.size < 0) {
1127
+ if (fork.size == -3 /* SpecialRecord.ContextChange */)
1128
+ localSkipped += 4;
1129
+ else
1130
+ break scan;
1131
+ }
1132
+ else if (fork.id >= minRepeatType) {
1133
+ localSkipped += 4;
1134
+ }
1135
+ fork.next();
1136
+ }
1137
+ start = nodeStart;
1138
+ size += nodeSize;
1139
+ skip += localSkipped;
1140
+ }
1141
+ if (inRepeat < 0 || size == maxSize) {
1142
+ result.size = size;
1143
+ result.start = start;
1144
+ result.skip = skip;
1145
+ }
1146
+ return result.size > 4 ? result : undefined;
1147
+ }
1148
+ function copyToBuffer(bufferStart, buffer, index) {
1149
+ let { id, start, end, size } = cursor;
1150
+ cursor.next();
1151
+ if (size >= 0 && id < minRepeatType) {
1152
+ let startIndex = index;
1153
+ if (size > 4) {
1154
+ let endPos = cursor.pos - (size - 4);
1155
+ while (cursor.pos > endPos)
1156
+ index = copyToBuffer(bufferStart, buffer, index);
1157
+ }
1158
+ buffer[--index] = startIndex;
1159
+ buffer[--index] = end - bufferStart;
1160
+ buffer[--index] = start - bufferStart;
1161
+ buffer[--index] = id;
1162
+ }
1163
+ else if (size == -3 /* SpecialRecord.ContextChange */) {
1164
+ contextHash = id;
1165
+ }
1166
+ else if (size == -4 /* SpecialRecord.LookAhead */) {
1167
+ lookAhead = id;
1168
+ }
1169
+ return index;
1170
+ }
1171
+ let children = [], positions = [];
1172
+ while (cursor.pos > 0)
1173
+ takeNode(data.start || 0, data.bufferStart || 0, children, positions, -1);
1174
+ let length = (_a = data.length) !== null && _a !== void 0 ? _a : (children.length ? positions[0] + children[0].length : 0);
1175
+ return new Tree(types[data.topID], children.reverse(), positions.reverse(), length);
1176
+ }
1177
+ const nodeSizeCache = new WeakMap;
1178
+ function nodeSize(balanceType, node) {
1179
+ if (!balanceType.isAnonymous || node instanceof TreeBuffer || node.type != balanceType)
1180
+ return 1;
1181
+ let size = nodeSizeCache.get(node);
1182
+ if (size == null) {
1183
+ size = 1;
1184
+ for (let child of node.children) {
1185
+ if (child.type != balanceType || !(child instanceof Tree)) {
1186
+ size = 1;
1187
+ break;
1188
+ }
1189
+ size += nodeSize(balanceType, child);
1190
+ }
1191
+ nodeSizeCache.set(node, size);
1192
+ }
1193
+ return size;
1194
+ }
1195
+ function balanceRange(
1196
+ // The type the balanced tree's inner nodes.
1197
+ balanceType,
1198
+ // The direct children and their positions
1199
+ children, positions,
1200
+ // The index range in children/positions to use
1201
+ from, to,
1202
+ // The start position of the nodes, relative to their parent.
1203
+ start,
1204
+ // Length of the outer node
1205
+ length,
1206
+ // Function to build the top node of the balanced tree
1207
+ mkTop,
1208
+ // Function to build internal nodes for the balanced tree
1209
+ mkTree) {
1210
+ let total = 0;
1211
+ for (let i = from; i < to; i++)
1212
+ total += nodeSize(balanceType, children[i]);
1213
+ let maxChild = Math.ceil((total * 1.5) / 8 /* Balance.BranchFactor */);
1214
+ let localChildren = [], localPositions = [];
1215
+ function divide(children, positions, from, to, offset) {
1216
+ for (let i = from; i < to;) {
1217
+ let groupFrom = i, groupStart = positions[i], groupSize = nodeSize(balanceType, children[i]);
1218
+ i++;
1219
+ for (; i < to; i++) {
1220
+ let nextSize = nodeSize(balanceType, children[i]);
1221
+ if (groupSize + nextSize >= maxChild)
1222
+ break;
1223
+ groupSize += nextSize;
1224
+ }
1225
+ if (i == groupFrom + 1) {
1226
+ if (groupSize > maxChild) {
1227
+ let only = children[groupFrom]; // Only trees can have a size > 1
1228
+ divide(only.children, only.positions, 0, only.children.length, positions[groupFrom] + offset);
1229
+ continue;
1230
+ }
1231
+ localChildren.push(children[groupFrom]);
1232
+ }
1233
+ else {
1234
+ let length = positions[i - 1] + children[i - 1].length - groupStart;
1235
+ localChildren.push(balanceRange(balanceType, children, positions, groupFrom, i, groupStart, length, null, mkTree));
1236
+ }
1237
+ localPositions.push(groupStart + offset - start);
1238
+ }
1239
+ }
1240
+ divide(children, positions, from, to, 0);
1241
+ return (mkTop || mkTree)(localChildren, localPositions, length);
1242
+ }
1243
+ /// A superclass that parsers should extend.
1244
+ class Parser {
1245
+ /// Start a parse, returning a [partial parse](#common.PartialParse)
1246
+ /// object. [`fragments`](#common.TreeFragment) can be passed in to
1247
+ /// make the parse incremental.
1248
+ ///
1249
+ /// By default, the entire input is parsed. You can pass `ranges`,
1250
+ /// which should be a sorted array of non-empty, non-overlapping
1251
+ /// ranges, to parse only those ranges. The tree returned in that
1252
+ /// case will start at `ranges[0].from`.
1253
+ startParse(input, fragments, ranges) {
1254
+ if (typeof input == "string")
1255
+ input = new StringInput(input);
1256
+ ranges = !ranges ? [new Range(0, input.length)] : ranges.length ? ranges.map(r => new Range(r.from, r.to)) : [new Range(0, 0)];
1257
+ return this.createParse(input, fragments || [], ranges);
1258
+ }
1259
+ /// Run a full parse, returning the resulting tree.
1260
+ parse(input, fragments, ranges) {
1261
+ let parse = this.startParse(input, fragments, ranges);
1262
+ for (;;) {
1263
+ let done = parse.advance();
1264
+ if (done)
1265
+ return done;
1266
+ }
1267
+ }
1268
+ }
1269
+ class StringInput {
1270
+ constructor(string) {
1271
+ this.string = string;
1272
+ }
1273
+ get length() { return this.string.length; }
1274
+ chunk(from) { return this.string.slice(from); }
1275
+ get lineChunks() { return false; }
1276
+ read(from, to) { return this.string.slice(from, to); }
1277
+ }
1278
+ new NodeProp({ perNode: true });
1279
+
1280
+ exports.DefaultBufferLength = DefaultBufferLength;
1281
+ exports.NodeProp = NodeProp;
1282
+ exports.NodeSet = NodeSet;
1283
+ exports.NodeType = NodeType;
1284
+ exports.Parser = Parser;
1285
+ exports.Tree = Tree;
1286
+ exports.TreeBuffer = TreeBuffer;
1287
+ exports.TreeCursor = TreeCursor;
1288
+ //# sourceMappingURL=index.js.map