@codegraft/core 0.1.0-beta.11 → 0.1.0-beta.12

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.
@@ -0,0 +1,1021 @@
1
+ /**
2
+ * A position in a multi-line text document, in terms of rows and columns.
3
+ *
4
+ * Rows and columns are zero-based.
5
+ */
6
+ export interface Point {
7
+ /** The zero-based row number. */
8
+ row: number;
9
+ /** The zero-based column number. */
10
+ column: number;
11
+ }
12
+ /**
13
+ * A range of positions in a multi-line text document, both in terms of bytes
14
+ * and of rows and columns.
15
+ */
16
+ export interface Range {
17
+ /** The start position of the range. */
18
+ startPosition: Point;
19
+ /** The end position of the range. */
20
+ endPosition: Point;
21
+ /** The start index of the range. */
22
+ startIndex: number;
23
+ /** The end index of the range. */
24
+ endIndex: number;
25
+ }
26
+ /**
27
+ * A callback for parsing that takes an index and point, and should return a string.
28
+ */
29
+ export type ParseCallback = (index: number, position: Point) => string | undefined;
30
+ /**
31
+ * A callback that receives the parse state during parsing.
32
+ */
33
+ export type ProgressCallback = (progress: ParseState) => boolean;
34
+ /**
35
+ * A callback for logging messages.
36
+ *
37
+ * If `isLex` is `true`, the message is from the lexer, otherwise it's from the parser.
38
+ */
39
+ export type LogCallback = (message: string, isLex: boolean) => void;
40
+ export class Edit {
41
+ /** The start position of the change. */
42
+ startPosition: Point;
43
+ /** The end position of the change before the edit. */
44
+ oldEndPosition: Point;
45
+ /** The end position of the change after the edit. */
46
+ newEndPosition: Point;
47
+ /** The start index of the change. */
48
+ startIndex: number;
49
+ /** The end index of the change before the edit. */
50
+ oldEndIndex: number;
51
+ /** The end index of the change after the edit. */
52
+ newEndIndex: number;
53
+ constructor({ startIndex, oldEndIndex, newEndIndex, startPosition, oldEndPosition, newEndPosition, }: {
54
+ startIndex: number;
55
+ oldEndIndex: number;
56
+ newEndIndex: number;
57
+ startPosition: Point;
58
+ oldEndPosition: Point;
59
+ newEndPosition: Point;
60
+ });
61
+ /**
62
+ * Edit a point and index to keep it in-sync with source code that has been edited.
63
+ *
64
+ * This function updates a single point's byte offset and row/column position
65
+ * based on an edit operation. This is useful for editing points without
66
+ * requiring a tree or node instance.
67
+ */
68
+ editPoint(point: Point, index: number): {
69
+ point: Point;
70
+ index: number;
71
+ };
72
+ /**
73
+ * Edit a range to keep it in-sync with source code that has been edited.
74
+ *
75
+ * This function updates a range's start and end positions based on an edit
76
+ * operation. This is useful for editing ranges without requiring a tree
77
+ * or node instance.
78
+ */
79
+ editRange(range: Range): Range;
80
+ }
81
+ /**
82
+ * Options for parsing
83
+ *
84
+ * The `includedRanges` property is an array of {@link Range} objects that
85
+ * represent the ranges of text that the parser should include when parsing.
86
+ *
87
+ * The `progressCallback` property is a function that is called periodically
88
+ * during parsing to check whether parsing should be cancelled.
89
+ *
90
+ * See {@link Parser#parse} for more information.
91
+ */
92
+ export interface ParseOptions {
93
+ /**
94
+ * An array of {@link Range} objects that
95
+ * represent the ranges of text that the parser should include when parsing.
96
+ *
97
+ * This sets the ranges of text that the parser should include when parsing.
98
+ * By default, the parser will always include entire documents. This
99
+ * function allows you to parse only a *portion* of a document but
100
+ * still return a syntax tree whose ranges match up with the document
101
+ * as a whole. You can also pass multiple disjoint ranges.
102
+ * If `ranges` is empty, then the entire document will be parsed.
103
+ * Otherwise, the given ranges must be ordered from earliest to latest
104
+ * in the document, and they must not overlap. That is, the following
105
+ * must hold for all `i` < `length - 1`:
106
+ * ```text
107
+ * ranges[i].end_byte <= ranges[i + 1].start_byte
108
+ * ```
109
+ */
110
+ includedRanges?: Range[];
111
+ /**
112
+ * A function that is called periodically during parsing to check
113
+ * whether parsing should be cancelled. If the progress callback returns
114
+ * `true`, then parsing will be cancelled. You can also use this to instrument
115
+ * parsing and check where the parser is at in the document. The progress callback
116
+ * takes a single argument, which is a {@link ParseState} representing the current
117
+ * state of the parser.
118
+ */
119
+ progressCallback?: (state: ParseState) => void;
120
+ }
121
+ /**
122
+ * A stateful object that is passed into the progress callback {@link ParseOptions#progressCallback}
123
+ * to provide the current state of the parser.
124
+ */
125
+ export interface ParseState {
126
+ /** The byte offset in the document that the parser is at. */
127
+ currentOffset: number;
128
+ /** Indicates whether the parser has encountered an error during parsing. */
129
+ hasError: boolean;
130
+ }
131
+ /**
132
+ * The latest ABI version that is supported by the current version of the
133
+ * library.
134
+ *
135
+ * When Languages are generated by the Tree-sitter CLI, they are
136
+ * assigned an ABI version number that corresponds to the current CLI version.
137
+ * The Tree-sitter library is generally backwards-compatible with languages
138
+ * generated using older CLI versions, but is not forwards-compatible.
139
+ */
140
+ export let LANGUAGE_VERSION: number;
141
+ /**
142
+ * The earliest ABI version that is supported by the current version of the
143
+ * library.
144
+ */
145
+ export let MIN_COMPATIBLE_VERSION: number;
146
+ /**
147
+ * A stateful object that is used to produce a {@link Tree} based on some
148
+ * source code.
149
+ */
150
+ export class Parser {
151
+ /** The parser's current language. */
152
+ language: Language | null;
153
+ /**
154
+ * This must always be called before creating a Parser.
155
+ *
156
+ * You can optionally pass in options to configure the Wasm module, the most common
157
+ * one being `locateFile` to help the module find the `.wasm` file.
158
+ */
159
+ static init(moduleOptions?: Partial<EmscriptenModule>): Promise<void>;
160
+ /**
161
+ * Create a new parser.
162
+ */
163
+ constructor();
164
+ /** Delete the parser, freeing its resources. */
165
+ delete(): void;
166
+ /**
167
+ * Set the language that the parser should use for parsing.
168
+ *
169
+ * If the language was not successfully assigned, an error will be thrown.
170
+ * This happens if the language was generated with an incompatible
171
+ * version of the Tree-sitter CLI. Check the language's version using
172
+ * {@link Language#version} and compare it to this library's
173
+ * {@link LANGUAGE_VERSION} and {@link MIN_COMPATIBLE_VERSION} constants.
174
+ */
175
+ setLanguage(language: Language | null): this;
176
+ /**
177
+ * Parse a slice of UTF8 text.
178
+ *
179
+ * @param callback - The UTF8-encoded text to parse or a callback function.
180
+ *
181
+ * @param oldTree - A previous syntax tree parsed from the same document. If the text of the
182
+ * document has changed since `oldTree` was created, then you must edit `oldTree` to match
183
+ * the new text using {@link Tree#edit}.
184
+ *
185
+ * @param options - Options for parsing the text.
186
+ * This can be used to set the included ranges, or a progress callback.
187
+ *
188
+ * @returns A {@link Tree} if parsing succeeded, or `null` if:
189
+ * - The parser has not yet had a language assigned with {@link Parser#setLanguage}.
190
+ * - The progress callback returned true.
191
+ */
192
+ parse(callback: string | ParseCallback, oldTree?: Tree | null, options?: ParseOptions): Tree | null;
193
+ /**
194
+ * Instruct the parser to start the next parse from the beginning.
195
+ *
196
+ * If the parser previously failed because of a callback,
197
+ * then by default, it will resume where it left off on the
198
+ * next call to {@link Parser#parse} or other parsing functions.
199
+ * If you don't want to resume, and instead intend to use this parser to
200
+ * parse some other document, you must call `reset` first.
201
+ */
202
+ reset(): void;
203
+ /** Get the ranges of text that the parser will include when parsing. */
204
+ getIncludedRanges(): Range[];
205
+ /** Set the logging callback that a parser should use during parsing. */
206
+ setLogger(callback: LogCallback | boolean | null): this;
207
+ /** Get the parser's current logger. */
208
+ getLogger(): LogCallback | null;
209
+ }
210
+ interface LanguageMetadata {
211
+ readonly major_version: number;
212
+ readonly minor_version: number;
213
+ readonly patch_version: number;
214
+ }
215
+ /**
216
+ * An opaque object that defines how to parse a particular language.
217
+ * The code for each `Language` is generated by the Tree-sitter CLI.
218
+ */
219
+ export class Language {
220
+ /**
221
+ * A list of all node types in the language. The index of each type in this
222
+ * array is its node type id.
223
+ */
224
+ types: string[];
225
+ /**
226
+ * A list of all field names in the language. The index of each field name in
227
+ * this array is its field id.
228
+ */
229
+ fields: (string | null)[];
230
+ /**
231
+ * Gets the name of the language.
232
+ */
233
+ get name(): string | null;
234
+ /**
235
+ * Gets the ABI version of the language.
236
+ */
237
+ get abiVersion(): number;
238
+ /**
239
+ * Get the metadata for this language. This information is generated by the
240
+ * CLI, and relies on the language author providing the correct metadata in
241
+ * the language's `tree-sitter.json` file.
242
+ */
243
+ get metadata(): LanguageMetadata | null;
244
+ /**
245
+ * Gets the number of fields in the language.
246
+ */
247
+ get fieldCount(): number;
248
+ /**
249
+ * Gets the number of states in the language.
250
+ */
251
+ get stateCount(): number;
252
+ /**
253
+ * Get the field id for a field name.
254
+ */
255
+ fieldIdForName(fieldName: string): number | null;
256
+ /**
257
+ * Get the field name for a field id.
258
+ */
259
+ fieldNameForId(fieldId: number): string | null;
260
+ /**
261
+ * Get the node type id for a node type name.
262
+ */
263
+ idForNodeType(type: string, named: boolean): number | null;
264
+ /**
265
+ * Gets the number of node types in the language.
266
+ */
267
+ get nodeTypeCount(): number;
268
+ /**
269
+ * Get the node type name for a node type id.
270
+ */
271
+ nodeTypeForId(typeId: number): string | null;
272
+ /**
273
+ * Check if a node type is named.
274
+ *
275
+ * @see {@link https://tree-sitter.github.io/tree-sitter/using-parsers/2-basic-parsing.html#named-vs-anonymous-nodes}
276
+ */
277
+ nodeTypeIsNamed(typeId: number): boolean;
278
+ /**
279
+ * Check if a node type is visible.
280
+ */
281
+ nodeTypeIsVisible(typeId: number): boolean;
282
+ /**
283
+ * Get the supertypes ids of this language.
284
+ *
285
+ * @see {@link https://tree-sitter.github.io/tree-sitter/using-parsers/6-static-node-types.html?highlight=supertype#supertype-nodes}
286
+ */
287
+ get supertypes(): number[];
288
+ /**
289
+ * Get the subtype ids for a given supertype node id.
290
+ */
291
+ subtypes(supertype: number): number[];
292
+ /**
293
+ * Get the next state id for a given state id and node type id.
294
+ */
295
+ nextState(stateId: number, typeId: number): number;
296
+ /**
297
+ * Create a new lookahead iterator for this language and parse state.
298
+ *
299
+ * This returns `null` if state is invalid for this language.
300
+ *
301
+ * Iterating {@link LookaheadIterator} will yield valid symbols in the given
302
+ * parse state. Newly created lookahead iterators will return the `ERROR`
303
+ * symbol from {@link LookaheadIterator#currentType}.
304
+ *
305
+ * Lookahead iterators can be useful for generating suggestions and improving
306
+ * syntax error diagnostics. To get symbols valid in an `ERROR` node, use the
307
+ * lookahead iterator on its first leaf node state. For `MISSING` nodes, a
308
+ * lookahead iterator created on the previous non-extra leaf node may be
309
+ * appropriate.
310
+ */
311
+ lookaheadIterator(stateId: number): LookaheadIterator | null;
312
+ /**
313
+ * Load a language from a WebAssembly module.
314
+ * The module can be provided as a path to a file or as a buffer.
315
+ */
316
+ static load(input: string | Uint8Array): Promise<Language>;
317
+ }
318
+ /** A tree that represents the syntactic structure of a source code file. */
319
+ export class Tree {
320
+ /** The language that was used to parse the syntax tree. */
321
+ language: Language;
322
+ /** Create a shallow copy of the syntax tree. This is very fast. */
323
+ copy(): Tree;
324
+ /** Delete the syntax tree, freeing its resources. */
325
+ delete(): void;
326
+ /** Get the root node of the syntax tree. */
327
+ get rootNode(): Node;
328
+ /**
329
+ * Get the root node of the syntax tree, but with its position shifted
330
+ * forward by the given offset.
331
+ */
332
+ rootNodeWithOffset(offsetBytes: number, offsetExtent: Point): Node;
333
+ /**
334
+ * Edit the syntax tree to keep it in sync with source code that has been
335
+ * edited.
336
+ *
337
+ * You must describe the edit both in terms of byte offsets and in terms of
338
+ * row/column coordinates.
339
+ */
340
+ edit(edit: Edit): void;
341
+ /** Create a new {@link TreeCursor} starting from the root of the tree. */
342
+ walk(): TreeCursor;
343
+ /**
344
+ * Compare this old edited syntax tree to a new syntax tree representing
345
+ * the same document, returning a sequence of ranges whose syntactic
346
+ * structure has changed.
347
+ *
348
+ * For this to work correctly, this syntax tree must have been edited such
349
+ * that its ranges match up to the new tree. Generally, you'll want to
350
+ * call this method right after calling one of the [`Parser::parse`]
351
+ * functions. Call it on the old tree that was passed to parse, and
352
+ * pass the new tree that was returned from `parse`.
353
+ */
354
+ getChangedRanges(other: Tree): Range[];
355
+ /** Get the included ranges that were used to parse the syntax tree. */
356
+ getIncludedRanges(): Range[];
357
+ }
358
+ /** A single node within a syntax {@link Tree}. */
359
+ export class Node {
360
+ /**
361
+ * The numeric id for this node that is unique.
362
+ *
363
+ * Within a given syntax tree, no two nodes have the same id. However:
364
+ *
365
+ * * If a new tree is created based on an older tree, and a node from the old tree is reused in
366
+ * the process, then that node will have the same id in both trees.
367
+ *
368
+ * * A node not marked as having changes does not guarantee it was reused.
369
+ *
370
+ * * If a node is marked as having changed in the old tree, it will not be reused.
371
+ */
372
+ id: number;
373
+ /** The byte index where this node starts. */
374
+ startIndex: number;
375
+ /** The position where this node starts. */
376
+ startPosition: Point;
377
+ /** The tree that this node belongs to. */
378
+ tree: Tree;
379
+ /** Get this node's type as a numerical id. */
380
+ get typeId(): number;
381
+ /**
382
+ * Get the node's type as a numerical id as it appears in the grammar,
383
+ * ignoring aliases.
384
+ */
385
+ get grammarId(): number;
386
+ /** Get this node's type as a string. */
387
+ get type(): string;
388
+ /**
389
+ * Get this node's symbol name as it appears in the grammar, ignoring
390
+ * aliases as a string.
391
+ */
392
+ get grammarType(): string;
393
+ /**
394
+ * Check if this node is *named*.
395
+ *
396
+ * Named nodes correspond to named rules in the grammar, whereas
397
+ * *anonymous* nodes correspond to string literals in the grammar.
398
+ */
399
+ get isNamed(): boolean;
400
+ /**
401
+ * Check if this node is *extra*.
402
+ *
403
+ * Extra nodes represent things like comments, which are not required
404
+ * by the grammar, but can appear anywhere.
405
+ */
406
+ get isExtra(): boolean;
407
+ /**
408
+ * Check if this node represents a syntax error.
409
+ *
410
+ * Syntax errors represent parts of the code that could not be incorporated
411
+ * into a valid syntax tree.
412
+ */
413
+ get isError(): boolean;
414
+ /**
415
+ * Check if this node is *missing*.
416
+ *
417
+ * Missing nodes are inserted by the parser in order to recover from
418
+ * certain kinds of syntax errors.
419
+ */
420
+ get isMissing(): boolean;
421
+ /** Check if this node has been edited. */
422
+ get hasChanges(): boolean;
423
+ /**
424
+ * Check if this node represents a syntax error or contains any syntax
425
+ * errors anywhere within it.
426
+ */
427
+ get hasError(): boolean;
428
+ /** Get the byte index where this node ends. */
429
+ get endIndex(): number;
430
+ /** Get the position where this node ends. */
431
+ get endPosition(): Point;
432
+ /** Get the string content of this node. */
433
+ get text(): string;
434
+ /** Get this node's parse state. */
435
+ get parseState(): number;
436
+ /** Get the parse state after this node. */
437
+ get nextParseState(): number;
438
+ /** Check if this node is equal to another node. */
439
+ equals(other: Node): boolean;
440
+ /**
441
+ * Get the node's child at the given index, where zero represents the first child.
442
+ *
443
+ * This method is fairly fast, but its cost is technically log(n), so if
444
+ * you might be iterating over a long list of children, you should use
445
+ * {@link Node#children} instead.
446
+ */
447
+ child(index: number): Node | null;
448
+ /**
449
+ * Get this node's *named* child at the given index.
450
+ *
451
+ * See also {@link Node#isNamed}.
452
+ * This method is fairly fast, but its cost is technically log(n), so if
453
+ * you might be iterating over a long list of children, you should use
454
+ * {@link Node#namedChildren} instead.
455
+ */
456
+ namedChild(index: number): Node | null;
457
+ /**
458
+ * Get this node's child with the given numerical field id.
459
+ *
460
+ * See also {@link Node#childForFieldName}. You can
461
+ * convert a field name to an id using {@link Language#fieldIdForName}.
462
+ */
463
+ childForFieldId(fieldId: number): Node | null;
464
+ /**
465
+ * Get the first child with the given field name.
466
+ *
467
+ * If multiple children may have the same field name, access them using
468
+ * {@link Node#childrenForFieldName}.
469
+ */
470
+ childForFieldName(fieldName: string): Node | null;
471
+ /** Get the field name of this node's child at the given index. */
472
+ fieldNameForChild(index: number): string | null;
473
+ /** Get the field name of this node's named child at the given index. */
474
+ fieldNameForNamedChild(index: number): string | null;
475
+ /**
476
+ * Get an array of this node's children with a given field name.
477
+ *
478
+ * See also {@link Node#children}.
479
+ */
480
+ childrenForFieldName(fieldName: string): Node[];
481
+ /**
482
+ * Get an array of this node's children with a given field id.
483
+ *
484
+ * See also {@link Node#childrenForFieldName}.
485
+ */
486
+ childrenForFieldId(fieldId: number): Node[];
487
+ /** Get the node's first child that contains or starts after the given byte offset. */
488
+ firstChildForIndex(index: number): Node | null;
489
+ /** Get the node's first named child that contains or starts after the given byte offset. */
490
+ firstNamedChildForIndex(index: number): Node | null;
491
+ /** Get this node's number of children. */
492
+ get childCount(): number;
493
+ /**
494
+ * Get this node's number of *named* children.
495
+ *
496
+ * See also {@link Node#isNamed}.
497
+ */
498
+ get namedChildCount(): number;
499
+ /** Get this node's first child. */
500
+ get firstChild(): Node | null;
501
+ /**
502
+ * Get this node's first named child.
503
+ *
504
+ * See also {@link Node#isNamed}.
505
+ */
506
+ get firstNamedChild(): Node | null;
507
+ /** Get this node's last child. */
508
+ get lastChild(): Node | null;
509
+ /**
510
+ * Get this node's last named child.
511
+ *
512
+ * See also {@link Node#isNamed}.
513
+ */
514
+ get lastNamedChild(): Node | null;
515
+ /**
516
+ * Iterate over this node's children.
517
+ *
518
+ * If you're walking the tree recursively, you may want to use the
519
+ * {@link TreeCursor} APIs directly instead.
520
+ */
521
+ get children(): Node[];
522
+ /**
523
+ * Iterate over this node's named children.
524
+ *
525
+ * See also {@link Node#children}.
526
+ */
527
+ get namedChildren(): Node[];
528
+ /**
529
+ * Get the descendants of this node that are the given type, or in the given types array.
530
+ *
531
+ * The types array should contain node type strings, which can be retrieved from {@link Language#types}.
532
+ *
533
+ * Additionally, a `startPosition` and `endPosition` can be passed in to restrict the search to a byte range.
534
+ */
535
+ descendantsOfType(types: string | string[], startPosition?: Point, endPosition?: Point): Node[];
536
+ /** Get this node's next sibling. */
537
+ get nextSibling(): Node | null;
538
+ /** Get this node's previous sibling. */
539
+ get previousSibling(): Node | null;
540
+ /**
541
+ * Get this node's next *named* sibling.
542
+ *
543
+ * See also {@link Node#isNamed}.
544
+ */
545
+ get nextNamedSibling(): Node | null;
546
+ /**
547
+ * Get this node's previous *named* sibling.
548
+ *
549
+ * See also {@link Node#isNamed}.
550
+ */
551
+ get previousNamedSibling(): Node | null;
552
+ /** Get the node's number of descendants, including one for the node itself. */
553
+ get descendantCount(): number;
554
+ /**
555
+ * Get this node's immediate parent.
556
+ * Prefer {@link Node#childWithDescendant} for iterating over this node's ancestors.
557
+ */
558
+ get parent(): Node | null;
559
+ /**
560
+ * Get the node that contains `descendant`.
561
+ *
562
+ * Note that this can return `descendant` itself.
563
+ */
564
+ childWithDescendant(descendant: Node): Node | null;
565
+ /** Get the smallest node within this node that spans the given byte range. */
566
+ descendantForIndex(start: number, end?: number): Node | null;
567
+ /** Get the smallest named node within this node that spans the given byte range. */
568
+ namedDescendantForIndex(start: number, end?: number): Node | null;
569
+ /** Get the smallest node within this node that spans the given point range. */
570
+ descendantForPosition(start: Point, end?: Point): Node | null;
571
+ /** Get the smallest named node within this node that spans the given point range. */
572
+ namedDescendantForPosition(start: Point, end?: Point): Node | null;
573
+ /**
574
+ * Create a new {@link TreeCursor} starting from this node.
575
+ *
576
+ * Note that the given node is considered the root of the cursor,
577
+ * and the cursor cannot walk outside this node.
578
+ */
579
+ walk(): TreeCursor;
580
+ /**
581
+ * Edit this node to keep it in-sync with source code that has been edited.
582
+ *
583
+ * This function is only rarely needed. When you edit a syntax tree with
584
+ * the {@link Tree#edit} method, all of the nodes that you retrieve from
585
+ * the tree afterward will already reflect the edit. You only need to
586
+ * use {@link Node#edit} when you have a specific {@link Node} instance that
587
+ * you want to keep and continue to use after an edit.
588
+ */
589
+ edit(edit: Edit): void;
590
+ /** Get the S-expression representation of this node. */
591
+ toString(): string;
592
+ }
593
+ /** A stateful object for walking a syntax {@link Tree} efficiently. */
594
+ export class TreeCursor {
595
+ /** Creates a deep copy of the tree cursor. This allocates new memory. */
596
+ copy(): TreeCursor;
597
+ /** Delete the tree cursor, freeing its resources. */
598
+ delete(): void;
599
+ /** Get the tree cursor's current {@link Node}. */
600
+ get currentNode(): Node;
601
+ /**
602
+ * Get the numerical field id of this tree cursor's current node.
603
+ *
604
+ * See also {@link TreeCursor#currentFieldName}.
605
+ */
606
+ get currentFieldId(): number;
607
+ /** Get the field name of this tree cursor's current node. */
608
+ get currentFieldName(): string | null;
609
+ /**
610
+ * Get the depth of the cursor's current node relative to the original
611
+ * node that the cursor was constructed with.
612
+ */
613
+ get currentDepth(): number;
614
+ /**
615
+ * Get the index of the cursor's current node out of all of the
616
+ * descendants of the original node that the cursor was constructed with.
617
+ */
618
+ get currentDescendantIndex(): number;
619
+ /** Get the type of the cursor's current node. */
620
+ get nodeType(): string;
621
+ /** Get the type id of the cursor's current node. */
622
+ get nodeTypeId(): number;
623
+ /** Get the state id of the cursor's current node. */
624
+ get nodeStateId(): number;
625
+ /** Get the id of the cursor's current node. */
626
+ get nodeId(): number;
627
+ /**
628
+ * Check if the cursor's current node is *named*.
629
+ *
630
+ * Named nodes correspond to named rules in the grammar, whereas
631
+ * *anonymous* nodes correspond to string literals in the grammar.
632
+ */
633
+ get nodeIsNamed(): boolean;
634
+ /**
635
+ * Check if the cursor's current node is *missing*.
636
+ *
637
+ * Missing nodes are inserted by the parser in order to recover from
638
+ * certain kinds of syntax errors.
639
+ */
640
+ get nodeIsMissing(): boolean;
641
+ /** Get the string content of the cursor's current node. */
642
+ get nodeText(): string;
643
+ /** Get the start position of the cursor's current node. */
644
+ get startPosition(): Point;
645
+ /** Get the end position of the cursor's current node. */
646
+ get endPosition(): Point;
647
+ /** Get the start index of the cursor's current node. */
648
+ get startIndex(): number;
649
+ /** Get the end index of the cursor's current node. */
650
+ get endIndex(): number;
651
+ /**
652
+ * Move this cursor to the first child of its current node.
653
+ *
654
+ * This returns `true` if the cursor successfully moved, and returns
655
+ * `false` if there were no children.
656
+ */
657
+ gotoFirstChild(): boolean;
658
+ /**
659
+ * Move this cursor to the last child of its current node.
660
+ *
661
+ * This returns `true` if the cursor successfully moved, and returns
662
+ * `false` if there were no children.
663
+ *
664
+ * Note that this function may be slower than
665
+ * {@link TreeCursor#gotoFirstChild} because it needs to
666
+ * iterate through all the children to compute the child's position.
667
+ */
668
+ gotoLastChild(): boolean;
669
+ /**
670
+ * Move this cursor to the parent of its current node.
671
+ *
672
+ * This returns `true` if the cursor successfully moved, and returns
673
+ * `false` if there was no parent node (the cursor was already on the
674
+ * root node).
675
+ *
676
+ * Note that the node the cursor was constructed with is considered the root
677
+ * of the cursor, and the cursor cannot walk outside this node.
678
+ */
679
+ gotoParent(): boolean;
680
+ /**
681
+ * Move this cursor to the next sibling of its current node.
682
+ *
683
+ * This returns `true` if the cursor successfully moved, and returns
684
+ * `false` if there was no next sibling node.
685
+ *
686
+ * Note that the node the cursor was constructed with is considered the root
687
+ * of the cursor, and the cursor cannot walk outside this node.
688
+ */
689
+ gotoNextSibling(): boolean;
690
+ /**
691
+ * Move this cursor to the previous sibling of its current node.
692
+ *
693
+ * This returns `true` if the cursor successfully moved, and returns
694
+ * `false` if there was no previous sibling node.
695
+ *
696
+ * Note that this function may be slower than
697
+ * {@link TreeCursor#gotoNextSibling} due to how node
698
+ * positions are stored. In the worst case, this will need to iterate
699
+ * through all the children up to the previous sibling node to recalculate
700
+ * its position. Also note that the node the cursor was constructed with is
701
+ * considered the root of the cursor, and the cursor cannot walk outside this node.
702
+ */
703
+ gotoPreviousSibling(): boolean;
704
+ /**
705
+ * Move the cursor to the node that is the nth descendant of
706
+ * the original node that the cursor was constructed with, where
707
+ * zero represents the original node itself.
708
+ */
709
+ gotoDescendant(goalDescendantIndex: number): void;
710
+ /**
711
+ * Move this cursor to the first child of its current node that contains or
712
+ * starts after the given byte offset.
713
+ *
714
+ * This returns `true` if the cursor successfully moved to a child node, and returns
715
+ * `false` if no such child was found.
716
+ */
717
+ gotoFirstChildForIndex(goalIndex: number): boolean;
718
+ /**
719
+ * Move this cursor to the first child of its current node that contains or
720
+ * starts after the given byte offset.
721
+ *
722
+ * This returns the index of the child node if one was found, and returns
723
+ * `null` if no such child was found.
724
+ */
725
+ gotoFirstChildForPosition(goalPosition: Point): boolean;
726
+ /**
727
+ * Re-initialize this tree cursor to start at the original node that the
728
+ * cursor was constructed with.
729
+ */
730
+ reset(node: Node): void;
731
+ /**
732
+ * Re-initialize a tree cursor to the same position as another cursor.
733
+ *
734
+ * Unlike {@link TreeCursor#reset}, this will not lose parent
735
+ * information and allows reusing already created cursors.
736
+ */
737
+ resetTo(cursor: TreeCursor): void;
738
+ }
739
+ /**
740
+ * Options for query execution
741
+ */
742
+ export interface QueryOptions {
743
+ /** The start position of the range to query */
744
+ startPosition?: Point;
745
+ /** The end position of the range to query */
746
+ endPosition?: Point;
747
+ /** The start position of the range to query Only the matches that are fully
748
+ * contained within provided range will be returned.
749
+ **/
750
+ startContainingPosition?: Point;
751
+ /** The end position of the range to query Only the matches that are fully
752
+ * contained within provided range will be returned.
753
+ **/
754
+ endContainingPosition?: Point;
755
+ /** The start index of the range to query */
756
+ startIndex?: number;
757
+ /** The end index of the range to query */
758
+ endIndex?: number;
759
+ /** The start index of the range to query Only the matches that are fully
760
+ * contained within provided range will be returned.
761
+ **/
762
+ startContainingIndex?: number;
763
+ /** The end index of the range to query Only the matches that are fully
764
+ * contained within provided range will be returned.
765
+ **/
766
+ endContainingIndex?: number;
767
+ /**
768
+ * The maximum number of in-progress matches for this query.
769
+ * The limit must be > 0 and <= 65536.
770
+ */
771
+ matchLimit?: number;
772
+ /**
773
+ * The maximum start depth for a query cursor.
774
+ *
775
+ * This prevents cursors from exploring children nodes at a certain depth.
776
+ * Note if a pattern includes many children, then they will still be
777
+ * checked.
778
+ *
779
+ * The zero max start depth value can be used as a special behavior and
780
+ * it helps to destructure a subtree by staying on a node and using
781
+ * captures for interested parts. Note that the zero max start depth
782
+ * only limit a search depth for a pattern's root node but other nodes
783
+ * that are parts of the pattern may be searched at any depth what
784
+ * defined by the pattern structure.
785
+ *
786
+ * Set to `null` to remove the maximum start depth.
787
+ */
788
+ maxStartDepth?: number;
789
+ /**
790
+ * A function that will be called periodically during the execution of the query to check
791
+ * if query execution should be cancelled. You can also use this to instrument query execution
792
+ * and check where the query is at in the document. The progress callback takes a single argument,
793
+ * which is a {@link QueryState} representing the current state of the query.
794
+ */
795
+ progressCallback?: (state: QueryState) => void;
796
+ }
797
+ /**
798
+ * A stateful object that is passed into the progress callback {@link QueryOptions#progressCallback}
799
+ * to provide the current state of the query.
800
+ */
801
+ export interface QueryState {
802
+ /** The byte offset in the document that the query is at. */
803
+ currentOffset: number;
804
+ }
805
+ /** A record of key-value pairs associated with a particular pattern in a {@link Query}. */
806
+ export type QueryProperties = Record<string, string | null>;
807
+ /**
808
+ * A predicate that contains an operator and list of operands.
809
+ */
810
+ export interface QueryPredicate {
811
+ /** The operator of the predicate, like `match?`, `eq?`, `set!`, etc. */
812
+ operator: string;
813
+ /** The operands of the predicate, which are either captures or strings. */
814
+ operands: PredicateStep[];
815
+ }
816
+ /**
817
+ * A particular {@link Node} that has been captured with a particular name within a
818
+ * {@link Query}.
819
+ */
820
+ export interface QueryCapture {
821
+ /** The index of the pattern that matched. */
822
+ patternIndex: number;
823
+ /** The name of the capture */
824
+ name: string;
825
+ /** The captured node */
826
+ node: Node;
827
+ /** The properties for predicates declared with the operator `set!`. */
828
+ setProperties?: QueryProperties;
829
+ /** The properties for predicates declared with the operator `is?`. */
830
+ assertedProperties?: QueryProperties;
831
+ /** The properties for predicates declared with the operator `is-not?`. */
832
+ refutedProperties?: QueryProperties;
833
+ }
834
+ /** A match of a {@link Query} to a particular set of {@link Node}s. */
835
+ export interface QueryMatch {
836
+ /** The index of the pattern that matched. */
837
+ patternIndex: number;
838
+ /** The captures associated with the match. */
839
+ captures: QueryCapture[];
840
+ /** The properties for predicates declared with the operator `set!`. */
841
+ setProperties?: QueryProperties;
842
+ /** The properties for predicates declared with the operator `is?`. */
843
+ assertedProperties?: QueryProperties;
844
+ /** The properties for predicates declared with the operator `is-not?`. */
845
+ refutedProperties?: QueryProperties;
846
+ }
847
+ /** A quantifier for captures */
848
+ export const CaptureQuantifier: {
849
+ readonly Zero: 0;
850
+ readonly ZeroOrOne: 1;
851
+ readonly ZeroOrMore: 2;
852
+ readonly One: 3;
853
+ readonly OneOrMore: 4;
854
+ };
855
+ /** A quantifier for captures */
856
+ export type CaptureQuantifier = typeof CaptureQuantifier[keyof typeof CaptureQuantifier];
857
+ /**
858
+ * Predicates are represented as a single array of steps. There are two
859
+ * types of steps, which correspond to the two legal values for
860
+ * the `type` field:
861
+ *
862
+ * - `CapturePredicateStep` - Steps with this type represent names
863
+ * of captures.
864
+ *
865
+ * - `StringPredicateStep` - Steps with this type represent literal
866
+ * strings.
867
+ */
868
+ export type PredicateStep = CapturePredicateStep | StringPredicateStep;
869
+ /**
870
+ * A step in a predicate that refers to a capture.
871
+ *
872
+ * The `name` field is the name of the capture.
873
+ */
874
+ interface CapturePredicateStep {
875
+ type: 'capture';
876
+ name: string;
877
+ }
878
+ /**
879
+ * A step in a predicate that refers to a string.
880
+ *
881
+ * The `value` field is the string value.
882
+ */
883
+ interface StringPredicateStep {
884
+ type: 'string';
885
+ value: string;
886
+ }
887
+ export class Query {
888
+ /** The names of the captures used in the query. */
889
+ readonly captureNames: string[];
890
+ /** The quantifiers of the captures used in the query. */
891
+ readonly captureQuantifiers: CaptureQuantifier[][];
892
+ /**
893
+ * The other user-defined predicates associated with the given index.
894
+ *
895
+ * This includes predicates with operators other than:
896
+ * - `match?`
897
+ * - `eq?` and `not-eq?`
898
+ * - `any-of?` and `not-any-of?`
899
+ * - `is?` and `is-not?`
900
+ * - `set!`
901
+ */
902
+ readonly predicates: QueryPredicate[][];
903
+ /** The properties for predicates with the operator `set!`. */
904
+ readonly setProperties: QueryProperties[];
905
+ /** The properties for predicates with the operator `is?`. */
906
+ readonly assertedProperties: QueryProperties[];
907
+ /** The properties for predicates with the operator `is-not?`. */
908
+ readonly refutedProperties: QueryProperties[];
909
+ /** The maximum number of in-progress matches for this cursor. */
910
+ matchLimit?: number;
911
+ /**
912
+ * Create a new query from a string containing one or more S-expression
913
+ * patterns.
914
+ *
915
+ * The query is associated with a particular language, and can only be run
916
+ * on syntax nodes parsed with that language. References to Queries can be
917
+ * shared between multiple threads.
918
+ *
919
+ * @link {@see https://tree-sitter.github.io/tree-sitter/using-parsers/queries}
920
+ */
921
+ constructor(language: Language, source: string);
922
+ /** Delete the query, freeing its resources. */
923
+ delete(): void;
924
+ /**
925
+ * Iterate over all of the matches in the order that they were found.
926
+ *
927
+ * Each match contains the index of the pattern that matched, and a list of
928
+ * captures. Because multiple patterns can match the same set of nodes,
929
+ * one match may contain captures that appear *before* some of the
930
+ * captures from a previous match.
931
+ *
932
+ * @param node - The node to execute the query on.
933
+ *
934
+ * @param options - Options for query execution.
935
+ */
936
+ matches(node: Node, options?: QueryOptions): QueryMatch[];
937
+ /**
938
+ * Iterate over all of the individual captures in the order that they
939
+ * appear.
940
+ *
941
+ * This is useful if you don't care about which pattern matched, and just
942
+ * want a single, ordered sequence of captures.
943
+ *
944
+ * @param node - The node to execute the query on.
945
+ *
946
+ * @param options - Options for query execution.
947
+ */
948
+ captures(node: Node, options?: QueryOptions): QueryCapture[];
949
+ /** Get the predicates for a given pattern. */
950
+ predicatesForPattern(patternIndex: number): QueryPredicate[];
951
+ /**
952
+ * Disable a certain capture within a query.
953
+ *
954
+ * This prevents the capture from being returned in matches, and also
955
+ * avoids any resource usage associated with recording the capture.
956
+ */
957
+ disableCapture(captureName: string): void;
958
+ /**
959
+ * Disable a certain pattern within a query.
960
+ *
961
+ * This prevents the pattern from matching, and also avoids any resource
962
+ * usage associated with the pattern. This throws an error if the pattern
963
+ * index is out of bounds.
964
+ */
965
+ disablePattern(patternIndex: number): void;
966
+ /**
967
+ * Check if, on its last execution, this cursor exceeded its maximum number
968
+ * of in-progress matches.
969
+ */
970
+ didExceedMatchLimit(): boolean;
971
+ /** Get the byte offset where the given pattern starts in the query's source. */
972
+ startIndexForPattern(patternIndex: number): number;
973
+ /** Get the byte offset where the given pattern ends in the query's source. */
974
+ endIndexForPattern(patternIndex: number): number;
975
+ /** Get the number of patterns in the query. */
976
+ patternCount(): number;
977
+ /** Get the index for a given capture name. */
978
+ captureIndexForName(captureName: string): number;
979
+ /** Check if a given pattern within a query has a single root node. */
980
+ isPatternRooted(patternIndex: number): boolean;
981
+ /** Check if a given pattern within a query has a single root node. */
982
+ isPatternNonLocal(patternIndex: number): boolean;
983
+ /**
984
+ * Check if a given step in a query is 'definite'.
985
+ *
986
+ * A query step is 'definite' if its parent pattern will be guaranteed to
987
+ * match successfully once it reaches the step.
988
+ */
989
+ isPatternGuaranteedAtStep(byteIndex: number): boolean;
990
+ }
991
+ export class LookaheadIterator implements Iterable<string> {
992
+ /** Get the current symbol of the lookahead iterator. */
993
+ get currentTypeId(): number;
994
+ /** Get the current symbol name of the lookahead iterator. */
995
+ get currentType(): string;
996
+ /** Delete the lookahead iterator, freeing its resources. */
997
+ delete(): void;
998
+ /**
999
+ * Reset the lookahead iterator.
1000
+ *
1001
+ * This returns `true` if the language was set successfully and `false`
1002
+ * otherwise.
1003
+ */
1004
+ reset(language: Language, stateId: number): boolean;
1005
+ /**
1006
+ * Reset the lookahead iterator to another state.
1007
+ *
1008
+ * This returns `true` if the iterator was reset to the given state and
1009
+ * `false` otherwise.
1010
+ */
1011
+ resetState(stateId: number): boolean;
1012
+ /**
1013
+ * Returns an iterator that iterates over the symbols of the lookahead iterator.
1014
+ *
1015
+ * The iterator will yield the current symbol name as a string for each step
1016
+ * until there are no more symbols to iterate over.
1017
+ */
1018
+ [Symbol.iterator](): Iterator<string>;
1019
+ }
1020
+
1021
+ export {};