@progressive-development/pd-content 1.0.3 → 1.1.1

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 (84) hide show
  1. package/dist/index.d.ts +2 -4
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +0 -4
  4. package/dist/node_modules/.pnpm/@codemirror_autocomplete@6.20.1/node_modules/@codemirror/autocomplete/dist/index.js +550 -0
  5. package/dist/node_modules/.pnpm/@codemirror_lang-css@6.3.1/node_modules/@codemirror/lang-css/dist/index.js +264 -0
  6. package/dist/node_modules/.pnpm/@codemirror_lang-html@6.4.11/node_modules/@codemirror/lang-html/dist/index.js +661 -0
  7. package/dist/node_modules/.pnpm/@codemirror_lang-java@6.0.2/node_modules/@codemirror/lang-java/dist/index.js +44 -0
  8. package/dist/node_modules/.pnpm/@codemirror_lang-javascript@6.2.5/node_modules/@codemirror/lang-javascript/dist/index.js +346 -0
  9. package/dist/node_modules/.pnpm/@codemirror_lang-json@6.0.2/node_modules/@codemirror/lang-json/dist/index.js +32 -0
  10. package/dist/node_modules/.pnpm/@codemirror_lang-markdown@6.5.0/node_modules/@codemirror/lang-markdown/dist/index.js +492 -0
  11. package/dist/node_modules/.pnpm/@codemirror_lang-python@6.2.1/node_modules/@codemirror/lang-python/dist/index.js +308 -0
  12. package/dist/node_modules/.pnpm/@codemirror_language@6.12.3/node_modules/@codemirror/language/dist/index.js +1572 -0
  13. package/dist/node_modules/.pnpm/@codemirror_state@6.6.0/node_modules/@codemirror/state/dist/index.js +3881 -0
  14. package/dist/node_modules/.pnpm/@codemirror_view@6.40.0/node_modules/@codemirror/view/dist/index.js +9657 -0
  15. package/dist/node_modules/.pnpm/@lezer_common@1.5.1/node_modules/@lezer/common/dist/index.js +2196 -0
  16. package/dist/node_modules/.pnpm/@lezer_css@1.3.3/node_modules/@lezer/css/dist/index.js +147 -0
  17. package/dist/node_modules/.pnpm/@lezer_highlight@1.2.3/node_modules/@lezer/highlight/dist/index.js +898 -0
  18. package/dist/node_modules/.pnpm/@lezer_html@1.3.13/node_modules/@lezer/html/dist/index.js +349 -0
  19. package/dist/node_modules/.pnpm/@lezer_java@1.1.3/node_modules/@lezer/java/dist/index.js +67 -0
  20. package/dist/node_modules/.pnpm/@lezer_javascript@1.5.4/node_modules/@lezer/javascript/dist/index.js +192 -0
  21. package/dist/node_modules/.pnpm/@lezer_json@1.0.3/node_modules/@lezer/json/dist/index.js +37 -0
  22. package/dist/node_modules/.pnpm/@lezer_lr@1.4.8/node_modules/@lezer/lr/dist/index.js +1884 -0
  23. package/dist/node_modules/.pnpm/@lezer_markdown@1.6.3/node_modules/@lezer/markdown/dist/index.js +2335 -0
  24. package/dist/node_modules/.pnpm/@lezer_python@1.1.18/node_modules/@lezer/python/dist/index.js +326 -0
  25. package/dist/node_modules/.pnpm/@marijn_find-cluster-break@1.0.2/node_modules/@marijn/find-cluster-break/src/index.js +82 -0
  26. package/dist/node_modules/.pnpm/style-mod@4.1.3/node_modules/style-mod/src/style-mod.js +174 -0
  27. package/dist/node_modules/.pnpm/w3c-keyname@2.2.8/node_modules/w3c-keyname/index.js +121 -0
  28. package/dist/pd-badge-order/PdBadgeItem.d.ts +11 -0
  29. package/dist/pd-badge-order/PdBadgeItem.d.ts.map +1 -1
  30. package/dist/pd-badge-order/PdBadgeItem.js +162 -10
  31. package/dist/pd-badge-order/PdBadgeOrder.d.ts +56 -17
  32. package/dist/pd-badge-order/PdBadgeOrder.d.ts.map +1 -1
  33. package/dist/pd-badge-order/PdBadgeOrder.js +308 -153
  34. package/dist/pd-badge-order/types.js +3 -1
  35. package/dist/pd-code-snippet/PdCodeSnippet.d.ts +29 -0
  36. package/dist/pd-code-snippet/PdCodeSnippet.d.ts.map +1 -1
  37. package/dist/pd-code-snippet/PdCodeSnippet.js +117 -67
  38. package/dist/pd-code-snippet/codemirror-setup.d.ts +10 -0
  39. package/dist/pd-code-snippet/codemirror-setup.d.ts.map +1 -0
  40. package/dist/pd-code-snippet/codemirror-setup.js +101 -0
  41. package/dist/pd-loading-state/PdLoadingState.d.ts +4 -1
  42. package/dist/pd-loading-state/PdLoadingState.d.ts.map +1 -1
  43. package/dist/pd-loading-state/pd-loading-state.d.ts +1 -1
  44. package/dist/pd-loading-state/pd-loading-state.d.ts.map +1 -1
  45. package/dist/pd-loading-state/pd-logo-loader.js +2 -2
  46. package/dist/pd-more-info/PdMoreInfo.d.ts +52 -4
  47. package/dist/pd-more-info/PdMoreInfo.d.ts.map +1 -1
  48. package/dist/pd-more-info/PdMoreInfo.js +146 -24
  49. package/dist/pd-notice-box/PdNoticeBox.d.ts +8 -1
  50. package/dist/pd-notice-box/PdNoticeBox.d.ts.map +1 -1
  51. package/dist/pd-notice-box/PdNoticeBox.js +41 -2
  52. package/dist/pd-panel-viewer/PdPanel.d.ts +3 -0
  53. package/dist/pd-panel-viewer/PdPanel.d.ts.map +1 -1
  54. package/dist/pd-panel-viewer/PdPanel.js +8 -1
  55. package/dist/pd-panel-viewer/PdPanelViewer.d.ts +3 -0
  56. package/dist/pd-panel-viewer/PdPanelViewer.d.ts.map +1 -1
  57. package/dist/pd-panel-viewer/PdPanelViewer.js +59 -26
  58. package/dist/pd-tab/PdTab.d.ts +8 -5
  59. package/dist/pd-tab/PdTab.d.ts.map +1 -1
  60. package/dist/pd-tab/PdTab.js +110 -56
  61. package/dist/pd-timeline/PdTimeline.d.ts +2 -0
  62. package/dist/pd-timeline/PdTimeline.d.ts.map +1 -1
  63. package/dist/pd-timeline/PdTimeline.js +50 -19
  64. package/dist/types.d.ts +2 -2
  65. package/dist/types.d.ts.map +1 -1
  66. package/package.json +16 -8
  67. package/dist/pd-gallery/PdGallery.d.ts +0 -72
  68. package/dist/pd-gallery/PdGallery.d.ts.map +0 -1
  69. package/dist/pd-gallery/PdGallery.js +0 -660
  70. package/dist/pd-gallery/PdGalleryLightbox.d.ts +0 -53
  71. package/dist/pd-gallery/PdGalleryLightbox.d.ts.map +0 -1
  72. package/dist/pd-gallery/PdGalleryLightbox.js +0 -530
  73. package/dist/pd-gallery/index.d.ts +0 -4
  74. package/dist/pd-gallery/index.d.ts.map +0 -1
  75. package/dist/pd-gallery/pd-gallery-lightbox.d.ts +0 -3
  76. package/dist/pd-gallery/pd-gallery-lightbox.d.ts.map +0 -1
  77. package/dist/pd-gallery/pd-gallery.d.ts +0 -3
  78. package/dist/pd-gallery/pd-gallery.d.ts.map +0 -1
  79. package/dist/pd-gallery/types.d.ts +0 -23
  80. package/dist/pd-gallery/types.d.ts.map +0 -1
  81. package/dist/pd-gallery-lightbox.d.ts +0 -2
  82. package/dist/pd-gallery-lightbox.js +0 -8
  83. package/dist/pd-gallery.d.ts +0 -2
  84. package/dist/pd-gallery.js +0 -8
