@vuu-ui/vuu-codemirror 0.8.34 → 0.8.36

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