@hpcc-js/observablehq-compiler 3.4.0 → 3.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/dist/dot-DK4iDc2-.js +40 -0
  2. package/dist/dot-DK4iDc2-.js.map +1 -0
  3. package/dist/duckdb-DkM9_nXT.js +298 -0
  4. package/dist/duckdb-DkM9_nXT.js.map +1 -0
  5. package/dist/highlight-Bv8PkwN-.js +2402 -0
  6. package/dist/highlight-Bv8PkwN-.js.map +1 -0
  7. package/dist/index-B7vRtYlU.js +1807 -0
  8. package/dist/index-B7vRtYlU.js.map +1 -0
  9. package/dist/index-COHaJzee.js +260 -0
  10. package/dist/index-COHaJzee.js.map +1 -0
  11. package/dist/index-CP3HrqN0.js +1815 -0
  12. package/dist/index-CP3HrqN0.js.map +1 -0
  13. package/dist/index-D3TL70UM.js +163 -0
  14. package/dist/index-D3TL70UM.js.map +1 -0
  15. package/dist/index-DNwLE6Kk.js +198 -0
  16. package/dist/index-DNwLE6Kk.js.map +1 -0
  17. package/dist/index.js +4381 -4944
  18. package/dist/index.js.map +1 -1
  19. package/dist/inputs-CsCXZHQ8.js +3 -0
  20. package/dist/inputs-CsCXZHQ8.js.map +1 -0
  21. package/dist/leaflet-CkvVhxBL.js +9 -0
  22. package/dist/leaflet-CkvVhxBL.js.map +1 -0
  23. package/dist/mapboxgl-C0i2HzjJ.js +9 -0
  24. package/dist/mapboxgl-C0i2HzjJ.js.map +1 -0
  25. package/dist/md-Bxvu6Hld.js +7603 -0
  26. package/dist/md-Bxvu6Hld.js.map +1 -0
  27. package/dist/mermaid-CFg6sgdO.js +17 -0
  28. package/dist/mermaid-CFg6sgdO.js.map +1 -0
  29. package/dist/node/index.cjs +10 -6
  30. package/dist/node/index.cjs.map +4 -4
  31. package/dist/node/index.js +8 -4
  32. package/dist/node/index.js.map +4 -4
  33. package/dist/runtime.js +2501 -0
  34. package/dist/runtime.js.map +1 -0
  35. package/dist/tex-ayRXOMLZ.js +24 -0
  36. package/dist/tex-ayRXOMLZ.js.map +1 -0
  37. package/dist/vega-lite-CESXoehe.js +8 -0
  38. package/dist/vega-lite-CESXoehe.js.map +1 -0
  39. package/package.json +12 -10
  40. package/src/__package__.ts +2 -2
  41. package/src/compiler.ts +2 -16
  42. package/src/cst.ts +1 -2
  43. package/src/index.ts +2 -3
  44. package/src/kit/compiler.ts +41 -0
  45. package/src/kit/index.ts +3 -0
  46. package/src/kit/runtime.ts +57 -0
  47. package/src/kit/util.ts +157 -0
  48. package/src/util.ts +1 -134
  49. package/types/compiler.d.ts +1 -3
  50. package/types/index.d.ts +2 -2
  51. package/types/kit/compiler.d.ts +6 -0
  52. package/types/kit/index.d.ts +3 -0
  53. package/types/kit/runtime.d.ts +13 -0
  54. package/types/kit/util.d.ts +52 -0
  55. package/types/util.d.ts +1 -49
  56. package/dist/index.umd.cjs +0 -5
  57. package/dist/index.umd.cjs.map +0 -1
  58. package/dist/node/index.css +0 -2
  59. package/dist/node/index.css.map +0 -7
  60. package/src/index.css +0 -460
