@willbooster/web-tree-sitter 0.0.0-placeholder.0 → 1.0.0

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