@vscode/tree-sitter-wasm 0.0.5 → 0.1.1

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