@@ -0,0 +1,2402 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+ const DefaultBufferLength = 1024;
4
+ let nextPropID = 0;
5
+ const _Range = class _Range {
6
+ constructor(from, to) {
7
+ this.from = from;
8
+ this.to = to;
9
+ }
10
+ };
11
+ __name(_Range, "Range");
12
+ let Range = _Range;
13
+ const _NodeProp = class _NodeProp {
14
+ /**
15
+ Create a new node prop type.
16
+ */
17
+ constructor(config = {}) {
18
+ this.id = nextPropID++;
19
+ this.perNode = !!config.perNode;
20
+ this.deserialize = config.deserialize || (() => {
21
+ throw new Error("This node type doesn't define a deserialize function");
22
+ });
23
+ }
24
+ /**
25
+ This is meant to be used with
26
+ [`NodeSet.extend`](#common.NodeSet.extend) or
27
+ [`LRParser.configure`](#lr.ParserConfig.props) to compute
28
+ prop values for each node type in the set. Takes a [match
29
+ object](#common.NodeType^match) or function that returns undefined
30
+ if the node type doesn't get this prop, and the prop's value if
31
+ it does.
32
+ */
33
+ add(match) {
34
+ if (this.perNode)
35
+ throw new RangeError("Can't add per-node props to node types");
36
+ if (typeof match != "function")
37
+ match = NodeType.match(match);
38
+ return (type) => {
39
+ let result = match(type);
40
+ return result === void 0 ? null : [this, result];
41
+ };
42
+ }
43
+ };
44
+ __name(_NodeProp, "NodeProp");
45
+ let NodeProp = _NodeProp;
46
+ NodeProp.closedBy = new NodeProp({ deserialize: /* @__PURE__ */ __name((str) => str.split(" "), "deserialize") });
47
+ NodeProp.openedBy = new NodeProp({ deserialize: /* @__PURE__ */ __name((str) => str.split(" "), "deserialize") });
48
+ NodeProp.group = new NodeProp({ deserialize: /* @__PURE__ */ __name((str) => str.split(" "), "deserialize") });
49
+ NodeProp.isolate = new NodeProp({ deserialize: /* @__PURE__ */ __name((value) => {
50
+ if (value && value != "rtl" && value != "ltr" && value != "auto")
51
+ throw new RangeError("Invalid value for isolate: " + value);
52
+ return value || "auto";
53
+ }, "deserialize") });
54
+ NodeProp.contextHash = new NodeProp({ perNode: true });
55
+ NodeProp.lookAhead = new NodeProp({ perNode: true });
56
+ NodeProp.mounted = new NodeProp({ perNode: true });
57
+ const _MountedTree = class _MountedTree {
58
+ constructor(tree, overlay, parser) {
59
+ this.tree = tree;
60
+ this.overlay = overlay;
61
+ this.parser = parser;
62
+ }
63
+ /**
64
+ @internal
65
+ */
66
+ static get(tree) {
67
+ return tree && tree.props && tree.props[NodeProp.mounted.id];
68
+ }
69
+ };
70
+ __name(_MountedTree, "MountedTree");
71
+ let MountedTree = _MountedTree;
72
+ const noProps = /* @__PURE__ */ Object.create(null);
73
+ const _NodeType = class _NodeType {
74
+ /**
75
+ @internal
76
+ */
77
+ constructor(name2, props, id, flags = 0) {
78
+ this.name = name2;
79
+ this.props = props;
80
+ this.id = id;
81
+ this.flags = flags;
82
+ }
83
+ /**
84
+ Define a node type.
85
+ */
86
+ static define(spec) {
87
+ let props = spec.props && spec.props.length ? /* @__PURE__ */ Object.create(null) : noProps;
88
+ let flags = (spec.top ? 1 : 0) | (spec.skipped ? 2 : 0) | (spec.error ? 4 : 0) | (spec.name == null ? 8 : 0);
89
+ let type = new _NodeType(spec.name || "", props, spec.id, flags);
90
+ if (spec.props)
91
+ for (let src of spec.props) {
92
+ if (!Array.isArray(src))
93
+ src = src(type);
94
+ if (src) {
95
+ if (src[0].perNode)
96
+ throw new RangeError("Can't store a per-node prop on a node type");
97
+ props[src[0].id] = src[1];
98
+ }
99
+ }
100
+ return type;
101
+ }
102
+ /**
103
+ Retrieves a node prop for this type. Will return `undefined` if
104
+ the prop isn't present on this node.
105
+ */
106
+ prop(prop) {
107
+ return this.props[prop.id];
108
+ }
109
+ /**
110
+ True when this is the top node of a grammar.
111
+ */
112
+ get isTop() {
113
+ return (this.flags & 1) > 0;
114
+ }
115
+ /**
116
+ True when this node is produced by a skip rule.
117
+ */
118
+ get isSkipped() {
119
+ return (this.flags & 2) > 0;
120
+ }
121
+ /**
122
+ Indicates whether this is an error node.
123
+ */
124
+ get isError() {
125
+ return (this.flags & 4) > 0;
126
+ }
127
+ /**
128
+ When true, this node type doesn't correspond to a user-declared
129
+ named node, for example because it is used to cache repetition.
130
+ */
131
+ get isAnonymous() {
132
+ return (this.flags & 8) > 0;
133
+ }
134
+ /**
135
+ Returns true when this node's name or one of its
136
+ [groups](#common.NodeProp^group) matches the given string.
137
+ */
138
+ is(name2) {
139
+ if (typeof name2 == "string") {
140
+ if (this.name == name2)
141
+ return true;
142
+ let group = this.prop(NodeProp.group);
143
+ return group ? group.indexOf(name2) > -1 : false;
144
+ }
145
+ return this.id == name2;
146
+ }
147
+ /**
148
+ Create a function from node types to arbitrary values by
149
+ specifying an object whose property names are node or
150
+ [group](#common.NodeProp^group) names. Often useful with
151
+ [`NodeProp.add`](#common.NodeProp.add). You can put multiple
152
+ names, separated by spaces, in a single property name to map
153
+ multiple node names to a single value.
154
+ */
155
+ static match(map) {
156
+ let direct = /* @__PURE__ */ Object.create(null);
157
+ for (let prop in map)
158
+ for (let name2 of prop.split(" "))
159
+ direct[name2] = map[prop];
160
+ return (node) => {
161
+ for (let groups = node.prop(NodeProp.group), i = -1; i < (groups ? groups.length : 0); i++) {
162
+ let found = direct[i < 0 ? node.name : groups[i]];
163
+ if (found)
164
+ return found;
165
+ }
166
+ };
167
+ }
168
+ };
169
+ __name(_NodeType, "NodeType");
170
+ let NodeType = _NodeType;
171
+ NodeType.none = new NodeType(
172
+ "",
173
+ /* @__PURE__ */ Object.create(null),
174
+ 0,
175
+ 8
176
+ /* NodeFlag.Anonymous */
177
+ );
178
+ const _NodeSet = class _NodeSet {
179
+ /**
180
+ Create a set with the given types. The `id` property of each
181
+ type should correspond to its position within the array.
182
+ */
183
+ constructor(types) {
184
+ this.types = types;
185
+ for (let i = 0; i < types.length; i++)
186
+ if (types[i].id != i)
187
+ throw new RangeError("Node type ids should correspond to array positions when creating a node set");
188
+ }
189
+ /**
190
+ Create a copy of this set with some node properties added. The
191
+ arguments to this method can be created with
192
+ [`NodeProp.add`](#common.NodeProp.add).
193
+ */
194
+ extend(...props) {
195
+ let newTypes = [];
196
+ for (let type of this.types) {
197
+ let newProps = null;
198
+ for (let source of props) {
199
+ let add = source(type);
200
+ if (add) {
201
+ if (!newProps)
202
+ newProps = Object.assign({}, type.props);
203
+ newProps[add[0].id] = add[1];
204
+ }
205
+ }
206
+ newTypes.push(newProps ? new NodeType(type.name, newProps, type.id, type.flags) : type);
207
+ }
208
+ return new _NodeSet(newTypes);
209
+ }
210
+ };
211
+ __name(_NodeSet, "NodeSet");
212
+ let NodeSet = _NodeSet;
213
+ const CachedNode = /* @__PURE__ */ new WeakMap(), CachedInnerNode = /* @__PURE__ */ new WeakMap();
214
+ var IterMode;
215
+ (function(IterMode2) {
216
+ IterMode2[IterMode2["ExcludeBuffers"] = 1] = "ExcludeBuffers";
217
+ IterMode2[IterMode2["IncludeAnonymous"] = 2] = "IncludeAnonymous";
218
+ IterMode2[IterMode2["IgnoreMounts"] = 4] = "IgnoreMounts";
219
+ IterMode2[IterMode2["IgnoreOverlays"] = 8] = "IgnoreOverlays";
220
+ })(IterMode || (IterMode = {}));
221
+ const _Tree = class _Tree {
222
+ /**
223
+ Construct a new tree. See also [`Tree.build`](#common.Tree^build).
224
+ */
225
+ constructor(type, children, positions, length, props) {
226
+ this.type = type;
227
+ this.children = children;
228
+ this.positions = positions;
229
+ this.length = length;
230
+ this.props = null;
231
+ if (props && props.length) {
232
+ this.props = /* @__PURE__ */ Object.create(null);
233
+ for (let [prop, value] of props)
234
+ this.props[typeof prop == "number" ? prop : prop.id] = value;
235
+ }
236
+ }
237
+ /**
238
+ @internal
239
+ */
240
+ toString() {
241
+ let mounted = MountedTree.get(this);
242
+ if (mounted && !mounted.overlay)
243
+ return mounted.tree.toString();
244
+ let children = "";
245
+ for (let ch of this.children) {
246
+ let str = ch.toString();
247
+ if (str) {
248
+ if (children)
249
+ children += ",";
250
+ children += str;
251
+ }
252
+ }
253
+ return !this.type.name ? children : (/\W/.test(this.type.name) && !this.type.isError ? JSON.stringify(this.type.name) : this.type.name) + (children.length ? "(" + children + ")" : "");
254
+ }
255
+ /**
256
+ Get a [tree cursor](#common.TreeCursor) positioned at the top of
257
+ the tree. Mode can be used to [control](#common.IterMode) which
258
+ nodes the cursor visits.
259
+ */
260
+ cursor(mode = 0) {
261
+ return new TreeCursor(this.topNode, mode);
262
+ }
263
+ /**
264
+ Get a [tree cursor](#common.TreeCursor) pointing into this tree
265
+ at the given position and side (see
266
+ [`moveTo`](#common.TreeCursor.moveTo).
267
+ */
268
+ cursorAt(pos, side = 0, mode = 0) {
269
+ let scope = CachedNode.get(this) || this.topNode;
270
+ let cursor = new TreeCursor(scope);
271
+ cursor.moveTo(pos, side);
272
+ CachedNode.set(this, cursor._tree);
273
+ return cursor;
274
+ }
275
+ /**
276
+ Get a [syntax node](#common.SyntaxNode) object for the top of the
277
+ tree.
278
+ */
279
+ get topNode() {
280
+ return new TreeNode(this, 0, 0, null);
281
+ }
282
+ /**
283
+ Get the [syntax node](#common.SyntaxNode) at the given position.
284
+ If `side` is -1, this will move into nodes that end at the
285
+ position. If 1, it'll move into nodes that start at the
286
+ position. With 0, it'll only enter nodes that cover the position
287
+ from both sides.
288
+
289
+ Note that this will not enter
290
+ [overlays](#common.MountedTree.overlay), and you often want
291
+ [`resolveInner`](#common.Tree.resolveInner) instead.
292
+ */
293
+ resolve(pos, side = 0) {
294
+ let node = resolveNode(CachedNode.get(this) || this.topNode, pos, side, false);
295
+ CachedNode.set(this, node);
296
+ return node;
297
+ }
298
+ /**
299
+ Like [`resolve`](#common.Tree.resolve), but will enter
300
+ [overlaid](#common.MountedTree.overlay) nodes, producing a syntax node
301
+ pointing into the innermost overlaid tree at the given position
302
+ (with parent links going through all parent structure, including
303
+ the host trees).
304
+ */
305
+ resolveInner(pos, side = 0) {
306
+ let node = resolveNode(CachedInnerNode.get(this) || this.topNode, pos, side, true);
307
+ CachedInnerNode.set(this, node);
308
+ return node;
309
+ }
310
+ /**
311
+ In some situations, it can be useful to iterate through all
312
+ nodes around a position, including those in overlays that don't
313
+ directly cover the position. This method gives you an iterator
314
+ that will produce all nodes, from small to big, around the given
315
+ position.
316
+ */
317
+ resolveStack(pos, side = 0) {
318
+ return stackIterator(this, pos, side);
319
+ }
320
+ /**
321
+ Iterate over the tree and its children, calling `enter` for any
322
+ node that touches the `from`/`to` region (if given) before
323
+ running over such a node's children, and `leave` (if given) when
324
+ leaving the node. When `enter` returns `false`, that node will
325
+ not have its children iterated over (or `leave` called).
326
+ */
327
+ iterate(spec) {
328
+ let { enter, leave, from = 0, to = this.length } = spec;
329
+ let mode = spec.mode || 0, anon = (mode & IterMode.IncludeAnonymous) > 0;
330
+ for (let c = this.cursor(mode | IterMode.IncludeAnonymous); ; ) {
331
+ let entered = false;
332
+ if (c.from <= to && c.to >= from && (!anon && c.type.isAnonymous || enter(c) !== false)) {
333
+ if (c.firstChild())
334
+ continue;
335
+ entered = true;
336
+ }
337
+ for (; ; ) {
338
+ if (entered && leave && (anon || !c.type.isAnonymous))
339
+ leave(c);
340
+ if (c.nextSibling())
341
+ break;
342
+ if (!c.parent())
343
+ return;
344
+ entered = true;
345
+ }
346
+ }
347
+ }
348
+ /**
349
+ Get the value of the given [node prop](#common.NodeProp) for this
350
+ node. Works with both per-node and per-type props.
351
+ */
352
+ prop(prop) {
353
+ return !prop.perNode ? this.type.prop(prop) : this.props ? this.props[prop.id] : void 0;
354
+ }
355
+ /**
356
+ Returns the node's [per-node props](#common.NodeProp.perNode) in a
357
+ format that can be passed to the [`Tree`](#common.Tree)
358
+ constructor.
359
+ */
360
+ get propValues() {
361
+ let result = [];
362
+ if (this.props)
363
+ for (let id in this.props)
364
+ result.push([+id, this.props[id]]);
365
+ return result;
366
+ }
367
+ /**
368
+ Balance the direct children of this tree, producing a copy of
369
+ which may have children grouped into subtrees with type
370
+ [`NodeType.none`](#common.NodeType^none).
371
+ */
372
+ balance(config = {}) {
373
+ return this.children.length <= 8 ? this : 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)));
374
+ }
375
+ /**
376
+ Build a tree from a postfix-ordered buffer of node information,
377
+ or a cursor over such a buffer.
378
+ */
379
+ static build(data) {
380
+ return buildTree(data);
381
+ }
382
+ };
383
+ __name(_Tree, "Tree");
384
+ let Tree = _Tree;
385
+ Tree.empty = new Tree(NodeType.none, [], [], 0);
386
+ const _FlatBufferCursor = class _FlatBufferCursor {
387
+ constructor(buffer, index) {
388
+ this.buffer = buffer;
389
+ this.index = index;
390
+ }
391
+ get id() {
392
+ return this.buffer[this.index - 4];
393
+ }
394
+ get start() {
395
+ return this.buffer[this.index - 3];
396
+ }
397
+ get end() {
398
+ return this.buffer[this.index - 2];
399
+ }
400
+ get size() {
401
+ return this.buffer[this.index - 1];
402
+ }
403
+ get pos() {
404
+ return this.index;
405
+ }
406
+ next() {
407
+ this.index -= 4;
408
+ }
409
+ fork() {
410
+ return new _FlatBufferCursor(this.buffer, this.index);
411
+ }
412
+ };
413
+ __name(_FlatBufferCursor, "FlatBufferCursor");
414
+ let FlatBufferCursor = _FlatBufferCursor;
415
+ const _TreeBuffer = class _TreeBuffer {
416
+ /**
417
+ Create a tree buffer.
418
+ */
419
+ constructor(buffer, length, set) {
420
+ this.buffer = buffer;
421
+ this.length = length;
422
+ this.set = set;
423
+ }
424
+ /**
425
+ @internal
426
+ */
427
+ get type() {
428
+ return NodeType.none;
429
+ }
430
+ /**
431
+ @internal
432
+ */
433
+ toString() {
434
+ let result = [];
435
+ for (let index = 0; index < this.buffer.length; ) {
436
+ result.push(this.childString(index));
437
+ index = this.buffer[index + 3];
438
+ }
439
+ return result.join(",");
440
+ }
441
+ /**
442
+ @internal
443
+ */
444
+ childString(index) {
445
+ let id = this.buffer[index], endIndex = this.buffer[index + 3];
446
+ let type = this.set.types[id], result = type.name;
447
+ if (/\W/.test(result) && !type.isError)
448
+ result = JSON.stringify(result);
449
+ index += 4;
450
+ if (endIndex == index)
451
+ return result;
452
+ let children = [];
453
+ while (index < endIndex) {
454
+ children.push(this.childString(index));
455
+ index = this.buffer[index + 3];
456
+ }
457
+ return result + "(" + children.join(",") + ")";
458
+ }
459
+ /**
460
+ @internal
461
+ */
462
+ findChild(startIndex, endIndex, dir, pos, side) {
463
+ let { buffer } = this, pick = -1;
464
+ for (let i = startIndex; i != endIndex; i = buffer[i + 3]) {
465
+ if (checkSide(side, pos, buffer[i + 1], buffer[i + 2])) {
466
+ pick = i;
467
+ if (dir > 0)
468
+ break;
469
+ }
470
+ }
471
+ return pick;
472
+ }
473
+ /**
474
+ @internal
475
+ */
476
+ slice(startI, endI, from) {
477
+ let b = this.buffer;
478
+ let copy = new Uint16Array(endI - startI), len = 0;
479
+ for (let i = startI, j = 0; i < endI; ) {
480
+ copy[j++] = b[i++];
481
+ copy[j++] = b[i++] - from;
482
+ let to = copy[j++] = b[i++] - from;
483
+ copy[j++] = b[i++] - startI;
484
+ len = Math.max(len, to);
485
+ }
486
+ return new _TreeBuffer(copy, len, this.set);
487
+ }
488
+ };
489
+ __name(_TreeBuffer, "TreeBuffer");
490
+ let TreeBuffer = _TreeBuffer;
491
+ function checkSide(side, pos, from, to) {
492
+ switch (side) {
493
+ case -2:
494
+ return from < pos;
495
+ case -1:
496
+ return to >= pos && from < pos;
497
+ case 0:
498
+ return from < pos && to > pos;
499
+ case 1:
500
+ return from <= pos && to > pos;
501
+ case 2:
502
+ return to > pos;
503
+ case 4:
504
+ return true;
505
+ }
506
+ }
507
+ __name(checkSide, "checkSide");
508
+ function resolveNode(node, pos, side, overlays) {
509
+ var _a;
510
+ while (node.from == node.to || (side < 1 ? node.from >= pos : node.from > pos) || (side > -1 ? node.to <= pos : node.to < pos)) {
511
+ let parent = !overlays && node instanceof TreeNode && node.index < 0 ? null : node.parent;
512
+ if (!parent)
513
+ return node;
514
+ node = parent;
515
+ }
516
+ let mode = overlays ? 0 : IterMode.IgnoreOverlays;
517
+ if (overlays)
518
+ for (let scan = node, parent = scan.parent; parent; scan = parent, parent = scan.parent) {
519
+ if (scan instanceof TreeNode && scan.index < 0 && ((_a = parent.enter(pos, side, mode)) === null || _a === void 0 ? void 0 : _a.from) != scan.from)
520
+ node = parent;
521
+ }
522
+ for (; ; ) {
523
+ let inner = node.enter(pos, side, mode);
524
+ if (!inner)
525
+ return node;
526
+ node = inner;
527
+ }
528
+ }
529
+ __name(resolveNode, "resolveNode");
530
+ const _BaseNode = class _BaseNode {
531
+ cursor(mode = 0) {
532
+ return new TreeCursor(this, mode);
533
+ }
534
+ getChild(type, before = null, after = null) {
535
+ let r = getChildren(this, type, before, after);
536
+ return r.length ? r[0] : null;
537
+ }
538
+ getChildren(type, before = null, after = null) {
539
+ return getChildren(this, type, before, after);
540
+ }
541
+ resolve(pos, side = 0) {
542
+ return resolveNode(this, pos, side, false);
543
+ }
544
+ resolveInner(pos, side = 0) {
545
+ return resolveNode(this, pos, side, true);
546
+ }
547
+ matchContext(context) {
548
+ return matchNodeContext(this.parent, context);
549
+ }
550
+ enterUnfinishedNodesBefore(pos) {
551
+ let scan = this.childBefore(pos), node = this;
552
+ while (scan) {
553
+ let last = scan.lastChild;
554
+ if (!last || last.to != scan.to)
555
+ break;
556
+ if (last.type.isError && last.from == last.to) {
557
+ node = scan;
558
+ scan = last.prevSibling;
559
+ } else {
560
+ scan = last;
561
+ }
562
+ }
563
+ return node;
564
+ }
565
+ get node() {
566
+ return this;
567
+ }
568
+ get next() {
569
+ return this.parent;
570
+ }
571
+ };
572
+ __name(_BaseNode, "BaseNode");
573
+ let BaseNode = _BaseNode;
574
+ const _TreeNode = class _TreeNode extends BaseNode {
575
+ constructor(_tree, from, index, _parent) {
576
+ super();
577
+ this._tree = _tree;
578
+ this.from = from;
579
+ this.index = index;
580
+ this._parent = _parent;
581
+ }
582
+ get type() {
583
+ return this._tree.type;
584
+ }
585
+ get name() {
586
+ return this._tree.type.name;
587
+ }
588
+ get to() {
589
+ return this.from + this._tree.length;
590
+ }
591
+ nextChild(i, dir, pos, side, mode = 0) {
592
+ for (let parent = this; ; ) {
593
+ for (let { children, positions } = parent._tree, e = dir > 0 ? children.length : -1; i != e; i += dir) {
594
+ let next = children[i], start = positions[i] + parent.from;
595
+ if (!checkSide(side, pos, start, start + next.length))
596
+ continue;
597
+ if (next instanceof TreeBuffer) {
598
+ if (mode & IterMode.ExcludeBuffers)
599
+ continue;
600
+ let index = next.findChild(0, next.buffer.length, dir, pos - start, side);
601
+ if (index > -1)
602
+ return new BufferNode(new BufferContext(parent, next, i, start), null, index);
603
+ } else if (mode & IterMode.IncludeAnonymous || (!next.type.isAnonymous || hasChild(next))) {
604
+ let mounted;
605
+ if (!(mode & IterMode.IgnoreMounts) && (mounted = MountedTree.get(next)) && !mounted.overlay)
606
+ return new _TreeNode(mounted.tree, start, i, parent);
607
+ let inner = new _TreeNode(next, start, i, parent);
608
+ return mode & IterMode.IncludeAnonymous || !inner.type.isAnonymous ? inner : inner.nextChild(dir < 0 ? next.children.length - 1 : 0, dir, pos, side);
609
+ }
610
+ }
611
+ if (mode & IterMode.IncludeAnonymous || !parent.type.isAnonymous)
612
+ return null;
613
+ if (parent.index >= 0)
614
+ i = parent.index + dir;
615
+ else
616
+ i = dir < 0 ? -1 : parent._parent._tree.children.length;
617
+ parent = parent._parent;
618
+ if (!parent)
619
+ return null;
620
+ }
621
+ }
622
+ get firstChild() {
623
+ return this.nextChild(
624
+ 0,
625
+ 1,
626
+ 0,
627
+ 4
628
+ /* Side.DontCare */
629
+ );
630
+ }
631
+ get lastChild() {
632
+ return this.nextChild(
633
+ this._tree.children.length - 1,
634
+ -1,
635
+ 0,
636
+ 4
637
+ /* Side.DontCare */
638
+ );
639
+ }
640
+ childAfter(pos) {
641
+ return this.nextChild(
642
+ 0,
643
+ 1,
644
+ pos,
645
+ 2
646
+ /* Side.After */
647
+ );
648
+ }
649
+ childBefore(pos) {
650
+ return this.nextChild(
651
+ this._tree.children.length - 1,
652
+ -1,
653
+ pos,
654
+ -2
655
+ /* Side.Before */
656
+ );
657
+ }
658
+ enter(pos, side, mode = 0) {
659
+ let mounted;
660
+ if (!(mode & IterMode.IgnoreOverlays) && (mounted = MountedTree.get(this._tree)) && mounted.overlay) {
661
+ let rPos = pos - this.from;
662
+ for (let { from, to } of mounted.overlay) {
663
+ if ((side > 0 ? from <= rPos : from < rPos) && (side < 0 ? to >= rPos : to > rPos))
664
+ return new _TreeNode(mounted.tree, mounted.overlay[0].from + this.from, -1, this);
665
+ }
666
+ }
667
+ return this.nextChild(0, 1, pos, side, mode);
668
+ }
669
+ nextSignificantParent() {
670
+ let val = this;
671
+ while (val.type.isAnonymous && val._parent)
672
+ val = val._parent;
673
+ return val;
674
+ }
675
+ get parent() {
676
+ return this._parent ? this._parent.nextSignificantParent() : null;
677
+ }
678
+ get nextSibling() {
679
+ return this._parent && this.index >= 0 ? this._parent.nextChild(
680
+ this.index + 1,
681
+ 1,
682
+ 0,
683
+ 4
684
+ /* Side.DontCare */
685
+ ) : null;
686
+ }
687
+ get prevSibling() {
688
+ return this._parent && this.index >= 0 ? this._parent.nextChild(
689
+ this.index - 1,
690
+ -1,
691
+ 0,
692
+ 4
693
+ /* Side.DontCare */
694
+ ) : null;
695
+ }
696
+ get tree() {
697
+ return this._tree;
698
+ }
699
+ toTree() {
700
+ return this._tree;
701
+ }
702
+ /**
703
+ @internal
704
+ */
705
+ toString() {
706
+ return this._tree.toString();
707
+ }
708
+ };
709
+ __name(_TreeNode, "TreeNode");
710
+ let TreeNode = _TreeNode;
711
+ function getChildren(node, type, before, after) {
712
+ let cur = node.cursor(), result = [];
713
+ if (!cur.firstChild())
714
+ return result;
715
+ if (before != null)
716
+ for (let found = false; !found; ) {
717
+ found = cur.type.is(before);
718
+ if (!cur.nextSibling())
719
+ return result;
720
+ }
721
+ for (; ; ) {
722
+ if (after != null && cur.type.is(after))
723
+ return result;
724
+ if (cur.type.is(type))
725
+ result.push(cur.node);
726
+ if (!cur.nextSibling())
727
+ return after == null ? result : [];
728
+ }
729
+ }
730
+ __name(getChildren, "getChildren");
731
+ function matchNodeContext(node, context, i = context.length - 1) {
732
+ for (let p = node; i >= 0; p = p.parent) {
733
+ if (!p)
734
+ return false;
735
+ if (!p.type.isAnonymous) {
736
+ if (context[i] && context[i] != p.name)
737
+ return false;
738
+ i--;
739
+ }
740
+ }
741
+ return true;
742
+ }
743
+ __name(matchNodeContext, "matchNodeContext");
744
+ const _BufferContext = class _BufferContext {
745
+ constructor(parent, buffer, index, start) {
746
+ this.parent = parent;
747
+ this.buffer = buffer;
748
+ this.index = index;
749
+ this.start = start;
750
+ }
751
+ };
752
+ __name(_BufferContext, "BufferContext");
753
+ let BufferContext = _BufferContext;
754
+ const _BufferNode = class _BufferNode extends BaseNode {
755
+ get name() {
756
+ return this.type.name;
757
+ }
758
+ get from() {
759
+ return this.context.start + this.context.buffer.buffer[this.index + 1];
760
+ }
761
+ get to() {
762
+ return this.context.start + this.context.buffer.buffer[this.index + 2];
763
+ }
764
+ constructor(context, _parent, index) {
765
+ super();
766
+ this.context = context;
767
+ this._parent = _parent;
768
+ this.index = index;
769
+ this.type = context.buffer.set.types[context.buffer.buffer[index]];
770
+ }
771
+ child(dir, pos, side) {
772
+ let { buffer } = this.context;
773
+ let index = buffer.findChild(this.index + 4, buffer.buffer[this.index + 3], dir, pos - this.context.start, side);
774
+ return index < 0 ? null : new _BufferNode(this.context, this, index);
775
+ }
776
+ get firstChild() {
777
+ return this.child(
778
+ 1,
779
+ 0,
780
+ 4
781
+ /* Side.DontCare */
782
+ );
783
+ }
784
+ get lastChild() {
785
+ return this.child(
786
+ -1,
787
+ 0,
788
+ 4
789
+ /* Side.DontCare */
790
+ );
791
+ }
792
+ childAfter(pos) {
793
+ return this.child(
794
+ 1,
795
+ pos,
796
+ 2
797
+ /* Side.After */
798
+ );
799
+ }
800
+ childBefore(pos) {
801
+ return this.child(
802
+ -1,
803
+ pos,
804
+ -2
805
+ /* Side.Before */
806
+ );
807
+ }
808
+ enter(pos, side, mode = 0) {
809
+ if (mode & IterMode.ExcludeBuffers)
810
+ return null;
811
+ let { buffer } = this.context;
812
+ let index = buffer.findChild(this.index + 4, buffer.buffer[this.index + 3], side > 0 ? 1 : -1, pos - this.context.start, side);
813
+ return index < 0 ? null : new _BufferNode(this.context, this, index);
814
+ }
815
+ get parent() {
816
+ return this._parent || this.context.parent.nextSignificantParent();
817
+ }
818
+ externalSibling(dir) {
819
+ return this._parent ? null : this.context.parent.nextChild(
820
+ this.context.index + dir,
821
+ dir,
822
+ 0,
823
+ 4
824
+ /* Side.DontCare */
825
+ );
826
+ }
827
+ get nextSibling() {
828
+ let { buffer } = this.context;
829
+ let after = buffer.buffer[this.index + 3];
830
+ if (after < (this._parent ? buffer.buffer[this._parent.index + 3] : buffer.buffer.length))
831
+ return new _BufferNode(this.context, this._parent, after);
832
+ return this.externalSibling(1);
833
+ }
834
+ get prevSibling() {
835
+ let { buffer } = this.context;
836
+ let parentStart = this._parent ? this._parent.index + 4 : 0;
837
+ if (this.index == parentStart)
838
+ return this.externalSibling(-1);
839
+ return new _BufferNode(this.context, this._parent, buffer.findChild(
840
+ parentStart,
841
+ this.index,
842
+ -1,
843
+ 0,
844
+ 4
845
+ /* Side.DontCare */
846
+ ));
847
+ }
848
+ get tree() {
849
+ return null;
850
+ }
851
+ toTree() {
852
+ let children = [], positions = [];
853
+ let { buffer } = this.context;
854
+ let startI = this.index + 4, endI = buffer.buffer[this.index + 3];
855
+ if (endI > startI) {
856
+ let from = buffer.buffer[this.index + 1];
857
+ children.push(buffer.slice(startI, endI, from));
858
+ positions.push(0);
859
+ }
860
+ return new Tree(this.type, children, positions, this.to - this.from);
861
+ }
862
+ /**
863
+ @internal
864
+ */
865
+ toString() {
866
+ return this.context.buffer.childString(this.index);
867
+ }
868
+ };
869
+ __name(_BufferNode, "BufferNode");
870
+ let BufferNode = _BufferNode;
871
+ function iterStack(heads) {
872
+ if (!heads.length)
873
+ return null;
874
+ let pick = 0, picked = heads[0];
875
+ for (let i = 1; i < heads.length; i++) {
876
+ let node = heads[i];
877
+ if (node.from > picked.from || node.to < picked.to) {
878
+ picked = node;
879
+ pick = i;
880
+ }
881
+ }
882
+ let next = picked instanceof TreeNode && picked.index < 0 ? null : picked.parent;
883
+ let newHeads = heads.slice();
884
+ if (next)
885
+ newHeads[pick] = next;
886
+ else
887
+ newHeads.splice(pick, 1);
888
+ return new StackIterator(newHeads, picked);
889
+ }
890
+ __name(iterStack, "iterStack");
891
+ const _StackIterator = class _StackIterator {
892
+ constructor(heads, node) {
893
+ this.heads = heads;
894
+ this.node = node;
895
+ }
896
+ get next() {
897
+ return iterStack(this.heads);
898
+ }
899
+ };
900
+ __name(_StackIterator, "StackIterator");
901
+ let StackIterator = _StackIterator;
902
+ function stackIterator(tree, pos, side) {
903
+ let inner = tree.resolveInner(pos, side), layers = null;
904
+ for (let scan = inner instanceof TreeNode ? inner : inner.context.parent; scan; scan = scan.parent) {
905
+ if (scan.index < 0) {
906
+ let parent = scan.parent;
907
+ (layers || (layers = [inner])).push(parent.resolve(pos, side));
908
+ scan = parent;
909
+ } else {
910
+ let mount = MountedTree.get(scan.tree);
911
+ if (mount && mount.overlay && mount.overlay[0].from <= pos && mount.overlay[mount.overlay.length - 1].to >= pos) {
912
+ let root = new TreeNode(mount.tree, mount.overlay[0].from + scan.from, -1, scan);
913
+ (layers || (layers = [inner])).push(resolveNode(root, pos, side, false));
914
+ }
915
+ }
916
+ }
917
+ return layers ? iterStack(layers) : inner;
918
+ }
919
+ __name(stackIterator, "stackIterator");
920
+ const _TreeCursor = class _TreeCursor {
921
+ /**
922
+ Shorthand for `.type.name`.
923
+ */
924
+ get name() {
925
+ return this.type.name;
926
+ }
927
+ /**
928
+ @internal
929
+ */
930
+ constructor(node, mode = 0) {
931
+ this.mode = mode;
932
+ this.buffer = null;
933
+ this.stack = [];
934
+ this.index = 0;
935
+ this.bufferNode = null;
936
+ if (node instanceof TreeNode) {
937
+ this.yieldNode(node);
938
+ } else {
939
+ this._tree = node.context.parent;
940
+ this.buffer = node.context;
941
+ for (let n = node._parent; n; n = n._parent)
942
+ this.stack.unshift(n.index);
943
+ this.bufferNode = node;
944
+ this.yieldBuf(node.index);
945
+ }
946
+ }
947
+ yieldNode(node) {
948
+ if (!node)
949
+ return false;
950
+ this._tree = node;
951
+ this.type = node.type;
952
+ this.from = node.from;
953
+ this.to = node.to;
954
+ return true;
955
+ }
956
+ yieldBuf(index, type) {
957
+ this.index = index;
958
+ let { start, buffer } = this.buffer;
959
+ this.type = type || buffer.set.types[buffer.buffer[index]];
960
+ this.from = start + buffer.buffer[index + 1];
961
+ this.to = start + buffer.buffer[index + 2];
962
+ return true;
963
+ }
964
+ /**
965
+ @internal
966
+ */
967
+ yield(node) {
968
+ if (!node)
969
+ return false;
970
+ if (node instanceof TreeNode) {
971
+ this.buffer = null;
972
+ return this.yieldNode(node);
973
+ }
974
+ this.buffer = node.context;
975
+ return this.yieldBuf(node.index, node.type);
976
+ }
977
+ /**
978
+ @internal
979
+ */
980
+ toString() {
981
+ return this.buffer ? this.buffer.buffer.childString(this.index) : this._tree.toString();
982
+ }
983
+ /**
984
+ @internal
985
+ */
986
+ enterChild(dir, pos, side) {
987
+ if (!this.buffer)
988
+ return this.yield(this._tree.nextChild(dir < 0 ? this._tree._tree.children.length - 1 : 0, dir, pos, side, this.mode));
989
+ let { buffer } = this.buffer;
990
+ let index = buffer.findChild(this.index + 4, buffer.buffer[this.index + 3], dir, pos - this.buffer.start, side);
991
+ if (index < 0)
992
+ return false;
993
+ this.stack.push(this.index);
994
+ return this.yieldBuf(index);
995
+ }
996
+ /**
997
+ Move the cursor to this node's first child. When this returns
998
+ false, the node has no child, and the cursor has not been moved.
999
+ */
1000
+ firstChild() {
1001
+ return this.enterChild(
1002
+ 1,
1003
+ 0,
1004
+ 4
1005
+ /* Side.DontCare */
1006
+ );
1007
+ }
1008
+ /**
1009
+ Move the cursor to this node's last child.
1010
+ */
1011
+ lastChild() {
1012
+ return this.enterChild(
1013
+ -1,
1014
+ 0,
1015
+ 4
1016
+ /* Side.DontCare */
1017
+ );
1018
+ }
1019
+ /**
1020
+ Move the cursor to the first child that ends after `pos`.
1021
+ */
1022
+ childAfter(pos) {
1023
+ return this.enterChild(
1024
+ 1,
1025
+ pos,
1026
+ 2
1027
+ /* Side.After */
1028
+ );
1029
+ }
1030
+ /**
1031
+ Move to the last child that starts before `pos`.
1032
+ */
1033
+ childBefore(pos) {
1034
+ return this.enterChild(
1035
+ -1,
1036
+ pos,
1037
+ -2
1038
+ /* Side.Before */
1039
+ );
1040
+ }
1041
+ /**
1042
+ Move the cursor to the child around `pos`. If side is -1 the
1043
+ child may end at that position, when 1 it may start there. This
1044
+ will also enter [overlaid](#common.MountedTree.overlay)
1045
+ [mounted](#common.NodeProp^mounted) trees unless `overlays` is
1046
+ set to false.
1047
+ */
1048
+ enter(pos, side, mode = this.mode) {
1049
+ if (!this.buffer)
1050
+ return this.yield(this._tree.enter(pos, side, mode));
1051
+ return mode & IterMode.ExcludeBuffers ? false : this.enterChild(1, pos, side);
1052
+ }
1053
+ /**
1054
+ Move to the node's parent node, if this isn't the top node.
1055
+ */
1056
+ parent() {
1057
+ if (!this.buffer)
1058
+ return this.yieldNode(this.mode & IterMode.IncludeAnonymous ? this._tree._parent : this._tree.parent);
1059
+ if (this.stack.length)
1060
+ return this.yieldBuf(this.stack.pop());
1061
+ let parent = this.mode & IterMode.IncludeAnonymous ? this.buffer.parent : this.buffer.parent.nextSignificantParent();
1062
+ this.buffer = null;
1063
+ return this.yieldNode(parent);
1064
+ }
1065
+ /**
1066
+ @internal
1067
+ */
1068
+ sibling(dir) {
1069
+ if (!this.buffer)
1070
+ return !this._tree._parent ? false : this.yield(this._tree.index < 0 ? null : this._tree._parent.nextChild(this._tree.index + dir, dir, 0, 4, this.mode));
1071
+ let { buffer } = this.buffer, d = this.stack.length - 1;
1072
+ if (dir < 0) {
1073
+ let parentStart = d < 0 ? 0 : this.stack[d] + 4;
1074
+ if (this.index != parentStart)
1075
+ return this.yieldBuf(buffer.findChild(
1076
+ parentStart,
1077
+ this.index,
1078
+ -1,
1079
+ 0,
1080
+ 4
1081
+ /* Side.DontCare */
1082
+ ));
1083
+ } else {
1084
+ let after = buffer.buffer[this.index + 3];
1085
+ if (after < (d < 0 ? buffer.buffer.length : buffer.buffer[this.stack[d] + 3]))
1086
+ return this.yieldBuf(after);
1087
+ }
1088
+ return d < 0 ? this.yield(this.buffer.parent.nextChild(this.buffer.index + dir, dir, 0, 4, this.mode)) : false;
1089
+ }
1090
+ /**
1091
+ Move to this node's next sibling, if any.
1092
+ */
1093
+ nextSibling() {
1094
+ return this.sibling(1);
1095
+ }
1096
+ /**
1097
+ Move to this node's previous sibling, if any.
1098
+ */
1099
+ prevSibling() {
1100
+ return this.sibling(-1);
1101
+ }
1102
+ atLastNode(dir) {
1103
+ let index, parent, { buffer } = this;
1104
+ if (buffer) {
1105
+ if (dir > 0) {
1106
+ if (this.index < buffer.buffer.buffer.length)
1107
+ return false;
1108
+ } else {
1109
+ for (let i = 0; i < this.index; i++)
1110
+ if (buffer.buffer.buffer[i + 3] < this.index)
1111
+ return false;
1112
+ }
1113
+ ({ index, parent } = buffer);
1114
+ } else {
1115
+ ({ index, _parent: parent } = this._tree);
1116
+ }
1117
+ for (; parent; { index, _parent: parent } = parent) {
1118
+ if (index > -1)
1119
+ for (let i = index + dir, e = dir < 0 ? -1 : parent._tree.children.length; i != e; i += dir) {
1120
+ let child = parent._tree.children[i];
1121
+ if (this.mode & IterMode.IncludeAnonymous || child instanceof TreeBuffer || !child.type.isAnonymous || hasChild(child))
1122
+ return false;
1123
+ }
1124
+ }
1125
+ return true;
1126
+ }
1127
+ move(dir, enter) {
1128
+ if (enter && this.enterChild(
1129
+ dir,
1130
+ 0,
1131
+ 4
1132
+ /* Side.DontCare */
1133
+ ))
1134
+ return true;
1135
+ for (; ; ) {
1136
+ if (this.sibling(dir))
1137
+ return true;
1138
+ if (this.atLastNode(dir) || !this.parent())
1139
+ return false;
1140
+ }
1141
+ }
1142
+ /**
1143
+ Move to the next node in a
1144
+ [pre-order](https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR)
1145
+ traversal, going from a node to its first child or, if the
1146
+ current node is empty or `enter` is false, its next sibling or
1147
+ the next sibling of the first parent node that has one.
1148
+ */
1149
+ next(enter = true) {
1150
+ return this.move(1, enter);
1151
+ }
1152
+ /**
1153
+ Move to the next node in a last-to-first pre-order traversal. A
1154
+ node is followed by its last child or, if it has none, its
1155
+ previous sibling or the previous sibling of the first parent
1156
+ node that has one.
1157
+ */
1158
+ prev(enter = true) {
1159
+ return this.move(-1, enter);
1160
+ }
1161
+ /**
1162
+ Move the cursor to the innermost node that covers `pos`. If
1163
+ `side` is -1, it will enter nodes that end at `pos`. If it is 1,
1164
+ it will enter nodes that start at `pos`.
1165
+ */
1166
+ moveTo(pos, side = 0) {
1167
+ while (this.from == this.to || (side < 1 ? this.from >= pos : this.from > pos) || (side > -1 ? this.to <= pos : this.to < pos))
1168
+ if (!this.parent())
1169
+ break;
1170
+ while (this.enterChild(1, pos, side)) {
1171
+ }
1172
+ return this;
1173
+ }
1174
+ /**
1175
+ Get a [syntax node](#common.SyntaxNode) at the cursor's current
1176
+ position.
1177
+ */
1178
+ get node() {
1179
+ if (!this.buffer)
1180
+ return this._tree;
1181
+ let cache = this.bufferNode, result = null, depth = 0;
1182
+ if (cache && cache.context == this.buffer) {
1183
+ scan: for (let index = this.index, d = this.stack.length; d >= 0; ) {
1184
+ for (let c = cache; c; c = c._parent)
1185
+ if (c.index == index) {
1186
+ if (index == this.index)
1187
+ return c;
1188
+ result = c;
1189
+ depth = d + 1;
1190
+ break scan;
1191
+ }
1192
+ index = this.stack[--d];
1193
+ }
1194
+ }
1195
+ for (let i = depth; i < this.stack.length; i++)
1196
+ result = new BufferNode(this.buffer, result, this.stack[i]);
1197
+ return this.bufferNode = new BufferNode(this.buffer, result, this.index);
1198
+ }
1199
+ /**
1200
+ Get the [tree](#common.Tree) that represents the current node, if
1201
+ any. Will return null when the node is in a [tree
1202
+ buffer](#common.TreeBuffer).
1203
+ */
1204
+ get tree() {
1205
+ return this.buffer ? null : this._tree._tree;
1206
+ }
1207
+ /**
1208
+ Iterate over the current node and all its descendants, calling
1209
+ `enter` when entering a node and `leave`, if given, when leaving
1210
+ one. When `enter` returns `false`, any children of that node are
1211
+ skipped, and `leave` isn't called for it.
1212
+ */
1213
+ iterate(enter, leave) {
1214
+ for (let depth = 0; ; ) {
1215
+ let mustLeave = false;
1216
+ if (this.type.isAnonymous || enter(this) !== false) {
1217
+ if (this.firstChild()) {
1218
+ depth++;
1219
+ continue;
1220
+ }
1221
+ if (!this.type.isAnonymous)
1222
+ mustLeave = true;
1223
+ }
1224
+ for (; ; ) {
1225
+ if (mustLeave && leave)
1226
+ leave(this);
1227
+ mustLeave = this.type.isAnonymous;
1228
+ if (!depth)
1229
+ return;
1230
+ if (this.nextSibling())
1231
+ break;
1232
+ this.parent();
1233
+ depth--;
1234
+ mustLeave = true;
1235
+ }
1236
+ }
1237
+ }
1238
+ /**
1239
+ Test whether the current node matches a given context—a sequence
1240
+ of direct parent node names. Empty strings in the context array
1241
+ are treated as wildcards.
1242
+ */
1243
+ matchContext(context) {
1244
+ if (!this.buffer)
1245
+ return matchNodeContext(this.node.parent, context);
1246
+ let { buffer } = this.buffer, { types } = buffer.set;
1247
+ for (let i = context.length - 1, d = this.stack.length - 1; i >= 0; d--) {
1248
+ if (d < 0)
1249
+ return matchNodeContext(this._tree, context, i);
1250
+ let type = types[buffer.buffer[this.stack[d]]];
1251
+ if (!type.isAnonymous) {
1252
+ if (context[i] && context[i] != type.name)
1253
+ return false;
1254
+ i--;
1255
+ }
1256
+ }
1257
+ return true;
1258
+ }
1259
+ };
1260
+ __name(_TreeCursor, "TreeCursor");
1261
+ let TreeCursor = _TreeCursor;
1262
+ function hasChild(tree) {
1263
+ return tree.children.some((ch) => ch instanceof TreeBuffer || !ch.type.isAnonymous || hasChild(ch));
1264
+ }
1265
+ __name(hasChild, "hasChild");
1266
+ function buildTree(data) {
1267
+ var _a;
1268
+ let { buffer, nodeSet, maxBufferLength = DefaultBufferLength, reused = [], minRepeatType = nodeSet.types.length } = data;
1269
+ let cursor = Array.isArray(buffer) ? new FlatBufferCursor(buffer, buffer.length) : buffer;
1270
+ let types = nodeSet.types;
1271
+ let contextHash = 0, lookAhead = 0;
1272
+ function takeNode(parentStart, minPos, children2, positions2, inRepeat, depth) {
1273
+ let { id, start, end, size } = cursor;
1274
+ let lookAheadAtStart = lookAhead, contextAtStart = contextHash;
1275
+ while (size < 0) {
1276
+ cursor.next();
1277
+ if (size == -1) {
1278
+ let node2 = reused[id];
1279
+ children2.push(node2);
1280
+ positions2.push(start - parentStart);
1281
+ return;
1282
+ } else if (size == -3) {
1283
+ contextHash = id;
1284
+ return;
1285
+ } else if (size == -4) {
1286
+ lookAhead = id;
1287
+ return;
1288
+ } else {
1289
+ throw new RangeError(`Unrecognized record size: ${size}`);
1290
+ }
1291
+ }
1292
+ let type = types[id], node, buffer2;
1293
+ let startPos = start - parentStart;
1294
+ if (end - start <= maxBufferLength && (buffer2 = findBufferSize(cursor.pos - minPos, inRepeat))) {
1295
+ let data2 = new Uint16Array(buffer2.size - buffer2.skip);
1296
+ let endPos = cursor.pos - buffer2.size, index = data2.length;
1297
+ while (cursor.pos > endPos)
1298
+ index = copyToBuffer(buffer2.start, data2, index);
1299
+ node = new TreeBuffer(data2, end - buffer2.start, nodeSet);
1300
+ startPos = buffer2.start - parentStart;
1301
+ } else {
1302
+ let endPos = cursor.pos - size;
1303
+ cursor.next();
1304
+ let localChildren = [], localPositions = [];
1305
+ let localInRepeat = id >= minRepeatType ? id : -1;
1306
+ let lastGroup = 0, lastEnd = end;
1307
+ while (cursor.pos > endPos) {
1308
+ if (localInRepeat >= 0 && cursor.id == localInRepeat && cursor.size >= 0) {
1309
+ if (cursor.end <= lastEnd - maxBufferLength) {
1310
+ makeRepeatLeaf(localChildren, localPositions, start, lastGroup, cursor.end, lastEnd, localInRepeat, lookAheadAtStart, contextAtStart);
1311
+ lastGroup = localChildren.length;
1312
+ lastEnd = cursor.end;
1313
+ }
1314
+ cursor.next();
1315
+ } else if (depth > 2500) {
1316
+ takeFlatNode(start, endPos, localChildren, localPositions);
1317
+ } else {
1318
+ takeNode(start, endPos, localChildren, localPositions, localInRepeat, depth + 1);
1319
+ }
1320
+ }
1321
+ if (localInRepeat >= 0 && lastGroup > 0 && lastGroup < localChildren.length)
1322
+ makeRepeatLeaf(localChildren, localPositions, start, lastGroup, start, lastEnd, localInRepeat, lookAheadAtStart, contextAtStart);
1323
+ localChildren.reverse();
1324
+ localPositions.reverse();
1325
+ if (localInRepeat > -1 && lastGroup > 0) {
1326
+ let make = makeBalanced(type, contextAtStart);
1327
+ node = balanceRange(type, localChildren, localPositions, 0, localChildren.length, 0, end - start, make, make);
1328
+ } else {
1329
+ node = makeTree(type, localChildren, localPositions, end - start, lookAheadAtStart - end, contextAtStart);
1330
+ }
1331
+ }
1332
+ children2.push(node);
1333
+ positions2.push(startPos);
1334
+ }
1335
+ __name(takeNode, "takeNode");
1336
+ function takeFlatNode(parentStart, minPos, children2, positions2) {
1337
+ let nodes = [];
1338
+ let nodeCount = 0, stopAt = -1;
1339
+ while (cursor.pos > minPos) {
1340
+ let { id, start, end, size } = cursor;
1341
+ if (size > 4) {
1342
+ cursor.next();
1343
+ } else if (stopAt > -1 && start < stopAt) {
1344
+ break;
1345
+ } else {
1346
+ if (stopAt < 0)
1347
+ stopAt = end - maxBufferLength;
1348
+ nodes.push(id, start, end);
1349
+ nodeCount++;
1350
+ cursor.next();
1351
+ }
1352
+ }
1353
+ if (nodeCount) {
1354
+ let buffer2 = new Uint16Array(nodeCount * 4);
1355
+ let start = nodes[nodes.length - 2];
1356
+ for (let i = nodes.length - 3, j = 0; i >= 0; i -= 3) {
1357
+ buffer2[j++] = nodes[i];
1358
+ buffer2[j++] = nodes[i + 1] - start;
1359
+ buffer2[j++] = nodes[i + 2] - start;
1360
+ buffer2[j++] = j;
1361
+ }
1362
+ children2.push(new TreeBuffer(buffer2, nodes[2] - start, nodeSet));
1363
+ positions2.push(start - parentStart);
1364
+ }
1365
+ }
1366
+ __name(takeFlatNode, "takeFlatNode");
1367
+ function makeBalanced(type, contextHash2) {
1368
+ return (children2, positions2, length2) => {
1369
+ let lookAhead2 = 0, lastI = children2.length - 1, last, lookAheadProp;
1370
+ if (lastI >= 0 && (last = children2[lastI]) instanceof Tree) {
1371
+ if (!lastI && last.type == type && last.length == length2)
1372
+ return last;
1373
+ if (lookAheadProp = last.prop(NodeProp.lookAhead))
1374
+ lookAhead2 = positions2[lastI] + last.length + lookAheadProp;
1375
+ }
1376
+ return makeTree(type, children2, positions2, length2, lookAhead2, contextHash2);
1377
+ };
1378
+ }
1379
+ __name(makeBalanced, "makeBalanced");
1380
+ function makeRepeatLeaf(children2, positions2, base, i, from, to, type, lookAhead2, contextHash2) {
1381
+ let localChildren = [], localPositions = [];
1382
+ while (children2.length > i) {
1383
+ localChildren.push(children2.pop());
1384
+ localPositions.push(positions2.pop() + base - from);
1385
+ }
1386
+ children2.push(makeTree(nodeSet.types[type], localChildren, localPositions, to - from, lookAhead2 - to, contextHash2));
1387
+ positions2.push(from - base);
1388
+ }
1389
+ __name(makeRepeatLeaf, "makeRepeatLeaf");
1390
+ function makeTree(type, children2, positions2, length2, lookAhead2, contextHash2, props) {
1391
+ if (contextHash2) {
1392
+ let pair = [NodeProp.contextHash, contextHash2];
1393
+ props = props ? [pair].concat(props) : [pair];
1394
+ }
1395
+ if (lookAhead2 > 25) {
1396
+ let pair = [NodeProp.lookAhead, lookAhead2];
1397
+ props = props ? [pair].concat(props) : [pair];
1398
+ }
1399
+ return new Tree(type, children2, positions2, length2, props);
1400
+ }
1401
+ __name(makeTree, "makeTree");
1402
+ function findBufferSize(maxSize, inRepeat) {
1403
+ let fork = cursor.fork();
1404
+ let size = 0, start = 0, skip = 0, minStart = fork.end - maxBufferLength;
1405
+ let result = { size: 0, start: 0, skip: 0 };
1406
+ scan: for (let minPos = fork.pos - maxSize; fork.pos > minPos; ) {
1407
+ let nodeSize2 = fork.size;
1408
+ if (fork.id == inRepeat && nodeSize2 >= 0) {
1409
+ result.size = size;
1410
+ result.start = start;
1411
+ result.skip = skip;
1412
+ skip += 4;
1413
+ size += 4;
1414
+ fork.next();
1415
+ continue;
1416
+ }
1417
+ let startPos = fork.pos - nodeSize2;
1418
+ if (nodeSize2 < 0 || startPos < minPos || fork.start < minStart)
1419
+ break;
1420
+ let localSkipped = fork.id >= minRepeatType ? 4 : 0;
1421
+ let nodeStart = fork.start;
1422
+ fork.next();
1423
+ while (fork.pos > startPos) {
1424
+ if (fork.size < 0) {
1425
+ if (fork.size == -3)
1426
+ localSkipped += 4;
1427
+ else
1428
+ break scan;
1429
+ } else if (fork.id >= minRepeatType) {
1430
+ localSkipped += 4;
1431
+ }
1432
+ fork.next();
1433
+ }
1434
+ start = nodeStart;
1435
+ size += nodeSize2;
1436
+ skip += localSkipped;
1437
+ }
1438
+ if (inRepeat < 0 || size == maxSize) {
1439
+ result.size = size;
1440
+ result.start = start;
1441
+ result.skip = skip;
1442
+ }
1443
+ return result.size > 4 ? result : void 0;
1444
+ }
1445
+ __name(findBufferSize, "findBufferSize");
1446
+ function copyToBuffer(bufferStart, buffer2, index) {
1447
+ let { id, start, end, size } = cursor;
1448
+ cursor.next();
1449
+ if (size >= 0 && id < minRepeatType) {
1450
+ let startIndex = index;
1451
+ if (size > 4) {
1452
+ let endPos = cursor.pos - (size - 4);
1453
+ while (cursor.pos > endPos)
1454
+ index = copyToBuffer(bufferStart, buffer2, index);
1455
+ }
1456
+ buffer2[--index] = startIndex;
1457
+ buffer2[--index] = end - bufferStart;
1458
+ buffer2[--index] = start - bufferStart;
1459
+ buffer2[--index] = id;
1460
+ } else if (size == -3) {
1461
+ contextHash = id;
1462
+ } else if (size == -4) {
1463
+ lookAhead = id;
1464
+ }
1465
+ return index;
1466
+ }
1467
+ __name(copyToBuffer, "copyToBuffer");
1468
+ let children = [], positions = [];
1469
+ while (cursor.pos > 0)
1470
+ takeNode(data.start || 0, data.bufferStart || 0, children, positions, -1, 0);
1471
+ let length = (_a = data.length) !== null && _a !== void 0 ? _a : children.length ? positions[0] + children[0].length : 0;
1472
+ return new Tree(types[data.topID], children.reverse(), positions.reverse(), length);
1473
+ }
1474
+ __name(buildTree, "buildTree");
1475
+ const nodeSizeCache = /* @__PURE__ */ new WeakMap();
1476
+ function nodeSize(balanceType, node) {
1477
+ if (!balanceType.isAnonymous || node instanceof TreeBuffer || node.type != balanceType)
1478
+ return 1;
1479
+ let size = nodeSizeCache.get(node);
1480
+ if (size == null) {
1481
+ size = 1;
1482
+ for (let child of node.children) {
1483
+ if (child.type != balanceType || !(child instanceof Tree)) {
1484
+ size = 1;
1485
+ break;
1486
+ }
1487
+ size += nodeSize(balanceType, child);
1488
+ }
1489
+ nodeSizeCache.set(node, size);
1490
+ }
1491
+ return size;
1492
+ }
1493
+ __name(nodeSize, "nodeSize");
1494
+ function balanceRange(balanceType, children, positions, from, to, start, length, mkTop, mkTree) {
1495
+ let total = 0;
1496
+ for (let i = from; i < to; i++)
1497
+ total += nodeSize(balanceType, children[i]);
1498
+ let maxChild = Math.ceil(
1499
+ total * 1.5 / 8
1500
+ /* Balance.BranchFactor */
1501
+ );
1502
+ let localChildren = [], localPositions = [];
1503
+ function divide(children2, positions2, from2, to2, offset) {
1504
+ for (let i = from2; i < to2; ) {
1505
+ let groupFrom = i, groupStart = positions2[i], groupSize = nodeSize(balanceType, children2[i]);
1506
+ i++;
1507
+ for (; i < to2; i++) {
1508
+ let nextSize = nodeSize(balanceType, children2[i]);
1509
+ if (groupSize + nextSize >= maxChild)
1510
+ break;
1511
+ groupSize += nextSize;
1512
+ }
1513
+ if (i == groupFrom + 1) {
1514
+ if (groupSize > maxChild) {
1515
+ let only = children2[groupFrom];
1516
+ divide(only.children, only.positions, 0, only.children.length, positions2[groupFrom] + offset);
1517
+ continue;
1518
+ }
1519
+ localChildren.push(children2[groupFrom]);
1520
+ } else {
1521
+ let length2 = positions2[i - 1] + children2[i - 1].length - groupStart;
1522
+ localChildren.push(balanceRange(balanceType, children2, positions2, groupFrom, i, groupStart, length2, null, mkTree));
1523
+ }
1524
+ localPositions.push(groupStart + offset - start);
1525
+ }
1526
+ }
1527
+ __name(divide, "divide");
1528
+ divide(children, positions, from, to, 0);
1529
+ return (mkTop || mkTree)(localChildren, localPositions, length);
1530
+ }
1531
+ __name(balanceRange, "balanceRange");
1532
+ const _Parser = class _Parser {
1533
+ /**
1534
+ Start a parse, returning a [partial parse](#common.PartialParse)
1535
+ object. [`fragments`](#common.TreeFragment) can be passed in to
1536
+ make the parse incremental.
1537
+
1538
+ By default, the entire input is parsed. You can pass `ranges`,
1539
+ which should be a sorted array of non-empty, non-overlapping
1540
+ ranges, to parse only those ranges. The tree returned in that
1541
+ case will start at `ranges[0].from`.
1542
+ */
1543
+ startParse(input, fragments, ranges) {
1544
+ if (typeof input == "string")
1545
+ input = new StringInput(input);
1546
+ ranges = !ranges ? [new Range(0, input.length)] : ranges.length ? ranges.map((r) => new Range(r.from, r.to)) : [new Range(0, 0)];
1547
+ return this.createParse(input, fragments || [], ranges);
1548
+ }
1549
+ /**
1550
+ Run a full parse, returning the resulting tree.
1551
+ */
1552
+ parse(input, fragments, ranges) {
1553
+ let parse = this.startParse(input, fragments, ranges);
1554
+ for (; ; ) {
1555
+ let done = parse.advance();
1556
+ if (done)
1557
+ return done;
1558
+ }
1559
+ }
1560
+ };
1561
+ __name(_Parser, "Parser");
1562
+ let Parser = _Parser;
1563
+ const _StringInput = class _StringInput {
1564
+ constructor(string2) {
1565
+ this.string = string2;
1566
+ }
1567
+ get length() {
1568
+ return this.string.length;
1569
+ }
1570
+ chunk(from) {
1571
+ return this.string.slice(from);
1572
+ }
1573
+ get lineChunks() {
1574
+ return false;
1575
+ }
1576
+ read(from, to) {
1577
+ return this.string.slice(from, to);
1578
+ }
1579
+ };
1580
+ __name(_StringInput, "StringInput");
1581
+ let StringInput = _StringInput;
1582
+ new NodeProp({ perNode: true });
1583
+ let nextTagID = 0;
1584
+ const _Tag = class _Tag {
1585
+ /**
1586
+ @internal
1587
+ */
1588
+ constructor(name2, set, base, modified) {
1589
+ this.name = name2;
1590
+ this.set = set;
1591
+ this.base = base;
1592
+ this.modified = modified;
1593
+ this.id = nextTagID++;
1594
+ }
1595
+ toString() {
1596
+ let { name: name2 } = this;
1597
+ for (let mod of this.modified)
1598
+ if (mod.name)
1599
+ name2 = `${mod.name}(${name2})`;
1600
+ return name2;
1601
+ }
1602
+ static define(nameOrParent, parent) {
1603
+ let name2 = typeof nameOrParent == "string" ? nameOrParent : "?";
1604
+ if (nameOrParent instanceof _Tag)
1605
+ parent = nameOrParent;
1606
+ if (parent === null || parent === void 0 ? void 0 : parent.base)
1607
+ throw new Error("Can not derive from a modified tag");
1608
+ let tag = new _Tag(name2, [], null, []);
1609
+ tag.set.push(tag);
1610
+ if (parent)
1611
+ for (let t2 of parent.set)
1612
+ tag.set.push(t2);
1613
+ return tag;
1614
+ }
1615
+ /**
1616
+ Define a tag _modifier_, which is a function that, given a tag,
1617
+ will return a tag that is a subtag of the original. Applying the
1618
+ same modifier to a twice tag will return the same value (`m1(t1)
1619
+ == m1(t1)`) and applying multiple modifiers will, regardless or
1620
+ order, produce the same tag (`m1(m2(t1)) == m2(m1(t1))`).
1621
+
1622
+ When multiple modifiers are applied to a given base tag, each
1623
+ smaller set of modifiers is registered as a parent, so that for
1624
+ example `m1(m2(m3(t1)))` is a subtype of `m1(m2(t1))`,
1625
+ `m1(m3(t1)`, and so on.
1626
+ */
1627
+ static defineModifier(name2) {
1628
+ let mod = new Modifier(name2);
1629
+ return (tag) => {
1630
+ if (tag.modified.indexOf(mod) > -1)
1631
+ return tag;
1632
+ return Modifier.get(tag.base || tag, tag.modified.concat(mod).sort((a, b) => a.id - b.id));
1633
+ };
1634
+ }
1635
+ };
1636
+ __name(_Tag, "Tag");
1637
+ let Tag = _Tag;
1638
+ let nextModifierID = 0;
1639
+ const _Modifier = class _Modifier {
1640
+ constructor(name2) {
1641
+ this.name = name2;
1642
+ this.instances = [];
1643
+ this.id = nextModifierID++;
1644
+ }
1645
+ static get(base, mods) {
1646
+ if (!mods.length)
1647
+ return base;
1648
+ let exists = mods[0].instances.find((t2) => t2.base == base && sameArray(mods, t2.modified));
1649
+ if (exists)
1650
+ return exists;
1651
+ let set = [], tag = new Tag(base.name, set, base, mods);
1652
+ for (let m of mods)
1653
+ m.instances.push(tag);
1654
+ let configs = powerSet(mods);
1655
+ for (let parent of base.set)
1656
+ if (!parent.modified.length)
1657
+ for (let config of configs)
1658
+ set.push(_Modifier.get(parent, config));
1659
+ return tag;
1660
+ }
1661
+ };
1662
+ __name(_Modifier, "Modifier");
1663
+ let Modifier = _Modifier;
1664
+ function sameArray(a, b) {
1665
+ return a.length == b.length && a.every((x, i) => x == b[i]);
1666
+ }
1667
+ __name(sameArray, "sameArray");
1668
+ function powerSet(array) {
1669
+ let sets = [[]];
1670
+ for (let i = 0; i < array.length; i++) {
1671
+ for (let j = 0, e = sets.length; j < e; j++) {
1672
+ sets.push(sets[j].concat(array[i]));
1673
+ }
1674
+ }
1675
+ return sets.sort((a, b) => b.length - a.length);
1676
+ }
1677
+ __name(powerSet, "powerSet");
1678
+ function styleTags(spec) {
1679
+ let byName = /* @__PURE__ */ Object.create(null);
1680
+ for (let prop in spec) {
1681
+ let tags2 = spec[prop];
1682
+ if (!Array.isArray(tags2))
1683
+ tags2 = [tags2];
1684
+ for (let part of prop.split(" "))
1685
+ if (part) {
1686
+ let pieces = [], mode = 2, rest = part;
1687
+ for (let pos = 0; ; ) {
1688
+ if (rest == "..." && pos > 0 && pos + 3 == part.length) {
1689
+ mode = 1;
1690
+ break;
1691
+ }
1692
+ let m = /^"(?:[^"\\]|\\.)*?"|[^\/!]+/.exec(rest);
1693
+ if (!m)
1694
+ throw new RangeError("Invalid path: " + part);
1695
+ pieces.push(m[0] == "*" ? "" : m[0][0] == '"' ? JSON.parse(m[0]) : m[0]);
1696
+ pos += m[0].length;
1697
+ if (pos == part.length)
1698
+ break;
1699
+ let next = part[pos++];
1700
+ if (pos == part.length && next == "!") {
1701
+ mode = 0;
1702
+ break;
1703
+ }
1704
+ if (next != "/")
1705
+ throw new RangeError("Invalid path: " + part);
1706
+ rest = part.slice(pos);
1707
+ }
1708
+ let last = pieces.length - 1, inner = pieces[last];
1709
+ if (!inner)
1710
+ throw new RangeError("Invalid path: " + part);
1711
+ let rule = new Rule(tags2, mode, last > 0 ? pieces.slice(0, last) : null);
1712
+ byName[inner] = rule.sort(byName[inner]);
1713
+ }
1714
+ }
1715
+ return ruleNodeProp.add(byName);
1716
+ }
1717
+ __name(styleTags, "styleTags");
1718
+ const ruleNodeProp = new NodeProp();
1719
+ const _Rule = class _Rule {
1720
+ constructor(tags2, mode, context, next) {
1721
+ this.tags = tags2;
1722
+ this.mode = mode;
1723
+ this.context = context;
1724
+ this.next = next;
1725
+ }
1726
+ get opaque() {
1727
+ return this.mode == 0;
1728
+ }
1729
+ get inherit() {
1730
+ return this.mode == 1;
1731
+ }
1732
+ sort(other) {
1733
+ if (!other || other.depth < this.depth) {
1734
+ this.next = other;
1735
+ return this;
1736
+ }
1737
+ other.next = this.sort(other.next);
1738
+ return other;
1739
+ }
1740
+ get depth() {
1741
+ return this.context ? this.context.length : 0;
1742
+ }
1743
+ };
1744
+ __name(_Rule, "Rule");
1745
+ let Rule = _Rule;
1746
+ Rule.empty = new Rule([], 2, null);
1747
+ function tagHighlighter(tags2, options) {
1748
+ let map = /* @__PURE__ */ Object.create(null);
1749
+ for (let style of tags2) {
1750
+ if (!Array.isArray(style.tag))
1751
+ map[style.tag.id] = style.class;
1752
+ else
1753
+ for (let tag of style.tag)
1754
+ map[tag.id] = style.class;
1755
+ }
1756
+ let { scope, all = null } = options || {};
1757
+ return {
1758
+ style: /* @__PURE__ */ __name((tags3) => {
1759
+ let cls = all;
1760
+ for (let tag of tags3) {
1761
+ for (let sub of tag.set) {
1762
+ let tagClass = map[sub.id];
1763
+ if (tagClass) {
1764
+ cls = cls ? cls + " " + tagClass : tagClass;
1765
+ break;
1766
+ }
1767
+ }
1768
+ }
1769
+ return cls;
1770
+ }, "style"),
1771
+ scope
1772
+ };
1773
+ }
1774
+ __name(tagHighlighter, "tagHighlighter");
1775
+ function highlightTags(highlighters, tags2) {
1776
+ let result = null;
1777
+ for (let highlighter2 of highlighters) {
1778
+ let value = highlighter2.style(tags2);
1779
+ if (value)
1780
+ result = result ? result + " " + value : value;
1781
+ }
1782
+ return result;
1783
+ }
1784
+ __name(highlightTags, "highlightTags");
1785
+ function highlightTree(tree, highlighter2, putStyle, from = 0, to = tree.length) {
1786
+ let builder = new HighlightBuilder(from, Array.isArray(highlighter2) ? highlighter2 : [highlighter2], putStyle);
1787
+ builder.highlightRange(tree.cursor(), from, to, "", builder.highlighters);
1788
+ builder.flush(to);
1789
+ }
1790
+ __name(highlightTree, "highlightTree");
1791
+ function highlightCode(code, tree, highlighter2, putText, putBreak, from = 0, to = code.length) {
1792
+ let pos = from;
1793
+ function writeTo(p, classes) {
1794
+ if (p <= pos)
1795
+ return;
1796
+ for (let text = code.slice(pos, p), i = 0; ; ) {
1797
+ let nextBreak = text.indexOf("\n", i);
1798
+ let upto = nextBreak < 0 ? text.length : nextBreak;
1799
+ if (upto > i)
1800
+ putText(text.slice(i, upto), classes);
1801
+ if (nextBreak < 0)
1802
+ break;
1803
+ putBreak();
1804
+ i = nextBreak + 1;
1805
+ }
1806
+ pos = p;
1807
+ }
1808
+ __name(writeTo, "writeTo");
1809
+ highlightTree(tree, highlighter2, (from2, to2, classes) => {
1810
+ writeTo(from2, "");
1811
+ writeTo(to2, classes);
1812
+ }, from, to);
1813
+ writeTo(to, "");
1814
+ }
1815
+ __name(highlightCode, "highlightCode");
1816
+ const _HighlightBuilder = class _HighlightBuilder {
1817
+ constructor(at, highlighters, span) {
1818
+ this.at = at;
1819
+ this.highlighters = highlighters;
1820
+ this.span = span;
1821
+ this.class = "";
1822
+ }
1823
+ startSpan(at, cls) {
1824
+ if (cls != this.class) {
1825
+ this.flush(at);
1826
+ if (at > this.at)
1827
+ this.at = at;
1828
+ this.class = cls;
1829
+ }
1830
+ }
1831
+ flush(to) {
1832
+ if (to > this.at && this.class)
1833
+ this.span(this.at, to, this.class);
1834
+ }
1835
+ highlightRange(cursor, from, to, inheritedClass, highlighters) {
1836
+ let { type, from: start, to: end } = cursor;
1837
+ if (start >= to || end <= from)
1838
+ return;
1839
+ if (type.isTop)
1840
+ highlighters = this.highlighters.filter((h) => !h.scope || h.scope(type));
1841
+ let cls = inheritedClass;
1842
+ let rule = getStyleTags(cursor) || Rule.empty;
1843
+ let tagCls = highlightTags(highlighters, rule.tags);
1844
+ if (tagCls) {
1845
+ if (cls)
1846
+ cls += " ";
1847
+ cls += tagCls;
1848
+ if (rule.mode == 1)
1849
+ inheritedClass += (inheritedClass ? " " : "") + tagCls;
1850
+ }
1851
+ this.startSpan(Math.max(from, start), cls);
1852
+ if (rule.opaque)
1853
+ return;
1854
+ let mounted = cursor.tree && cursor.tree.prop(NodeProp.mounted);
1855
+ if (mounted && mounted.overlay) {
1856
+ let inner = cursor.node.enter(mounted.overlay[0].from + start, 1);
1857
+ let innerHighlighters = this.highlighters.filter((h) => !h.scope || h.scope(mounted.tree.type));
1858
+ let hasChild2 = cursor.firstChild();
1859
+ for (let i = 0, pos = start; ; i++) {
1860
+ let next = i < mounted.overlay.length ? mounted.overlay[i] : null;
1861
+ let nextPos = next ? next.from + start : end;
1862
+ let rangeFrom = Math.max(from, pos), rangeTo = Math.min(to, nextPos);
1863
+ if (rangeFrom < rangeTo && hasChild2) {
1864
+ while (cursor.from < rangeTo) {
1865
+ this.highlightRange(cursor, rangeFrom, rangeTo, inheritedClass, highlighters);
1866
+ this.startSpan(Math.min(rangeTo, cursor.to), cls);
1867
+ if (cursor.to >= nextPos || !cursor.nextSibling())
1868
+ break;
1869
+ }
1870
+ }
1871
+ if (!next || nextPos > to)
1872
+ break;
1873
+ pos = next.to + start;
1874
+ if (pos > from) {
1875
+ this.highlightRange(inner.cursor(), Math.max(from, next.from + start), Math.min(to, pos), "", innerHighlighters);
1876
+ this.startSpan(Math.min(to, pos), cls);
1877
+ }
1878
+ }
1879
+ if (hasChild2)
1880
+ cursor.parent();
1881
+ } else if (cursor.firstChild()) {
1882
+ if (mounted)
1883
+ inheritedClass = "";
1884
+ do {
1885
+ if (cursor.to <= from)
1886
+ continue;
1887
+ if (cursor.from >= to)
1888
+ break;
1889
+ this.highlightRange(cursor, from, to, inheritedClass, highlighters);
1890
+ this.startSpan(Math.min(to, cursor.to), cls);
1891
+ } while (cursor.nextSibling());
1892
+ cursor.parent();
1893
+ }
1894
+ }
1895
+ };
1896
+ __name(_HighlightBuilder, "HighlightBuilder");
1897
+ let HighlightBuilder = _HighlightBuilder;
1898
+ function getStyleTags(node) {
1899
+ let rule = node.type.prop(ruleNodeProp);
1900
+ while (rule && rule.context && !node.matchContext(rule.context))
1901
+ rule = rule.next;
1902
+ return rule || null;
1903
+ }
1904
+ __name(getStyleTags, "getStyleTags");
1905
+ const t = Tag.define;
1906
+ const comment = t(), name = t(), typeName = t(name), propertyName = t(name), literal = t(), string = t(literal), number = t(literal), content = t(), heading = t(content), keyword = t(), operator = t(), punctuation = t(), bracket = t(punctuation), meta = t();
1907
+ const tags = {
1908
+ /**
1909
+ A comment.
1910
+ */
1911
+ comment,
1912
+ /**
1913
+ A line [comment](#highlight.tags.comment).
1914
+ */
1915
+ lineComment: t(comment),
1916
+ /**
1917
+ A block [comment](#highlight.tags.comment).
1918
+ */
1919
+ blockComment: t(comment),
1920
+ /**
1921
+ A documentation [comment](#highlight.tags.comment).
1922
+ */
1923
+ docComment: t(comment),
1924
+ /**
1925
+ Any kind of identifier.
1926
+ */
1927
+ name,
1928
+ /**
1929
+ The [name](#highlight.tags.name) of a variable.
1930
+ */
1931
+ variableName: t(name),
1932
+ /**
1933
+ A type [name](#highlight.tags.name).
1934
+ */
1935
+ typeName,
1936
+ /**
1937
+ A tag name (subtag of [`typeName`](#highlight.tags.typeName)).
1938
+ */
1939
+ tagName: t(typeName),
1940
+ /**
1941
+ A property or field [name](#highlight.tags.name).
1942
+ */
1943
+ propertyName,
1944
+ /**
1945
+ An attribute name (subtag of [`propertyName`](#highlight.tags.propertyName)).
1946
+ */
1947
+ attributeName: t(propertyName),
1948
+ /**
1949
+ The [name](#highlight.tags.name) of a class.
1950
+ */
1951
+ className: t(name),
1952
+ /**
1953
+ A label [name](#highlight.tags.name).
1954
+ */
1955
+ labelName: t(name),
1956
+ /**
1957
+ A namespace [name](#highlight.tags.name).
1958
+ */
1959
+ namespace: t(name),
1960
+ /**
1961
+ The [name](#highlight.tags.name) of a macro.
1962
+ */
1963
+ macroName: t(name),
1964
+ /**
1965
+ A literal value.
1966
+ */
1967
+ literal,
1968
+ /**
1969
+ A string [literal](#highlight.tags.literal).
1970
+ */
1971
+ string,
1972
+ /**
1973
+ A documentation [string](#highlight.tags.string).
1974
+ */
1975
+ docString: t(string),
1976
+ /**
1977
+ A character literal (subtag of [string](#highlight.tags.string)).
1978
+ */
1979
+ character: t(string),
1980
+ /**
1981
+ An attribute value (subtag of [string](#highlight.tags.string)).
1982
+ */
1983
+ attributeValue: t(string),
1984
+ /**
1985
+ A number [literal](#highlight.tags.literal).
1986
+ */
1987
+ number,
1988
+ /**
1989
+ An integer [number](#highlight.tags.number) literal.
1990
+ */
1991
+ integer: t(number),
1992
+ /**
1993
+ A floating-point [number](#highlight.tags.number) literal.
1994
+ */
1995
+ float: t(number),
1996
+ /**
1997
+ A boolean [literal](#highlight.tags.literal).
1998
+ */
1999
+ bool: t(literal),
2000
+ /**
2001
+ Regular expression [literal](#highlight.tags.literal).
2002
+ */
2003
+ regexp: t(literal),
2004
+ /**
2005
+ An escape [literal](#highlight.tags.literal), for example a
2006
+ backslash escape in a string.
2007
+ */
2008
+ escape: t(literal),
2009
+ /**
2010
+ A color [literal](#highlight.tags.literal).
2011
+ */
2012
+ color: t(literal),
2013
+ /**
2014
+ A URL [literal](#highlight.tags.literal).
2015
+ */
2016
+ url: t(literal),
2017
+ /**
2018
+ A language keyword.
2019
+ */
2020
+ keyword,
2021
+ /**
2022
+ The [keyword](#highlight.tags.keyword) for the self or this
2023
+ object.
2024
+ */
2025
+ self: t(keyword),
2026
+ /**
2027
+ The [keyword](#highlight.tags.keyword) for null.
2028
+ */
2029
+ null: t(keyword),
2030
+ /**
2031
+ A [keyword](#highlight.tags.keyword) denoting some atomic value.
2032
+ */
2033
+ atom: t(keyword),
2034
+ /**
2035
+ A [keyword](#highlight.tags.keyword) that represents a unit.
2036
+ */
2037
+ unit: t(keyword),
2038
+ /**
2039
+ A modifier [keyword](#highlight.tags.keyword).
2040
+ */
2041
+ modifier: t(keyword),
2042
+ /**
2043
+ A [keyword](#highlight.tags.keyword) that acts as an operator.
2044
+ */
2045
+ operatorKeyword: t(keyword),
2046
+ /**
2047
+ A control-flow related [keyword](#highlight.tags.keyword).
2048
+ */
2049
+ controlKeyword: t(keyword),
2050
+ /**
2051
+ A [keyword](#highlight.tags.keyword) that defines something.
2052
+ */
2053
+ definitionKeyword: t(keyword),
2054
+ /**
2055
+ A [keyword](#highlight.tags.keyword) related to defining or
2056
+ interfacing with modules.
2057
+ */
2058
+ moduleKeyword: t(keyword),
2059
+ /**
2060
+ An operator.
2061
+ */
2062
+ operator,
2063
+ /**
2064
+ An [operator](#highlight.tags.operator) that dereferences something.
2065
+ */
2066
+ derefOperator: t(operator),
2067
+ /**
2068
+ Arithmetic-related [operator](#highlight.tags.operator).
2069
+ */
2070
+ arithmeticOperator: t(operator),
2071
+ /**
2072
+ Logical [operator](#highlight.tags.operator).
2073
+ */
2074
+ logicOperator: t(operator),
2075
+ /**
2076
+ Bit [operator](#highlight.tags.operator).
2077
+ */
2078
+ bitwiseOperator: t(operator),
2079
+ /**
2080
+ Comparison [operator](#highlight.tags.operator).
2081
+ */
2082
+ compareOperator: t(operator),
2083
+ /**
2084
+ [Operator](#highlight.tags.operator) that updates its operand.
2085
+ */
2086
+ updateOperator: t(operator),
2087
+ /**
2088
+ [Operator](#highlight.tags.operator) that defines something.
2089
+ */
2090
+ definitionOperator: t(operator),
2091
+ /**
2092
+ Type-related [operator](#highlight.tags.operator).
2093
+ */
2094
+ typeOperator: t(operator),
2095
+ /**
2096
+ Control-flow [operator](#highlight.tags.operator).
2097
+ */
2098
+ controlOperator: t(operator),
2099
+ /**
2100
+ Program or markup punctuation.
2101
+ */
2102
+ punctuation,
2103
+ /**
2104
+ [Punctuation](#highlight.tags.punctuation) that separates
2105
+ things.
2106
+ */
2107
+ separator: t(punctuation),
2108
+ /**
2109
+ Bracket-style [punctuation](#highlight.tags.punctuation).
2110
+ */
2111
+ bracket,
2112
+ /**
2113
+ Angle [brackets](#highlight.tags.bracket) (usually `<` and `>`
2114
+ tokens).
2115
+ */
2116
+ angleBracket: t(bracket),
2117
+ /**
2118
+ Square [brackets](#highlight.tags.bracket) (usually `[` and `]`
2119
+ tokens).
2120
+ */
2121
+ squareBracket: t(bracket),
2122
+ /**
2123
+ Parentheses (usually `(` and `)` tokens). Subtag of
2124
+ [bracket](#highlight.tags.bracket).
2125
+ */
2126
+ paren: t(bracket),
2127
+ /**
2128
+ Braces (usually `{` and `}` tokens). Subtag of
2129
+ [bracket](#highlight.tags.bracket).
2130
+ */
2131
+ brace: t(bracket),
2132
+ /**
2133
+ Content, for example plain text in XML or markup documents.
2134
+ */
2135
+ content,
2136
+ /**
2137
+ [Content](#highlight.tags.content) that represents a heading.
2138
+ */
2139
+ heading,
2140
+ /**
2141
+ A level 1 [heading](#highlight.tags.heading).
2142
+ */
2143
+ heading1: t(heading),
2144
+ /**
2145
+ A level 2 [heading](#highlight.tags.heading).
2146
+ */
2147
+ heading2: t(heading),
2148
+ /**
2149
+ A level 3 [heading](#highlight.tags.heading).
2150
+ */
2151
+ heading3: t(heading),
2152
+ /**
2153
+ A level 4 [heading](#highlight.tags.heading).
2154
+ */
2155
+ heading4: t(heading),
2156
+ /**
2157
+ A level 5 [heading](#highlight.tags.heading).
2158
+ */
2159
+ heading5: t(heading),
2160
+ /**
2161
+ A level 6 [heading](#highlight.tags.heading).
2162
+ */
2163
+ heading6: t(heading),
2164
+ /**
2165
+ A prose [content](#highlight.tags.content) separator (such as a horizontal rule).
2166
+ */
2167
+ contentSeparator: t(content),
2168
+ /**
2169
+ [Content](#highlight.tags.content) that represents a list.
2170
+ */
2171
+ list: t(content),
2172
+ /**
2173
+ [Content](#highlight.tags.content) that represents a quote.
2174
+ */
2175
+ quote: t(content),
2176
+ /**
2177
+ [Content](#highlight.tags.content) that is emphasized.
2178
+ */
2179
+ emphasis: t(content),
2180
+ /**
2181
+ [Content](#highlight.tags.content) that is styled strong.
2182
+ */
2183
+ strong: t(content),
2184
+ /**
2185
+ [Content](#highlight.tags.content) that is part of a link.
2186
+ */
2187
+ link: t(content),
2188
+ /**
2189
+ [Content](#highlight.tags.content) that is styled as code or
2190
+ monospace.
2191
+ */
2192
+ monospace: t(content),
2193
+ /**
2194
+ [Content](#highlight.tags.content) that has a strike-through
2195
+ style.
2196
+ */
2197
+ strikethrough: t(content),
2198
+ /**
2199
+ Inserted text in a change-tracking format.
2200
+ */
2201
+ inserted: t(),
2202
+ /**
2203
+ Deleted text.
2204
+ */
2205
+ deleted: t(),
2206
+ /**
2207
+ Changed text.
2208
+ */
2209
+ changed: t(),
2210
+ /**
2211
+ An invalid or unsyntactic element.
2212
+ */
2213
+ invalid: t(),
2214
+ /**
2215
+ Metadata or meta-instruction.
2216
+ */
2217
+ meta,
2218
+ /**
2219
+ [Metadata](#highlight.tags.meta) that applies to the entire
2220
+ document.
2221
+ */
2222
+ documentMeta: t(meta),
2223
+ /**
2224
+ [Metadata](#highlight.tags.meta) that annotates or adds
2225
+ attributes to a given syntactic element.
2226
+ */
2227
+ annotation: t(meta),
2228
+ /**
2229
+ Processing instruction or preprocessor directive. Subtag of
2230
+ [meta](#highlight.tags.meta).
2231
+ */
2232
+ processingInstruction: t(meta),
2233
+ /**
2234
+ [Modifier](#highlight.Tag^defineModifier) that indicates that a
2235
+ given element is being defined. Expected to be used with the
2236
+ various [name](#highlight.tags.name) tags.
2237
+ */
2238
+ definition: Tag.defineModifier("definition"),
2239
+ /**
2240
+ [Modifier](#highlight.Tag^defineModifier) that indicates that
2241
+ something is constant. Mostly expected to be used with
2242
+ [variable names](#highlight.tags.variableName).
2243
+ */
2244
+ constant: Tag.defineModifier("constant"),
2245
+ /**
2246
+ [Modifier](#highlight.Tag^defineModifier) used to indicate that
2247
+ a [variable](#highlight.tags.variableName) or [property
2248
+ name](#highlight.tags.propertyName) is being called or defined
2249
+ as a function.
2250
+ */
2251
+ function: Tag.defineModifier("function"),
2252
+ /**
2253
+ [Modifier](#highlight.Tag^defineModifier) that can be applied to
2254
+ [names](#highlight.tags.name) to indicate that they belong to
2255
+ the language's standard environment.
2256
+ */
2257
+ standard: Tag.defineModifier("standard"),
2258
+ /**
2259
+ [Modifier](#highlight.Tag^defineModifier) that indicates a given
2260
+ [names](#highlight.tags.name) is local to some scope.
2261
+ */
2262
+ local: Tag.defineModifier("local"),
2263
+ /**
2264
+ A generic variant [modifier](#highlight.Tag^defineModifier) that
2265
+ can be used to tag language-specific alternative variants of
2266
+ some common tag. It is recommended for themes to define special
2267
+ forms of at least the [string](#highlight.tags.string) and
2268
+ [variable name](#highlight.tags.variableName) tags, since those
2269
+ come up a lot.
2270
+ */
2271
+ special: Tag.defineModifier("special")
2272
+ };
2273
+ for (let name2 in tags) {
2274
+ let val = tags[name2];
2275
+ if (val instanceof Tag)
2276
+ val.name = name2;
2277
+ }
2278
+ tagHighlighter([
2279
+ { tag: tags.link, class: "tok-link" },
2280
+ { tag: tags.heading, class: "tok-heading" },
2281
+ { tag: tags.emphasis, class: "tok-emphasis" },
2282
+ { tag: tags.strong, class: "tok-strong" },
2283
+ { tag: tags.keyword, class: "tok-keyword" },
2284
+ { tag: tags.atom, class: "tok-atom" },
2285
+ { tag: tags.bool, class: "tok-bool" },
2286
+ { tag: tags.url, class: "tok-url" },
2287
+ { tag: tags.labelName, class: "tok-labelName" },
2288
+ { tag: tags.inserted, class: "tok-inserted" },
2289
+ { tag: tags.deleted, class: "tok-deleted" },
2290
+ { tag: tags.literal, class: "tok-literal" },
2291
+ { tag: tags.string, class: "tok-string" },
2292
+ { tag: tags.number, class: "tok-number" },
2293
+ { tag: [tags.regexp, tags.escape, tags.special(tags.string)], class: "tok-string2" },
2294
+ { tag: tags.variableName, class: "tok-variableName" },
2295
+ { tag: tags.local(tags.variableName), class: "tok-variableName tok-local" },
2296
+ { tag: tags.definition(tags.variableName), class: "tok-variableName tok-definition" },
2297
+ { tag: tags.special(tags.variableName), class: "tok-variableName2" },
2298
+ { tag: tags.definition(tags.propertyName), class: "tok-propertyName tok-definition" },
2299
+ { tag: tags.typeName, class: "tok-typeName" },
2300
+ { tag: tags.namespace, class: "tok-namespace" },
2301
+ { tag: tags.className, class: "tok-className" },
2302
+ { tag: tags.macroName, class: "tok-macroName" },
2303
+ { tag: tags.propertyName, class: "tok-propertyName" },
2304
+ { tag: tags.operator, class: "tok-operator" },
2305
+ { tag: tags.comment, class: "tok-comment" },
2306
+ { tag: tags.meta, class: "tok-meta" },
2307
+ { tag: tags.invalid, class: "tok-invalid" },
2308
+ { tag: tags.punctuation, class: "tok-punctuation" }
2309
+ ]);
2310
+ const highlighter = tagHighlighter([
2311
+ { tag: tags.link, class: "tok-link" },
2312
+ { tag: [tags.heading, tags.strong], class: "tok-strong" },
2313
+ { tag: tags.emphasis, class: "tok-emphasis" },
2314
+ { tag: [tags.keyword, tags.typeName], class: "tok-keyword" },
2315
+ { tag: tags.atom, class: "tok-atom" },
2316
+ { tag: [tags.bool, tags.escape, tags.number], class: "tok-literal" },
2317
+ { tag: [tags.string, tags.regexp], class: "tok-string" },
2318
+ { tag: tags.comment, class: "tok-comment" },
2319
+ { tag: tags.invalid, class: "tok-invalid" },
2320
+ { tag: tags.variableName, class: "tok-variable" },
2321
+ { tag: [tags.definition(tags.variableName), tags.className, tags.propertyName], class: "tok-definition" },
2322
+ { tag: tags.meta, class: "tok-meta" }
2323
+ ]);
2324
+ async function highlight(code) {
2325
+ const language = getLanguage(code);
2326
+ if (!language)
2327
+ return;
2328
+ const parent = code.parentElement;
2329
+ if (parent)
2330
+ parent.dataset.language = language;
2331
+ const parser = await getParser(language);
2332
+ if (!parser)
2333
+ return;
2334
+ const document = code.ownerDocument;
2335
+ const text = code.textContent;
2336
+ const tree = parser.parse(text);
2337
+ while (code.lastChild)
2338
+ code.lastChild.remove();
2339
+ function emit(text2, classes) {
2340
+ let node = document.createTextNode(text2);
2341
+ if (classes) {
2342
+ const span = document.createElement("span");
2343
+ span.appendChild(node);
2344
+ span.className = classes;
2345
+ node = span;
2346
+ }
2347
+ code.appendChild(node);
2348
+ }
2349
+ __name(emit, "emit");
2350
+ function emitBreak() {
2351
+ code.appendChild(document.createTextNode("\n"));
2352
+ }
2353
+ __name(emitBreak, "emitBreak");
2354
+ highlightCode(text, tree, highlighter, emit, emitBreak);
2355
+ }
2356
+ __name(highlight, "highlight");
2357
+ async function getParser(language) {
2358
+ switch (language) {
2359
+ case "js":
2360
+ case "ts":
2361
+ case "jsx":
2362
+ return (await import("./index-DNwLE6Kk.js")).parser.configure({ dialect: language });
2363
+ case "html":
2364
+ return (await import("./index-COHaJzee.js")).parser;
2365
+ case "css":
2366
+ return (await import("./index-D3TL70UM.js")).parser;
2367
+ case "md":
2368
+ case "markdown":
2369
+ return (await import("./index-CP3HrqN0.js")).parser;
2370
+ }
2371
+ }
2372
+ __name(getParser, "getParser");
2373
+ function getLanguage(code) {
2374
+ const language = [...code.classList].find((c) => c.startsWith("language-"))?.slice("language-".length)?.toLowerCase();
2375
+ switch (language) {
2376
+ case "javascript":
2377
+ return "js";
2378
+ case "typescript":
2379
+ return "ts";
2380
+ }
2381
+ return language;
2382
+ }
2383
+ __name(getLanguage, "getLanguage");
2384
+ const highlight$1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
2385
+ __proto__: null,
2386
+ highlight,
2387
+ highlighter
2388
+ }, Symbol.toStringTag, { value: "Module" }));
2389
+ export {
2390
+ DefaultBufferLength as D,
2391
+ IterMode as I,
2392
+ NodeSet as N,
2393
+ Parser as P,
2394
+ Tree as T,
2395
+ NodeType as a,
2396
+ NodeProp as b,
2397
+ Tag as c,
2398
+ highlight$1 as h,
2399
+ styleTags as s,
2400
+ tags as t
2401
+ };
2402
+ //# sourceMappingURL=highlight-Bv8PkwN-.js.map