@maketribe/ms-app 3.0.36 → 3.0.38

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 (83) hide show
  1. package/dist/cjs/dataview/member/MemberTable.js +2 -2
  2. package/dist/cjs/dataview/member/MemberTable.js.map +1 -1
  3. package/dist/cjs/layouts/components/header/tools.js +7 -2
  4. package/dist/cjs/layouts/components/header/tools.js.map +1 -1
  5. package/dist/cjs/node_modules/.pnpm/{@codemirror_autocomplete@6.11.1_@codemirror_language@6.9.3_@codemirror_state@6.3.2_@codemirro_dop6xooh6ucho44vivbvrw3b2e → @codemirror_autocomplete@6.15.0_@codemirror_language@6.10.1_@codemirror_state@6.4.1_@codemirr_djjyqdn7bhwopzrz2h2s2bwhzm}/node_modules/@codemirror/autocomplete/dist/index.js +77 -25
  6. package/dist/cjs/node_modules/.pnpm/@codemirror_autocomplete@6.15.0_@codemirror_language@6.10.1_@codemirror_state@6.4.1_@codemirr_djjyqdn7bhwopzrz2h2s2bwhzm/node_modules/@codemirror/autocomplete/dist/index.js.map +1 -0
  7. package/dist/cjs/node_modules/.pnpm/@codemirror_commands@6.3.2/node_modules/@codemirror/commands/dist/index.js +7 -1433
  8. package/dist/cjs/node_modules/.pnpm/@codemirror_commands@6.3.2/node_modules/@codemirror/commands/dist/index.js.map +1 -1
  9. package/dist/cjs/node_modules/.pnpm/@codemirror_commands@6.3.3/node_modules/@codemirror/commands/dist/index.js +1484 -0
  10. package/dist/cjs/node_modules/.pnpm/@codemirror_commands@6.3.3/node_modules/@codemirror/commands/dist/index.js.map +1 -0
  11. package/dist/cjs/node_modules/.pnpm/@codemirror_language@6.10.1/node_modules/@codemirror/language/dist/index.js +1751 -0
  12. package/dist/cjs/node_modules/.pnpm/@codemirror_language@6.10.1/node_modules/@codemirror/language/dist/index.js.map +1 -0
  13. package/dist/cjs/node_modules/.pnpm/@codemirror_language@6.9.3/node_modules/@codemirror/language/dist/index.js +0 -1030
  14. package/dist/cjs/node_modules/.pnpm/@codemirror_language@6.9.3/node_modules/@codemirror/language/dist/index.js.map +1 -1
  15. package/dist/cjs/node_modules/.pnpm/{@codemirror_lint@6.4.2 → @codemirror_lint@6.5.0}/node_modules/@codemirror/lint/dist/index.js +8 -7
  16. package/dist/cjs/node_modules/.pnpm/@codemirror_lint@6.5.0/node_modules/@codemirror/lint/dist/index.js.map +1 -0
  17. package/dist/cjs/node_modules/.pnpm/{@codemirror_search@6.5.5 → @codemirror_search@6.5.6}/node_modules/@codemirror/search/dist/index.js +9 -9
  18. package/dist/cjs/node_modules/.pnpm/@codemirror_search@6.5.6/node_modules/@codemirror/search/dist/index.js.map +1 -0
  19. package/dist/cjs/node_modules/.pnpm/@codemirror_state@6.3.2/node_modules/@codemirror/state/dist/index.js +0 -42
  20. package/dist/cjs/node_modules/.pnpm/@codemirror_state@6.3.2/node_modules/@codemirror/state/dist/index.js.map +1 -1
  21. package/dist/cjs/node_modules/.pnpm/@codemirror_state@6.4.1/node_modules/@codemirror/state/dist/index.js +3958 -0
  22. package/dist/cjs/node_modules/.pnpm/@codemirror_state@6.4.1/node_modules/@codemirror/state/dist/index.js.map +1 -0
  23. package/dist/cjs/node_modules/.pnpm/@codemirror_view@6.22.1/node_modules/@codemirror/view/dist/index.js +3 -2161
  24. package/dist/cjs/node_modules/.pnpm/@codemirror_view@6.22.1/node_modules/@codemirror/view/dist/index.js.map +1 -1
  25. package/dist/cjs/node_modules/.pnpm/@codemirror_view@6.26.1/node_modules/@codemirror/view/dist/index.js +10580 -0
  26. package/dist/cjs/node_modules/.pnpm/@codemirror_view@6.26.1/node_modules/@codemirror/view/dist/index.js.map +1 -0
  27. package/dist/cjs/node_modules/.pnpm/@lezer_common@1.2.1/node_modules/@lezer/common/dist/index.js +1668 -0
  28. package/dist/cjs/node_modules/.pnpm/@lezer_common@1.2.1/node_modules/@lezer/common/dist/index.js.map +1 -0
  29. package/dist/cjs/node_modules/.pnpm/{codemirror@6.0.1_@lezer_common@1.1.1 → codemirror@6.0.1_@lezer_common@1.2.1}/node_modules/codemirror/dist/index.js +7 -7
  30. package/dist/cjs/node_modules/.pnpm/{codemirror@6.0.1_@lezer_common@1.1.1 → codemirror@6.0.1_@lezer_common@1.2.1}/node_modules/codemirror/dist/index.js.map +1 -1
  31. package/dist/cjs/node_modules/.pnpm/style-mod@4.1.2/node_modules/style-mod/src/style-mod.js +177 -0
  32. package/dist/cjs/node_modules/.pnpm/style-mod@4.1.2/node_modules/style-mod/src/style-mod.js.map +1 -0
  33. package/dist/cjs/node_modules/.pnpm/vue-codemirror@6.1.1_codemirror@6.0.1_vue@3.3.4/node_modules/vue-codemirror/dist/vue-codemirror.esm.js +1 -1
  34. package/dist/esm/dataview/member/MemberTable.js +2 -2
  35. package/dist/esm/dataview/member/MemberTable.js.map +1 -1
  36. package/dist/esm/layouts/components/header/tools.js +7 -2
  37. package/dist/esm/layouts/components/header/tools.js.map +1 -1
  38. package/dist/esm/node_modules/.pnpm/{@codemirror_autocomplete@6.11.1_@codemirror_language@6.9.3_@codemirror_state@6.3.2_@codemirro_dop6xooh6ucho44vivbvrw3b2e → @codemirror_autocomplete@6.15.0_@codemirror_language@6.10.1_@codemirror_state@6.4.1_@codemirr_djjyqdn7bhwopzrz2h2s2bwhzm}/node_modules/@codemirror/autocomplete/dist/index.js +77 -25
  39. package/dist/esm/node_modules/.pnpm/@codemirror_autocomplete@6.15.0_@codemirror_language@6.10.1_@codemirror_state@6.4.1_@codemirr_djjyqdn7bhwopzrz2h2s2bwhzm/node_modules/@codemirror/autocomplete/dist/index.js.map +1 -0
  40. package/dist/esm/node_modules/.pnpm/@codemirror_commands@6.3.2/node_modules/@codemirror/commands/dist/index.js +4 -1357
  41. package/dist/esm/node_modules/.pnpm/@codemirror_commands@6.3.2/node_modules/@codemirror/commands/dist/index.js.map +1 -1
  42. package/dist/esm/node_modules/.pnpm/@codemirror_commands@6.3.3/node_modules/@codemirror/commands/dist/index.js +1408 -0
  43. package/dist/esm/node_modules/.pnpm/@codemirror_commands@6.3.3/node_modules/@codemirror/commands/dist/index.js.map +1 -0
  44. package/dist/esm/node_modules/.pnpm/@codemirror_language@6.10.1/node_modules/@codemirror/language/dist/index.js +1715 -0
  45. package/dist/esm/node_modules/.pnpm/@codemirror_language@6.10.1/node_modules/@codemirror/language/dist/index.js.map +1 -0
  46. package/dist/esm/node_modules/.pnpm/@codemirror_language@6.9.3/node_modules/@codemirror/language/dist/index.js +4 -1011
  47. package/dist/esm/node_modules/.pnpm/@codemirror_language@6.9.3/node_modules/@codemirror/language/dist/index.js.map +1 -1
  48. package/dist/esm/node_modules/.pnpm/{@codemirror_lint@6.4.2 → @codemirror_lint@6.5.0}/node_modules/@codemirror/lint/dist/index.js +8 -7
  49. package/dist/esm/node_modules/.pnpm/@codemirror_lint@6.5.0/node_modules/@codemirror/lint/dist/index.js.map +1 -0
  50. package/dist/esm/node_modules/.pnpm/{@codemirror_search@6.5.5 → @codemirror_search@6.5.6}/node_modules/@codemirror/search/dist/index.js +9 -9
  51. package/dist/esm/node_modules/.pnpm/@codemirror_search@6.5.6/node_modules/@codemirror/search/dist/index.js.map +1 -0
  52. package/dist/esm/node_modules/.pnpm/@codemirror_state@6.3.2/node_modules/@codemirror/state/dist/index.js +1 -41
  53. package/dist/esm/node_modules/.pnpm/@codemirror_state@6.3.2/node_modules/@codemirror/state/dist/index.js.map +1 -1
  54. package/dist/esm/node_modules/.pnpm/@codemirror_state@6.4.1/node_modules/@codemirror/state/dist/index.js +3930 -0
  55. package/dist/esm/node_modules/.pnpm/@codemirror_state@6.4.1/node_modules/@codemirror/state/dist/index.js.map +1 -0
  56. package/dist/esm/node_modules/.pnpm/@codemirror_view@6.22.1/node_modules/@codemirror/view/dist/index.js +5 -2142
  57. package/dist/esm/node_modules/.pnpm/@codemirror_view@6.22.1/node_modules/@codemirror/view/dist/index.js.map +1 -1
  58. package/dist/esm/node_modules/.pnpm/@codemirror_view@6.26.1/node_modules/@codemirror/view/dist/index.js +10548 -0
  59. package/dist/esm/node_modules/.pnpm/@codemirror_view@6.26.1/node_modules/@codemirror/view/dist/index.js.map +1 -0
  60. package/dist/esm/node_modules/.pnpm/@lezer_common@1.2.1/node_modules/@lezer/common/dist/index.js +1658 -0
  61. package/dist/esm/node_modules/.pnpm/@lezer_common@1.2.1/node_modules/@lezer/common/dist/index.js.map +1 -0
  62. package/dist/esm/node_modules/.pnpm/{codemirror@6.0.1_@lezer_common@1.1.1 → codemirror@6.0.1_@lezer_common@1.2.1}/node_modules/codemirror/dist/index.js +8 -8
  63. package/dist/esm/node_modules/.pnpm/{codemirror@6.0.1_@lezer_common@1.1.1 → codemirror@6.0.1_@lezer_common@1.2.1}/node_modules/codemirror/dist/index.js.map +1 -1
  64. package/dist/esm/node_modules/.pnpm/style-mod@4.1.2/node_modules/style-mod/src/style-mod.js +175 -0
  65. package/dist/esm/node_modules/.pnpm/style-mod@4.1.2/node_modules/style-mod/src/style-mod.js.map +1 -0
  66. package/dist/esm/node_modules/.pnpm/vue-codemirror@6.1.1_codemirror@6.0.1_vue@3.3.4/node_modules/vue-codemirror/dist/vue-codemirror.esm.js +1 -1
  67. package/dist/style/components/classify-search.css +1 -1
  68. package/dist/style/components/index.css +1 -1
  69. package/dist/style/index.css +2 -2
  70. package/dist/style/layouts/default/header.css +1 -1
  71. package/dist/style/layouts/default/index.css +1 -1
  72. package/dist/style/layouts/index.css +1 -1
  73. package/dist/style/src/components/classify-search.scss +5 -2
  74. package/dist/style/src/layouts/default/header.scss +4 -0
  75. package/package.json +3 -3
  76. package/src/dataview/member/MemberTable.tsx +2 -2
  77. package/src/layouts/components/header/tools.tsx +6 -1
  78. package/dist/cjs/node_modules/.pnpm/@codemirror_autocomplete@6.11.1_@codemirror_language@6.9.3_@codemirror_state@6.3.2_@codemirro_dop6xooh6ucho44vivbvrw3b2e/node_modules/@codemirror/autocomplete/dist/index.js.map +0 -1
  79. package/dist/cjs/node_modules/.pnpm/@codemirror_lint@6.4.2/node_modules/@codemirror/lint/dist/index.js.map +0 -1
  80. package/dist/cjs/node_modules/.pnpm/@codemirror_search@6.5.5/node_modules/@codemirror/search/dist/index.js.map +0 -1
  81. package/dist/esm/node_modules/.pnpm/@codemirror_autocomplete@6.11.1_@codemirror_language@6.9.3_@codemirror_state@6.3.2_@codemirro_dop6xooh6ucho44vivbvrw3b2e/node_modules/@codemirror/autocomplete/dist/index.js.map +0 -1
  82. package/dist/esm/node_modules/.pnpm/@codemirror_lint@6.4.2/node_modules/@codemirror/lint/dist/index.js.map +0 -1
  83. package/dist/esm/node_modules/.pnpm/@codemirror_search@6.5.5/node_modules/@codemirror/search/dist/index.js.map +0 -1
