feelin 4.3.0

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