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