@@ -0,0 +1,2196 @@
1
+ /**
2
+ The default maximum length of a `TreeBuffer` node.
3
+ */
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
+ /**
13
+ Each [node type](#common.NodeType) or [individual tree](#common.Tree)
14
+ can have metadata associated with it in props. Instances of this
15
+ class represent prop names.
16
+ */
17
+ class NodeProp {
18
+ /**
19
+ Create a new node prop type.
20
+ */
21
+ constructor(config = {}) {
22
+ this.id = nextPropID++;
23
+ this.perNode = !!config.perNode;
24
+ this.deserialize = config.deserialize || (() => {
25
+ throw new Error("This node type doesn't define a deserialize function");
26
+ });
27
+ this.combine = config.combine || null;
28
+ }
29
+ /**
30
+ This is meant to be used with
31
+ [`NodeSet.extend`](#common.NodeSet.extend) or
32
+ [`LRParser.configure`](#lr.ParserConfig.props) to compute
33
+ prop values for each node type in the set. Takes a [match
34
+ object](#common.NodeType^match) or function that returns undefined
35
+ if the node type doesn't get this prop, and the prop's value if
36
+ it does.
37
+ */
38
+ add(match) {
39
+ if (this.perNode)
40
+ throw new RangeError("Can't add per-node props to node types");
41
+ if (typeof match != "function")
42
+ match = NodeType.match(match);
43
+ return (type) => {
44
+ let result = match(type);
45
+ return result === undefined ? null : [this, result];
46
+ };
47
+ }
48
+ }
49
+ /**
50
+ Prop that is used to describe matching delimiters. For opening
51
+ delimiters, this holds an array of node names (written as a
52
+ space-separated string when declaring this prop in a grammar)
53
+ for the node types of closing delimiters that match it.
54
+ */
55
+ NodeProp.closedBy = new NodeProp({ deserialize: str => str.split(" ") });
56
+ /**
57
+ The inverse of [`closedBy`](#common.NodeProp^closedBy). This is
58
+ attached to closing delimiters, holding an array of node names
59
+ of types of matching opening delimiters.
60
+ */
61
+ NodeProp.openedBy = new NodeProp({ deserialize: str => str.split(" ") });
62
+ /**
63
+ Used to assign node types to groups (for example, all node
64
+ types that represent an expression could be tagged with an
65
+ `"Expression"` group).
66
+ */
67
+ NodeProp.group = new NodeProp({ deserialize: str => str.split(" ") });
68
+ /**
69
+ Attached to nodes to indicate these should be
70
+ [displayed](https://codemirror.net/docs/ref/#language.syntaxTree)
71
+ in a bidirectional text isolate, so that direction-neutral
72
+ characters on their sides don't incorrectly get associated with
73
+ surrounding text. You'll generally want to set this for nodes
74
+ that contain arbitrary text, like strings and comments, and for
75
+ nodes that appear _inside_ arbitrary text, like HTML tags. When
76
+ not given a value, in a grammar declaration, defaults to
77
+ `"auto"`.
78
+ */
79
+ NodeProp.isolate = new NodeProp({ deserialize: value => {
80
+ if (value && value != "rtl" && value != "ltr" && value != "auto")
81
+ throw new RangeError("Invalid value for isolate: " + value);
82
+ return value || "auto";
83
+ } });
84
+ /**
85
+ The hash of the [context](#lr.ContextTracker.constructor)
86
+ that the node was parsed in, if any. Used to limit reuse of
87
+ contextual nodes.
88
+ */
89
+ NodeProp.contextHash = new NodeProp({ perNode: true });
90
+ /**
91
+ The distance beyond the end of the node that the tokenizer
92
+ looked ahead for any of the tokens inside the node. (The LR
93
+ parser only stores this when it is larger than 25, for
94
+ efficiency reasons.)
95
+ */
96
+ NodeProp.lookAhead = new NodeProp({ perNode: true });
97
+ /**
98
+ This per-node prop is used to replace a given node, or part of a
99
+ node, with another tree. This is useful to include trees from
100
+ different languages in mixed-language parsers.
101
+ */
102
+ NodeProp.mounted = new NodeProp({ perNode: true });
103
+ /**
104
+ A mounted tree, which can be [stored](#common.NodeProp^mounted) on
105
+ a tree node to indicate that parts of its content are
106
+ represented by another tree.
107
+ */
108
+ class MountedTree {
109
+ constructor(
110
+ /**
111
+ The inner tree.
112
+ */
113
+ tree,
114
+ /**
115
+ If this is null, this tree replaces the entire node (it will
116
+ be included in the regular iteration instead of its host
117
+ node). If not, only the given ranges are considered to be
118
+ covered by this tree. This is used for trees that are mixed in
119
+ a way that isn't strictly hierarchical. Such mounted trees are
120
+ only entered by [`resolveInner`](#common.Tree.resolveInner)
121
+ and [`enter`](#common.SyntaxNode.enter).
122
+ */
123
+ overlay,
124
+ /**
125
+ The parser used to create this subtree.
126
+ */
127
+ parser,
128
+ /**
129
+ [Indicates](#common.IterMode.EnterBracketed) that the nested
130
+ content is delineated with some kind
131
+ of bracket token.
132
+ */
133
+ bracketed = false) {
134
+ this.tree = tree;
135
+ this.overlay = overlay;
136
+ this.parser = parser;
137
+ this.bracketed = bracketed;
138
+ }
139
+ /**
140
+ @internal
141
+ */
142
+ static get(tree) {
143
+ return tree && tree.props && tree.props[NodeProp.mounted.id];
144
+ }
145
+ }
146
+ const noProps = Object.create(null);
147
+ /**
148
+ Each node in a syntax tree has a node type associated with it.
149
+ */
150
+ class NodeType {
151
+ /**
152
+ @internal
153
+ */
154
+ constructor(
155
+ /**
156
+ The name of the node type. Not necessarily unique, but if the
157
+ grammar was written properly, different node types with the
158
+ same name within a node set should play the same semantic
159
+ role.
160
+ */
161
+ name,
162
+ /**
163
+ @internal
164
+ */
165
+ props,
166
+ /**
167
+ The id of this node in its set. Corresponds to the term ids
168
+ used in the parser.
169
+ */
170
+ id,
171
+ /**
172
+ @internal
173
+ */
174
+ flags = 0) {
175
+ this.name = name;
176
+ this.props = props;
177
+ this.id = id;
178
+ this.flags = flags;
179
+ }
180
+ /**
181
+ Define a node type.
182
+ */
183
+ static define(spec) {
184
+ let props = spec.props && spec.props.length ? Object.create(null) : noProps;
185
+ let flags = (spec.top ? 1 /* NodeFlag.Top */ : 0) | (spec.skipped ? 2 /* NodeFlag.Skipped */ : 0) |
186
+ (spec.error ? 4 /* NodeFlag.Error */ : 0) | (spec.name == null ? 8 /* NodeFlag.Anonymous */ : 0);
187
+ let type = new NodeType(spec.name || "", props, spec.id, flags);
188
+ if (spec.props)
189
+ for (let src of spec.props) {
190
+ if (!Array.isArray(src))
191
+ src = src(type);
192
+ if (src) {
193
+ if (src[0].perNode)
194
+ throw new RangeError("Can't store a per-node prop on a node type");
195
+ props[src[0].id] = src[1];
196
+ }
197
+ }
198
+ return type;
199
+ }
200
+ /**
201
+ Retrieves a node prop for this type. Will return `undefined` if
202
+ the prop isn't present on this node.
203
+ */
204
+ prop(prop) { return this.props[prop.id]; }
205
+ /**
206
+ True when this is the top node of a grammar.
207
+ */
208
+ get isTop() { return (this.flags & 1 /* NodeFlag.Top */) > 0; }
209
+ /**
210
+ True when this node is produced by a skip rule.
211
+ */
212
+ get isSkipped() { return (this.flags & 2 /* NodeFlag.Skipped */) > 0; }
213
+ /**
214
+ Indicates whether this is an error node.
215
+ */
216
+ get isError() { return (this.flags & 4 /* NodeFlag.Error */) > 0; }
217
+ /**
218
+ When true, this node type doesn't correspond to a user-declared
219
+ named node, for example because it is used to cache repetition.
220
+ */
221
+ get isAnonymous() { return (this.flags & 8 /* NodeFlag.Anonymous */) > 0; }
222
+ /**
223
+ Returns true when this node's name or one of its
224
+ [groups](#common.NodeProp^group) matches the given string.
225
+ */
226
+ is(name) {
227
+ if (typeof name == 'string') {
228
+ if (this.name == name)
229
+ return true;
230
+ let group = this.prop(NodeProp.group);
231
+ return group ? group.indexOf(name) > -1 : false;
232
+ }
233
+ return this.id == name;
234
+ }
235
+ /**
236
+ Create a function from node types to arbitrary values by
237
+ specifying an object whose property names are node or
238
+ [group](#common.NodeProp^group) names. Often useful with
239
+ [`NodeProp.add`](#common.NodeProp.add). You can put multiple
240
+ names, separated by spaces, in a single property name to map
241
+ multiple node names to a single value.
242
+ */
243
+ static match(map) {
244
+ let direct = Object.create(null);
245
+ for (let prop in map)
246
+ for (let name of prop.split(" "))
247
+ direct[name] = map[prop];
248
+ return (node) => {
249
+ for (let groups = node.prop(NodeProp.group), i = -1; i < (groups ? groups.length : 0); i++) {
250
+ let found = direct[i < 0 ? node.name : groups[i]];
251
+ if (found)
252
+ return found;
253
+ }
254
+ };
255
+ }
256
+ }
257
+ /**
258
+ An empty dummy node type to use when no actual type is available.
259
+ */
260
+ NodeType.none = new NodeType("", Object.create(null), 0, 8 /* NodeFlag.Anonymous */);
261
+ /**
262
+ A node set holds a collection of node types. It is used to
263
+ compactly represent trees by storing their type ids, rather than a
264
+ full pointer to the type object, in a numeric array. Each parser
265
+ [has](#lr.LRParser.nodeSet) a node set, and [tree
266
+ buffers](#common.TreeBuffer) can only store collections of nodes
267
+ from the same set. A set can have a maximum of 2**16 (65536) node
268
+ types in it, so that the ids fit into 16-bit typed array slots.
269
+ */
270
+ class NodeSet {
271
+ /**
272
+ Create a set with the given types. The `id` property of each
273
+ type should correspond to its position within the array.
274
+ */
275
+ constructor(
276
+ /**
277
+ The node types in this set, by id.
278
+ */
279
+ types) {
280
+ this.types = types;
281
+ for (let i = 0; i < types.length; i++)
282
+ if (types[i].id != i)
283
+ throw new RangeError("Node type ids should correspond to array positions when creating a node set");
284
+ }
285
+ /**
286
+ Create a copy of this set with some node properties added. The
287
+ arguments to this method can be created with
288
+ [`NodeProp.add`](#common.NodeProp.add).
289
+ */
290
+ extend(...props) {
291
+ let newTypes = [];
292
+ for (let type of this.types) {
293
+ let newProps = null;
294
+ for (let source of props) {
295
+ let add = source(type);
296
+ if (add) {
297
+ if (!newProps)
298
+ newProps = Object.assign({}, type.props);
299
+ let value = add[1], prop = add[0];
300
+ if (prop.combine && prop.id in newProps)
301
+ value = prop.combine(newProps[prop.id], value);
302
+ newProps[prop.id] = value;
303
+ }
304
+ }
305
+ newTypes.push(newProps ? new NodeType(type.name, newProps, type.id, type.flags) : type);
306
+ }
307
+ return new NodeSet(newTypes);
308
+ }
309
+ }
310
+ const CachedNode = new WeakMap(), CachedInnerNode = new WeakMap();
311
+ /**
312
+ Options that control iteration. Can be combined with the `|`
313
+ operator to enable multiple ones.
314
+ */
315
+ var IterMode;
316
+ (function (IterMode) {
317
+ /**
318
+ When enabled, iteration will only visit [`Tree`](#common.Tree)
319
+ objects, not nodes packed into
320
+ [`TreeBuffer`](#common.TreeBuffer)s.
321
+ */
322
+ IterMode[IterMode["ExcludeBuffers"] = 1] = "ExcludeBuffers";
323
+ /**
324
+ Enable this to make iteration include anonymous nodes (such as
325
+ the nodes that wrap repeated grammar constructs into a balanced
326
+ tree).
327
+ */
328
+ IterMode[IterMode["IncludeAnonymous"] = 2] = "IncludeAnonymous";
329
+ /**
330
+ By default, regular [mounted](#common.NodeProp^mounted) nodes
331
+ replace their base node in iteration. Enable this to ignore them
332
+ instead.
333
+ */
334
+ IterMode[IterMode["IgnoreMounts"] = 4] = "IgnoreMounts";
335
+ /**
336
+ This option only applies in
337
+ [`enter`](#common.SyntaxNode.enter)-style methods. It tells the
338
+ library to not enter mounted overlays if one covers the given
339
+ position.
340
+ */
341
+ IterMode[IterMode["IgnoreOverlays"] = 8] = "IgnoreOverlays";
342
+ /**
343
+ When set, positions on the boundary of a mounted overlay tree
344
+ that has its [`bracketed`](#common.NestedParse.bracketed) flag
345
+ set will enter that tree regardless of side. Only supported in
346
+ [`enter`](#common.SyntaxNode.enter), not in cursors.
347
+ */
348
+ IterMode[IterMode["EnterBracketed"] = 16] = "EnterBracketed";
349
+ })(IterMode || (IterMode = {}));
350
+ /**
351
+ A piece of syntax tree. There are two ways to approach these
352
+ trees: the way they are actually stored in memory, and the
353
+ convenient way.
354
+
355
+ Syntax trees are stored as a tree of `Tree` and `TreeBuffer`
356
+ objects. By packing detail information into `TreeBuffer` leaf
357
+ nodes, the representation is made a lot more memory-efficient.
358
+
359
+ However, when you want to actually work with tree nodes, this
360
+ representation is very awkward, so most client code will want to
361
+ use the [`TreeCursor`](#common.TreeCursor) or
362
+ [`SyntaxNode`](#common.SyntaxNode) interface instead, which provides
363
+ a view on some part of this data structure, and can be used to
364
+ move around to adjacent nodes.
365
+ */
366
+ class Tree {
367
+ /**
368
+ Construct a new tree. See also [`Tree.build`](#common.Tree^build).
369
+ */
370
+ constructor(
371
+ /**
372
+ The type of the top node.
373
+ */
374
+ type,
375
+ /**
376
+ This node's child nodes.
377
+ */
378
+ children,
379
+ /**
380
+ The positions (offsets relative to the start of this tree) of
381
+ the children.
382
+ */
383
+ positions,
384
+ /**
385
+ The total length of this tree
386
+ */
387
+ length,
388
+ /**
389
+ Per-node [node props](#common.NodeProp) to associate with this node.
390
+ */
391
+ props) {
392
+ this.type = type;
393
+ this.children = children;
394
+ this.positions = positions;
395
+ this.length = length;
396
+ /**
397
+ @internal
398
+ */
399
+ this.props = null;
400
+ if (props && props.length) {
401
+ this.props = Object.create(null);
402
+ for (let [prop, value] of props)
403
+ this.props[typeof prop == "number" ? prop : prop.id] = value;
404
+ }
405
+ }
406
+ /**
407
+ @internal
408
+ */
409
+ toString() {
410
+ let mounted = MountedTree.get(this);
411
+ if (mounted && !mounted.overlay)
412
+ return mounted.tree.toString();
413
+ let children = "";
414
+ for (let ch of this.children) {
415
+ let str = ch.toString();
416
+ if (str) {
417
+ if (children)
418
+ children += ",";
419
+ children += str;
420
+ }
421
+ }
422
+ return !this.type.name ? children :
423
+ (/\W/.test(this.type.name) && !this.type.isError ? JSON.stringify(this.type.name) : this.type.name) +
424
+ (children.length ? "(" + children + ")" : "");
425
+ }
426
+ /**
427
+ Get a [tree cursor](#common.TreeCursor) positioned at the top of
428
+ the tree. Mode can be used to [control](#common.IterMode) which
429
+ nodes the cursor visits.
430
+ */
431
+ cursor(mode = 0) {
432
+ return new TreeCursor(this.topNode, mode);
433
+ }
434
+ /**
435
+ Get a [tree cursor](#common.TreeCursor) pointing into this tree
436
+ at the given position and side (see
437
+ [`moveTo`](#common.TreeCursor.moveTo).
438
+ */
439
+ cursorAt(pos, side = 0, mode = 0) {
440
+ let scope = CachedNode.get(this) || this.topNode;
441
+ let cursor = new TreeCursor(scope);
442
+ cursor.moveTo(pos, side);
443
+ CachedNode.set(this, cursor._tree);
444
+ return cursor;
445
+ }
446
+ /**
447
+ Get a [syntax node](#common.SyntaxNode) object for the top of the
448
+ tree.
449
+ */
450
+ get topNode() {
451
+ return new TreeNode(this, 0, 0, null);
452
+ }
453
+ /**
454
+ Get the [syntax node](#common.SyntaxNode) at the given position.
455
+ If `side` is -1, this will move into nodes that end at the
456
+ position. If 1, it'll move into nodes that start at the
457
+ position. With 0, it'll only enter nodes that cover the position
458
+ from both sides.
459
+
460
+ Note that this will not enter
461
+ [overlays](#common.MountedTree.overlay), and you often want
462
+ [`resolveInner`](#common.Tree.resolveInner) instead.
463
+ */
464
+ resolve(pos, side = 0) {
465
+ let node = resolveNode(CachedNode.get(this) || this.topNode, pos, side, false);
466
+ CachedNode.set(this, node);
467
+ return node;
468
+ }
469
+ /**
470
+ Like [`resolve`](#common.Tree.resolve), but will enter
471
+ [overlaid](#common.MountedTree.overlay) nodes, producing a syntax node
472
+ pointing into the innermost overlaid tree at the given position
473
+ (with parent links going through all parent structure, including
474
+ the host trees).
475
+ */
476
+ resolveInner(pos, side = 0) {
477
+ let node = resolveNode(CachedInnerNode.get(this) || this.topNode, pos, side, true);
478
+ CachedInnerNode.set(this, node);
479
+ return node;
480
+ }
481
+ /**
482
+ In some situations, it can be useful to iterate through all
483
+ nodes around a position, including those in overlays that don't
484
+ directly cover the position. This method gives you an iterator
485
+ that will produce all nodes, from small to big, around the given
486
+ position.
487
+ */
488
+ resolveStack(pos, side = 0) {
489
+ return stackIterator(this, pos, side);
490
+ }
491
+ /**
492
+ Iterate over the tree and its children, calling `enter` for any
493
+ node that touches the `from`/`to` region (if given) before
494
+ running over such a node's children, and `leave` (if given) when
495
+ leaving the node. When `enter` returns `false`, that node will
496
+ not have its children iterated over (or `leave` called).
497
+ */
498
+ iterate(spec) {
499
+ let { enter, leave, from = 0, to = this.length } = spec;
500
+ let mode = spec.mode || 0, anon = (mode & IterMode.IncludeAnonymous) > 0;
501
+ for (let c = this.cursor(mode | IterMode.IncludeAnonymous);;) {
502
+ let entered = false;
503
+ if (c.from <= to && c.to >= from && (!anon && c.type.isAnonymous || enter(c) !== false)) {
504
+ if (c.firstChild())
505
+ continue;
506
+ entered = true;
507
+ }
508
+ for (;;) {
509
+ if (entered && leave && (anon || !c.type.isAnonymous))
510
+ leave(c);
511
+ if (c.nextSibling())
512
+ break;
513
+ if (!c.parent())
514
+ return;
515
+ entered = true;
516
+ }
517
+ }
518
+ }
519
+ /**
520
+ Get the value of the given [node prop](#common.NodeProp) for this
521
+ node. Works with both per-node and per-type props.
522
+ */
523
+ prop(prop) {
524
+ return !prop.perNode ? this.type.prop(prop) : this.props ? this.props[prop.id] : undefined;
525
+ }
526
+ /**
527
+ Returns the node's [per-node props](#common.NodeProp.perNode) in a
528
+ format that can be passed to the [`Tree`](#common.Tree)
529
+ constructor.
530
+ */
531
+ get propValues() {
532
+ let result = [];
533
+ if (this.props)
534
+ for (let id in this.props)
535
+ result.push([+id, this.props[id]]);
536
+ return result;
537
+ }
538
+ /**
539
+ Balance the direct children of this tree, producing a copy of
540
+ which may have children grouped into subtrees with type
541
+ [`NodeType.none`](#common.NodeType^none).
542
+ */
543
+ balance(config = {}) {
544
+ return this.children.length <= 8 /* Balance.BranchFactor */ ? this :
545
+ 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)));
546
+ }
547
+ /**
548
+ Build a tree from a postfix-ordered buffer of node information,
549
+ or a cursor over such a buffer.
550
+ */
551
+ static build(data) { return buildTree(data); }
552
+ }
553
+ /**
554
+ The empty tree
555
+ */
556
+ Tree.empty = new Tree(NodeType.none, [], [], 0);
557
+ class FlatBufferCursor {
558
+ constructor(buffer, index) {
559
+ this.buffer = buffer;
560
+ this.index = index;
561
+ }
562
+ get id() { return this.buffer[this.index - 4]; }
563
+ get start() { return this.buffer[this.index - 3]; }
564
+ get end() { return this.buffer[this.index - 2]; }
565
+ get size() { return this.buffer[this.index - 1]; }
566
+ get pos() { return this.index; }
567
+ next() { this.index -= 4; }
568
+ fork() { return new FlatBufferCursor(this.buffer, this.index); }
569
+ }
570
+ /**
571
+ Tree buffers contain (type, start, end, endIndex) quads for each
572
+ node. In such a buffer, nodes are stored in prefix order (parents
573
+ before children, with the endIndex of the parent indicating which
574
+ children belong to it).
575
+ */
576
+ class TreeBuffer {
577
+ /**
578
+ Create a tree buffer.
579
+ */
580
+ constructor(
581
+ /**
582
+ The buffer's content.
583
+ */
584
+ buffer,
585
+ /**
586
+ The total length of the group of nodes in the buffer.
587
+ */
588
+ length,
589
+ /**
590
+ The node set used in this buffer.
591
+ */
592
+ set) {
593
+ this.buffer = buffer;
594
+ this.length = length;
595
+ this.set = set;
596
+ }
597
+ /**
598
+ @internal
599
+ */
600
+ get type() { return NodeType.none; }
601
+ /**
602
+ @internal
603
+ */
604
+ toString() {
605
+ let result = [];
606
+ for (let index = 0; index < this.buffer.length;) {
607
+ result.push(this.childString(index));
608
+ index = this.buffer[index + 3];
609
+ }
610
+ return result.join(",");
611
+ }
612
+ /**
613
+ @internal
614
+ */
615
+ childString(index) {
616
+ let id = this.buffer[index], endIndex = this.buffer[index + 3];
617
+ let type = this.set.types[id], result = type.name;
618
+ if (/\W/.test(result) && !type.isError)
619
+ result = JSON.stringify(result);
620
+ index += 4;
621
+ if (endIndex == index)
622
+ return result;
623
+ let children = [];
624
+ while (index < endIndex) {
625
+ children.push(this.childString(index));
626
+ index = this.buffer[index + 3];
627
+ }
628
+ return result + "(" + children.join(",") + ")";
629
+ }
630
+ /**
631
+ @internal
632
+ */
633
+ findChild(startIndex, endIndex, dir, pos, side) {
634
+ let { buffer } = this, pick = -1;
635
+ for (let i = startIndex; i != endIndex; i = buffer[i + 3]) {
636
+ if (checkSide(side, pos, buffer[i + 1], buffer[i + 2])) {
637
+ pick = i;
638
+ if (dir > 0)
639
+ break;
640
+ }
641
+ }
642
+ return pick;
643
+ }
644
+ /**
645
+ @internal
646
+ */
647
+ slice(startI, endI, from) {
648
+ let b = this.buffer;
649
+ let copy = new Uint16Array(endI - startI), len = 0;
650
+ for (let i = startI, j = 0; i < endI;) {
651
+ copy[j++] = b[i++];
652
+ copy[j++] = b[i++] - from;
653
+ let to = copy[j++] = b[i++] - from;
654
+ copy[j++] = b[i++] - startI;
655
+ len = Math.max(len, to);
656
+ }
657
+ return new TreeBuffer(copy, len, this.set);
658
+ }
659
+ }
660
+ function checkSide(side, pos, from, to) {
661
+ switch (side) {
662
+ case -2 /* Side.Before */: return from < pos;
663
+ case -1 /* Side.AtOrBefore */: return to >= pos && from < pos;
664
+ case 0 /* Side.Around */: return from < pos && to > pos;
665
+ case 1 /* Side.AtOrAfter */: return from <= pos && to > pos;
666
+ case 2 /* Side.After */: return to > pos;
667
+ case 4 /* Side.DontCare */: return true;
668
+ }
669
+ }
670
+ function resolveNode(node, pos, side, overlays) {
671
+ var _a;
672
+ // Move up to a node that actually holds the position, if possible
673
+ while (node.from == node.to ||
674
+ (side < 1 ? node.from >= pos : node.from > pos) ||
675
+ (side > -1 ? node.to <= pos : node.to < pos)) {
676
+ let parent = !overlays && node instanceof TreeNode && node.index < 0 ? null : node.parent;
677
+ if (!parent)
678
+ return node;
679
+ node = parent;
680
+ }
681
+ let mode = overlays ? 0 : IterMode.IgnoreOverlays;
682
+ // Must go up out of overlays when those do not overlap with pos
683
+ if (overlays)
684
+ for (let scan = node, parent = scan.parent; parent; scan = parent, parent = scan.parent) {
685
+ if (scan instanceof TreeNode && scan.index < 0 && ((_a = parent.enter(pos, side, mode)) === null || _a === void 0 ? void 0 : _a.from) != scan.from)
686
+ node = parent;
687
+ }
688
+ for (;;) {
689
+ let inner = node.enter(pos, side, mode);
690
+ if (!inner)
691
+ return node;
692
+ node = inner;
693
+ }
694
+ }
695
+ class BaseNode {
696
+ cursor(mode = 0) { return new TreeCursor(this, mode); }
697
+ getChild(type, before = null, after = null) {
698
+ let r = getChildren(this, type, before, after);
699
+ return r.length ? r[0] : null;
700
+ }
701
+ getChildren(type, before = null, after = null) {
702
+ return getChildren(this, type, before, after);
703
+ }
704
+ resolve(pos, side = 0) {
705
+ return resolveNode(this, pos, side, false);
706
+ }
707
+ resolveInner(pos, side = 0) {
708
+ return resolveNode(this, pos, side, true);
709
+ }
710
+ matchContext(context) {
711
+ return matchNodeContext(this.parent, context);
712
+ }
713
+ enterUnfinishedNodesBefore(pos) {
714
+ let scan = this.childBefore(pos), node = this;
715
+ while (scan) {
716
+ let last = scan.lastChild;
717
+ if (!last || last.to != scan.to)
718
+ break;
719
+ if (last.type.isError && last.from == last.to) {
720
+ node = scan;
721
+ scan = last.prevSibling;
722
+ }
723
+ else {
724
+ scan = last;
725
+ }
726
+ }
727
+ return node;
728
+ }
729
+ get node() { return this; }
730
+ get next() { return this.parent; }
731
+ }
732
+ class TreeNode extends BaseNode {
733
+ constructor(_tree, from,
734
+ // Index in parent node, set to -1 if the node is not a direct child of _parent.node (overlay)
735
+ index, _parent) {
736
+ super();
737
+ this._tree = _tree;
738
+ this.from = from;
739
+ this.index = index;
740
+ this._parent = _parent;
741
+ }
742
+ get type() { return this._tree.type; }
743
+ get name() { return this._tree.type.name; }
744
+ get to() { return this.from + this._tree.length; }
745
+ nextChild(i, dir, pos, side, mode = 0) {
746
+ for (let parent = this;;) {
747
+ for (let { children, positions } = parent._tree, e = dir > 0 ? children.length : -1; i != e; i += dir) {
748
+ let next = children[i], start = positions[i] + parent.from, mounted;
749
+ if (!((mode & IterMode.EnterBracketed) && next instanceof Tree &&
750
+ (mounted = MountedTree.get(next)) && !mounted.overlay && mounted.bracketed &&
751
+ pos >= start && pos <= start + next.length) &&
752
+ !checkSide(side, pos, start, start + next.length))
753
+ continue;
754
+ if (next instanceof TreeBuffer) {
755
+ if (mode & IterMode.ExcludeBuffers)
756
+ continue;
757
+ let index = next.findChild(0, next.buffer.length, dir, pos - start, side);
758
+ if (index > -1)
759
+ return new BufferNode(new BufferContext(parent, next, i, start), null, index);
760
+ }
761
+ else if ((mode & IterMode.IncludeAnonymous) || (!next.type.isAnonymous || hasChild(next))) {
762
+ let mounted;
763
+ if (!(mode & IterMode.IgnoreMounts) && (mounted = MountedTree.get(next)) && !mounted.overlay)
764
+ return new TreeNode(mounted.tree, start, i, parent);
765
+ let inner = new TreeNode(next, start, i, parent);
766
+ return (mode & IterMode.IncludeAnonymous) || !inner.type.isAnonymous ? inner
767
+ : inner.nextChild(dir < 0 ? next.children.length - 1 : 0, dir, pos, side, mode);
768
+ }
769
+ }
770
+ if ((mode & IterMode.IncludeAnonymous) || !parent.type.isAnonymous)
771
+ return null;
772
+ if (parent.index >= 0)
773
+ i = parent.index + dir;
774
+ else
775
+ i = dir < 0 ? -1 : parent._parent._tree.children.length;
776
+ parent = parent._parent;
777
+ if (!parent)
778
+ return null;
779
+ }
780
+ }
781
+ get firstChild() { return this.nextChild(0, 1, 0, 4 /* Side.DontCare */); }
782
+ get lastChild() { return this.nextChild(this._tree.children.length - 1, -1, 0, 4 /* Side.DontCare */); }
783
+ childAfter(pos) { return this.nextChild(0, 1, pos, 2 /* Side.After */); }
784
+ childBefore(pos) { return this.nextChild(this._tree.children.length - 1, -1, pos, -2 /* Side.Before */); }
785
+ prop(prop) { return this._tree.prop(prop); }
786
+ enter(pos, side, mode = 0) {
787
+ let mounted;
788
+ if (!(mode & IterMode.IgnoreOverlays) && (mounted = MountedTree.get(this._tree)) && mounted.overlay) {
789
+ let rPos = pos - this.from, enterBracketed = (mode & IterMode.EnterBracketed) && mounted.bracketed;
790
+ for (let { from, to } of mounted.overlay) {
791
+ if ((side > 0 || enterBracketed ? from <= rPos : from < rPos) &&
792
+ (side < 0 || enterBracketed ? to >= rPos : to > rPos))
793
+ return new TreeNode(mounted.tree, mounted.overlay[0].from + this.from, -1, this);
794
+ }
795
+ }
796
+ return this.nextChild(0, 1, pos, side, mode);
797
+ }
798
+ nextSignificantParent() {
799
+ let val = this;
800
+ while (val.type.isAnonymous && val._parent)
801
+ val = val._parent;
802
+ return val;
803
+ }
804
+ get parent() {
805
+ return this._parent ? this._parent.nextSignificantParent() : null;
806
+ }
807
+ get nextSibling() {
808
+ return this._parent && this.index >= 0 ? this._parent.nextChild(this.index + 1, 1, 0, 4 /* Side.DontCare */) : null;
809
+ }
810
+ get prevSibling() {
811
+ return this._parent && this.index >= 0 ? this._parent.nextChild(this.index - 1, -1, 0, 4 /* Side.DontCare */) : null;
812
+ }
813
+ get tree() { return this._tree; }
814
+ toTree() { return this._tree; }
815
+ /**
816
+ @internal
817
+ */
818
+ toString() { return this._tree.toString(); }
819
+ }
820
+ function getChildren(node, type, before, after) {
821
+ let cur = node.cursor(), result = [];
822
+ if (!cur.firstChild())
823
+ return result;
824
+ if (before != null)
825
+ for (let found = false; !found;) {
826
+ found = cur.type.is(before);
827
+ if (!cur.nextSibling())
828
+ return result;
829
+ }
830
+ for (;;) {
831
+ if (after != null && cur.type.is(after))
832
+ return result;
833
+ if (cur.type.is(type))
834
+ result.push(cur.node);
835
+ if (!cur.nextSibling())
836
+ return after == null ? result : [];
837
+ }
838
+ }
839
+ function matchNodeContext(node, context, i = context.length - 1) {
840
+ for (let p = node; i >= 0; p = p.parent) {
841
+ if (!p)
842
+ return false;
843
+ if (!p.type.isAnonymous) {
844
+ if (context[i] && context[i] != p.name)
845
+ return false;
846
+ i--;
847
+ }
848
+ }
849
+ return true;
850
+ }
851
+ class BufferContext {
852
+ constructor(parent, buffer, index, start) {
853
+ this.parent = parent;
854
+ this.buffer = buffer;
855
+ this.index = index;
856
+ this.start = start;
857
+ }
858
+ }
859
+ class BufferNode extends BaseNode {
860
+ get name() { return this.type.name; }
861
+ get from() { return this.context.start + this.context.buffer.buffer[this.index + 1]; }
862
+ get to() { return this.context.start + this.context.buffer.buffer[this.index + 2]; }
863
+ constructor(context, _parent, index) {
864
+ super();
865
+ this.context = context;
866
+ this._parent = _parent;
867
+ this.index = index;
868
+ this.type = context.buffer.set.types[context.buffer.buffer[index]];
869
+ }
870
+ child(dir, pos, side) {
871
+ let { buffer } = this.context;
872
+ let index = buffer.findChild(this.index + 4, buffer.buffer[this.index + 3], dir, pos - this.context.start, side);
873
+ return index < 0 ? null : new BufferNode(this.context, this, index);
874
+ }
875
+ get firstChild() { return this.child(1, 0, 4 /* Side.DontCare */); }
876
+ get lastChild() { return this.child(-1, 0, 4 /* Side.DontCare */); }
877
+ childAfter(pos) { return this.child(1, pos, 2 /* Side.After */); }
878
+ childBefore(pos) { return this.child(-1, pos, -2 /* Side.Before */); }
879
+ prop(prop) { return this.type.prop(prop); }
880
+ enter(pos, side, mode = 0) {
881
+ if (mode & IterMode.ExcludeBuffers)
882
+ return null;
883
+ let { buffer } = this.context;
884
+ let index = buffer.findChild(this.index + 4, buffer.buffer[this.index + 3], side > 0 ? 1 : -1, pos - this.context.start, side);
885
+ return index < 0 ? null : new BufferNode(this.context, this, index);
886
+ }
887
+ get parent() {
888
+ return this._parent || this.context.parent.nextSignificantParent();
889
+ }
890
+ externalSibling(dir) {
891
+ return this._parent ? null : this.context.parent.nextChild(this.context.index + dir, dir, 0, 4 /* Side.DontCare */);
892
+ }
893
+ get nextSibling() {
894
+ let { buffer } = this.context;
895
+ let after = buffer.buffer[this.index + 3];
896
+ if (after < (this._parent ? buffer.buffer[this._parent.index + 3] : buffer.buffer.length))
897
+ return new BufferNode(this.context, this._parent, after);
898
+ return this.externalSibling(1);
899
+ }
900
+ get prevSibling() {
901
+ let { buffer } = this.context;
902
+ let parentStart = this._parent ? this._parent.index + 4 : 0;
903
+ if (this.index == parentStart)
904
+ return this.externalSibling(-1);
905
+ return new BufferNode(this.context, this._parent, buffer.findChild(parentStart, this.index, -1, 0, 4 /* Side.DontCare */));
906
+ }
907
+ get tree() { return null; }
908
+ toTree() {
909
+ let children = [], positions = [];
910
+ let { buffer } = this.context;
911
+ let startI = this.index + 4, endI = buffer.buffer[this.index + 3];
912
+ if (endI > startI) {
913
+ let from = buffer.buffer[this.index + 1];
914
+ children.push(buffer.slice(startI, endI, from));
915
+ positions.push(0);
916
+ }
917
+ return new Tree(this.type, children, positions, this.to - this.from);
918
+ }
919
+ /**
920
+ @internal
921
+ */
922
+ toString() { return this.context.buffer.childString(this.index); }
923
+ }
924
+ function iterStack(heads) {
925
+ if (!heads.length)
926
+ return null;
927
+ let pick = 0, picked = heads[0];
928
+ for (let i = 1; i < heads.length; i++) {
929
+ let node = heads[i];
930
+ if (node.from > picked.from || node.to < picked.to) {
931
+ picked = node;
932
+ pick = i;
933
+ }
934
+ }
935
+ let next = picked instanceof TreeNode && picked.index < 0 ? null : picked.parent;
936
+ let newHeads = heads.slice();
937
+ if (next)
938
+ newHeads[pick] = next;
939
+ else
940
+ newHeads.splice(pick, 1);
941
+ return new StackIterator(newHeads, picked);
942
+ }
943
+ class StackIterator {
944
+ constructor(heads, node) {
945
+ this.heads = heads;
946
+ this.node = node;
947
+ }
948
+ get next() { return iterStack(this.heads); }
949
+ }
950
+ function stackIterator(tree, pos, side) {
951
+ let inner = tree.resolveInner(pos, side), layers = null;
952
+ for (let scan = inner instanceof TreeNode ? inner : inner.context.parent; scan; scan = scan.parent) {
953
+ if (scan.index < 0) { // This is an overlay root
954
+ let parent = scan.parent;
955
+ (layers || (layers = [inner])).push(parent.resolve(pos, side));
956
+ scan = parent;
957
+ }
958
+ else {
959
+ let mount = MountedTree.get(scan.tree);
960
+ // Relevant overlay branching off
961
+ if (mount && mount.overlay && mount.overlay[0].from <= pos && mount.overlay[mount.overlay.length - 1].to >= pos) {
962
+ let root = new TreeNode(mount.tree, mount.overlay[0].from + scan.from, -1, scan);
963
+ (layers || (layers = [inner])).push(resolveNode(root, pos, side, false));
964
+ }
965
+ }
966
+ }
967
+ return layers ? iterStack(layers) : inner;
968
+ }
969
+ /**
970
+ A tree cursor object focuses on a given node in a syntax tree, and
971
+ allows you to move to adjacent nodes.
972
+ */
973
+ class TreeCursor {
974
+ /**
975
+ Shorthand for `.type.name`.
976
+ */
977
+ get name() { return this.type.name; }
978
+ /**
979
+ @internal
980
+ */
981
+ constructor(node, mode = 0) {
982
+ /**
983
+ @internal
984
+ */
985
+ this.buffer = null;
986
+ this.stack = [];
987
+ /**
988
+ @internal
989
+ */
990
+ this.index = 0;
991
+ this.bufferNode = null;
992
+ this.mode = mode & ~IterMode.EnterBracketed;
993
+ if (node instanceof TreeNode) {
994
+ this.yieldNode(node);
995
+ }
996
+ else {
997
+ this._tree = node.context.parent;
998
+ this.buffer = node.context;
999
+ for (let n = node._parent; n; n = n._parent)
1000
+ this.stack.unshift(n.index);
1001
+ this.bufferNode = node;
1002
+ this.yieldBuf(node.index);
1003
+ }
1004
+ }
1005
+ yieldNode(node) {
1006
+ if (!node)
1007
+ return false;
1008
+ this._tree = node;
1009
+ this.type = node.type;
1010
+ this.from = node.from;
1011
+ this.to = node.to;
1012
+ return true;
1013
+ }
1014
+ yieldBuf(index, type) {
1015
+ this.index = index;
1016
+ let { start, buffer } = this.buffer;
1017
+ this.type = type || buffer.set.types[buffer.buffer[index]];
1018
+ this.from = start + buffer.buffer[index + 1];
1019
+ this.to = start + buffer.buffer[index + 2];
1020
+ return true;
1021
+ }
1022
+ /**
1023
+ @internal
1024
+ */
1025
+ yield(node) {
1026
+ if (!node)
1027
+ return false;
1028
+ if (node instanceof TreeNode) {
1029
+ this.buffer = null;
1030
+ return this.yieldNode(node);
1031
+ }
1032
+ this.buffer = node.context;
1033
+ return this.yieldBuf(node.index, node.type);
1034
+ }
1035
+ /**
1036
+ @internal
1037
+ */
1038
+ toString() {
1039
+ return this.buffer ? this.buffer.buffer.childString(this.index) : this._tree.toString();
1040
+ }
1041
+ /**
1042
+ @internal
1043
+ */
1044
+ enterChild(dir, pos, side) {
1045
+ if (!this.buffer)
1046
+ return this.yield(this._tree.nextChild(dir < 0 ? this._tree._tree.children.length - 1 : 0, dir, pos, side, this.mode));
1047
+ let { buffer } = this.buffer;
1048
+ let index = buffer.findChild(this.index + 4, buffer.buffer[this.index + 3], dir, pos - this.buffer.start, side);
1049
+ if (index < 0)
1050
+ return false;
1051
+ this.stack.push(this.index);
1052
+ return this.yieldBuf(index);
1053
+ }
1054
+ /**
1055
+ Move the cursor to this node's first child. When this returns
1056
+ false, the node has no child, and the cursor has not been moved.
1057
+ */
1058
+ firstChild() { return this.enterChild(1, 0, 4 /* Side.DontCare */); }
1059
+ /**
1060
+ Move the cursor to this node's last child.
1061
+ */
1062
+ lastChild() { return this.enterChild(-1, 0, 4 /* Side.DontCare */); }
1063
+ /**
1064
+ Move the cursor to the first child that ends after `pos`.
1065
+ */
1066
+ childAfter(pos) { return this.enterChild(1, pos, 2 /* Side.After */); }
1067
+ /**
1068
+ Move to the last child that starts before `pos`.
1069
+ */
1070
+ childBefore(pos) { return this.enterChild(-1, pos, -2 /* Side.Before */); }
1071
+ /**
1072
+ Move the cursor to the child around `pos`. If side is -1 the
1073
+ child may end at that position, when 1 it may start there. This
1074
+ will also enter [overlaid](#common.MountedTree.overlay)
1075
+ [mounted](#common.NodeProp^mounted) trees unless `overlays` is
1076
+ set to false.
1077
+ */
1078
+ enter(pos, side, mode = this.mode) {
1079
+ if (!this.buffer)
1080
+ return this.yield(this._tree.enter(pos, side, mode));
1081
+ return mode & IterMode.ExcludeBuffers ? false : this.enterChild(1, pos, side);
1082
+ }
1083
+ /**
1084
+ Move to the node's parent node, if this isn't the top node.
1085
+ */
1086
+ parent() {
1087
+ if (!this.buffer)
1088
+ return this.yieldNode((this.mode & IterMode.IncludeAnonymous) ? this._tree._parent : this._tree.parent);
1089
+ if (this.stack.length)
1090
+ return this.yieldBuf(this.stack.pop());
1091
+ let parent = (this.mode & IterMode.IncludeAnonymous) ? this.buffer.parent : this.buffer.parent.nextSignificantParent();
1092
+ this.buffer = null;
1093
+ return this.yieldNode(parent);
1094
+ }
1095
+ /**
1096
+ @internal
1097
+ */
1098
+ sibling(dir) {
1099
+ if (!this.buffer)
1100
+ return !this._tree._parent ? false
1101
+ : this.yield(this._tree.index < 0 ? null
1102
+ : this._tree._parent.nextChild(this._tree.index + dir, dir, 0, 4 /* Side.DontCare */, this.mode));
1103
+ let { buffer } = this.buffer, d = this.stack.length - 1;
1104
+ if (dir < 0) {
1105
+ let parentStart = d < 0 ? 0 : this.stack[d] + 4;
1106
+ if (this.index != parentStart)
1107
+ return this.yieldBuf(buffer.findChild(parentStart, this.index, -1, 0, 4 /* Side.DontCare */));
1108
+ }
1109
+ else {
1110
+ let after = buffer.buffer[this.index + 3];
1111
+ if (after < (d < 0 ? buffer.buffer.length : buffer.buffer[this.stack[d] + 3]))
1112
+ return this.yieldBuf(after);
1113
+ }
1114
+ return d < 0 ? this.yield(this.buffer.parent.nextChild(this.buffer.index + dir, dir, 0, 4 /* Side.DontCare */, this.mode)) : false;
1115
+ }
1116
+ /**
1117
+ Move to this node's next sibling, if any.
1118
+ */
1119
+ nextSibling() { return this.sibling(1); }
1120
+ /**
1121
+ Move to this node's previous sibling, if any.
1122
+ */
1123
+ prevSibling() { return this.sibling(-1); }
1124
+ atLastNode(dir) {
1125
+ let index, parent, { buffer } = this;
1126
+ if (buffer) {
1127
+ if (dir > 0) {
1128
+ if (this.index < buffer.buffer.buffer.length)
1129
+ return false;
1130
+ }
1131
+ else {
1132
+ for (let i = 0; i < this.index; i++)
1133
+ if (buffer.buffer.buffer[i + 3] < this.index)
1134
+ return false;
1135
+ }
1136
+ ({ index, parent } = buffer);
1137
+ }
1138
+ else {
1139
+ ({ index, _parent: parent } = this._tree);
1140
+ }
1141
+ for (; parent; { index, _parent: parent } = parent) {
1142
+ if (index > -1)
1143
+ for (let i = index + dir, e = dir < 0 ? -1 : parent._tree.children.length; i != e; i += dir) {
1144
+ let child = parent._tree.children[i];
1145
+ if ((this.mode & IterMode.IncludeAnonymous) ||
1146
+ child instanceof TreeBuffer ||
1147
+ !child.type.isAnonymous ||
1148
+ hasChild(child))
1149
+ return false;
1150
+ }
1151
+ }
1152
+ return true;
1153
+ }
1154
+ move(dir, enter) {
1155
+ if (enter && this.enterChild(dir, 0, 4 /* Side.DontCare */))
1156
+ return true;
1157
+ for (;;) {
1158
+ if (this.sibling(dir))
1159
+ return true;
1160
+ if (this.atLastNode(dir) || !this.parent())
1161
+ return false;
1162
+ }
1163
+ }
1164
+ /**
1165
+ Move to the next node in a
1166
+ [pre-order](https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR)
1167
+ traversal, going from a node to its first child or, if the
1168
+ current node is empty or `enter` is false, its next sibling or
1169
+ the next sibling of the first parent node that has one.
1170
+ */
1171
+ next(enter = true) { return this.move(1, enter); }
1172
+ /**
1173
+ Move to the next node in a last-to-first pre-order traversal. A
1174
+ node is followed by its last child or, if it has none, its
1175
+ previous sibling or the previous sibling of the first parent
1176
+ node that has one.
1177
+ */
1178
+ prev(enter = true) { return this.move(-1, enter); }
1179
+ /**
1180
+ Move the cursor to the innermost node that covers `pos`. If
1181
+ `side` is -1, it will enter nodes that end at `pos`. If it is 1,
1182
+ it will enter nodes that start at `pos`.
1183
+ */
1184
+ moveTo(pos, side = 0) {
1185
+ // Move up to a node that actually holds the position, if possible
1186
+ while (this.from == this.to ||
1187
+ (side < 1 ? this.from >= pos : this.from > pos) ||
1188
+ (side > -1 ? this.to <= pos : this.to < pos))
1189
+ if (!this.parent())
1190
+ break;
1191
+ // Then scan down into child nodes as far as possible
1192
+ while (this.enterChild(1, pos, side)) { }
1193
+ return this;
1194
+ }
1195
+ /**
1196
+ Get a [syntax node](#common.SyntaxNode) at the cursor's current
1197
+ position.
1198
+ */
1199
+ get node() {
1200
+ if (!this.buffer)
1201
+ return this._tree;
1202
+ let cache = this.bufferNode, result = null, depth = 0;
1203
+ if (cache && cache.context == this.buffer) {
1204
+ scan: for (let index = this.index, d = this.stack.length; d >= 0;) {
1205
+ for (let c = cache; c; c = c._parent)
1206
+ if (c.index == index) {
1207
+ if (index == this.index)
1208
+ return c;
1209
+ result = c;
1210
+ depth = d + 1;
1211
+ break scan;
1212
+ }
1213
+ index = this.stack[--d];
1214
+ }
1215
+ }
1216
+ for (let i = depth; i < this.stack.length; i++)
1217
+ result = new BufferNode(this.buffer, result, this.stack[i]);
1218
+ return this.bufferNode = new BufferNode(this.buffer, result, this.index);
1219
+ }
1220
+ /**
1221
+ Get the [tree](#common.Tree) that represents the current node, if
1222
+ any. Will return null when the node is in a [tree
1223
+ buffer](#common.TreeBuffer).
1224
+ */
1225
+ get tree() {
1226
+ return this.buffer ? null : this._tree._tree;
1227
+ }
1228
+ /**
1229
+ Iterate over the current node and all its descendants, calling
1230
+ `enter` when entering a node and `leave`, if given, when leaving
1231
+ one. When `enter` returns `false`, any children of that node are
1232
+ skipped, and `leave` isn't called for it.
1233
+ */
1234
+ iterate(enter, leave) {
1235
+ for (let depth = 0;;) {
1236
+ let mustLeave = false;
1237
+ if (this.type.isAnonymous || enter(this) !== false) {
1238
+ if (this.firstChild()) {
1239
+ depth++;
1240
+ continue;
1241
+ }
1242
+ if (!this.type.isAnonymous)
1243
+ mustLeave = true;
1244
+ }
1245
+ for (;;) {
1246
+ if (mustLeave && leave)
1247
+ leave(this);
1248
+ mustLeave = this.type.isAnonymous;
1249
+ if (!depth)
1250
+ return;
1251
+ if (this.nextSibling())
1252
+ break;
1253
+ this.parent();
1254
+ depth--;
1255
+ mustLeave = true;
1256
+ }
1257
+ }
1258
+ }
1259
+ /**
1260
+ Test whether the current node matches a given context—a sequence
1261
+ of direct parent node names. Empty strings in the context array
1262
+ are treated as wildcards.
1263
+ */
1264
+ matchContext(context) {
1265
+ if (!this.buffer)
1266
+ return matchNodeContext(this.node.parent, context);
1267
+ let { buffer } = this.buffer, { types } = buffer.set;
1268
+ for (let i = context.length - 1, d = this.stack.length - 1; i >= 0; d--) {
1269
+ if (d < 0)
1270
+ return matchNodeContext(this._tree, context, i);
1271
+ let type = types[buffer.buffer[this.stack[d]]];
1272
+ if (!type.isAnonymous) {
1273
+ if (context[i] && context[i] != type.name)
1274
+ return false;
1275
+ i--;
1276
+ }
1277
+ }
1278
+ return true;
1279
+ }
1280
+ }
1281
+ function hasChild(tree) {
1282
+ return tree.children.some(ch => ch instanceof TreeBuffer || !ch.type.isAnonymous || hasChild(ch));
1283
+ }
1284
+ function buildTree(data) {
1285
+ var _a;
1286
+ let { buffer, nodeSet, maxBufferLength = DefaultBufferLength, reused = [], minRepeatType = nodeSet.types.length } = data;
1287
+ let cursor = Array.isArray(buffer) ? new FlatBufferCursor(buffer, buffer.length) : buffer;
1288
+ let types = nodeSet.types;
1289
+ let contextHash = 0, lookAhead = 0;
1290
+ function takeNode(parentStart, minPos, children, positions, inRepeat, depth) {
1291
+ let { id, start, end, size } = cursor;
1292
+ let lookAheadAtStart = lookAhead, contextAtStart = contextHash;
1293
+ if (size < 0) {
1294
+ cursor.next();
1295
+ if (size == -1 /* SpecialRecord.Reuse */) {
1296
+ let node = reused[id];
1297
+ children.push(node);
1298
+ positions.push(start - parentStart);
1299
+ return;
1300
+ }
1301
+ else if (size == -3 /* SpecialRecord.ContextChange */) { // Context change
1302
+ contextHash = id;
1303
+ return;
1304
+ }
1305
+ else if (size == -4 /* SpecialRecord.LookAhead */) {
1306
+ lookAhead = id;
1307
+ return;
1308
+ }
1309
+ else {
1310
+ throw new RangeError(`Unrecognized record size: ${size}`);
1311
+ }
1312
+ }
1313
+ let type = types[id], node, buffer;
1314
+ let startPos = start - parentStart;
1315
+ if (end - start <= maxBufferLength && (buffer = findBufferSize(cursor.pos - minPos, inRepeat))) {
1316
+ // Small enough for a buffer, and no reused nodes inside
1317
+ let data = new Uint16Array(buffer.size - buffer.skip);
1318
+ let endPos = cursor.pos - buffer.size, index = data.length;
1319
+ while (cursor.pos > endPos)
1320
+ index = copyToBuffer(buffer.start, data, index);
1321
+ node = new TreeBuffer(data, end - buffer.start, nodeSet);
1322
+ startPos = buffer.start - parentStart;
1323
+ }
1324
+ else { // Make it a node
1325
+ let endPos = cursor.pos - size;
1326
+ cursor.next();
1327
+ let localChildren = [], localPositions = [];
1328
+ let localInRepeat = id >= minRepeatType ? id : -1;
1329
+ let lastGroup = 0, lastEnd = end;
1330
+ while (cursor.pos > endPos) {
1331
+ if (localInRepeat >= 0 && cursor.id == localInRepeat && cursor.size >= 0) {
1332
+ if (cursor.end <= lastEnd - maxBufferLength) {
1333
+ makeRepeatLeaf(localChildren, localPositions, start, lastGroup, cursor.end, lastEnd, localInRepeat, lookAheadAtStart, contextAtStart);
1334
+ lastGroup = localChildren.length;
1335
+ lastEnd = cursor.end;
1336
+ }
1337
+ cursor.next();
1338
+ }
1339
+ else if (depth > 2500 /* CutOff.Depth */) {
1340
+ takeFlatNode(start, endPos, localChildren, localPositions);
1341
+ }
1342
+ else {
1343
+ takeNode(start, endPos, localChildren, localPositions, localInRepeat, depth + 1);
1344
+ }
1345
+ }
1346
+ if (localInRepeat >= 0 && lastGroup > 0 && lastGroup < localChildren.length)
1347
+ makeRepeatLeaf(localChildren, localPositions, start, lastGroup, start, lastEnd, localInRepeat, lookAheadAtStart, contextAtStart);
1348
+ localChildren.reverse();
1349
+ localPositions.reverse();
1350
+ if (localInRepeat > -1 && lastGroup > 0) {
1351
+ let make = makeBalanced(type, contextAtStart);
1352
+ node = balanceRange(type, localChildren, localPositions, 0, localChildren.length, 0, end - start, make, make);
1353
+ }
1354
+ else {
1355
+ node = makeTree(type, localChildren, localPositions, end - start, lookAheadAtStart - end, contextAtStart);
1356
+ }
1357
+ }
1358
+ children.push(node);
1359
+ positions.push(startPos);
1360
+ }
1361
+ function takeFlatNode(parentStart, minPos, children, positions) {
1362
+ let nodes = []; // Temporary, inverted array of leaf nodes found, with absolute positions
1363
+ let nodeCount = 0, stopAt = -1;
1364
+ while (cursor.pos > minPos) {
1365
+ let { id, start, end, size } = cursor;
1366
+ if (size > 4) { // Not a leaf
1367
+ cursor.next();
1368
+ }
1369
+ else if (stopAt > -1 && start < stopAt) {
1370
+ break;
1371
+ }
1372
+ else {
1373
+ if (stopAt < 0)
1374
+ stopAt = end - maxBufferLength;
1375
+ nodes.push(id, start, end);
1376
+ nodeCount++;
1377
+ cursor.next();
1378
+ }
1379
+ }
1380
+ if (nodeCount) {
1381
+ let buffer = new Uint16Array(nodeCount * 4);
1382
+ let start = nodes[nodes.length - 2];
1383
+ for (let i = nodes.length - 3, j = 0; i >= 0; i -= 3) {
1384
+ buffer[j++] = nodes[i];
1385
+ buffer[j++] = nodes[i + 1] - start;
1386
+ buffer[j++] = nodes[i + 2] - start;
1387
+ buffer[j++] = j;
1388
+ }
1389
+ children.push(new TreeBuffer(buffer, nodes[2] - start, nodeSet));
1390
+ positions.push(start - parentStart);
1391
+ }
1392
+ }
1393
+ function makeBalanced(type, contextHash) {
1394
+ return (children, positions, length) => {
1395
+ let lookAhead = 0, lastI = children.length - 1, last, lookAheadProp;
1396
+ if (lastI >= 0 && (last = children[lastI]) instanceof Tree) {
1397
+ if (!lastI && last.type == type && last.length == length)
1398
+ return last;
1399
+ if (lookAheadProp = last.prop(NodeProp.lookAhead))
1400
+ lookAhead = positions[lastI] + last.length + lookAheadProp;
1401
+ }
1402
+ return makeTree(type, children, positions, length, lookAhead, contextHash);
1403
+ };
1404
+ }
1405
+ function makeRepeatLeaf(children, positions, base, i, from, to, type, lookAhead, contextHash) {
1406
+ let localChildren = [], localPositions = [];
1407
+ while (children.length > i) {
1408
+ localChildren.push(children.pop());
1409
+ localPositions.push(positions.pop() + base - from);
1410
+ }
1411
+ children.push(makeTree(nodeSet.types[type], localChildren, localPositions, to - from, lookAhead - to, contextHash));
1412
+ positions.push(from - base);
1413
+ }
1414
+ function makeTree(type, children, positions, length, lookAhead, contextHash, props) {
1415
+ if (contextHash) {
1416
+ let pair = [NodeProp.contextHash, contextHash];
1417
+ props = props ? [pair].concat(props) : [pair];
1418
+ }
1419
+ if (lookAhead > 25) {
1420
+ let pair = [NodeProp.lookAhead, lookAhead];
1421
+ props = props ? [pair].concat(props) : [pair];
1422
+ }
1423
+ return new Tree(type, children, positions, length, props);
1424
+ }
1425
+ function findBufferSize(maxSize, inRepeat) {
1426
+ // Scan through the buffer to find previous siblings that fit
1427
+ // together in a TreeBuffer, and don't contain any reused nodes
1428
+ // (which can't be stored in a buffer).
1429
+ // If `inRepeat` is > -1, ignore node boundaries of that type for
1430
+ // nesting, but make sure the end falls either at the start
1431
+ // (`maxSize`) or before such a node.
1432
+ let fork = cursor.fork();
1433
+ let size = 0, start = 0, skip = 0, minStart = fork.end - maxBufferLength;
1434
+ let result = { size: 0, start: 0, skip: 0 };
1435
+ scan: for (let minPos = fork.pos - maxSize; fork.pos > minPos;) {
1436
+ let nodeSize = fork.size;
1437
+ // Pretend nested repeat nodes of the same type don't exist
1438
+ if (fork.id == inRepeat && nodeSize >= 0) {
1439
+ // Except that we store the current state as a valid return
1440
+ // value.
1441
+ result.size = size;
1442
+ result.start = start;
1443
+ result.skip = skip;
1444
+ skip += 4;
1445
+ size += 4;
1446
+ fork.next();
1447
+ continue;
1448
+ }
1449
+ let startPos = fork.pos - nodeSize;
1450
+ if (nodeSize < 0 || startPos < minPos || fork.start < minStart)
1451
+ break;
1452
+ let localSkipped = fork.id >= minRepeatType ? 4 : 0;
1453
+ let nodeStart = fork.start;
1454
+ fork.next();
1455
+ while (fork.pos > startPos) {
1456
+ if (fork.size < 0) {
1457
+ if (fork.size == -3 /* SpecialRecord.ContextChange */ || fork.size == -4 /* SpecialRecord.LookAhead */)
1458
+ localSkipped += 4;
1459
+ else
1460
+ break scan;
1461
+ }
1462
+ else if (fork.id >= minRepeatType) {
1463
+ localSkipped += 4;
1464
+ }
1465
+ fork.next();
1466
+ }
1467
+ start = nodeStart;
1468
+ size += nodeSize;
1469
+ skip += localSkipped;
1470
+ }
1471
+ if (inRepeat < 0 || size == maxSize) {
1472
+ result.size = size;
1473
+ result.start = start;
1474
+ result.skip = skip;
1475
+ }
1476
+ return result.size > 4 ? result : undefined;
1477
+ }
1478
+ function copyToBuffer(bufferStart, buffer, index) {
1479
+ let { id, start, end, size } = cursor;
1480
+ cursor.next();
1481
+ if (size >= 0 && id < minRepeatType) {
1482
+ let startIndex = index;
1483
+ if (size > 4) {
1484
+ let endPos = cursor.pos - (size - 4);
1485
+ while (cursor.pos > endPos)
1486
+ index = copyToBuffer(bufferStart, buffer, index);
1487
+ }
1488
+ buffer[--index] = startIndex;
1489
+ buffer[--index] = end - bufferStart;
1490
+ buffer[--index] = start - bufferStart;
1491
+ buffer[--index] = id;
1492
+ }
1493
+ else if (size == -3 /* SpecialRecord.ContextChange */) {
1494
+ contextHash = id;
1495
+ }
1496
+ else if (size == -4 /* SpecialRecord.LookAhead */) {
1497
+ lookAhead = id;
1498
+ }
1499
+ return index;
1500
+ }
1501
+ let children = [], positions = [];
1502
+ while (cursor.pos > 0)
1503
+ takeNode(data.start || 0, data.bufferStart || 0, children, positions, -1, 0);
1504
+ let length = (_a = data.length) !== null && _a !== void 0 ? _a : (children.length ? positions[0] + children[0].length : 0);
1505
+ return new Tree(types[data.topID], children.reverse(), positions.reverse(), length);
1506
+ }
1507
+ const nodeSizeCache = new WeakMap;
1508
+ function nodeSize(balanceType, node) {
1509
+ if (!balanceType.isAnonymous || node instanceof TreeBuffer || node.type != balanceType)
1510
+ return 1;
1511
+ let size = nodeSizeCache.get(node);
1512
+ if (size == null) {
1513
+ size = 1;
1514
+ for (let child of node.children) {
1515
+ if (child.type != balanceType || !(child instanceof Tree)) {
1516
+ size = 1;
1517
+ break;
1518
+ }
1519
+ size += nodeSize(balanceType, child);
1520
+ }
1521
+ nodeSizeCache.set(node, size);
1522
+ }
1523
+ return size;
1524
+ }
1525
+ function balanceRange(
1526
+ // The type the balanced tree's inner nodes.
1527
+ balanceType,
1528
+ // The direct children and their positions
1529
+ children, positions,
1530
+ // The index range in children/positions to use
1531
+ from, to,
1532
+ // The start position of the nodes, relative to their parent.
1533
+ start,
1534
+ // Length of the outer node
1535
+ length,
1536
+ // Function to build the top node of the balanced tree
1537
+ mkTop,
1538
+ // Function to build internal nodes for the balanced tree
1539
+ mkTree) {
1540
+ let total = 0;
1541
+ for (let i = from; i < to; i++)
1542
+ total += nodeSize(balanceType, children[i]);
1543
+ let maxChild = Math.ceil((total * 1.5) / 8 /* Balance.BranchFactor */);
1544
+ let localChildren = [], localPositions = [];
1545
+ function divide(children, positions, from, to, offset) {
1546
+ for (let i = from; i < to;) {
1547
+ let groupFrom = i, groupStart = positions[i], groupSize = nodeSize(balanceType, children[i]);
1548
+ i++;
1549
+ for (; i < to; i++) {
1550
+ let nextSize = nodeSize(balanceType, children[i]);
1551
+ if (groupSize + nextSize >= maxChild)
1552
+ break;
1553
+ groupSize += nextSize;
1554
+ }
1555
+ if (i == groupFrom + 1) {
1556
+ if (groupSize > maxChild) {
1557
+ let only = children[groupFrom]; // Only trees can have a size > 1
1558
+ divide(only.children, only.positions, 0, only.children.length, positions[groupFrom] + offset);
1559
+ continue;
1560
+ }
1561
+ localChildren.push(children[groupFrom]);
1562
+ }
1563
+ else {
1564
+ let length = positions[i - 1] + children[i - 1].length - groupStart;
1565
+ localChildren.push(balanceRange(balanceType, children, positions, groupFrom, i, groupStart, length, null, mkTree));
1566
+ }
1567
+ localPositions.push(groupStart + offset - start);
1568
+ }
1569
+ }
1570
+ divide(children, positions, from, to, 0);
1571
+ return (mkTop || mkTree)(localChildren, localPositions, length);
1572
+ }
1573
+ /**
1574
+ Provides a way to associate values with pieces of trees. As long
1575
+ as that part of the tree is reused, the associated values can be
1576
+ retrieved from an updated tree.
1577
+ */
1578
+ class NodeWeakMap {
1579
+ constructor() {
1580
+ this.map = new WeakMap();
1581
+ }
1582
+ setBuffer(buffer, index, value) {
1583
+ let inner = this.map.get(buffer);
1584
+ if (!inner)
1585
+ this.map.set(buffer, inner = new Map);
1586
+ inner.set(index, value);
1587
+ }
1588
+ getBuffer(buffer, index) {
1589
+ let inner = this.map.get(buffer);
1590
+ return inner && inner.get(index);
1591
+ }
1592
+ /**
1593
+ Set the value for this syntax node.
1594
+ */
1595
+ set(node, value) {
1596
+ if (node instanceof BufferNode)
1597
+ this.setBuffer(node.context.buffer, node.index, value);
1598
+ else if (node instanceof TreeNode)
1599
+ this.map.set(node.tree, value);
1600
+ }
1601
+ /**
1602
+ Retrieve value for this syntax node, if it exists in the map.
1603
+ */
1604
+ get(node) {
1605
+ return node instanceof BufferNode ? this.getBuffer(node.context.buffer, node.index)
1606
+ : node instanceof TreeNode ? this.map.get(node.tree) : undefined;
1607
+ }
1608
+ /**
1609
+ Set the value for the node that a cursor currently points to.
1610
+ */
1611
+ cursorSet(cursor, value) {
1612
+ if (cursor.buffer)
1613
+ this.setBuffer(cursor.buffer.buffer, cursor.index, value);
1614
+ else
1615
+ this.map.set(cursor.tree, value);
1616
+ }
1617
+ /**
1618
+ Retrieve the value for the node that a cursor currently points
1619
+ to.
1620
+ */
1621
+ cursorGet(cursor) {
1622
+ return cursor.buffer ? this.getBuffer(cursor.buffer.buffer, cursor.index) : this.map.get(cursor.tree);
1623
+ }
1624
+ }
1625
+
1626
+ /**
1627
+ Tree fragments are used during [incremental
1628
+ parsing](#common.Parser.startParse) to track parts of old trees
1629
+ that can be reused in a new parse. An array of fragments is used
1630
+ to track regions of an old tree whose nodes might be reused in new
1631
+ parses. Use the static
1632
+ [`applyChanges`](#common.TreeFragment^applyChanges) method to
1633
+ update fragments for document changes.
1634
+ */
1635
+ class TreeFragment {
1636
+ /**
1637
+ Construct a tree fragment. You'll usually want to use
1638
+ [`addTree`](#common.TreeFragment^addTree) and
1639
+ [`applyChanges`](#common.TreeFragment^applyChanges) instead of
1640
+ calling this directly.
1641
+ */
1642
+ constructor(
1643
+ /**
1644
+ The start of the unchanged range pointed to by this fragment.
1645
+ This refers to an offset in the _updated_ document (as opposed
1646
+ to the original tree).
1647
+ */
1648
+ from,
1649
+ /**
1650
+ The end of the unchanged range.
1651
+ */
1652
+ to,
1653
+ /**
1654
+ The tree that this fragment is based on.
1655
+ */
1656
+ tree,
1657
+ /**
1658
+ The offset between the fragment's tree and the document that
1659
+ this fragment can be used against. Add this when going from
1660
+ document to tree positions, subtract it to go from tree to
1661
+ document positions.
1662
+ */
1663
+ offset, openStart = false, openEnd = false) {
1664
+ this.from = from;
1665
+ this.to = to;
1666
+ this.tree = tree;
1667
+ this.offset = offset;
1668
+ this.open = (openStart ? 1 /* Open.Start */ : 0) | (openEnd ? 2 /* Open.End */ : 0);
1669
+ }
1670
+ /**
1671
+ Whether the start of the fragment represents the start of a
1672
+ parse, or the end of a change. (In the second case, it may not
1673
+ be safe to reuse some nodes at the start, depending on the
1674
+ parsing algorithm.)
1675
+ */
1676
+ get openStart() { return (this.open & 1 /* Open.Start */) > 0; }
1677
+ /**
1678
+ Whether the end of the fragment represents the end of a
1679
+ full-document parse, or the start of a change.
1680
+ */
1681
+ get openEnd() { return (this.open & 2 /* Open.End */) > 0; }
1682
+ /**
1683
+ Create a set of fragments from a freshly parsed tree, or update
1684
+ an existing set of fragments by replacing the ones that overlap
1685
+ with a tree with content from the new tree. When `partial` is
1686
+ true, the parse is treated as incomplete, and the resulting
1687
+ fragment has [`openEnd`](#common.TreeFragment.openEnd) set to
1688
+ true.
1689
+ */
1690
+ static addTree(tree, fragments = [], partial = false) {
1691
+ let result = [new TreeFragment(0, tree.length, tree, 0, false, partial)];
1692
+ for (let f of fragments)
1693
+ if (f.to > tree.length)
1694
+ result.push(f);
1695
+ return result;
1696
+ }
1697
+ /**
1698
+ Apply a set of edits to an array of fragments, removing or
1699
+ splitting fragments as necessary to remove edited ranges, and
1700
+ adjusting offsets for fragments that moved.
1701
+ */
1702
+ static applyChanges(fragments, changes, minGap = 128) {
1703
+ if (!changes.length)
1704
+ return fragments;
1705
+ let result = [];
1706
+ let fI = 1, nextF = fragments.length ? fragments[0] : null;
1707
+ for (let cI = 0, pos = 0, off = 0;; cI++) {
1708
+ let nextC = cI < changes.length ? changes[cI] : null;
1709
+ let nextPos = nextC ? nextC.fromA : 1e9;
1710
+ if (nextPos - pos >= minGap)
1711
+ while (nextF && nextF.from < nextPos) {
1712
+ let cut = nextF;
1713
+ if (pos >= cut.from || nextPos <= cut.to || off) {
1714
+ let fFrom = Math.max(cut.from, pos) - off, fTo = Math.min(cut.to, nextPos) - off;
1715
+ cut = fFrom >= fTo ? null : new TreeFragment(fFrom, fTo, cut.tree, cut.offset + off, cI > 0, !!nextC);
1716
+ }
1717
+ if (cut)
1718
+ result.push(cut);
1719
+ if (nextF.to > nextPos)
1720
+ break;
1721
+ nextF = fI < fragments.length ? fragments[fI++] : null;
1722
+ }
1723
+ if (!nextC)
1724
+ break;
1725
+ pos = nextC.toA;
1726
+ off = nextC.toA - nextC.toB;
1727
+ }
1728
+ return result;
1729
+ }
1730
+ }
1731
+ /**
1732
+ A superclass that parsers should extend.
1733
+ */
1734
+ class Parser {
1735
+ /**
1736
+ Start a parse, returning a [partial parse](#common.PartialParse)
1737
+ object. [`fragments`](#common.TreeFragment) can be passed in to
1738
+ make the parse incremental.
1739
+
1740
+ By default, the entire input is parsed. You can pass `ranges`,
1741
+ which should be a sorted array of non-empty, non-overlapping
1742
+ ranges, to parse only those ranges. The tree returned in that
1743
+ case will start at `ranges[0].from`.
1744
+ */
1745
+ startParse(input, fragments, ranges) {
1746
+ if (typeof input == "string")
1747
+ input = new StringInput(input);
1748
+ ranges = !ranges ? [new Range(0, input.length)] : ranges.length ? ranges.map(r => new Range(r.from, r.to)) : [new Range(0, 0)];
1749
+ return this.createParse(input, fragments || [], ranges);
1750
+ }
1751
+ /**
1752
+ Run a full parse, returning the resulting tree.
1753
+ */
1754
+ parse(input, fragments, ranges) {
1755
+ let parse = this.startParse(input, fragments, ranges);
1756
+ for (;;) {
1757
+ let done = parse.advance();
1758
+ if (done)
1759
+ return done;
1760
+ }
1761
+ }
1762
+ }
1763
+ class StringInput {
1764
+ constructor(string) {
1765
+ this.string = string;
1766
+ }
1767
+ get length() { return this.string.length; }
1768
+ chunk(from) { return this.string.slice(from); }
1769
+ get lineChunks() { return false; }
1770
+ read(from, to) { return this.string.slice(from, to); }
1771
+ }
1772
+
1773
+ /**
1774
+ Create a parse wrapper that, after the inner parse completes,
1775
+ scans its tree for mixed language regions with the `nest`
1776
+ function, runs the resulting [inner parses](#common.NestedParse),
1777
+ and then [mounts](#common.NodeProp^mounted) their results onto the
1778
+ tree.
1779
+ */
1780
+ function parseMixed(nest) {
1781
+ return (parse, input, fragments, ranges) => new MixedParse(parse, nest, input, fragments, ranges);
1782
+ }
1783
+ class InnerParse {
1784
+ constructor(parser, parse, overlay, bracketed, target, from) {
1785
+ this.parser = parser;
1786
+ this.parse = parse;
1787
+ this.overlay = overlay;
1788
+ this.bracketed = bracketed;
1789
+ this.target = target;
1790
+ this.from = from;
1791
+ }
1792
+ }
1793
+ function checkRanges(ranges) {
1794
+ if (!ranges.length || ranges.some(r => r.from >= r.to))
1795
+ throw new RangeError("Invalid inner parse ranges given: " + JSON.stringify(ranges));
1796
+ }
1797
+ class ActiveOverlay {
1798
+ constructor(parser, predicate, mounts, index, start, bracketed, target, prev) {
1799
+ this.parser = parser;
1800
+ this.predicate = predicate;
1801
+ this.mounts = mounts;
1802
+ this.index = index;
1803
+ this.start = start;
1804
+ this.bracketed = bracketed;
1805
+ this.target = target;
1806
+ this.prev = prev;
1807
+ this.depth = 0;
1808
+ this.ranges = [];
1809
+ }
1810
+ }
1811
+ const stoppedInner = new NodeProp({ perNode: true });
1812
+ class MixedParse {
1813
+ constructor(base, nest, input, fragments, ranges) {
1814
+ this.nest = nest;
1815
+ this.input = input;
1816
+ this.fragments = fragments;
1817
+ this.ranges = ranges;
1818
+ this.inner = [];
1819
+ this.innerDone = 0;
1820
+ this.baseTree = null;
1821
+ this.stoppedAt = null;
1822
+ this.baseParse = base;
1823
+ }
1824
+ advance() {
1825
+ if (this.baseParse) {
1826
+ let done = this.baseParse.advance();
1827
+ if (!done)
1828
+ return null;
1829
+ this.baseParse = null;
1830
+ this.baseTree = done;
1831
+ this.startInner();
1832
+ if (this.stoppedAt != null)
1833
+ for (let inner of this.inner)
1834
+ inner.parse.stopAt(this.stoppedAt);
1835
+ }
1836
+ if (this.innerDone == this.inner.length) {
1837
+ let result = this.baseTree;
1838
+ if (this.stoppedAt != null)
1839
+ result = new Tree(result.type, result.children, result.positions, result.length, result.propValues.concat([[stoppedInner, this.stoppedAt]]));
1840
+ return result;
1841
+ }
1842
+ let inner = this.inner[this.innerDone], done = inner.parse.advance();
1843
+ if (done) {
1844
+ this.innerDone++;
1845
+ // This is a somewhat dodgy but super helpful hack where we
1846
+ // patch up nodes created by the inner parse (and thus
1847
+ // presumably not aliased anywhere else) to hold the information
1848
+ // about the inner parse.
1849
+ let props = Object.assign(Object.create(null), inner.target.props);
1850
+ props[NodeProp.mounted.id] = new MountedTree(done, inner.overlay, inner.parser, inner.bracketed);
1851
+ inner.target.props = props;
1852
+ }
1853
+ return null;
1854
+ }
1855
+ get parsedPos() {
1856
+ if (this.baseParse)
1857
+ return 0;
1858
+ let pos = this.input.length;
1859
+ for (let i = this.innerDone; i < this.inner.length; i++) {
1860
+ if (this.inner[i].from < pos)
1861
+ pos = Math.min(pos, this.inner[i].parse.parsedPos);
1862
+ }
1863
+ return pos;
1864
+ }
1865
+ stopAt(pos) {
1866
+ this.stoppedAt = pos;
1867
+ if (this.baseParse)
1868
+ this.baseParse.stopAt(pos);
1869
+ else
1870
+ for (let i = this.innerDone; i < this.inner.length; i++)
1871
+ this.inner[i].parse.stopAt(pos);
1872
+ }
1873
+ startInner() {
1874
+ let fragmentCursor = new FragmentCursor(this.fragments);
1875
+ let overlay = null;
1876
+ let covered = null;
1877
+ let cursor = new TreeCursor(new TreeNode(this.baseTree, this.ranges[0].from, 0, null), IterMode.IncludeAnonymous | IterMode.IgnoreMounts);
1878
+ scan: for (let nest, isCovered;;) {
1879
+ let enter = true, range;
1880
+ if (this.stoppedAt != null && cursor.from >= this.stoppedAt) {
1881
+ enter = false;
1882
+ }
1883
+ else if (fragmentCursor.hasNode(cursor)) {
1884
+ if (overlay) {
1885
+ let match = overlay.mounts.find(m => m.frag.from <= cursor.from && m.frag.to >= cursor.to && m.mount.overlay);
1886
+ if (match)
1887
+ for (let r of match.mount.overlay) {
1888
+ let from = r.from + match.pos, to = r.to + match.pos;
1889
+ if (from >= cursor.from && to <= cursor.to && !overlay.ranges.some(r => r.from < to && r.to > from))
1890
+ overlay.ranges.push({ from, to });
1891
+ }
1892
+ }
1893
+ enter = false;
1894
+ }
1895
+ else if (covered && (isCovered = checkCover(covered.ranges, cursor.from, cursor.to))) {
1896
+ enter = isCovered != 2 /* Cover.Full */;
1897
+ }
1898
+ else if (!cursor.type.isAnonymous && (nest = this.nest(cursor, this.input)) &&
1899
+ (cursor.from < cursor.to || !nest.overlay)) {
1900
+ if (!cursor.tree) {
1901
+ materialize(cursor);
1902
+ // materialize create one more level of nesting
1903
+ // we need to add depth to active overlay for going backwards
1904
+ if (overlay)
1905
+ overlay.depth++;
1906
+ if (covered)
1907
+ covered.depth++;
1908
+ }
1909
+ let oldMounts = fragmentCursor.findMounts(cursor.from, nest.parser);
1910
+ if (typeof nest.overlay == "function") {
1911
+ overlay = new ActiveOverlay(nest.parser, nest.overlay, oldMounts, this.inner.length, cursor.from, !!nest.bracketed, cursor.tree, overlay);
1912
+ }
1913
+ else {
1914
+ let ranges = punchRanges(this.ranges, nest.overlay ||
1915
+ (cursor.from < cursor.to ? [new Range(cursor.from, cursor.to)] : []));
1916
+ if (ranges.length)
1917
+ checkRanges(ranges);
1918
+ if (ranges.length || !nest.overlay)
1919
+ this.inner.push(new InnerParse(nest.parser, ranges.length ? nest.parser.startParse(this.input, enterFragments(oldMounts, ranges), ranges)
1920
+ : nest.parser.startParse(""), nest.overlay ? nest.overlay.map(r => new Range(r.from - cursor.from, r.to - cursor.from)) : null, !!nest.bracketed, cursor.tree, ranges.length ? ranges[0].from : cursor.from));
1921
+ if (!nest.overlay)
1922
+ enter = false;
1923
+ else if (ranges.length)
1924
+ covered = { ranges, depth: 0, prev: covered };
1925
+ }
1926
+ }
1927
+ else if (overlay && (range = overlay.predicate(cursor))) {
1928
+ if (range === true)
1929
+ range = new Range(cursor.from, cursor.to);
1930
+ if (range.from < range.to) {
1931
+ let last = overlay.ranges.length - 1;
1932
+ if (last >= 0 && overlay.ranges[last].to == range.from)
1933
+ overlay.ranges[last] = { from: overlay.ranges[last].from, to: range.to };
1934
+ else
1935
+ overlay.ranges.push(range);
1936
+ }
1937
+ }
1938
+ if (enter && cursor.firstChild()) {
1939
+ if (overlay)
1940
+ overlay.depth++;
1941
+ if (covered)
1942
+ covered.depth++;
1943
+ }
1944
+ else {
1945
+ for (;;) {
1946
+ if (cursor.nextSibling())
1947
+ break;
1948
+ if (!cursor.parent())
1949
+ break scan;
1950
+ if (overlay && !--overlay.depth) {
1951
+ let ranges = punchRanges(this.ranges, overlay.ranges);
1952
+ if (ranges.length) {
1953
+ checkRanges(ranges);
1954
+ this.inner.splice(overlay.index, 0, new InnerParse(overlay.parser, overlay.parser.startParse(this.input, enterFragments(overlay.mounts, ranges), ranges), overlay.ranges.map(r => new Range(r.from - overlay.start, r.to - overlay.start)), overlay.bracketed, overlay.target, ranges[0].from));
1955
+ }
1956
+ overlay = overlay.prev;
1957
+ }
1958
+ if (covered && !--covered.depth)
1959
+ covered = covered.prev;
1960
+ }
1961
+ }
1962
+ }
1963
+ }
1964
+ }
1965
+ function checkCover(covered, from, to) {
1966
+ for (let range of covered) {
1967
+ if (range.from >= to)
1968
+ break;
1969
+ if (range.to > from)
1970
+ return range.from <= from && range.to >= to ? 2 /* Cover.Full */ : 1 /* Cover.Partial */;
1971
+ }
1972
+ return 0 /* Cover.None */;
1973
+ }
1974
+ // Take a piece of buffer and convert it into a stand-alone
1975
+ // TreeBuffer.
1976
+ function sliceBuf(buf, startI, endI, nodes, positions, off) {
1977
+ if (startI < endI) {
1978
+ let from = buf.buffer[startI + 1];
1979
+ nodes.push(buf.slice(startI, endI, from));
1980
+ positions.push(from - off);
1981
+ }
1982
+ }
1983
+ // This function takes a node that's in a buffer, and converts it, and
1984
+ // its parent buffer nodes, into a Tree. This is again acting on the
1985
+ // assumption that the trees and buffers have been constructed by the
1986
+ // parse that was ran via the mix parser, and thus aren't shared with
1987
+ // any other code, making violations of the immutability safe.
1988
+ function materialize(cursor) {
1989
+ let { node } = cursor, stack = [];
1990
+ let buffer = node.context.buffer;
1991
+ // Scan up to the nearest tree
1992
+ do {
1993
+ stack.push(cursor.index);
1994
+ cursor.parent();
1995
+ } while (!cursor.tree);
1996
+ // Find the index of the buffer in that tree
1997
+ let base = cursor.tree, i = base.children.indexOf(buffer);
1998
+ let buf = base.children[i], b = buf.buffer, newStack = [i];
1999
+ // Split a level in the buffer, putting the nodes before and after
2000
+ // the child that contains `node` into new buffers.
2001
+ function split(startI, endI, type, innerOffset, length, stackPos) {
2002
+ let targetI = stack[stackPos];
2003
+ let children = [], positions = [];
2004
+ sliceBuf(buf, startI, targetI, children, positions, innerOffset);
2005
+ let from = b[targetI + 1], to = b[targetI + 2];
2006
+ newStack.push(children.length);
2007
+ let child = stackPos
2008
+ ? split(targetI + 4, b[targetI + 3], buf.set.types[b[targetI]], from, to - from, stackPos - 1)
2009
+ : node.toTree();
2010
+ children.push(child);
2011
+ positions.push(from - innerOffset);
2012
+ sliceBuf(buf, b[targetI + 3], endI, children, positions, innerOffset);
2013
+ return new Tree(type, children, positions, length);
2014
+ }
2015
+ base.children[i] = split(0, b.length, NodeType.none, 0, buf.length, stack.length - 1);
2016
+ // Move the cursor back to the target node
2017
+ for (let index of newStack) {
2018
+ let tree = cursor.tree.children[index], pos = cursor.tree.positions[index];
2019
+ cursor.yield(new TreeNode(tree, pos + cursor.from, index, cursor._tree));
2020
+ }
2021
+ }
2022
+ class StructureCursor {
2023
+ constructor(root, offset) {
2024
+ this.offset = offset;
2025
+ this.done = false;
2026
+ this.cursor = root.cursor(IterMode.IncludeAnonymous | IterMode.IgnoreMounts);
2027
+ }
2028
+ // Move to the first node (in pre-order) that starts at or after `pos`.
2029
+ moveTo(pos) {
2030
+ let { cursor } = this, p = pos - this.offset;
2031
+ while (!this.done && cursor.from < p) {
2032
+ if (cursor.to >= pos && cursor.enter(p, 1, IterMode.IgnoreOverlays | IterMode.ExcludeBuffers)) ;
2033
+ else if (!cursor.next(false))
2034
+ this.done = true;
2035
+ }
2036
+ }
2037
+ hasNode(cursor) {
2038
+ this.moveTo(cursor.from);
2039
+ if (!this.done && this.cursor.from + this.offset == cursor.from && this.cursor.tree) {
2040
+ for (let tree = this.cursor.tree;;) {
2041
+ if (tree == cursor.tree)
2042
+ return true;
2043
+ if (tree.children.length && tree.positions[0] == 0 && tree.children[0] instanceof Tree)
2044
+ tree = tree.children[0];
2045
+ else
2046
+ break;
2047
+ }
2048
+ }
2049
+ return false;
2050
+ }
2051
+ }
2052
+ class FragmentCursor {
2053
+ constructor(fragments) {
2054
+ var _a;
2055
+ this.fragments = fragments;
2056
+ this.curTo = 0;
2057
+ this.fragI = 0;
2058
+ if (fragments.length) {
2059
+ let first = this.curFrag = fragments[0];
2060
+ this.curTo = (_a = first.tree.prop(stoppedInner)) !== null && _a !== void 0 ? _a : first.to;
2061
+ this.inner = new StructureCursor(first.tree, -first.offset);
2062
+ }
2063
+ else {
2064
+ this.curFrag = this.inner = null;
2065
+ }
2066
+ }
2067
+ hasNode(node) {
2068
+ while (this.curFrag && node.from >= this.curTo)
2069
+ this.nextFrag();
2070
+ return this.curFrag && this.curFrag.from <= node.from && this.curTo >= node.to && this.inner.hasNode(node);
2071
+ }
2072
+ nextFrag() {
2073
+ var _a;
2074
+ this.fragI++;
2075
+ if (this.fragI == this.fragments.length) {
2076
+ this.curFrag = this.inner = null;
2077
+ }
2078
+ else {
2079
+ let frag = this.curFrag = this.fragments[this.fragI];
2080
+ this.curTo = (_a = frag.tree.prop(stoppedInner)) !== null && _a !== void 0 ? _a : frag.to;
2081
+ this.inner = new StructureCursor(frag.tree, -frag.offset);
2082
+ }
2083
+ }
2084
+ findMounts(pos, parser) {
2085
+ var _a;
2086
+ let result = [];
2087
+ if (this.inner) {
2088
+ this.inner.cursor.moveTo(pos, 1);
2089
+ for (let pos = this.inner.cursor.node; pos; pos = pos.parent) {
2090
+ let mount = (_a = pos.tree) === null || _a === void 0 ? void 0 : _a.prop(NodeProp.mounted);
2091
+ if (mount && mount.parser == parser) {
2092
+ for (let i = this.fragI; i < this.fragments.length; i++) {
2093
+ let frag = this.fragments[i];
2094
+ if (frag.from >= pos.to)
2095
+ break;
2096
+ if (frag.tree == this.curFrag.tree)
2097
+ result.push({
2098
+ frag,
2099
+ pos: pos.from - frag.offset,
2100
+ mount
2101
+ });
2102
+ }
2103
+ }
2104
+ }
2105
+ }
2106
+ return result;
2107
+ }
2108
+ }
2109
+ function punchRanges(outer, ranges) {
2110
+ let copy = null, current = ranges;
2111
+ for (let i = 1, j = 0; i < outer.length; i++) {
2112
+ let gapFrom = outer[i - 1].to, gapTo = outer[i].from;
2113
+ for (; j < current.length; j++) {
2114
+ let r = current[j];
2115
+ if (r.from >= gapTo)
2116
+ break;
2117
+ if (r.to <= gapFrom)
2118
+ continue;
2119
+ if (!copy)
2120
+ current = copy = ranges.slice();
2121
+ if (r.from < gapFrom) {
2122
+ copy[j] = new Range(r.from, gapFrom);
2123
+ if (r.to > gapTo)
2124
+ copy.splice(j + 1, 0, new Range(gapTo, r.to));
2125
+ }
2126
+ else if (r.to > gapTo) {
2127
+ copy[j--] = new Range(gapTo, r.to);
2128
+ }
2129
+ else {
2130
+ copy.splice(j--, 1);
2131
+ }
2132
+ }
2133
+ }
2134
+ return current;
2135
+ }
2136
+ function findCoverChanges(a, b, from, to) {
2137
+ let iA = 0, iB = 0, inA = false, inB = false, pos = -1e9;
2138
+ let result = [];
2139
+ for (;;) {
2140
+ let nextA = iA == a.length ? 1e9 : inA ? a[iA].to : a[iA].from;
2141
+ let nextB = iB == b.length ? 1e9 : inB ? b[iB].to : b[iB].from;
2142
+ if (inA != inB) {
2143
+ let start = Math.max(pos, from), end = Math.min(nextA, nextB, to);
2144
+ if (start < end)
2145
+ result.push(new Range(start, end));
2146
+ }
2147
+ pos = Math.min(nextA, nextB);
2148
+ if (pos == 1e9)
2149
+ break;
2150
+ if (nextA == pos) {
2151
+ if (!inA)
2152
+ inA = true;
2153
+ else {
2154
+ inA = false;
2155
+ iA++;
2156
+ }
2157
+ }
2158
+ if (nextB == pos) {
2159
+ if (!inB)
2160
+ inB = true;
2161
+ else {
2162
+ inB = false;
2163
+ iB++;
2164
+ }
2165
+ }
2166
+ }
2167
+ return result;
2168
+ }
2169
+ // Given a number of fragments for the outer tree, and a set of ranges
2170
+ // to parse, find fragments for inner trees mounted around those
2171
+ // ranges, if any.
2172
+ function enterFragments(mounts, ranges) {
2173
+ let result = [];
2174
+ for (let { pos, mount, frag } of mounts) {
2175
+ let startPos = pos + (mount.overlay ? mount.overlay[0].from : 0), endPos = startPos + mount.tree.length;
2176
+ let from = Math.max(frag.from, startPos), to = Math.min(frag.to, endPos);
2177
+ if (mount.overlay) {
2178
+ let overlay = mount.overlay.map(r => new Range(r.from + pos, r.to + pos));
2179
+ let changes = findCoverChanges(ranges, overlay, from, to);
2180
+ for (let i = 0, pos = from;; i++) {
2181
+ let last = i == changes.length, end = last ? to : changes[i].from;
2182
+ if (end > pos)
2183
+ result.push(new TreeFragment(pos, end, mount.tree, -startPos, frag.from >= pos || frag.openStart, frag.to <= end || frag.openEnd));
2184
+ if (last)
2185
+ break;
2186
+ pos = changes[i].to;
2187
+ }
2188
+ }
2189
+ else {
2190
+ result.push(new TreeFragment(from, to, mount.tree, -startPos, frag.from >= startPos || frag.openStart, frag.to <= endPos || frag.openEnd));
2191
+ }
2192
+ }
2193
+ return result;
2194
+ }
2195
+
2196
+ export { DefaultBufferLength, IterMode, MountedTree, NodeProp, NodeSet, NodeType, NodeWeakMap, Parser, Tree, TreeBuffer, TreeCursor, TreeFragment, parseMixed };