@@ -0,0 +1,1658 @@
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
+ const CachedNode = new WeakMap(), CachedInnerNode = new WeakMap();
254
+ /**
255
+ Options that control iteration. Can be combined with the `|`
256
+ operator to enable multiple ones.
257
+ */
258
+ var IterMode;
259
+ (function (IterMode) {
260
+ /**
261
+ When enabled, iteration will only visit [`Tree`](#common.Tree)
262
+ objects, not nodes packed into
263
+ [`TreeBuffer`](#common.TreeBuffer)s.
264
+ */
265
+ IterMode[IterMode["ExcludeBuffers"] = 1] = "ExcludeBuffers";
266
+ /**
267
+ Enable this to make iteration include anonymous nodes (such as
268
+ the nodes that wrap repeated grammar constructs into a balanced
269
+ tree).
270
+ */
271
+ IterMode[IterMode["IncludeAnonymous"] = 2] = "IncludeAnonymous";
272
+ /**
273
+ By default, regular [mounted](#common.NodeProp^mounted) nodes
274
+ replace their base node in iteration. Enable this to ignore them
275
+ instead.
276
+ */
277
+ IterMode[IterMode["IgnoreMounts"] = 4] = "IgnoreMounts";
278
+ /**
279
+ This option only applies in
280
+ [`enter`](#common.SyntaxNode.enter)-style methods. It tells the
281
+ library to not enter mounted overlays if one covers the given
282
+ position.
283
+ */
284
+ IterMode[IterMode["IgnoreOverlays"] = 8] = "IgnoreOverlays";
285
+ })(IterMode || (IterMode = {}));
286
+ /**
287
+ A piece of syntax tree. There are two ways to approach these
288
+ trees: the way they are actually stored in memory, and the
289
+ convenient way.
290
+
291
+ Syntax trees are stored as a tree of `Tree` and `TreeBuffer`
292
+ objects. By packing detail information into `TreeBuffer` leaf
293
+ nodes, the representation is made a lot more memory-efficient.
294
+
295
+ However, when you want to actually work with tree nodes, this
296
+ representation is very awkward, so most client code will want to
297
+ use the [`TreeCursor`](#common.TreeCursor) or
298
+ [`SyntaxNode`](#common.SyntaxNode) interface instead, which provides
299
+ a view on some part of this data structure, and can be used to
300
+ move around to adjacent nodes.
301
+ */
302
+ class Tree {
303
+ /**
304
+ Construct a new tree. See also [`Tree.build`](#common.Tree^build).
305
+ */
306
+ constructor(
307
+ /**
308
+ The type of the top node.
309
+ */
310
+ type,
311
+ /**
312
+ This node's child nodes.
313
+ */
314
+ children,
315
+ /**
316
+ The positions (offsets relative to the start of this tree) of
317
+ the children.
318
+ */
319
+ positions,
320
+ /**
321
+ The total length of this tree
322
+ */
323
+ length,
324
+ /**
325
+ Per-node [node props](#common.NodeProp) to associate with this node.
326
+ */
327
+ props) {
328
+ this.type = type;
329
+ this.children = children;
330
+ this.positions = positions;
331
+ this.length = length;
332
+ /**
333
+ @internal
334
+ */
335
+ this.props = null;
336
+ if (props && props.length) {
337
+ this.props = Object.create(null);
338
+ for (let [prop, value] of props)
339
+ this.props[typeof prop == "number" ? prop : prop.id] = value;
340
+ }
341
+ }
342
+ /**
343
+ @internal
344
+ */
345
+ toString() {
346
+ let mounted = MountedTree.get(this);
347
+ if (mounted && !mounted.overlay)
348
+ return mounted.tree.toString();
349
+ let children = "";
350
+ for (let ch of this.children) {
351
+ let str = ch.toString();
352
+ if (str) {
353
+ if (children)
354
+ children += ",";
355
+ children += str;
356
+ }
357
+ }
358
+ return !this.type.name ? children :
359
+ (/\W/.test(this.type.name) && !this.type.isError ? JSON.stringify(this.type.name) : this.type.name) +
360
+ (children.length ? "(" + children + ")" : "");
361
+ }
362
+ /**
363
+ Get a [tree cursor](#common.TreeCursor) positioned at the top of
364
+ the tree. Mode can be used to [control](#common.IterMode) which
365
+ nodes the cursor visits.
366
+ */
367
+ cursor(mode = 0) {
368
+ return new TreeCursor(this.topNode, mode);
369
+ }
370
+ /**
371
+ Get a [tree cursor](#common.TreeCursor) pointing into this tree
372
+ at the given position and side (see
373
+ [`moveTo`](#common.TreeCursor.moveTo).
374
+ */
375
+ cursorAt(pos, side = 0, mode = 0) {
376
+ let scope = CachedNode.get(this) || this.topNode;
377
+ let cursor = new TreeCursor(scope);
378
+ cursor.moveTo(pos, side);
379
+ CachedNode.set(this, cursor._tree);
380
+ return cursor;
381
+ }
382
+ /**
383
+ Get a [syntax node](#common.SyntaxNode) object for the top of the
384
+ tree.
385
+ */
386
+ get topNode() {
387
+ return new TreeNode(this, 0, 0, null);
388
+ }
389
+ /**
390
+ Get the [syntax node](#common.SyntaxNode) at the given position.
391
+ If `side` is -1, this will move into nodes that end at the
392
+ position. If 1, it'll move into nodes that start at the
393
+ position. With 0, it'll only enter nodes that cover the position
394
+ from both sides.
395
+
396
+ Note that this will not enter
397
+ [overlays](#common.MountedTree.overlay), and you often want
398
+ [`resolveInner`](#common.Tree.resolveInner) instead.
399
+ */
400
+ resolve(pos, side = 0) {
401
+ let node = resolveNode(CachedNode.get(this) || this.topNode, pos, side, false);
402
+ CachedNode.set(this, node);
403
+ return node;
404
+ }
405
+ /**
406
+ Like [`resolve`](#common.Tree.resolve), but will enter
407
+ [overlaid](#common.MountedTree.overlay) nodes, producing a syntax node
408
+ pointing into the innermost overlaid tree at the given position
409
+ (with parent links going through all parent structure, including
410
+ the host trees).
411
+ */
412
+ resolveInner(pos, side = 0) {
413
+ let node = resolveNode(CachedInnerNode.get(this) || this.topNode, pos, side, true);
414
+ CachedInnerNode.set(this, node);
415
+ return node;
416
+ }
417
+ /**
418
+ In some situations, it can be useful to iterate through all
419
+ nodes around a position, including those in overlays that don't
420
+ directly cover the position. This method gives you an iterator
421
+ that will produce all nodes, from small to big, around the given
422
+ position.
423
+ */
424
+ resolveStack(pos, side = 0) {
425
+ return stackIterator(this, pos, side);
426
+ }
427
+ /**
428
+ Iterate over the tree and its children, calling `enter` for any
429
+ node that touches the `from`/`to` region (if given) before
430
+ running over such a node's children, and `leave` (if given) when
431
+ leaving the node. When `enter` returns `false`, that node will
432
+ not have its children iterated over (or `leave` called).
433
+ */
434
+ iterate(spec) {
435
+ let { enter, leave, from = 0, to = this.length } = spec;
436
+ let mode = spec.mode || 0, anon = (mode & IterMode.IncludeAnonymous) > 0;
437
+ for (let c = this.cursor(mode | IterMode.IncludeAnonymous);;) {
438
+ let entered = false;
439
+ if (c.from <= to && c.to >= from && (!anon && c.type.isAnonymous || enter(c) !== false)) {
440
+ if (c.firstChild())
441
+ continue;
442
+ entered = true;
443
+ }
444
+ for (;;) {
445
+ if (entered && leave && (anon || !c.type.isAnonymous))
446
+ leave(c);
447
+ if (c.nextSibling())
448
+ break;
449
+ if (!c.parent())
450
+ return;
451
+ entered = true;
452
+ }
453
+ }
454
+ }
455
+ /**
456
+ Get the value of the given [node prop](#common.NodeProp) for this
457
+ node. Works with both per-node and per-type props.
458
+ */
459
+ prop(prop) {
460
+ return !prop.perNode ? this.type.prop(prop) : this.props ? this.props[prop.id] : undefined;
461
+ }
462
+ /**
463
+ Returns the node's [per-node props](#common.NodeProp.perNode) in a
464
+ format that can be passed to the [`Tree`](#common.Tree)
465
+ constructor.
466
+ */
467
+ get propValues() {
468
+ let result = [];
469
+ if (this.props)
470
+ for (let id in this.props)
471
+ result.push([+id, this.props[id]]);
472
+ return result;
473
+ }
474
+ /**
475
+ Balance the direct children of this tree, producing a copy of
476
+ which may have children grouped into subtrees with type
477
+ [`NodeType.none`](#common.NodeType^none).
478
+ */
479
+ balance(config = {}) {
480
+ return this.children.length <= 8 /* Balance.BranchFactor */ ? this :
481
+ 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)));
482
+ }
483
+ /**
484
+ Build a tree from a postfix-ordered buffer of node information,
485
+ or a cursor over such a buffer.
486
+ */
487
+ static build(data) { return buildTree(data); }
488
+ }
489
+ /**
490
+ The empty tree
491
+ */
492
+ Tree.empty = new Tree(NodeType.none, [], [], 0);
493
+ class FlatBufferCursor {
494
+ constructor(buffer, index) {
495
+ this.buffer = buffer;
496
+ this.index = index;
497
+ }
498
+ get id() { return this.buffer[this.index - 4]; }
499
+ get start() { return this.buffer[this.index - 3]; }
500
+ get end() { return this.buffer[this.index - 2]; }
501
+ get size() { return this.buffer[this.index - 1]; }
502
+ get pos() { return this.index; }
503
+ next() { this.index -= 4; }
504
+ fork() { return new FlatBufferCursor(this.buffer, this.index); }
505
+ }
506
+ /**
507
+ Tree buffers contain (type, start, end, endIndex) quads for each
508
+ node. In such a buffer, nodes are stored in prefix order (parents
509
+ before children, with the endIndex of the parent indicating which
510
+ children belong to it).
511
+ */
512
+ class TreeBuffer {
513
+ /**
514
+ Create a tree buffer.
515
+ */
516
+ constructor(
517
+ /**
518
+ The buffer's content.
519
+ */
520
+ buffer,
521
+ /**
522
+ The total length of the group of nodes in the buffer.
523
+ */
524
+ length,
525
+ /**
526
+ The node set used in this buffer.
527
+ */
528
+ set) {
529
+ this.buffer = buffer;
530
+ this.length = length;
531
+ this.set = set;
532
+ }
533
+ /**
534
+ @internal
535
+ */
536
+ get type() { return NodeType.none; }
537
+ /**
538
+ @internal
539
+ */
540
+ toString() {
541
+ let result = [];
542
+ for (let index = 0; index < this.buffer.length;) {
543
+ result.push(this.childString(index));
544
+ index = this.buffer[index + 3];
545
+ }
546
+ return result.join(",");
547
+ }
548
+ /**
549
+ @internal
550
+ */
551
+ childString(index) {
552
+ let id = this.buffer[index], endIndex = this.buffer[index + 3];
553
+ let type = this.set.types[id], result = type.name;
554
+ if (/\W/.test(result) && !type.isError)
555
+ result = JSON.stringify(result);
556
+ index += 4;
557
+ if (endIndex == index)
558
+ return result;
559
+ let children = [];
560
+ while (index < endIndex) {
561
+ children.push(this.childString(index));
562
+ index = this.buffer[index + 3];
563
+ }
564
+ return result + "(" + children.join(",") + ")";
565
+ }
566
+ /**
567
+ @internal
568
+ */
569
+ findChild(startIndex, endIndex, dir, pos, side) {
570
+ let { buffer } = this, pick = -1;
571
+ for (let i = startIndex; i != endIndex; i = buffer[i + 3]) {
572
+ if (checkSide(side, pos, buffer[i + 1], buffer[i + 2])) {
573
+ pick = i;
574
+ if (dir > 0)
575
+ break;
576
+ }
577
+ }
578
+ return pick;
579
+ }
580
+ /**
581
+ @internal
582
+ */
583
+ slice(startI, endI, from) {
584
+ let b = this.buffer;
585
+ let copy = new Uint16Array(endI - startI), len = 0;
586
+ for (let i = startI, j = 0; i < endI;) {
587
+ copy[j++] = b[i++];
588
+ copy[j++] = b[i++] - from;
589
+ let to = copy[j++] = b[i++] - from;
590
+ copy[j++] = b[i++] - startI;
591
+ len = Math.max(len, to);
592
+ }
593
+ return new TreeBuffer(copy, len, this.set);
594
+ }
595
+ }
596
+ function checkSide(side, pos, from, to) {
597
+ switch (side) {
598
+ case -2 /* Side.Before */: return from < pos;
599
+ case -1 /* Side.AtOrBefore */: return to >= pos && from < pos;
600
+ case 0 /* Side.Around */: return from < pos && to > pos;
601
+ case 1 /* Side.AtOrAfter */: return from <= pos && to > pos;
602
+ case 2 /* Side.After */: return to > pos;
603
+ case 4 /* Side.DontCare */: return true;
604
+ }
605
+ }
606
+ function resolveNode(node, pos, side, overlays) {
607
+ var _a;
608
+ // Move up to a node that actually holds the position, if possible
609
+ while (node.from == node.to ||
610
+ (side < 1 ? node.from >= pos : node.from > pos) ||
611
+ (side > -1 ? node.to <= pos : node.to < pos)) {
612
+ let parent = !overlays && node instanceof TreeNode && node.index < 0 ? null : node.parent;
613
+ if (!parent)
614
+ return node;
615
+ node = parent;
616
+ }
617
+ let mode = overlays ? 0 : IterMode.IgnoreOverlays;
618
+ // Must go up out of overlays when those do not overlap with pos
619
+ if (overlays)
620
+ for (let scan = node, parent = scan.parent; parent; scan = parent, parent = scan.parent) {
621
+ if (scan instanceof TreeNode && scan.index < 0 && ((_a = parent.enter(pos, side, mode)) === null || _a === void 0 ? void 0 : _a.from) != scan.from)
622
+ node = parent;
623
+ }
624
+ for (;;) {
625
+ let inner = node.enter(pos, side, mode);
626
+ if (!inner)
627
+ return node;
628
+ node = inner;
629
+ }
630
+ }
631
+ class BaseNode {
632
+ cursor(mode = 0) { return new TreeCursor(this, mode); }
633
+ getChild(type, before = null, after = null) {
634
+ let r = getChildren(this, type, before, after);
635
+ return r.length ? r[0] : null;
636
+ }
637
+ getChildren(type, before = null, after = null) {
638
+ return getChildren(this, type, before, after);
639
+ }
640
+ resolve(pos, side = 0) {
641
+ return resolveNode(this, pos, side, false);
642
+ }
643
+ resolveInner(pos, side = 0) {
644
+ return resolveNode(this, pos, side, true);
645
+ }
646
+ matchContext(context) {
647
+ return matchNodeContext(this, context);
648
+ }
649
+ enterUnfinishedNodesBefore(pos) {
650
+ let scan = this.childBefore(pos), node = this;
651
+ while (scan) {
652
+ let last = scan.lastChild;
653
+ if (!last || last.to != scan.to)
654
+ break;
655
+ if (last.type.isError && last.from == last.to) {
656
+ node = scan;
657
+ scan = last.prevSibling;
658
+ }
659
+ else {
660
+ scan = last;
661
+ }
662
+ }
663
+ return node;
664
+ }
665
+ get node() { return this; }
666
+ get next() { return this.parent; }
667
+ }
668
+ class TreeNode extends BaseNode {
669
+ constructor(_tree, from,
670
+ // Index in parent node, set to -1 if the node is not a direct child of _parent.node (overlay)
671
+ index, _parent) {
672
+ super();
673
+ this._tree = _tree;
674
+ this.from = from;
675
+ this.index = index;
676
+ this._parent = _parent;
677
+ }
678
+ get type() { return this._tree.type; }
679
+ get name() { return this._tree.type.name; }
680
+ get to() { return this.from + this._tree.length; }
681
+ nextChild(i, dir, pos, side, mode = 0) {
682
+ for (let parent = this;;) {
683
+ for (let { children, positions } = parent._tree, e = dir > 0 ? children.length : -1; i != e; i += dir) {
684
+ let next = children[i], start = positions[i] + parent.from;
685
+ if (!checkSide(side, pos, start, start + next.length))
686
+ continue;
687
+ if (next instanceof TreeBuffer) {
688
+ if (mode & IterMode.ExcludeBuffers)
689
+ continue;
690
+ let index = next.findChild(0, next.buffer.length, dir, pos - start, side);
691
+ if (index > -1)
692
+ return new BufferNode(new BufferContext(parent, next, i, start), null, index);
693
+ }
694
+ else if ((mode & IterMode.IncludeAnonymous) || (!next.type.isAnonymous || hasChild(next))) {
695
+ let mounted;
696
+ if (!(mode & IterMode.IgnoreMounts) && (mounted = MountedTree.get(next)) && !mounted.overlay)
697
+ return new TreeNode(mounted.tree, start, i, parent);
698
+ let inner = new TreeNode(next, start, i, parent);
699
+ return (mode & IterMode.IncludeAnonymous) || !inner.type.isAnonymous ? inner
700
+ : inner.nextChild(dir < 0 ? next.children.length - 1 : 0, dir, pos, side);
701
+ }
702
+ }
703
+ if ((mode & IterMode.IncludeAnonymous) || !parent.type.isAnonymous)
704
+ return null;
705
+ if (parent.index >= 0)
706
+ i = parent.index + dir;
707
+ else
708
+ i = dir < 0 ? -1 : parent._parent._tree.children.length;
709
+ parent = parent._parent;
710
+ if (!parent)
711
+ return null;
712
+ }
713
+ }
714
+ get firstChild() { return this.nextChild(0, 1, 0, 4 /* Side.DontCare */); }
715
+ get lastChild() { return this.nextChild(this._tree.children.length - 1, -1, 0, 4 /* Side.DontCare */); }
716
+ childAfter(pos) { return this.nextChild(0, 1, pos, 2 /* Side.After */); }
717
+ childBefore(pos) { return this.nextChild(this._tree.children.length - 1, -1, pos, -2 /* Side.Before */); }
718
+ enter(pos, side, mode = 0) {
719
+ let mounted;
720
+ if (!(mode & IterMode.IgnoreOverlays) && (mounted = MountedTree.get(this._tree)) && mounted.overlay) {
721
+ let rPos = pos - this.from;
722
+ for (let { from, to } of mounted.overlay) {
723
+ if ((side > 0 ? from <= rPos : from < rPos) &&
724
+ (side < 0 ? to >= rPos : to > rPos))
725
+ return new TreeNode(mounted.tree, mounted.overlay[0].from + this.from, -1, this);
726
+ }
727
+ }
728
+ return this.nextChild(0, 1, pos, side, mode);
729
+ }
730
+ nextSignificantParent() {
731
+ let val = this;
732
+ while (val.type.isAnonymous && val._parent)
733
+ val = val._parent;
734
+ return val;
735
+ }
736
+ get parent() {
737
+ return this._parent ? this._parent.nextSignificantParent() : null;
738
+ }
739
+ get nextSibling() {
740
+ return this._parent && this.index >= 0 ? this._parent.nextChild(this.index + 1, 1, 0, 4 /* Side.DontCare */) : null;
741
+ }
742
+ get prevSibling() {
743
+ return this._parent && this.index >= 0 ? this._parent.nextChild(this.index - 1, -1, 0, 4 /* Side.DontCare */) : null;
744
+ }
745
+ get tree() { return this._tree; }
746
+ toTree() { return this._tree; }
747
+ /**
748
+ @internal
749
+ */
750
+ toString() { return this._tree.toString(); }
751
+ }
752
+ function getChildren(node, type, before, after) {
753
+ let cur = node.cursor(), result = [];
754
+ if (!cur.firstChild())
755
+ return result;
756
+ if (before != null)
757
+ for (let found = false; !found;) {
758
+ found = cur.type.is(before);
759
+ if (!cur.nextSibling())
760
+ return result;
761
+ }
762
+ for (;;) {
763
+ if (after != null && cur.type.is(after))
764
+ return result;
765
+ if (cur.type.is(type))
766
+ result.push(cur.node);
767
+ if (!cur.nextSibling())
768
+ return after == null ? result : [];
769
+ }
770
+ }
771
+ function matchNodeContext(node, context, i = context.length - 1) {
772
+ for (let p = node.parent; i >= 0; p = p.parent) {
773
+ if (!p)
774
+ return false;
775
+ if (!p.type.isAnonymous) {
776
+ if (context[i] && context[i] != p.name)
777
+ return false;
778
+ i--;
779
+ }
780
+ }
781
+ return true;
782
+ }
783
+ class BufferContext {
784
+ constructor(parent, buffer, index, start) {
785
+ this.parent = parent;
786
+ this.buffer = buffer;
787
+ this.index = index;
788
+ this.start = start;
789
+ }
790
+ }
791
+ class BufferNode extends BaseNode {
792
+ get name() { return this.type.name; }
793
+ get from() { return this.context.start + this.context.buffer.buffer[this.index + 1]; }
794
+ get to() { return this.context.start + this.context.buffer.buffer[this.index + 2]; }
795
+ constructor(context, _parent, index) {
796
+ super();
797
+ this.context = context;
798
+ this._parent = _parent;
799
+ this.index = index;
800
+ this.type = context.buffer.set.types[context.buffer.buffer[index]];
801
+ }
802
+ child(dir, pos, side) {
803
+ let { buffer } = this.context;
804
+ let index = buffer.findChild(this.index + 4, buffer.buffer[this.index + 3], dir, pos - this.context.start, side);
805
+ return index < 0 ? null : new BufferNode(this.context, this, index);
806
+ }
807
+ get firstChild() { return this.child(1, 0, 4 /* Side.DontCare */); }
808
+ get lastChild() { return this.child(-1, 0, 4 /* Side.DontCare */); }
809
+ childAfter(pos) { return this.child(1, pos, 2 /* Side.After */); }
810
+ childBefore(pos) { return this.child(-1, pos, -2 /* Side.Before */); }
811
+ enter(pos, side, mode = 0) {
812
+ if (mode & IterMode.ExcludeBuffers)
813
+ return null;
814
+ let { buffer } = this.context;
815
+ let index = buffer.findChild(this.index + 4, buffer.buffer[this.index + 3], side > 0 ? 1 : -1, pos - this.context.start, side);
816
+ return index < 0 ? null : new BufferNode(this.context, this, index);
817
+ }
818
+ get parent() {
819
+ return this._parent || this.context.parent.nextSignificantParent();
820
+ }
821
+ externalSibling(dir) {
822
+ return this._parent ? null : this.context.parent.nextChild(this.context.index + dir, dir, 0, 4 /* Side.DontCare */);
823
+ }
824
+ get nextSibling() {
825
+ let { buffer } = this.context;
826
+ let after = buffer.buffer[this.index + 3];
827
+ if (after < (this._parent ? buffer.buffer[this._parent.index + 3] : buffer.buffer.length))
828
+ return new BufferNode(this.context, this._parent, after);
829
+ return this.externalSibling(1);
830
+ }
831
+ get prevSibling() {
832
+ let { buffer } = this.context;
833
+ let parentStart = this._parent ? this._parent.index + 4 : 0;
834
+ if (this.index == parentStart)
835
+ return this.externalSibling(-1);
836
+ return new BufferNode(this.context, this._parent, buffer.findChild(parentStart, this.index, -1, 0, 4 /* Side.DontCare */));
837
+ }
838
+ get tree() { return null; }
839
+ toTree() {
840
+ let children = [], positions = [];
841
+ let { buffer } = this.context;
842
+ let startI = this.index + 4, endI = buffer.buffer[this.index + 3];
843
+ if (endI > startI) {
844
+ let from = buffer.buffer[this.index + 1];
845
+ children.push(buffer.slice(startI, endI, from));
846
+ positions.push(0);
847
+ }
848
+ return new Tree(this.type, children, positions, this.to - this.from);
849
+ }
850
+ /**
851
+ @internal
852
+ */
853
+ toString() { return this.context.buffer.childString(this.index); }
854
+ }
855
+ function iterStack(heads) {
856
+ if (!heads.length)
857
+ return null;
858
+ let pick = 0, picked = heads[0];
859
+ for (let i = 1; i < heads.length; i++) {
860
+ let node = heads[i];
861
+ if (node.from > picked.from || node.to < picked.to) {
862
+ picked = node;
863
+ pick = i;
864
+ }
865
+ }
866
+ let next = picked instanceof TreeNode && picked.index < 0 ? null : picked.parent;
867
+ let newHeads = heads.slice();
868
+ if (next)
869
+ newHeads[pick] = next;
870
+ else
871
+ newHeads.splice(pick, 1);
872
+ return new StackIterator(newHeads, picked);
873
+ }
874
+ class StackIterator {
875
+ constructor(heads, node) {
876
+ this.heads = heads;
877
+ this.node = node;
878
+ }
879
+ get next() { return iterStack(this.heads); }
880
+ }
881
+ function stackIterator(tree, pos, side) {
882
+ let inner = tree.resolveInner(pos, side), layers = null;
883
+ for (let scan = inner instanceof TreeNode ? inner : inner.context.parent; scan; scan = scan.parent) {
884
+ if (scan.index < 0) { // This is an overlay root
885
+ let parent = scan.parent;
886
+ (layers || (layers = [inner])).push(parent.resolve(pos, side));
887
+ scan = parent;
888
+ }
889
+ else {
890
+ let mount = MountedTree.get(scan.tree);
891
+ // Relevant overlay branching off
892
+ if (mount && mount.overlay && mount.overlay[0].from <= pos && mount.overlay[mount.overlay.length - 1].to >= pos) {
893
+ let root = new TreeNode(mount.tree, mount.overlay[0].from + scan.from, -1, scan);
894
+ (layers || (layers = [inner])).push(resolveNode(root, pos, side, false));
895
+ }
896
+ }
897
+ }
898
+ return layers ? iterStack(layers) : inner;
899
+ }
900
+ /**
901
+ A tree cursor object focuses on a given node in a syntax tree, and
902
+ allows you to move to adjacent nodes.
903
+ */
904
+ class TreeCursor {
905
+ /**
906
+ Shorthand for `.type.name`.
907
+ */
908
+ get name() { return this.type.name; }
909
+ /**
910
+ @internal
911
+ */
912
+ constructor(node,
913
+ /**
914
+ @internal
915
+ */
916
+ mode = 0) {
917
+ this.mode = mode;
918
+ /**
919
+ @internal
920
+ */
921
+ this.buffer = null;
922
+ this.stack = [];
923
+ /**
924
+ @internal
925
+ */
926
+ this.index = 0;
927
+ this.bufferNode = null;
928
+ if (node instanceof TreeNode) {
929
+ this.yieldNode(node);
930
+ }
931
+ else {
932
+ this._tree = node.context.parent;
933
+ this.buffer = node.context;
934
+ for (let n = node._parent; n; n = n._parent)
935
+ this.stack.unshift(n.index);
936
+ this.bufferNode = node;
937
+ this.yieldBuf(node.index);
938
+ }
939
+ }
940
+ yieldNode(node) {
941
+ if (!node)
942
+ return false;
943
+ this._tree = node;
944
+ this.type = node.type;
945
+ this.from = node.from;
946
+ this.to = node.to;
947
+ return true;
948
+ }
949
+ yieldBuf(index, type) {
950
+ this.index = index;
951
+ let { start, buffer } = this.buffer;
952
+ this.type = type || buffer.set.types[buffer.buffer[index]];
953
+ this.from = start + buffer.buffer[index + 1];
954
+ this.to = start + buffer.buffer[index + 2];
955
+ return true;
956
+ }
957
+ /**
958
+ @internal
959
+ */
960
+ yield(node) {
961
+ if (!node)
962
+ return false;
963
+ if (node instanceof TreeNode) {
964
+ this.buffer = null;
965
+ return this.yieldNode(node);
966
+ }
967
+ this.buffer = node.context;
968
+ return this.yieldBuf(node.index, node.type);
969
+ }
970
+ /**
971
+ @internal
972
+ */
973
+ toString() {
974
+ return this.buffer ? this.buffer.buffer.childString(this.index) : this._tree.toString();
975
+ }
976
+ /**
977
+ @internal
978
+ */
979
+ enterChild(dir, pos, side) {
980
+ if (!this.buffer)
981
+ return this.yield(this._tree.nextChild(dir < 0 ? this._tree._tree.children.length - 1 : 0, dir, pos, side, this.mode));
982
+ let { buffer } = this.buffer;
983
+ let index = buffer.findChild(this.index + 4, buffer.buffer[this.index + 3], dir, pos - this.buffer.start, side);
984
+ if (index < 0)
985
+ return false;
986
+ this.stack.push(this.index);
987
+ return this.yieldBuf(index);
988
+ }
989
+ /**
990
+ Move the cursor to this node's first child. When this returns
991
+ false, the node has no child, and the cursor has not been moved.
992
+ */
993
+ firstChild() { return this.enterChild(1, 0, 4 /* Side.DontCare */); }
994
+ /**
995
+ Move the cursor to this node's last child.
996
+ */
997
+ lastChild() { return this.enterChild(-1, 0, 4 /* Side.DontCare */); }
998
+ /**
999
+ Move the cursor to the first child that ends after `pos`.
1000
+ */
1001
+ childAfter(pos) { return this.enterChild(1, pos, 2 /* Side.After */); }
1002
+ /**
1003
+ Move to the last child that starts before `pos`.
1004
+ */
1005
+ childBefore(pos) { return this.enterChild(-1, pos, -2 /* Side.Before */); }
1006
+ /**
1007
+ Move the cursor to the child around `pos`. If side is -1 the
1008
+ child may end at that position, when 1 it may start there. This
1009
+ will also enter [overlaid](#common.MountedTree.overlay)
1010
+ [mounted](#common.NodeProp^mounted) trees unless `overlays` is
1011
+ set to false.
1012
+ */
1013
+ enter(pos, side, mode = this.mode) {
1014
+ if (!this.buffer)
1015
+ return this.yield(this._tree.enter(pos, side, mode));
1016
+ return mode & IterMode.ExcludeBuffers ? false : this.enterChild(1, pos, side);
1017
+ }
1018
+ /**
1019
+ Move to the node's parent node, if this isn't the top node.
1020
+ */
1021
+ parent() {
1022
+ if (!this.buffer)
1023
+ return this.yieldNode((this.mode & IterMode.IncludeAnonymous) ? this._tree._parent : this._tree.parent);
1024
+ if (this.stack.length)
1025
+ return this.yieldBuf(this.stack.pop());
1026
+ let parent = (this.mode & IterMode.IncludeAnonymous) ? this.buffer.parent : this.buffer.parent.nextSignificantParent();
1027
+ this.buffer = null;
1028
+ return this.yieldNode(parent);
1029
+ }
1030
+ /**
1031
+ @internal
1032
+ */
1033
+ sibling(dir) {
1034
+ if (!this.buffer)
1035
+ return !this._tree._parent ? false
1036
+ : this.yield(this._tree.index < 0 ? null
1037
+ : this._tree._parent.nextChild(this._tree.index + dir, dir, 0, 4 /* Side.DontCare */, this.mode));
1038
+ let { buffer } = this.buffer, d = this.stack.length - 1;
1039
+ if (dir < 0) {
1040
+ let parentStart = d < 0 ? 0 : this.stack[d] + 4;
1041
+ if (this.index != parentStart)
1042
+ return this.yieldBuf(buffer.findChild(parentStart, this.index, -1, 0, 4 /* Side.DontCare */));
1043
+ }
1044
+ else {
1045
+ let after = buffer.buffer[this.index + 3];
1046
+ if (after < (d < 0 ? buffer.buffer.length : buffer.buffer[this.stack[d] + 3]))
1047
+ return this.yieldBuf(after);
1048
+ }
1049
+ return d < 0 ? this.yield(this.buffer.parent.nextChild(this.buffer.index + dir, dir, 0, 4 /* Side.DontCare */, this.mode)) : false;
1050
+ }
1051
+ /**
1052
+ Move to this node's next sibling, if any.
1053
+ */
1054
+ nextSibling() { return this.sibling(1); }
1055
+ /**
1056
+ Move to this node's previous sibling, if any.
1057
+ */
1058
+ prevSibling() { return this.sibling(-1); }
1059
+ atLastNode(dir) {
1060
+ let index, parent, { buffer } = this;
1061
+ if (buffer) {
1062
+ if (dir > 0) {
1063
+ if (this.index < buffer.buffer.buffer.length)
1064
+ return false;
1065
+ }
1066
+ else {
1067
+ for (let i = 0; i < this.index; i++)
1068
+ if (buffer.buffer.buffer[i + 3] < this.index)
1069
+ return false;
1070
+ }
1071
+ ({ index, parent } = buffer);
1072
+ }
1073
+ else {
1074
+ ({ index, _parent: parent } = this._tree);
1075
+ }
1076
+ for (; parent; { index, _parent: parent } = parent) {
1077
+ if (index > -1)
1078
+ for (let i = index + dir, e = dir < 0 ? -1 : parent._tree.children.length; i != e; i += dir) {
1079
+ let child = parent._tree.children[i];
1080
+ if ((this.mode & IterMode.IncludeAnonymous) ||
1081
+ child instanceof TreeBuffer ||
1082
+ !child.type.isAnonymous ||
1083
+ hasChild(child))
1084
+ return false;
1085
+ }
1086
+ }
1087
+ return true;
1088
+ }
1089
+ move(dir, enter) {
1090
+ if (enter && this.enterChild(dir, 0, 4 /* Side.DontCare */))
1091
+ return true;
1092
+ for (;;) {
1093
+ if (this.sibling(dir))
1094
+ return true;
1095
+ if (this.atLastNode(dir) || !this.parent())
1096
+ return false;
1097
+ }
1098
+ }
1099
+ /**
1100
+ Move to the next node in a
1101
+ [pre-order](https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR)
1102
+ traversal, going from a node to its first child or, if the
1103
+ current node is empty or `enter` is false, its next sibling or
1104
+ the next sibling of the first parent node that has one.
1105
+ */
1106
+ next(enter = true) { return this.move(1, enter); }
1107
+ /**
1108
+ Move to the next node in a last-to-first pre-order traveral. A
1109
+ node is followed by its last child or, if it has none, its
1110
+ previous sibling or the previous sibling of the first parent
1111
+ node that has one.
1112
+ */
1113
+ prev(enter = true) { return this.move(-1, enter); }
1114
+ /**
1115
+ Move the cursor to the innermost node that covers `pos`. If
1116
+ `side` is -1, it will enter nodes that end at `pos`. If it is 1,
1117
+ it will enter nodes that start at `pos`.
1118
+ */
1119
+ moveTo(pos, side = 0) {
1120
+ // Move up to a node that actually holds the position, if possible
1121
+ while (this.from == this.to ||
1122
+ (side < 1 ? this.from >= pos : this.from > pos) ||
1123
+ (side > -1 ? this.to <= pos : this.to < pos))
1124
+ if (!this.parent())
1125
+ break;
1126
+ // Then scan down into child nodes as far as possible
1127
+ while (this.enterChild(1, pos, side)) { }
1128
+ return this;
1129
+ }
1130
+ /**
1131
+ Get a [syntax node](#common.SyntaxNode) at the cursor's current
1132
+ position.
1133
+ */
1134
+ get node() {
1135
+ if (!this.buffer)
1136
+ return this._tree;
1137
+ let cache = this.bufferNode, result = null, depth = 0;
1138
+ if (cache && cache.context == this.buffer) {
1139
+ scan: for (let index = this.index, d = this.stack.length; d >= 0;) {
1140
+ for (let c = cache; c; c = c._parent)
1141
+ if (c.index == index) {
1142
+ if (index == this.index)
1143
+ return c;
1144
+ result = c;
1145
+ depth = d + 1;
1146
+ break scan;
1147
+ }
1148
+ index = this.stack[--d];
1149
+ }
1150
+ }
1151
+ for (let i = depth; i < this.stack.length; i++)
1152
+ result = new BufferNode(this.buffer, result, this.stack[i]);
1153
+ return this.bufferNode = new BufferNode(this.buffer, result, this.index);
1154
+ }
1155
+ /**
1156
+ Get the [tree](#common.Tree) that represents the current node, if
1157
+ any. Will return null when the node is in a [tree
1158
+ buffer](#common.TreeBuffer).
1159
+ */
1160
+ get tree() {
1161
+ return this.buffer ? null : this._tree._tree;
1162
+ }
1163
+ /**
1164
+ Iterate over the current node and all its descendants, calling
1165
+ `enter` when entering a node and `leave`, if given, when leaving
1166
+ one. When `enter` returns `false`, any children of that node are
1167
+ skipped, and `leave` isn't called for it.
1168
+ */
1169
+ iterate(enter, leave) {
1170
+ for (let depth = 0;;) {
1171
+ let mustLeave = false;
1172
+ if (this.type.isAnonymous || enter(this) !== false) {
1173
+ if (this.firstChild()) {
1174
+ depth++;
1175
+ continue;
1176
+ }
1177
+ if (!this.type.isAnonymous)
1178
+ mustLeave = true;
1179
+ }
1180
+ for (;;) {
1181
+ if (mustLeave && leave)
1182
+ leave(this);
1183
+ mustLeave = this.type.isAnonymous;
1184
+ if (this.nextSibling())
1185
+ break;
1186
+ if (!depth)
1187
+ return;
1188
+ this.parent();
1189
+ depth--;
1190
+ mustLeave = true;
1191
+ }
1192
+ }
1193
+ }
1194
+ /**
1195
+ Test whether the current node matches a given context—a sequence
1196
+ of direct parent node names. Empty strings in the context array
1197
+ are treated as wildcards.
1198
+ */
1199
+ matchContext(context) {
1200
+ if (!this.buffer)
1201
+ return matchNodeContext(this.node, context);
1202
+ let { buffer } = this.buffer, { types } = buffer.set;
1203
+ for (let i = context.length - 1, d = this.stack.length - 1; i >= 0; d--) {
1204
+ if (d < 0)
1205
+ return matchNodeContext(this.node, context, i);
1206
+ let type = types[buffer.buffer[this.stack[d]]];
1207
+ if (!type.isAnonymous) {
1208
+ if (context[i] && context[i] != type.name)
1209
+ return false;
1210
+ i--;
1211
+ }
1212
+ }
1213
+ return true;
1214
+ }
1215
+ }
1216
+ function hasChild(tree) {
1217
+ return tree.children.some(ch => ch instanceof TreeBuffer || !ch.type.isAnonymous || hasChild(ch));
1218
+ }
1219
+ function buildTree(data) {
1220
+ var _a;
1221
+ let { buffer, nodeSet, maxBufferLength = DefaultBufferLength, reused = [], minRepeatType = nodeSet.types.length } = data;
1222
+ let cursor = Array.isArray(buffer) ? new FlatBufferCursor(buffer, buffer.length) : buffer;
1223
+ let types = nodeSet.types;
1224
+ let contextHash = 0, lookAhead = 0;
1225
+ function takeNode(parentStart, minPos, children, positions, inRepeat, depth) {
1226
+ let { id, start, end, size } = cursor;
1227
+ let lookAheadAtStart = lookAhead;
1228
+ while (size < 0) {
1229
+ cursor.next();
1230
+ if (size == -1 /* SpecialRecord.Reuse */) {
1231
+ let node = reused[id];
1232
+ children.push(node);
1233
+ positions.push(start - parentStart);
1234
+ return;
1235
+ }
1236
+ else if (size == -3 /* SpecialRecord.ContextChange */) { // Context change
1237
+ contextHash = id;
1238
+ return;
1239
+ }
1240
+ else if (size == -4 /* SpecialRecord.LookAhead */) {
1241
+ lookAhead = id;
1242
+ return;
1243
+ }
1244
+ else {
1245
+ throw new RangeError(`Unrecognized record size: ${size}`);
1246
+ }
1247
+ }
1248
+ let type = types[id], node, buffer;
1249
+ let startPos = start - parentStart;
1250
+ if (end - start <= maxBufferLength && (buffer = findBufferSize(cursor.pos - minPos, inRepeat))) {
1251
+ // Small enough for a buffer, and no reused nodes inside
1252
+ let data = new Uint16Array(buffer.size - buffer.skip);
1253
+ let endPos = cursor.pos - buffer.size, index = data.length;
1254
+ while (cursor.pos > endPos)
1255
+ index = copyToBuffer(buffer.start, data, index);
1256
+ node = new TreeBuffer(data, end - buffer.start, nodeSet);
1257
+ startPos = buffer.start - parentStart;
1258
+ }
1259
+ else { // Make it a node
1260
+ let endPos = cursor.pos - size;
1261
+ cursor.next();
1262
+ let localChildren = [], localPositions = [];
1263
+ let localInRepeat = id >= minRepeatType ? id : -1;
1264
+ let lastGroup = 0, lastEnd = end;
1265
+ while (cursor.pos > endPos) {
1266
+ if (localInRepeat >= 0 && cursor.id == localInRepeat && cursor.size >= 0) {
1267
+ if (cursor.end <= lastEnd - maxBufferLength) {
1268
+ makeRepeatLeaf(localChildren, localPositions, start, lastGroup, cursor.end, lastEnd, localInRepeat, lookAheadAtStart);
1269
+ lastGroup = localChildren.length;
1270
+ lastEnd = cursor.end;
1271
+ }
1272
+ cursor.next();
1273
+ }
1274
+ else if (depth > 2500 /* CutOff.Depth */) {
1275
+ takeFlatNode(start, endPos, localChildren, localPositions);
1276
+ }
1277
+ else {
1278
+ takeNode(start, endPos, localChildren, localPositions, localInRepeat, depth + 1);
1279
+ }
1280
+ }
1281
+ if (localInRepeat >= 0 && lastGroup > 0 && lastGroup < localChildren.length)
1282
+ makeRepeatLeaf(localChildren, localPositions, start, lastGroup, start, lastEnd, localInRepeat, lookAheadAtStart);
1283
+ localChildren.reverse();
1284
+ localPositions.reverse();
1285
+ if (localInRepeat > -1 && lastGroup > 0) {
1286
+ let make = makeBalanced(type);
1287
+ node = balanceRange(type, localChildren, localPositions, 0, localChildren.length, 0, end - start, make, make);
1288
+ }
1289
+ else {
1290
+ node = makeTree(type, localChildren, localPositions, end - start, lookAheadAtStart - end);
1291
+ }
1292
+ }
1293
+ children.push(node);
1294
+ positions.push(startPos);
1295
+ }
1296
+ function takeFlatNode(parentStart, minPos, children, positions) {
1297
+ let nodes = []; // Temporary, inverted array of leaf nodes found, with absolute positions
1298
+ let nodeCount = 0, stopAt = -1;
1299
+ while (cursor.pos > minPos) {
1300
+ let { id, start, end, size } = cursor;
1301
+ if (size > 4) { // Not a leaf
1302
+ cursor.next();
1303
+ }
1304
+ else if (stopAt > -1 && start < stopAt) {
1305
+ break;
1306
+ }
1307
+ else {
1308
+ if (stopAt < 0)
1309
+ stopAt = end - maxBufferLength;
1310
+ nodes.push(id, start, end);
1311
+ nodeCount++;
1312
+ cursor.next();
1313
+ }
1314
+ }
1315
+ if (nodeCount) {
1316
+ let buffer = new Uint16Array(nodeCount * 4);
1317
+ let start = nodes[nodes.length - 2];
1318
+ for (let i = nodes.length - 3, j = 0; i >= 0; i -= 3) {
1319
+ buffer[j++] = nodes[i];
1320
+ buffer[j++] = nodes[i + 1] - start;
1321
+ buffer[j++] = nodes[i + 2] - start;
1322
+ buffer[j++] = j;
1323
+ }
1324
+ children.push(new TreeBuffer(buffer, nodes[2] - start, nodeSet));
1325
+ positions.push(start - parentStart);
1326
+ }
1327
+ }
1328
+ function makeBalanced(type) {
1329
+ return (children, positions, length) => {
1330
+ let lookAhead = 0, lastI = children.length - 1, last, lookAheadProp;
1331
+ if (lastI >= 0 && (last = children[lastI]) instanceof Tree) {
1332
+ if (!lastI && last.type == type && last.length == length)
1333
+ return last;
1334
+ if (lookAheadProp = last.prop(NodeProp.lookAhead))
1335
+ lookAhead = positions[lastI] + last.length + lookAheadProp;
1336
+ }
1337
+ return makeTree(type, children, positions, length, lookAhead);
1338
+ };
1339
+ }
1340
+ function makeRepeatLeaf(children, positions, base, i, from, to, type, lookAhead) {
1341
+ let localChildren = [], localPositions = [];
1342
+ while (children.length > i) {
1343
+ localChildren.push(children.pop());
1344
+ localPositions.push(positions.pop() + base - from);
1345
+ }
1346
+ children.push(makeTree(nodeSet.types[type], localChildren, localPositions, to - from, lookAhead - to));
1347
+ positions.push(from - base);
1348
+ }
1349
+ function makeTree(type, children, positions, length, lookAhead = 0, props) {
1350
+ if (contextHash) {
1351
+ let pair = [NodeProp.contextHash, contextHash];
1352
+ props = props ? [pair].concat(props) : [pair];
1353
+ }
1354
+ if (lookAhead > 25) {
1355
+ let pair = [NodeProp.lookAhead, lookAhead];
1356
+ props = props ? [pair].concat(props) : [pair];
1357
+ }
1358
+ return new Tree(type, children, positions, length, props);
1359
+ }
1360
+ function findBufferSize(maxSize, inRepeat) {
1361
+ // Scan through the buffer to find previous siblings that fit
1362
+ // together in a TreeBuffer, and don't contain any reused nodes
1363
+ // (which can't be stored in a buffer).
1364
+ // If `inRepeat` is > -1, ignore node boundaries of that type for
1365
+ // nesting, but make sure the end falls either at the start
1366
+ // (`maxSize`) or before such a node.
1367
+ let fork = cursor.fork();
1368
+ let size = 0, start = 0, skip = 0, minStart = fork.end - maxBufferLength;
1369
+ let result = { size: 0, start: 0, skip: 0 };
1370
+ scan: for (let minPos = fork.pos - maxSize; fork.pos > minPos;) {
1371
+ let nodeSize = fork.size;
1372
+ // Pretend nested repeat nodes of the same type don't exist
1373
+ if (fork.id == inRepeat && nodeSize >= 0) {
1374
+ // Except that we store the current state as a valid return
1375
+ // value.
1376
+ result.size = size;
1377
+ result.start = start;
1378
+ result.skip = skip;
1379
+ skip += 4;
1380
+ size += 4;
1381
+ fork.next();
1382
+ continue;
1383
+ }
1384
+ let startPos = fork.pos - nodeSize;
1385
+ if (nodeSize < 0 || startPos < minPos || fork.start < minStart)
1386
+ break;
1387
+ let localSkipped = fork.id >= minRepeatType ? 4 : 0;
1388
+ let nodeStart = fork.start;
1389
+ fork.next();
1390
+ while (fork.pos > startPos) {
1391
+ if (fork.size < 0) {
1392
+ if (fork.size == -3 /* SpecialRecord.ContextChange */)
1393
+ localSkipped += 4;
1394
+ else
1395
+ break scan;
1396
+ }
1397
+ else if (fork.id >= minRepeatType) {
1398
+ localSkipped += 4;
1399
+ }
1400
+ fork.next();
1401
+ }
1402
+ start = nodeStart;
1403
+ size += nodeSize;
1404
+ skip += localSkipped;
1405
+ }
1406
+ if (inRepeat < 0 || size == maxSize) {
1407
+ result.size = size;
1408
+ result.start = start;
1409
+ result.skip = skip;
1410
+ }
1411
+ return result.size > 4 ? result : undefined;
1412
+ }
1413
+ function copyToBuffer(bufferStart, buffer, index) {
1414
+ let { id, start, end, size } = cursor;
1415
+ cursor.next();
1416
+ if (size >= 0 && id < minRepeatType) {
1417
+ let startIndex = index;
1418
+ if (size > 4) {
1419
+ let endPos = cursor.pos - (size - 4);
1420
+ while (cursor.pos > endPos)
1421
+ index = copyToBuffer(bufferStart, buffer, index);
1422
+ }
1423
+ buffer[--index] = startIndex;
1424
+ buffer[--index] = end - bufferStart;
1425
+ buffer[--index] = start - bufferStart;
1426
+ buffer[--index] = id;
1427
+ }
1428
+ else if (size == -3 /* SpecialRecord.ContextChange */) {
1429
+ contextHash = id;
1430
+ }
1431
+ else if (size == -4 /* SpecialRecord.LookAhead */) {
1432
+ lookAhead = id;
1433
+ }
1434
+ return index;
1435
+ }
1436
+ let children = [], positions = [];
1437
+ while (cursor.pos > 0)
1438
+ takeNode(data.start || 0, data.bufferStart || 0, children, positions, -1, 0);
1439
+ let length = (_a = data.length) !== null && _a !== void 0 ? _a : (children.length ? positions[0] + children[0].length : 0);
1440
+ return new Tree(types[data.topID], children.reverse(), positions.reverse(), length);
1441
+ }
1442
+ const nodeSizeCache = new WeakMap;
1443
+ function nodeSize(balanceType, node) {
1444
+ if (!balanceType.isAnonymous || node instanceof TreeBuffer || node.type != balanceType)
1445
+ return 1;
1446
+ let size = nodeSizeCache.get(node);
1447
+ if (size == null) {
1448
+ size = 1;
1449
+ for (let child of node.children) {
1450
+ if (child.type != balanceType || !(child instanceof Tree)) {
1451
+ size = 1;
1452
+ break;
1453
+ }
1454
+ size += nodeSize(balanceType, child);
1455
+ }
1456
+ nodeSizeCache.set(node, size);
1457
+ }
1458
+ return size;
1459
+ }
1460
+ function balanceRange(
1461
+ // The type the balanced tree's inner nodes.
1462
+ balanceType,
1463
+ // The direct children and their positions
1464
+ children, positions,
1465
+ // The index range in children/positions to use
1466
+ from, to,
1467
+ // The start position of the nodes, relative to their parent.
1468
+ start,
1469
+ // Length of the outer node
1470
+ length,
1471
+ // Function to build the top node of the balanced tree
1472
+ mkTop,
1473
+ // Function to build internal nodes for the balanced tree
1474
+ mkTree) {
1475
+ let total = 0;
1476
+ for (let i = from; i < to; i++)
1477
+ total += nodeSize(balanceType, children[i]);
1478
+ let maxChild = Math.ceil((total * 1.5) / 8 /* Balance.BranchFactor */);
1479
+ let localChildren = [], localPositions = [];
1480
+ function divide(children, positions, from, to, offset) {
1481
+ for (let i = from; i < to;) {
1482
+ let groupFrom = i, groupStart = positions[i], groupSize = nodeSize(balanceType, children[i]);
1483
+ i++;
1484
+ for (; i < to; i++) {
1485
+ let nextSize = nodeSize(balanceType, children[i]);
1486
+ if (groupSize + nextSize >= maxChild)
1487
+ break;
1488
+ groupSize += nextSize;
1489
+ }
1490
+ if (i == groupFrom + 1) {
1491
+ if (groupSize > maxChild) {
1492
+ let only = children[groupFrom]; // Only trees can have a size > 1
1493
+ divide(only.children, only.positions, 0, only.children.length, positions[groupFrom] + offset);
1494
+ continue;
1495
+ }
1496
+ localChildren.push(children[groupFrom]);
1497
+ }
1498
+ else {
1499
+ let length = positions[i - 1] + children[i - 1].length - groupStart;
1500
+ localChildren.push(balanceRange(balanceType, children, positions, groupFrom, i, groupStart, length, null, mkTree));
1501
+ }
1502
+ localPositions.push(groupStart + offset - start);
1503
+ }
1504
+ }
1505
+ divide(children, positions, from, to, 0);
1506
+ return (mkTop || mkTree)(localChildren, localPositions, length);
1507
+ }
1508
+
1509
+ /**
1510
+ Tree fragments are used during [incremental
1511
+ parsing](#common.Parser.startParse) to track parts of old trees
1512
+ that can be reused in a new parse. An array of fragments is used
1513
+ to track regions of an old tree whose nodes might be reused in new
1514
+ parses. Use the static
1515
+ [`applyChanges`](#common.TreeFragment^applyChanges) method to
1516
+ update fragments for document changes.
1517
+ */
1518
+ class TreeFragment {
1519
+ /**
1520
+ Construct a tree fragment. You'll usually want to use
1521
+ [`addTree`](#common.TreeFragment^addTree) and
1522
+ [`applyChanges`](#common.TreeFragment^applyChanges) instead of
1523
+ calling this directly.
1524
+ */
1525
+ constructor(
1526
+ /**
1527
+ The start of the unchanged range pointed to by this fragment.
1528
+ This refers to an offset in the _updated_ document (as opposed
1529
+ to the original tree).
1530
+ */
1531
+ from,
1532
+ /**
1533
+ The end of the unchanged range.
1534
+ */
1535
+ to,
1536
+ /**
1537
+ The tree that this fragment is based on.
1538
+ */
1539
+ tree,
1540
+ /**
1541
+ The offset between the fragment's tree and the document that
1542
+ this fragment can be used against. Add this when going from
1543
+ document to tree positions, subtract it to go from tree to
1544
+ document positions.
1545
+ */
1546
+ offset, openStart = false, openEnd = false) {
1547
+ this.from = from;
1548
+ this.to = to;
1549
+ this.tree = tree;
1550
+ this.offset = offset;
1551
+ this.open = (openStart ? 1 /* Open.Start */ : 0) | (openEnd ? 2 /* Open.End */ : 0);
1552
+ }
1553
+ /**
1554
+ Whether the start of the fragment represents the start of a
1555
+ parse, or the end of a change. (In the second case, it may not
1556
+ be safe to reuse some nodes at the start, depending on the
1557
+ parsing algorithm.)
1558
+ */
1559
+ get openStart() { return (this.open & 1 /* Open.Start */) > 0; }
1560
+ /**
1561
+ Whether the end of the fragment represents the end of a
1562
+ full-document parse, or the start of a change.
1563
+ */
1564
+ get openEnd() { return (this.open & 2 /* Open.End */) > 0; }
1565
+ /**
1566
+ Create a set of fragments from a freshly parsed tree, or update
1567
+ an existing set of fragments by replacing the ones that overlap
1568
+ with a tree with content from the new tree. When `partial` is
1569
+ true, the parse is treated as incomplete, and the resulting
1570
+ fragment has [`openEnd`](#common.TreeFragment.openEnd) set to
1571
+ true.
1572
+ */
1573
+ static addTree(tree, fragments = [], partial = false) {
1574
+ let result = [new TreeFragment(0, tree.length, tree, 0, false, partial)];
1575
+ for (let f of fragments)
1576
+ if (f.to > tree.length)
1577
+ result.push(f);
1578
+ return result;
1579
+ }
1580
+ /**
1581
+ Apply a set of edits to an array of fragments, removing or
1582
+ splitting fragments as necessary to remove edited ranges, and
1583
+ adjusting offsets for fragments that moved.
1584
+ */
1585
+ static applyChanges(fragments, changes, minGap = 128) {
1586
+ if (!changes.length)
1587
+ return fragments;
1588
+ let result = [];
1589
+ let fI = 1, nextF = fragments.length ? fragments[0] : null;
1590
+ for (let cI = 0, pos = 0, off = 0;; cI++) {
1591
+ let nextC = cI < changes.length ? changes[cI] : null;
1592
+ let nextPos = nextC ? nextC.fromA : 1e9;
1593
+ if (nextPos - pos >= minGap)
1594
+ while (nextF && nextF.from < nextPos) {
1595
+ let cut = nextF;
1596
+ if (pos >= cut.from || nextPos <= cut.to || off) {
1597
+ let fFrom = Math.max(cut.from, pos) - off, fTo = Math.min(cut.to, nextPos) - off;
1598
+ cut = fFrom >= fTo ? null : new TreeFragment(fFrom, fTo, cut.tree, cut.offset + off, cI > 0, !!nextC);
1599
+ }
1600
+ if (cut)
1601
+ result.push(cut);
1602
+ if (nextF.to > nextPos)
1603
+ break;
1604
+ nextF = fI < fragments.length ? fragments[fI++] : null;
1605
+ }
1606
+ if (!nextC)
1607
+ break;
1608
+ pos = nextC.toA;
1609
+ off = nextC.toA - nextC.toB;
1610
+ }
1611
+ return result;
1612
+ }
1613
+ }
1614
+ /**
1615
+ A superclass that parsers should extend.
1616
+ */
1617
+ class Parser {
1618
+ /**
1619
+ Start a parse, returning a [partial parse](#common.PartialParse)
1620
+ object. [`fragments`](#common.TreeFragment) can be passed in to
1621
+ make the parse incremental.
1622
+
1623
+ By default, the entire input is parsed. You can pass `ranges`,
1624
+ which should be a sorted array of non-empty, non-overlapping
1625
+ ranges, to parse only those ranges. The tree returned in that
1626
+ case will start at `ranges[0].from`.
1627
+ */
1628
+ startParse(input, fragments, ranges) {
1629
+ if (typeof input == "string")
1630
+ input = new StringInput(input);
1631
+ ranges = !ranges ? [new Range(0, input.length)] : ranges.length ? ranges.map(r => new Range(r.from, r.to)) : [new Range(0, 0)];
1632
+ return this.createParse(input, fragments || [], ranges);
1633
+ }
1634
+ /**
1635
+ Run a full parse, returning the resulting tree.
1636
+ */
1637
+ parse(input, fragments, ranges) {
1638
+ let parse = this.startParse(input, fragments, ranges);
1639
+ for (;;) {
1640
+ let done = parse.advance();
1641
+ if (done)
1642
+ return done;
1643
+ }
1644
+ }
1645
+ }
1646
+ class StringInput {
1647
+ constructor(string) {
1648
+ this.string = string;
1649
+ }
1650
+ get length() { return this.string.length; }
1651
+ chunk(from) { return this.string.slice(from); }
1652
+ get lineChunks() { return false; }
1653
+ read(from, to) { return this.string.slice(from, to); }
1654
+ }
1655
+ new NodeProp({ perNode: true });
1656
+
1657
+ export { DefaultBufferLength, IterMode, MountedTree, NodeProp, NodeType, Parser, Tree, TreeBuffer, TreeCursor, TreeFragment };
1658
+ //# sourceMappingURL=index.js.map