@lezer/common 1.0.3 → 1.0.4

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.
package/dist/index.d.ts CHANGED
@@ -1,3 +1,1115 @@
1
- export { DefaultBufferLength, NodeProp, MountedTree, NodePropSource, NodeType, NodeSet, Tree, TreeBuffer, SyntaxNode, SyntaxNodeRef, TreeCursor, BufferCursor, NodeWeakMap, IterMode } from "./tree";
2
- export { ChangedRange, TreeFragment, PartialParse, Parser, Input, ParseWrapper } from "./parse";
3
- export { NestedParse, parseMixed } from "./mix";
1
+ /**
2
+ The [`TreeFragment.applyChanges`](#common.TreeFragment^applyChanges)
3
+ method expects changed ranges in this format.
4
+ */
5
+ interface ChangedRange {
6
+ /**
7
+ The start of the change in the start document
8
+ */
9
+ fromA: number;
10
+ /**
11
+ The end of the change in the start document
12
+ */
13
+ toA: number;
14
+ /**
15
+ The start of the replacement in the new document
16
+ */
17
+ fromB: number;
18
+ /**
19
+ The end of the replacement in the new document
20
+ */
21
+ toB: number;
22
+ }
23
+ /**
24
+ Tree fragments are used during [incremental
25
+ parsing](#common.Parser.startParse) to track parts of old trees
26
+ that can be reused in a new parse. An array of fragments is used
27
+ to track regions of an old tree whose nodes might be reused in new
28
+ parses. Use the static
29
+ [`applyChanges`](#common.TreeFragment^applyChanges) method to
30
+ update fragments for document changes.
31
+ */
32
+ declare class TreeFragment {
33
+ /**
34
+ The start of the unchanged range pointed to by this fragment.
35
+ This refers to an offset in the _updated_ document (as opposed
36
+ to the original tree).
37
+ */
38
+ readonly from: number;
39
+ /**
40
+ The end of the unchanged range.
41
+ */
42
+ readonly to: number;
43
+ /**
44
+ The tree that this fragment is based on.
45
+ */
46
+ readonly tree: Tree;
47
+ /**
48
+ The offset between the fragment's tree and the document that
49
+ this fragment can be used against. Add this when going from
50
+ document to tree positions, subtract it to go from tree to
51
+ document positions.
52
+ */
53
+ readonly offset: number;
54
+ /**
55
+ Construct a tree fragment. You'll usually want to use
56
+ [`addTree`](#common.TreeFragment^addTree) and
57
+ [`applyChanges`](#common.TreeFragment^applyChanges) instead of
58
+ calling this directly.
59
+ */
60
+ constructor(
61
+ /**
62
+ The start of the unchanged range pointed to by this fragment.
63
+ This refers to an offset in the _updated_ document (as opposed
64
+ to the original tree).
65
+ */
66
+ from: number,
67
+ /**
68
+ The end of the unchanged range.
69
+ */
70
+ to: number,
71
+ /**
72
+ The tree that this fragment is based on.
73
+ */
74
+ tree: Tree,
75
+ /**
76
+ The offset between the fragment's tree and the document that
77
+ this fragment can be used against. Add this when going from
78
+ document to tree positions, subtract it to go from tree to
79
+ document positions.
80
+ */
81
+ offset: number, openStart?: boolean, openEnd?: boolean);
82
+ /**
83
+ Whether the start of the fragment represents the start of a
84
+ parse, or the end of a change. (In the second case, it may not
85
+ be safe to reuse some nodes at the start, depending on the
86
+ parsing algorithm.)
87
+ */
88
+ get openStart(): boolean;
89
+ /**
90
+ Whether the end of the fragment represents the end of a
91
+ full-document parse, or the start of a change.
92
+ */
93
+ get openEnd(): boolean;
94
+ /**
95
+ Create a set of fragments from a freshly parsed tree, or update
96
+ an existing set of fragments by replacing the ones that overlap
97
+ with a tree with content from the new tree. When `partial` is
98
+ true, the parse is treated as incomplete, and the resulting
99
+ fragment has [`openEnd`](#common.TreeFragment.openEnd) set to
100
+ true.
101
+ */
102
+ static addTree(tree: Tree, fragments?: readonly TreeFragment[], partial?: boolean): readonly TreeFragment[];
103
+ /**
104
+ Apply a set of edits to an array of fragments, removing or
105
+ splitting fragments as necessary to remove edited ranges, and
106
+ adjusting offsets for fragments that moved.
107
+ */
108
+ static applyChanges(fragments: readonly TreeFragment[], changes: readonly ChangedRange[], minGap?: number): readonly TreeFragment[];
109
+ }
110
+ /**
111
+ Interface used to represent an in-progress parse, which can be
112
+ moved forward piece-by-piece.
113
+ */
114
+ interface PartialParse {
115
+ /**
116
+ Advance the parse state by some amount. Will return the finished
117
+ syntax tree when the parse completes.
118
+ */
119
+ advance(): Tree | null;
120
+ /**
121
+ The position up to which the document has been parsed. Note
122
+ that, in multi-pass parsers, this will stay back until the last
123
+ pass has moved past a given position.
124
+ */
125
+ readonly parsedPos: number;
126
+ /**
127
+ Tell the parse to not advance beyond the given position.
128
+ `advance` will return a tree when the parse has reached the
129
+ position. Note that, depending on the parser algorithm and the
130
+ state of the parse when `stopAt` was called, that tree may
131
+ contain nodes beyond the position. It is an error to call
132
+ `stopAt` with a higher position than it's [current
133
+ value](#common.PartialParse.stoppedAt).
134
+ */
135
+ stopAt(pos: number): void;
136
+ /**
137
+ Reports whether `stopAt` has been called on this parse.
138
+ */
139
+ readonly stoppedAt: number | null;
140
+ }
141
+ /**
142
+ A superclass that parsers should extend.
143
+ */
144
+ declare abstract class Parser {
145
+ /**
146
+ Start a parse for a single tree. This is the method concrete
147
+ parser implementations must implement. Called by `startParse`,
148
+ with the optional arguments resolved.
149
+ */
150
+ abstract createParse(input: Input, fragments: readonly TreeFragment[], ranges: readonly {
151
+ from: number;
152
+ to: number;
153
+ }[]): PartialParse;
154
+ /**
155
+ Start a parse, returning a [partial parse](#common.PartialParse)
156
+ object. [`fragments`](#common.TreeFragment) can be passed in to
157
+ make the parse incremental.
158
+
159
+ By default, the entire input is parsed. You can pass `ranges`,
160
+ which should be a sorted array of non-empty, non-overlapping
161
+ ranges, to parse only those ranges. The tree returned in that
162
+ case will start at `ranges[0].from`.
163
+ */
164
+ startParse(input: Input | string, fragments?: readonly TreeFragment[], ranges?: readonly {
165
+ from: number;
166
+ to: number;
167
+ }[]): PartialParse;
168
+ /**
169
+ Run a full parse, returning the resulting tree.
170
+ */
171
+ parse(input: Input | string, fragments?: readonly TreeFragment[], ranges?: readonly {
172
+ from: number;
173
+ to: number;
174
+ }[]): Tree;
175
+ }
176
+ /**
177
+ This is the interface parsers use to access the document. To run
178
+ Lezer directly on your own document data structure, you have to
179
+ write an implementation of it.
180
+ */
181
+ interface Input {
182
+ /**
183
+ The length of the document.
184
+ */
185
+ readonly length: number;
186
+ /**
187
+ Get the chunk after the given position. The returned string
188
+ should start at `from` and, if that isn't the end of the
189
+ document, may be of any length greater than zero.
190
+ */
191
+ chunk(from: number): string;
192
+ /**
193
+ Indicates whether the chunks already end at line breaks, so that
194
+ client code that wants to work by-line can avoid re-scanning
195
+ them for line breaks. When this is true, the result of `chunk()`
196
+ should either be a single line break, or the content between
197
+ `from` and the next line break.
198
+ */
199
+ readonly lineChunks: boolean;
200
+ /**
201
+ Read the part of the document between the given positions.
202
+ */
203
+ read(from: number, to: number): string;
204
+ }
205
+ /**
206
+ Parse wrapper functions are supported by some parsers to inject
207
+ additional parsing logic.
208
+ */
209
+ type ParseWrapper = (inner: PartialParse, input: Input, fragments: readonly TreeFragment[], ranges: readonly {
210
+ from: number;
211
+ to: number;
212
+ }[]) => PartialParse;
213
+
214
+ /**
215
+ The default maximum length of a `TreeBuffer` node.
216
+ */
217
+ declare const DefaultBufferLength = 1024;
218
+ /**
219
+ Each [node type](#common.NodeType) or [individual tree](#common.Tree)
220
+ can have metadata associated with it in props. Instances of this
221
+ class represent prop names.
222
+ */
223
+ declare class NodeProp<T> {
224
+ /**
225
+ Indicates whether this prop is stored per [node
226
+ type](#common.NodeType) or per [tree node](#common.Tree).
227
+ */
228
+ perNode: boolean;
229
+ /**
230
+ A method that deserializes a value of this prop from a string.
231
+ Can be used to allow a prop to be directly written in a grammar
232
+ file.
233
+ */
234
+ deserialize: (str: string) => T;
235
+ /**
236
+ Create a new node prop type.
237
+ */
238
+ constructor(config?: {
239
+ /**
240
+ The [deserialize](#common.NodeProp.deserialize) function to
241
+ use for this prop, used for example when directly providing
242
+ the prop from a grammar file. Defaults to a function that
243
+ raises an error.
244
+ */
245
+ deserialize?: (str: string) => T;
246
+ /**
247
+ By default, node props are stored in the [node
248
+ type](#common.NodeType). It can sometimes be useful to directly
249
+ store information (usually related to the parsing algorithm)
250
+ in [nodes](#common.Tree) themselves. Set this to true to enable
251
+ that for this prop.
252
+ */
253
+ perNode?: boolean;
254
+ });
255
+ /**
256
+ This is meant to be used with
257
+ [`NodeSet.extend`](#common.NodeSet.extend) or
258
+ [`LRParser.configure`](#lr.ParserConfig.props) to compute
259
+ prop values for each node type in the set. Takes a [match
260
+ object](#common.NodeType^match) or function that returns undefined
261
+ if the node type doesn't get this prop, and the prop's value if
262
+ it does.
263
+ */
264
+ add(match: {
265
+ [selector: string]: T;
266
+ } | ((type: NodeType) => T | undefined)): NodePropSource;
267
+ /**
268
+ Prop that is used to describe matching delimiters. For opening
269
+ delimiters, this holds an array of node names (written as a
270
+ space-separated string when declaring this prop in a grammar)
271
+ for the node types of closing delimiters that match it.
272
+ */
273
+ static closedBy: NodeProp<readonly string[]>;
274
+ /**
275
+ The inverse of [`closedBy`](#common.NodeProp^closedBy). This is
276
+ attached to closing delimiters, holding an array of node names
277
+ of types of matching opening delimiters.
278
+ */
279
+ static openedBy: NodeProp<readonly string[]>;
280
+ /**
281
+ Used to assign node types to groups (for example, all node
282
+ types that represent an expression could be tagged with an
283
+ `"Expression"` group).
284
+ */
285
+ static group: NodeProp<readonly string[]>;
286
+ /**
287
+ The hash of the [context](#lr.ContextTracker.constructor)
288
+ that the node was parsed in, if any. Used to limit reuse of
289
+ contextual nodes.
290
+ */
291
+ static contextHash: NodeProp<number>;
292
+ /**
293
+ The distance beyond the end of the node that the tokenizer
294
+ looked ahead for any of the tokens inside the node. (The LR
295
+ parser only stores this when it is larger than 25, for
296
+ efficiency reasons.)
297
+ */
298
+ static lookAhead: NodeProp<number>;
299
+ /**
300
+ This per-node prop is used to replace a given node, or part of a
301
+ node, with another tree. This is useful to include trees from
302
+ different languages in mixed-language parsers.
303
+ */
304
+ static mounted: NodeProp<MountedTree>;
305
+ }
306
+ /**
307
+ A mounted tree, which can be [stored](#common.NodeProp^mounted) on
308
+ a tree node to indicate that parts of its content are
309
+ represented by another tree.
310
+ */
311
+ declare class MountedTree {
312
+ /**
313
+ The inner tree.
314
+ */
315
+ readonly tree: Tree;
316
+ /**
317
+ If this is null, this tree replaces the entire node (it will
318
+ be included in the regular iteration instead of its host
319
+ node). If not, only the given ranges are considered to be
320
+ covered by this tree. This is used for trees that are mixed in
321
+ a way that isn't strictly hierarchical. Such mounted trees are
322
+ only entered by [`resolveInner`](#common.Tree.resolveInner)
323
+ and [`enter`](#common.SyntaxNode.enter).
324
+ */
325
+ readonly overlay: readonly {
326
+ from: number;
327
+ to: number;
328
+ }[] | null;
329
+ /**
330
+ The parser used to create this subtree.
331
+ */
332
+ readonly parser: Parser;
333
+ constructor(
334
+ /**
335
+ The inner tree.
336
+ */
337
+ tree: Tree,
338
+ /**
339
+ If this is null, this tree replaces the entire node (it will
340
+ be included in the regular iteration instead of its host
341
+ node). If not, only the given ranges are considered to be
342
+ covered by this tree. This is used for trees that are mixed in
343
+ a way that isn't strictly hierarchical. Such mounted trees are
344
+ only entered by [`resolveInner`](#common.Tree.resolveInner)
345
+ and [`enter`](#common.SyntaxNode.enter).
346
+ */
347
+ overlay: readonly {
348
+ from: number;
349
+ to: number;
350
+ }[] | null,
351
+ /**
352
+ The parser used to create this subtree.
353
+ */
354
+ parser: Parser);
355
+ }
356
+ /**
357
+ Type returned by [`NodeProp.add`](#common.NodeProp.add). Describes
358
+ whether a prop should be added to a given node type in a node set,
359
+ and what value it should have.
360
+ */
361
+ type NodePropSource = (type: NodeType) => null | [NodeProp<any>, any];
362
+ /**
363
+ Each node in a syntax tree has a node type associated with it.
364
+ */
365
+ declare class NodeType {
366
+ /**
367
+ The name of the node type. Not necessarily unique, but if the
368
+ grammar was written properly, different node types with the
369
+ same name within a node set should play the same semantic
370
+ role.
371
+ */
372
+ readonly name: string;
373
+ /**
374
+ The id of this node in its set. Corresponds to the term ids
375
+ used in the parser.
376
+ */
377
+ readonly id: number;
378
+ /**
379
+ Define a node type.
380
+ */
381
+ static define(spec: {
382
+ /**
383
+ The ID of the node type. When this type is used in a
384
+ [set](#common.NodeSet), the ID must correspond to its index in
385
+ the type array.
386
+ */
387
+ id: number;
388
+ /**
389
+ The name of the node type. Leave empty to define an anonymous
390
+ node.
391
+ */
392
+ name?: string;
393
+ /**
394
+ [Node props](#common.NodeProp) to assign to the type. The value
395
+ given for any given prop should correspond to the prop's type.
396
+ */
397
+ props?: readonly ([NodeProp<any>, any] | NodePropSource)[];
398
+ /**
399
+ Whether this is a [top node](#common.NodeType.isTop).
400
+ */
401
+ top?: boolean;
402
+ /**
403
+ Whether this node counts as an [error
404
+ node](#common.NodeType.isError).
405
+ */
406
+ error?: boolean;
407
+ /**
408
+ Whether this node is a [skipped](#common.NodeType.isSkipped)
409
+ node.
410
+ */
411
+ skipped?: boolean;
412
+ }): NodeType;
413
+ /**
414
+ Retrieves a node prop for this type. Will return `undefined` if
415
+ the prop isn't present on this node.
416
+ */
417
+ prop<T>(prop: NodeProp<T>): T | undefined;
418
+ /**
419
+ True when this is the top node of a grammar.
420
+ */
421
+ get isTop(): boolean;
422
+ /**
423
+ True when this node is produced by a skip rule.
424
+ */
425
+ get isSkipped(): boolean;
426
+ /**
427
+ Indicates whether this is an error node.
428
+ */
429
+ get isError(): boolean;
430
+ /**
431
+ When true, this node type doesn't correspond to a user-declared
432
+ named node, for example because it is used to cache repetition.
433
+ */
434
+ get isAnonymous(): boolean;
435
+ /**
436
+ Returns true when this node's name or one of its
437
+ [groups](#common.NodeProp^group) matches the given string.
438
+ */
439
+ is(name: string | number): boolean;
440
+ /**
441
+ An empty dummy node type to use when no actual type is available.
442
+ */
443
+ static none: NodeType;
444
+ /**
445
+ Create a function from node types to arbitrary values by
446
+ specifying an object whose property names are node or
447
+ [group](#common.NodeProp^group) names. Often useful with
448
+ [`NodeProp.add`](#common.NodeProp.add). You can put multiple
449
+ names, separated by spaces, in a single property name to map
450
+ multiple node names to a single value.
451
+ */
452
+ static match<T>(map: {
453
+ [selector: string]: T;
454
+ }): (node: NodeType) => T | undefined;
455
+ }
456
+ /**
457
+ A node set holds a collection of node types. It is used to
458
+ compactly represent trees by storing their type ids, rather than a
459
+ full pointer to the type object, in a numeric array. Each parser
460
+ [has](#lr.LRParser.nodeSet) a node set, and [tree
461
+ buffers](#common.TreeBuffer) can only store collections of nodes
462
+ from the same set. A set can have a maximum of 2**16 (65536) node
463
+ types in it, so that the ids fit into 16-bit typed array slots.
464
+ */
465
+ declare class NodeSet {
466
+ /**
467
+ The node types in this set, by id.
468
+ */
469
+ readonly types: readonly NodeType[];
470
+ /**
471
+ Create a set with the given types. The `id` property of each
472
+ type should correspond to its position within the array.
473
+ */
474
+ constructor(
475
+ /**
476
+ The node types in this set, by id.
477
+ */
478
+ types: readonly NodeType[]);
479
+ /**
480
+ Create a copy of this set with some node properties added. The
481
+ arguments to this method can be created with
482
+ [`NodeProp.add`](#common.NodeProp.add).
483
+ */
484
+ extend(...props: NodePropSource[]): NodeSet;
485
+ }
486
+ /**
487
+ Options that control iteration. Can be combined with the `|`
488
+ operator to enable multiple ones.
489
+ */
490
+ declare enum IterMode {
491
+ /**
492
+ When enabled, iteration will only visit [`Tree`](#common.Tree)
493
+ objects, not nodes packed into
494
+ [`TreeBuffer`](#common.TreeBuffer)s.
495
+ */
496
+ ExcludeBuffers = 1,
497
+ /**
498
+ Enable this to make iteration include anonymous nodes (such as
499
+ the nodes that wrap repeated grammar constructs into a balanced
500
+ tree).
501
+ */
502
+ IncludeAnonymous = 2,
503
+ /**
504
+ By default, regular [mounted](#common.NodeProp^mounted) nodes
505
+ replace their base node in iteration. Enable this to ignore them
506
+ instead.
507
+ */
508
+ IgnoreMounts = 4,
509
+ /**
510
+ This option only applies in
511
+ [`enter`](#common.SyntaxNode.enter)-style methods. It tells the
512
+ library to not enter mounted overlays if one covers the given
513
+ position.
514
+ */
515
+ IgnoreOverlays = 8
516
+ }
517
+ /**
518
+ A piece of syntax tree. There are two ways to approach these
519
+ trees: the way they are actually stored in memory, and the
520
+ convenient way.
521
+
522
+ Syntax trees are stored as a tree of `Tree` and `TreeBuffer`
523
+ objects. By packing detail information into `TreeBuffer` leaf
524
+ nodes, the representation is made a lot more memory-efficient.
525
+
526
+ However, when you want to actually work with tree nodes, this
527
+ representation is very awkward, so most client code will want to
528
+ use the [`TreeCursor`](#common.TreeCursor) or
529
+ [`SyntaxNode`](#common.SyntaxNode) interface instead, which provides
530
+ a view on some part of this data structure, and can be used to
531
+ move around to adjacent nodes.
532
+ */
533
+ declare class Tree {
534
+ /**
535
+ The type of the top node.
536
+ */
537
+ readonly type: NodeType;
538
+ /**
539
+ This node's child nodes.
540
+ */
541
+ readonly children: readonly (Tree | TreeBuffer)[];
542
+ /**
543
+ The positions (offsets relative to the start of this tree) of
544
+ the children.
545
+ */
546
+ readonly positions: readonly number[];
547
+ /**
548
+ The total length of this tree
549
+ */
550
+ readonly length: number;
551
+ /**
552
+ Construct a new tree. See also [`Tree.build`](#common.Tree^build).
553
+ */
554
+ constructor(
555
+ /**
556
+ The type of the top node.
557
+ */
558
+ type: NodeType,
559
+ /**
560
+ This node's child nodes.
561
+ */
562
+ children: readonly (Tree | TreeBuffer)[],
563
+ /**
564
+ The positions (offsets relative to the start of this tree) of
565
+ the children.
566
+ */
567
+ positions: readonly number[],
568
+ /**
569
+ The total length of this tree
570
+ */
571
+ length: number,
572
+ /**
573
+ Per-node [node props](#common.NodeProp) to associate with this node.
574
+ */
575
+ props?: readonly [NodeProp<any> | number, any][]);
576
+ /**
577
+ The empty tree
578
+ */
579
+ static empty: Tree;
580
+ /**
581
+ Get a [tree cursor](#common.TreeCursor) positioned at the top of
582
+ the tree. Mode can be used to [control](#common.IterMode) which
583
+ nodes the cursor visits.
584
+ */
585
+ cursor(mode?: IterMode): TreeCursor;
586
+ /**
587
+ Get a [tree cursor](#common.TreeCursor) pointing into this tree
588
+ at the given position and side (see
589
+ [`moveTo`](#common.TreeCursor.moveTo).
590
+ */
591
+ cursorAt(pos: number, side?: -1 | 0 | 1, mode?: IterMode): TreeCursor;
592
+ /**
593
+ Get a [syntax node](#common.SyntaxNode) object for the top of the
594
+ tree.
595
+ */
596
+ get topNode(): SyntaxNode;
597
+ /**
598
+ Get the [syntax node](#common.SyntaxNode) at the given position.
599
+ If `side` is -1, this will move into nodes that end at the
600
+ position. If 1, it'll move into nodes that start at the
601
+ position. With 0, it'll only enter nodes that cover the position
602
+ from both sides.
603
+
604
+ Note that this will not enter
605
+ [overlays](#common.MountedTree.overlay), and you often want
606
+ [`resolveInner`](#common.Tree.resolveInner) instead.
607
+ */
608
+ resolve(pos: number, side?: -1 | 0 | 1): SyntaxNode;
609
+ /**
610
+ Like [`resolve`](#common.Tree.resolve), but will enter
611
+ [overlaid](#common.MountedTree.overlay) nodes, producing a syntax node
612
+ pointing into the innermost overlaid tree at the given position
613
+ (with parent links going through all parent structure, including
614
+ the host trees).
615
+ */
616
+ resolveInner(pos: number, side?: -1 | 0 | 1): SyntaxNode;
617
+ /**
618
+ Iterate over the tree and its children, calling `enter` for any
619
+ node that touches the `from`/`to` region (if given) before
620
+ running over such a node's children, and `leave` (if given) when
621
+ leaving the node. When `enter` returns `false`, that node will
622
+ not have its children iterated over (or `leave` called).
623
+ */
624
+ iterate(spec: {
625
+ enter(node: SyntaxNodeRef): boolean | void;
626
+ leave?(node: SyntaxNodeRef): void;
627
+ from?: number;
628
+ to?: number;
629
+ mode?: IterMode;
630
+ }): void;
631
+ /**
632
+ Get the value of the given [node prop](#common.NodeProp) for this
633
+ node. Works with both per-node and per-type props.
634
+ */
635
+ prop<T>(prop: NodeProp<T>): T | undefined;
636
+ /**
637
+ Returns the node's [per-node props](#common.NodeProp.perNode) in a
638
+ format that can be passed to the [`Tree`](#common.Tree)
639
+ constructor.
640
+ */
641
+ get propValues(): readonly [NodeProp<any> | number, any][];
642
+ /**
643
+ Balance the direct children of this tree, producing a copy of
644
+ which may have children grouped into subtrees with type
645
+ [`NodeType.none`](#common.NodeType^none).
646
+ */
647
+ balance(config?: {
648
+ /**
649
+ Function to create the newly balanced subtrees.
650
+ */
651
+ makeTree?: (children: readonly (Tree | TreeBuffer)[], positions: readonly number[], length: number) => Tree;
652
+ }): Tree;
653
+ /**
654
+ Build a tree from a postfix-ordered buffer of node information,
655
+ or a cursor over such a buffer.
656
+ */
657
+ static build(data: BuildData): Tree;
658
+ }
659
+ type BuildData = {
660
+ /**
661
+ The buffer or buffer cursor to read the node data from.
662
+
663
+ When this is an array, it should contain four values for every
664
+ node in the tree.
665
+
666
+ - The first holds the node's type, as a node ID pointing into
667
+ the given `NodeSet`.
668
+ - The second holds the node's start offset.
669
+ - The third the end offset.
670
+ - The fourth the amount of space taken up in the array by this
671
+ node and its children. Since there's four values per node,
672
+ this is the total number of nodes inside this node (children
673
+ and transitive children) plus one for the node itself, times
674
+ four.
675
+
676
+ Parent nodes should appear _after_ child nodes in the array. As
677
+ an example, a node of type 10 spanning positions 0 to 4, with
678
+ two children, of type 11 and 12, might look like this:
679
+
680
+ [11, 0, 1, 4, 12, 2, 4, 4, 10, 0, 4, 12]
681
+ */
682
+ buffer: BufferCursor | readonly number[];
683
+ /**
684
+ The node types to use.
685
+ */
686
+ nodeSet: NodeSet;
687
+ /**
688
+ The id of the top node type.
689
+ */
690
+ topID: number;
691
+ /**
692
+ The position the tree should start at. Defaults to 0.
693
+ */
694
+ start?: number;
695
+ /**
696
+ The position in the buffer where the function should stop
697
+ reading. Defaults to 0.
698
+ */
699
+ bufferStart?: number;
700
+ /**
701
+ The length of the wrapping node. The end offset of the last
702
+ child is used when not provided.
703
+ */
704
+ length?: number;
705
+ /**
706
+ The maximum buffer length to use. Defaults to
707
+ [`DefaultBufferLength`](#common.DefaultBufferLength).
708
+ */
709
+ maxBufferLength?: number;
710
+ /**
711
+ An optional array holding reused nodes that the buffer can refer
712
+ to.
713
+ */
714
+ reused?: readonly Tree[];
715
+ /**
716
+ The first node type that indicates repeat constructs in this
717
+ grammar.
718
+ */
719
+ minRepeatType?: number;
720
+ };
721
+ /**
722
+ This is used by `Tree.build` as an abstraction for iterating over
723
+ a tree buffer. A cursor initially points at the very last element
724
+ in the buffer. Every time `next()` is called it moves on to the
725
+ previous one.
726
+ */
727
+ interface BufferCursor {
728
+ /**
729
+ The current buffer position (four times the number of nodes
730
+ remaining).
731
+ */
732
+ pos: number;
733
+ /**
734
+ The node ID of the next node in the buffer.
735
+ */
736
+ id: number;
737
+ /**
738
+ The start position of the next node in the buffer.
739
+ */
740
+ start: number;
741
+ /**
742
+ The end position of the next node.
743
+ */
744
+ end: number;
745
+ /**
746
+ The size of the next node (the number of nodes inside, counting
747
+ the node itself, times 4).
748
+ */
749
+ size: number;
750
+ /**
751
+ Moves `this.pos` down by 4.
752
+ */
753
+ next(): void;
754
+ /**
755
+ Create a copy of this cursor.
756
+ */
757
+ fork(): BufferCursor;
758
+ }
759
+ /**
760
+ Tree buffers contain (type, start, end, endIndex) quads for each
761
+ node. In such a buffer, nodes are stored in prefix order (parents
762
+ before children, with the endIndex of the parent indicating which
763
+ children belong to it).
764
+ */
765
+ declare class TreeBuffer {
766
+ /**
767
+ The buffer's content.
768
+ */
769
+ readonly buffer: Uint16Array;
770
+ /**
771
+ The total length of the group of nodes in the buffer.
772
+ */
773
+ readonly length: number;
774
+ /**
775
+ The node set used in this buffer.
776
+ */
777
+ readonly set: NodeSet;
778
+ /**
779
+ Create a tree buffer.
780
+ */
781
+ constructor(
782
+ /**
783
+ The buffer's content.
784
+ */
785
+ buffer: Uint16Array,
786
+ /**
787
+ The total length of the group of nodes in the buffer.
788
+ */
789
+ length: number,
790
+ /**
791
+ The node set used in this buffer.
792
+ */
793
+ set: NodeSet);
794
+ }
795
+ /**
796
+ The set of properties provided by both [`SyntaxNode`](#common.SyntaxNode)
797
+ and [`TreeCursor`](#common.TreeCursor). Note that, if you need
798
+ an object that is guaranteed to stay stable in the future, you
799
+ need to use the [`node`](#common.SyntaxNodeRef.node) accessor.
800
+ */
801
+ interface SyntaxNodeRef {
802
+ /**
803
+ The start position of the node.
804
+ */
805
+ readonly from: number;
806
+ /**
807
+ The end position of the node.
808
+ */
809
+ readonly to: number;
810
+ /**
811
+ The type of the node.
812
+ */
813
+ readonly type: NodeType;
814
+ /**
815
+ The name of the node (`.type.name`).
816
+ */
817
+ readonly name: string;
818
+ /**
819
+ Get the [tree](#common.Tree) that represents the current node,
820
+ if any. Will return null when the node is in a [tree
821
+ buffer](#common.TreeBuffer).
822
+ */
823
+ readonly tree: Tree | null;
824
+ /**
825
+ Retrieve a stable [syntax node](#common.SyntaxNode) at this
826
+ position.
827
+ */
828
+ readonly node: SyntaxNode;
829
+ /**
830
+ Test whether the node matches a given context.
831
+ */
832
+ matchContext(context: readonly string[]): boolean;
833
+ }
834
+ /**
835
+ A syntax node provides an immutable pointer to a given node in a
836
+ tree. When iterating over large amounts of nodes, you may want to
837
+ use a mutable [cursor](#common.TreeCursor) instead, which is more
838
+ efficient.
839
+ */
840
+ interface SyntaxNode extends SyntaxNodeRef {
841
+ /**
842
+ The node's parent node, if any.
843
+ */
844
+ parent: SyntaxNode | null;
845
+ /**
846
+ The first child, if the node has children.
847
+ */
848
+ firstChild: SyntaxNode | null;
849
+ /**
850
+ The node's last child, if available.
851
+ */
852
+ lastChild: SyntaxNode | null;
853
+ /**
854
+ The first child that ends after `pos`.
855
+ */
856
+ childAfter(pos: number): SyntaxNode | null;
857
+ /**
858
+ The last child that starts before `pos`.
859
+ */
860
+ childBefore(pos: number): SyntaxNode | null;
861
+ /**
862
+ Enter the child at the given position. If side is -1 the child
863
+ may end at that position, when 1 it may start there.
864
+
865
+ This will by default enter
866
+ [overlaid](#common.MountedTree.overlay)
867
+ [mounted](#common.NodeProp^mounted) trees. You can set
868
+ `overlays` to false to disable that.
869
+
870
+ Similarly, when `buffers` is false this will not enter
871
+ [buffers](#common.TreeBuffer), only [nodes](#common.Tree) (which
872
+ is mostly useful when looking for props, which cannot exist on
873
+ buffer-allocated nodes).
874
+ */
875
+ enter(pos: number, side: -1 | 0 | 1, mode?: IterMode): SyntaxNode | null;
876
+ /**
877
+ This node's next sibling, if any.
878
+ */
879
+ nextSibling: SyntaxNode | null;
880
+ /**
881
+ This node's previous sibling.
882
+ */
883
+ prevSibling: SyntaxNode | null;
884
+ /**
885
+ A [tree cursor](#common.TreeCursor) starting at this node.
886
+ */
887
+ cursor(mode?: IterMode): TreeCursor;
888
+ /**
889
+ Find the node around, before (if `side` is -1), or after (`side`
890
+ is 1) the given position. Will look in parent nodes if the
891
+ position is outside this node.
892
+ */
893
+ resolve(pos: number, side?: -1 | 0 | 1): SyntaxNode;
894
+ /**
895
+ Similar to `resolve`, but enter
896
+ [overlaid](#common.MountedTree.overlay) nodes.
897
+ */
898
+ resolveInner(pos: number, side?: -1 | 0 | 1): SyntaxNode;
899
+ /**
900
+ Move the position to the innermost node before `pos` that looks
901
+ like it is unfinished (meaning it ends in an error node or has a
902
+ child ending in an error node right at its end).
903
+ */
904
+ enterUnfinishedNodesBefore(pos: number): SyntaxNode;
905
+ /**
906
+ Get a [tree](#common.Tree) for this node. Will allocate one if it
907
+ points into a buffer.
908
+ */
909
+ toTree(): Tree;
910
+ /**
911
+ Get the first child of the given type (which may be a [node
912
+ name](#common.NodeType.name) or a [group
913
+ name](#common.NodeProp^group)). If `before` is non-null, only
914
+ return children that occur somewhere after a node with that name
915
+ or group. If `after` is non-null, only return children that
916
+ occur somewhere before a node with that name or group.
917
+ */
918
+ getChild(type: string | number, before?: string | number | null, after?: string | number | null): SyntaxNode | null;
919
+ /**
920
+ Like [`getChild`](#common.SyntaxNode.getChild), but return all
921
+ matching children, not just the first.
922
+ */
923
+ getChildren(type: string | number, before?: string | number | null, after?: string | number | null): SyntaxNode[];
924
+ /**
925
+ Test whether the node matches a given context—a sequence of
926
+ direct parent nodes. Empty strings in the context array act as
927
+ wildcards, other strings must match the ancestor node's name.
928
+ */
929
+ matchContext(context: readonly string[]): boolean;
930
+ }
931
+ /**
932
+ A tree cursor object focuses on a given node in a syntax tree, and
933
+ allows you to move to adjacent nodes.
934
+ */
935
+ declare class TreeCursor implements SyntaxNodeRef {
936
+ /**
937
+ The node's type.
938
+ */
939
+ type: NodeType;
940
+ /**
941
+ Shorthand for `.type.name`.
942
+ */
943
+ get name(): string;
944
+ /**
945
+ The start source offset of this node.
946
+ */
947
+ from: number;
948
+ /**
949
+ The end source offset.
950
+ */
951
+ to: number;
952
+ private stack;
953
+ private bufferNode;
954
+ private yieldNode;
955
+ private yieldBuf;
956
+ private yield;
957
+ /**
958
+ Move the cursor to this node's first child. When this returns
959
+ false, the node has no child, and the cursor has not been moved.
960
+ */
961
+ firstChild(): boolean;
962
+ /**
963
+ Move the cursor to this node's last child.
964
+ */
965
+ lastChild(): boolean;
966
+ /**
967
+ Move the cursor to the first child that ends after `pos`.
968
+ */
969
+ childAfter(pos: number): boolean;
970
+ /**
971
+ Move to the last child that starts before `pos`.
972
+ */
973
+ childBefore(pos: number): boolean;
974
+ /**
975
+ Move the cursor to the child around `pos`. If side is -1 the
976
+ child may end at that position, when 1 it may start there. This
977
+ will also enter [overlaid](#common.MountedTree.overlay)
978
+ [mounted](#common.NodeProp^mounted) trees unless `overlays` is
979
+ set to false.
980
+ */
981
+ enter(pos: number, side: -1 | 0 | 1, mode?: IterMode): boolean;
982
+ /**
983
+ Move to the node's parent node, if this isn't the top node.
984
+ */
985
+ parent(): boolean;
986
+ /**
987
+ Move to this node's next sibling, if any.
988
+ */
989
+ nextSibling(): boolean;
990
+ /**
991
+ Move to this node's previous sibling, if any.
992
+ */
993
+ prevSibling(): boolean;
994
+ private atLastNode;
995
+ private move;
996
+ /**
997
+ Move to the next node in a
998
+ [pre-order](https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR)
999
+ traversal, going from a node to its first child or, if the
1000
+ current node is empty or `enter` is false, its next sibling or
1001
+ the next sibling of the first parent node that has one.
1002
+ */
1003
+ next(enter?: boolean): boolean;
1004
+ /**
1005
+ Move to the next node in a last-to-first pre-order traveral. A
1006
+ node is followed by its last child or, if it has none, its
1007
+ previous sibling or the previous sibling of the first parent
1008
+ node that has one.
1009
+ */
1010
+ prev(enter?: boolean): boolean;
1011
+ /**
1012
+ Move the cursor to the innermost node that covers `pos`. If
1013
+ `side` is -1, it will enter nodes that end at `pos`. If it is 1,
1014
+ it will enter nodes that start at `pos`.
1015
+ */
1016
+ moveTo(pos: number, side?: -1 | 0 | 1): this;
1017
+ /**
1018
+ Get a [syntax node](#common.SyntaxNode) at the cursor's current
1019
+ position.
1020
+ */
1021
+ get node(): SyntaxNode;
1022
+ /**
1023
+ Get the [tree](#common.Tree) that represents the current node, if
1024
+ any. Will return null when the node is in a [tree
1025
+ buffer](#common.TreeBuffer).
1026
+ */
1027
+ get tree(): Tree | null;
1028
+ /**
1029
+ Iterate over the current node and all its descendants, calling
1030
+ `enter` when entering a node and `leave`, if given, when leaving
1031
+ one. When `enter` returns `false`, any children of that node are
1032
+ skipped, and `leave` isn't called for it.
1033
+ */
1034
+ iterate(enter: (node: SyntaxNodeRef) => boolean | void, leave?: (node: SyntaxNodeRef) => void): void;
1035
+ /**
1036
+ Test whether the current node matches a given context—a sequence
1037
+ of direct parent node names. Empty strings in the context array
1038
+ are treated as wildcards.
1039
+ */
1040
+ matchContext(context: readonly string[]): boolean;
1041
+ }
1042
+ /**
1043
+ Provides a way to associate values with pieces of trees. As long
1044
+ as that part of the tree is reused, the associated values can be
1045
+ retrieved from an updated tree.
1046
+ */
1047
+ declare class NodeWeakMap<T> {
1048
+ private map;
1049
+ private setBuffer;
1050
+ private getBuffer;
1051
+ /**
1052
+ Set the value for this syntax node.
1053
+ */
1054
+ set(node: SyntaxNode, value: T): void;
1055
+ /**
1056
+ Retrieve value for this syntax node, if it exists in the map.
1057
+ */
1058
+ get(node: SyntaxNode): T | undefined;
1059
+ /**
1060
+ Set the value for the node that a cursor currently points to.
1061
+ */
1062
+ cursorSet(cursor: TreeCursor, value: T): void;
1063
+ /**
1064
+ Retrieve the value for the node that a cursor currently points
1065
+ to.
1066
+ */
1067
+ cursorGet(cursor: TreeCursor): T | undefined;
1068
+ }
1069
+
1070
+ /**
1071
+ Objects returned by the function passed to
1072
+ [`parseMixed`](#common.parseMixed) should conform to this
1073
+ interface.
1074
+ */
1075
+ interface NestedParse {
1076
+ /**
1077
+ The parser to use for the inner region.
1078
+ */
1079
+ parser: Parser;
1080
+ /**
1081
+ When this property is not given, the entire node is parsed with
1082
+ this parser, and it is [mounted](#common.NodeProp^mounted) as a
1083
+ non-overlay node, replacing its host node in tree iteration.
1084
+
1085
+ When an array of ranges is given, only those ranges are parsed,
1086
+ and the tree is mounted as an
1087
+ [overlay](#common.MountedTree.overlay).
1088
+
1089
+ When a function is given, that function will be called for
1090
+ descendant nodes of the target node, not including child nodes
1091
+ that are covered by another nested parse, to determine the
1092
+ overlay ranges. When it returns true, the entire descendant is
1093
+ included, otherwise just the range given. The mixed parser will
1094
+ optimize range-finding in reused nodes, which means it's a good
1095
+ idea to use a function here when the target node is expected to
1096
+ have a large, deep structure.
1097
+ */
1098
+ overlay?: readonly {
1099
+ from: number;
1100
+ to: number;
1101
+ }[] | ((node: SyntaxNodeRef) => {
1102
+ from: number;
1103
+ to: number;
1104
+ } | boolean);
1105
+ }
1106
+ /**
1107
+ Create a parse wrapper that, after the inner parse completes,
1108
+ scans its tree for mixed language regions with the `nest`
1109
+ function, runs the resulting [inner parses](#common.NestedParse),
1110
+ and then [mounts](#common.NodeProp^mounted) their results onto the
1111
+ tree.
1112
+ */
1113
+ declare function parseMixed(nest: (node: SyntaxNodeRef, input: Input) => NestedParse | null): ParseWrapper;
1114
+
1115
+ export { BufferCursor, ChangedRange, DefaultBufferLength, Input, IterMode, MountedTree, NestedParse, NodeProp, NodePropSource, NodeSet, NodeType, NodeWeakMap, ParseWrapper, Parser, PartialParse, SyntaxNode, SyntaxNodeRef, Tree, TreeBuffer, TreeCursor, TreeFragment, parseMixed };