changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > core / lisp/ffi/tree-sitter/alien.h

changeset 696: 38e9c3be2392
author: Richard Westhaver <ellis@rwest.io>
date: Fri, 04 Oct 2024 21:11:52 -0400
permissions: -rw-r--r--
description: prep for adding zdict wrapper, change default control stack size of inferior-lisp to 8M
1 #ifndef TREE_SITTER_API_H_
2 #define TREE_SITTER_API_H_
3 
4 #ifndef TREE_SITTER_HIDE_SYMBOLS
5 #if defined(__GNUC__) || defined(__clang__)
6 #pragma GCC visibility push(default)
7 #endif
8 #endif
9 
10 #include <stdlib.h>
11 #include <stdint.h>
12 #include <stdbool.h>
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 /****************************/
19 /* Section - ABI Versioning */
20 /****************************/
21 
22 /**
23  * The latest ABI version that is supported by the current version of the
24  * library. When Languages are generated by the Tree-sitter CLI, they are
25  * assigned an ABI version number that corresponds to the current CLI version.
26  * The Tree-sitter library is generally backwards-compatible with languages
27  * generated using older CLI versions, but is not forwards-compatible.
28  */
29 #define TREE_SITTER_LANGUAGE_VERSION 14
30 
31 /**
32  * The earliest ABI version that is supported by the current version of the
33  * library.
34  */
35 #define TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION 13
36 
37 /*******************/
38 /* Section - Types */
39 /*******************/
40 
41 typedef uint16_t TSStateId;
42 typedef uint16_t TSSymbol;
43 typedef uint16_t TSFieldId;
44 typedef struct TSLanguage TSLanguage;
45 typedef struct TSParser TSParser;
46 typedef struct TSTree TSTree;
47 typedef struct TSQuery TSQuery;
48 typedef struct TSQueryCursor TSQueryCursor;
49 typedef struct TSLookaheadIterator TSLookaheadIterator;
50 
51 typedef enum TSInputEncoding {
52  TSInputEncodingUTF8,
53  TSInputEncodingUTF16,
54 } TSInputEncoding;
55 
56 typedef enum TSSymbolType {
57  TSSymbolTypeRegular,
58  TSSymbolTypeAnonymous,
59  TSSymbolTypeAuxiliary,
60 } TSSymbolType;
61 
62 typedef struct TSPoint {
63  uint32_t row;
64  uint32_t column;
65 } TSPoint;
66 
67 typedef struct TSRange {
68  TSPoint start_point;
69  TSPoint end_point;
70  uint32_t start_byte;
71  uint32_t end_byte;
72 } TSRange;
73 
74 typedef struct TSInput {
75  void *payload;
76  const char *(*read)(void *payload, uint32_t byte_index, TSPoint position, uint32_t *bytes_read);
77  TSInputEncoding encoding;
78 } TSInput;
79 
80 typedef enum TSLogType {
81  TSLogTypeParse,
82  TSLogTypeLex,
83 } TSLogType;
84 
85 typedef struct TSLogger {
86  void *payload;
87  void (*log)(void *payload, TSLogType log_type, const char *buffer);
88 } TSLogger;
89 
90 typedef struct TSInputEdit {
91  uint32_t start_byte;
92  uint32_t old_end_byte;
93  uint32_t new_end_byte;
94  TSPoint start_point;
95  TSPoint old_end_point;
96  TSPoint new_end_point;
97 } TSInputEdit;
98 
99 typedef struct TSNode {
100  uint32_t context[4];
101  const void *id;
102  const TSTree *tree;
103 } TSNode;
104 
105 typedef struct TSTreeCursor {
106  const void *tree;
107  const void *id;
108  uint32_t context[3];
109 } TSTreeCursor;
110 
111 typedef struct TSQueryCapture {
112  TSNode node;
113  uint32_t index;
114 } TSQueryCapture;
115 
116 typedef enum TSQuantifier {
117  TSQuantifierZero = 0, // must match the array initialization value
118  TSQuantifierZeroOrOne,
119  TSQuantifierZeroOrMore,
120  TSQuantifierOne,
121  TSQuantifierOneOrMore,
122 } TSQuantifier;
123 
124 typedef struct TSQueryMatch {
125  uint32_t id;
126  uint16_t pattern_index;
127  uint16_t capture_count;
128  const TSQueryCapture *captures;
129 } TSQueryMatch;
130 
131 typedef enum TSQueryPredicateStepType {
132  TSQueryPredicateStepTypeDone,
133  TSQueryPredicateStepTypeCapture,
134  TSQueryPredicateStepTypeString,
135 } TSQueryPredicateStepType;
136 
137 typedef struct TSQueryPredicateStep {
138  TSQueryPredicateStepType type;
139  uint32_t value_id;
140 } TSQueryPredicateStep;
141 
142 typedef enum TSQueryError {
143  TSQueryErrorNone = 0,
144  TSQueryErrorSyntax,
145  TSQueryErrorNodeType,
146  TSQueryErrorField,
147  TSQueryErrorCapture,
148  TSQueryErrorStructure,
149  TSQueryErrorLanguage,
150 } TSQueryError;
151 
152 /********************/
153 /* Section - Parser */
154 /********************/
155 
156 /**
157  * Create a new parser.
158  */
159 TSParser *ts_parser_new(void);
160 
161 /**
162  * Delete the parser, freeing all of the memory that it used.
163  */
164 void ts_parser_delete(TSParser *self);
165 
166 /**
167  * Get the parser's current language.
168  */
169 const TSLanguage *ts_parser_language(const TSParser *self);
170 
171 /**
172  * Set the language that the parser should use for parsing.
173  *
174  * Returns a boolean indicating whether or not the language was successfully
175  * assigned. True means assignment succeeded. False means there was a version
176  * mismatch: the language was generated with an incompatible version of the
177  * Tree-sitter CLI. Check the language's version using [`ts_language_version`]
178  * and compare it to this library's [`TREE_SITTER_LANGUAGE_VERSION`] and
179  * [`TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION`] constants.
180  */
181 bool ts_parser_set_language(TSParser *self, const TSLanguage *language);
182 
183 /**
184  * Set the ranges of text that the parser should include when parsing.
185  *
186  * By default, the parser will always include entire documents. This function
187  * allows you to parse only a *portion* of a document but still return a syntax
188  * tree whose ranges match up with the document as a whole. You can also pass
189  * multiple disjoint ranges.
190  *
191  * The second and third parameters specify the location and length of an array
192  * of ranges. The parser does *not* take ownership of these ranges; it copies
193  * the data, so it doesn't matter how these ranges are allocated.
194  *
195  * If `count` is zero, then the entire document will be parsed. Otherwise,
196  * the given ranges must be ordered from earliest to latest in the document,
197  * and they must not overlap. That is, the following must hold for all:
198  *
199  * `i < count - 1`: `ranges[i].end_byte <= ranges[i + 1].start_byte`
200  *
201  * If this requirement is not satisfied, the operation will fail, the ranges
202  * will not be assigned, and this function will return `false`. On success,
203  * this function returns `true`
204  */
205 bool ts_parser_set_included_ranges(
206  TSParser *self,
207  const TSRange *ranges,
208  uint32_t count
209 );
210 
211 /**
212  * Get the ranges of text that the parser will include when parsing.
213  *
214  * The returned pointer is owned by the parser. The caller should not free it
215  * or write to it. The length of the array will be written to the given
216  * `count` pointer.
217  */
218 const TSRange *ts_parser_included_ranges(
219  const TSParser *self,
220  uint32_t *count
221 );
222 
223 /**
224  * Use the parser to parse some source code and create a syntax tree.
225  *
226  * If you are parsing this document for the first time, pass `NULL` for the
227  * `old_tree` parameter. Otherwise, if you have already parsed an earlier
228  * version of this document and the document has since been edited, pass the
229  * previous syntax tree so that the unchanged parts of it can be reused.
230  * This will save time and memory. For this to work correctly, you must have
231  * already edited the old syntax tree using the [`ts_tree_edit`] function in a
232  * way that exactly matches the source code changes.
233  *
234  * The [`TSInput`] parameter lets you specify how to read the text. It has the
235  * following three fields:
236  * 1. [`read`]: A function to retrieve a chunk of text at a given byte offset
237  * and (row, column) position. The function should return a pointer to the
238  * text and write its length to the [`bytes_read`] pointer. The parser does
239  * not take ownership of this buffer; it just borrows it until it has
240  * finished reading it. The function should write a zero value to the
241  * [`bytes_read`] pointer to indicate the end of the document.
242  * 2. [`payload`]: An arbitrary pointer that will be passed to each invocation
243  * of the [`read`] function.
244  * 3. [`encoding`]: An indication of how the text is encoded. Either
245  * `TSInputEncodingUTF8` or `TSInputEncodingUTF16`.
246  *
247  * This function returns a syntax tree on success, and `NULL` on failure. There
248  * are three possible reasons for failure:
249  * 1. The parser does not have a language assigned. Check for this using the
250  [`ts_parser_language`] function.
251  * 2. Parsing was cancelled due to a timeout that was set by an earlier call to
252  * the [`ts_parser_set_timeout_micros`] function. You can resume parsing from
253  * where the parser left out by calling [`ts_parser_parse`] again with the
254  * same arguments. Or you can start parsing from scratch by first calling
255  * [`ts_parser_reset`].
256  * 3. Parsing was cancelled using a cancellation flag that was set by an
257  * earlier call to [`ts_parser_set_cancellation_flag`]. You can resume parsing
258  * from where the parser left out by calling [`ts_parser_parse`] again with
259  * the same arguments.
260  *
261  * [`read`]: TSInput::read
262  * [`payload`]: TSInput::payload
263  * [`encoding`]: TSInput::encoding
264  * [`bytes_read`]: TSInput::read
265  */
266 TSTree *ts_parser_parse(
267  TSParser *self,
268  const TSTree *old_tree,
269  TSInput input
270 );
271 
272 /**
273  * Use the parser to parse some source code stored in one contiguous buffer.
274  * The first two parameters are the same as in the [`ts_parser_parse`] function
275  * above. The second two parameters indicate the location of the buffer and its
276  * length in bytes.
277  */
278 TSTree *ts_parser_parse_string(
279  TSParser *self,
280  const TSTree *old_tree,
281  const char *string,
282  uint32_t length
283 );
284 
285 /**
286  * Use the parser to parse some source code stored in one contiguous buffer with
287  * a given encoding. The first four parameters work the same as in the
288  * [`ts_parser_parse_string`] method above. The final parameter indicates whether
289  * the text is encoded as UTF8 or UTF16.
290  */
291 TSTree *ts_parser_parse_string_encoding(
292  TSParser *self,
293  const TSTree *old_tree,
294  const char *string,
295  uint32_t length,
296  TSInputEncoding encoding
297 );
298 
299 /**
300  * Instruct the parser to start the next parse from the beginning.
301  *
302  * If the parser previously failed because of a timeout or a cancellation, then
303  * by default, it will resume where it left off on the next call to
304  * [`ts_parser_parse`] or other parsing functions. If you don't want to resume,
305  * and instead intend to use this parser to parse some other document, you must
306  * call [`ts_parser_reset`] first.
307  */
308 void ts_parser_reset(TSParser *self);
309 
310 /**
311  * Set the maximum duration in microseconds that parsing should be allowed to
312  * take before halting.
313  *
314  * If parsing takes longer than this, it will halt early, returning NULL.
315  * See [`ts_parser_parse`] for more information.
316  */
317 void ts_parser_set_timeout_micros(TSParser *self, uint64_t timeout_micros);
318 
319 /**
320  * Get the duration in microseconds that parsing is allowed to take.
321  */
322 uint64_t ts_parser_timeout_micros(const TSParser *self);
323 
324 /**
325  * Set the parser's current cancellation flag pointer.
326  *
327  * If a non-null pointer is assigned, then the parser will periodically read
328  * from this pointer during parsing. If it reads a non-zero value, it will
329  * halt early, returning NULL. See [`ts_parser_parse`] for more information.
330  */
331 void ts_parser_set_cancellation_flag(TSParser *self, const size_t *flag);
332 
333 /**
334  * Get the parser's current cancellation flag pointer.
335  */
336 const size_t *ts_parser_cancellation_flag(const TSParser *self);
337 
338 /**
339  * Set the logger that a parser should use during parsing.
340  *
341  * The parser does not take ownership over the logger payload. If a logger was
342  * previously assigned, the caller is responsible for releasing any memory
343  * owned by the previous logger.
344  */
345 void ts_parser_set_logger(TSParser *self, TSLogger logger);
346 
347 /**
348  * Get the parser's current logger.
349  */
350 TSLogger ts_parser_logger(const TSParser *self);
351 
352 /**
353  * Set the file descriptor to which the parser should write debugging graphs
354  * during parsing. The graphs are formatted in the DOT language. You may want
355  * to pipe these graphs directly to a `dot(1)` process in order to generate
356  * SVG output. You can turn off this logging by passing a negative number.
357  */
358 void ts_parser_print_dot_graphs(TSParser *self, int fd);
359 
360 /******************/
361 /* Section - Tree */
362 /******************/
363 
364 /**
365  * Create a shallow copy of the syntax tree. This is very fast.
366  *
367  * You need to copy a syntax tree in order to use it on more than one thread at
368  * a time, as syntax trees are not thread safe.
369  */
370 TSTree *ts_tree_copy(const TSTree *self);
371 
372 /**
373  * Delete the syntax tree, freeing all of the memory that it used.
374  */
375 void ts_tree_delete(TSTree *self);
376 
377 /**
378  * Get the root node of the syntax tree.
379  */
380 TSNode ts_tree_root_node(const TSTree *self);
381 
382 /**
383  * Get the root node of the syntax tree, but with its position
384  * shifted forward by the given offset.
385  */
386 TSNode ts_tree_root_node_with_offset(
387  const TSTree *self,
388  uint32_t offset_bytes,
389  TSPoint offset_extent
390 );
391 
392 /**
393  * Get the language that was used to parse the syntax tree.
394  */
395 const TSLanguage *ts_tree_language(const TSTree *self);
396 
397 /**
398  * Get the array of included ranges that was used to parse the syntax tree.
399  *
400  * The returned pointer must be freed by the caller.
401  */
402 TSRange *ts_tree_included_ranges(const TSTree *self, uint32_t *length);
403 
404 /**
405  * Edit the syntax tree to keep it in sync with source code that has been
406  * edited.
407  *
408  * You must describe the edit both in terms of byte offsets and in terms of
409  * (row, column) coordinates.
410  */
411 void ts_tree_edit(TSTree *self, const TSInputEdit *edit);
412 
413 /**
414  * Compare an old edited syntax tree to a new syntax tree representing the same
415  * document, returning an array of ranges whose syntactic structure has changed.
416  *
417  * For this to work correctly, the old syntax tree must have been edited such
418  * that its ranges match up to the new tree. Generally, you'll want to call
419  * this function right after calling one of the [`ts_parser_parse`] functions.
420  * You need to pass the old tree that was passed to parse, as well as the new
421  * tree that was returned from that function.
422  *
423  * The returned array is allocated using `malloc` and the caller is responsible
424  * for freeing it using `free`. The length of the array will be written to the
425  * given `length` pointer.
426  */
427 TSRange *ts_tree_get_changed_ranges(
428  const TSTree *old_tree,
429  const TSTree *new_tree,
430  uint32_t *length
431 );
432 
433 /**
434  * Write a DOT graph describing the syntax tree to the given file.
435  */
436 void ts_tree_print_dot_graph(const TSTree *self, int file_descriptor);
437 
438 /******************/
439 /* Section - Node */
440 /******************/
441 
442 /**
443  * Get the node's type as a null-terminated string.
444  */
445 const char *ts_node_type(TSNode self);
446 
447 /**
448  * Get the node's type as a numerical id.
449  */
450 TSSymbol ts_node_symbol(TSNode self);
451 
452 /**
453  * Get the node's language.
454  */
455 const TSLanguage *ts_node_language(TSNode self);
456 
457 /**
458  * Get the node's type as it appears in the grammar ignoring aliases as a
459  * null-terminated string.
460  */
461 const char *ts_node_grammar_type(TSNode self);
462 
463 /**
464  * Get the node's type as a numerical id as it appears in the grammar ignoring
465  * aliases. This should be used in [`ts_language_next_state`] instead of
466  * [`ts_node_symbol`].
467  */
468 TSSymbol ts_node_grammar_symbol(TSNode self);
469 
470 /**
471  * Get the node's start byte.
472  */
473 uint32_t ts_node_start_byte(TSNode self);
474 
475 /**
476  * Get the node's start position in terms of rows and columns.
477  */
478 TSPoint ts_node_start_point(TSNode self);
479 
480 /**
481  * Get the node's end byte.
482  */
483 uint32_t ts_node_end_byte(TSNode self);
484 
485 /**
486  * Get the node's end position in terms of rows and columns.
487  */
488 TSPoint ts_node_end_point(TSNode self);
489 
490 /**
491  * Get an S-expression representing the node as a string.
492  *
493  * This string is allocated with `malloc` and the caller is responsible for
494  * freeing it using `free`.
495  */
496 char *ts_node_string(TSNode self);
497 
498 /**
499  * Check if the node is null. Functions like [`ts_node_child`] and
500  * [`ts_node_next_sibling`] will return a null node to indicate that no such node
501  * was found.
502  */
503 bool ts_node_is_null(TSNode self);
504 
505 /**
506  * Check if the node is *named*. Named nodes correspond to named rules in the
507  * grammar, whereas *anonymous* nodes correspond to string literals in the
508  * grammar.
509  */
510 bool ts_node_is_named(TSNode self);
511 
512 /**
513  * Check if the node is *missing*. Missing nodes are inserted by the parser in
514  * order to recover from certain kinds of syntax errors.
515  */
516 bool ts_node_is_missing(TSNode self);
517 
518 /**
519  * Check if the node is *extra*. Extra nodes represent things like comments,
520  * which are not required the grammar, but can appear anywhere.
521  */
522 bool ts_node_is_extra(TSNode self);
523 
524 /**
525  * Check if a syntax node has been edited.
526  */
527 bool ts_node_has_changes(TSNode self);
528 
529 /**
530  * Check if the node is a syntax error or contains any syntax errors.
531  */
532 bool ts_node_has_error(TSNode self);
533 
534 /**
535  * Check if the node is a syntax error.
536 */
537 bool ts_node_is_error(TSNode self);
538 
539 /**
540  * Get this node's parse state.
541 */
542 TSStateId ts_node_parse_state(TSNode self);
543 
544 /**
545  * Get the parse state after this node.
546 */
547 TSStateId ts_node_next_parse_state(TSNode self);
548 
549 /**
550  * Get the node's immediate parent.
551  * Prefer [`ts_node_child_containing_descendant`] for
552  * iterating over the node's ancestors.
553  */
554 TSNode ts_node_parent(TSNode self);
555 
556 /**
557  * Get the node's child that contains `descendant`.
558  */
559 TSNode ts_node_child_containing_descendant(TSNode self, TSNode descendant);
560 
561 /**
562  * Get the node's child at the given index, where zero represents the first
563  * child.
564  */
565 TSNode ts_node_child(TSNode self, uint32_t child_index);
566 
567 /**
568  * Get the field name for node's child at the given index, where zero represents
569  * the first child. Returns NULL, if no field is found.
570  */
571 const char *ts_node_field_name_for_child(TSNode self, uint32_t child_index);
572 
573 /**
574  * Get the node's number of children.
575  */
576 uint32_t ts_node_child_count(TSNode self);
577 
578 /**
579  * Get the node's *named* child at the given index.
580  *
581  * See also [`ts_node_is_named`].
582  */
583 TSNode ts_node_named_child(TSNode self, uint32_t child_index);
584 
585 /**
586  * Get the node's number of *named* children.
587  *
588  * See also [`ts_node_is_named`].
589  */
590 uint32_t ts_node_named_child_count(TSNode self);
591 
592 /**
593  * Get the node's child with the given field name.
594  */
595 TSNode ts_node_child_by_field_name(
596  TSNode self,
597  const char *name,
598  uint32_t name_length
599 );
600 
601 /**
602  * Get the node's child with the given numerical field id.
603  *
604  * You can convert a field name to an id using the
605  * [`ts_language_field_id_for_name`] function.
606  */
607 TSNode ts_node_child_by_field_id(TSNode self, TSFieldId field_id);
608 
609 /**
610  * Get the node's next / previous sibling.
611  */
612 TSNode ts_node_next_sibling(TSNode self);
613 TSNode ts_node_prev_sibling(TSNode self);
614 
615 /**
616  * Get the node's next / previous *named* sibling.
617  */
618 TSNode ts_node_next_named_sibling(TSNode self);
619 TSNode ts_node_prev_named_sibling(TSNode self);
620 
621 /**
622  * Get the node's first child that extends beyond the given byte offset.
623  */
624 TSNode ts_node_first_child_for_byte(TSNode self, uint32_t byte);
625 
626 /**
627  * Get the node's first named child that extends beyond the given byte offset.
628  */
629 TSNode ts_node_first_named_child_for_byte(TSNode self, uint32_t byte);
630 
631 /**
632  * Get the node's number of descendants, including one for the node itself.
633  */
634 uint32_t ts_node_descendant_count(TSNode self);
635 
636 /**
637  * Get the smallest node within this node that spans the given range of bytes
638  * or (row, column) positions.
639  */
640 TSNode ts_node_descendant_for_byte_range(TSNode self, uint32_t start, uint32_t end);
641 TSNode ts_node_descendant_for_point_range(TSNode self, TSPoint start, TSPoint end);
642 
643 /**
644  * Get the smallest named node within this node that spans the given range of
645  * bytes or (row, column) positions.
646  */
647 TSNode ts_node_named_descendant_for_byte_range(TSNode self, uint32_t start, uint32_t end);
648 TSNode ts_node_named_descendant_for_point_range(TSNode self, TSPoint start, TSPoint end);
649 
650 /**
651  * Edit the node to keep it in-sync with source code that has been edited.
652  *
653  * This function is only rarely needed. When you edit a syntax tree with the
654  * [`ts_tree_edit`] function, all of the nodes that you retrieve from the tree
655  * afterward will already reflect the edit. You only need to use [`ts_node_edit`]
656  * when you have a [`TSNode`] instance that you want to keep and continue to use
657  * after an edit.
658  */
659 void ts_node_edit(TSNode *self, const TSInputEdit *edit);
660 
661 /**
662  * Check if two nodes are identical.
663  */
664 bool ts_node_eq(TSNode self, TSNode other);
665 
666 /************************/
667 /* Section - TreeCursor */
668 /************************/
669 
670 /**
671  * Create a new tree cursor starting from the given node.
672  *
673  * A tree cursor allows you to walk a syntax tree more efficiently than is
674  * possible using the [`TSNode`] functions. It is a mutable object that is always
675  * on a certain syntax node, and can be moved imperatively to different nodes.
676  */
677 TSTreeCursor ts_tree_cursor_new(TSNode node);
678 
679 /**
680  * Delete a tree cursor, freeing all of the memory that it used.
681  */
682 void ts_tree_cursor_delete(TSTreeCursor *self);
683 
684 /**
685  * Re-initialize a tree cursor to start at the original node that the cursor was
686  * constructed with.
687  */
688 void ts_tree_cursor_reset(TSTreeCursor *self, TSNode node);
689 
690 /**
691  * Re-initialize a tree cursor to the same position as another cursor.
692  *
693  * Unlike [`ts_tree_cursor_reset`], this will not lose parent information and
694  * allows reusing already created cursors.
695 */
696 void ts_tree_cursor_reset_to(TSTreeCursor *dst, const TSTreeCursor *src);
697 
698 /**
699  * Get the tree cursor's current node.
700  */
701 TSNode ts_tree_cursor_current_node(const TSTreeCursor *self);
702 
703 /**
704  * Get the field name of the tree cursor's current node.
705  *
706  * This returns `NULL` if the current node doesn't have a field.
707  * See also [`ts_node_child_by_field_name`].
708  */
709 const char *ts_tree_cursor_current_field_name(const TSTreeCursor *self);
710 
711 /**
712  * Get the field id of the tree cursor's current node.
713  *
714  * This returns zero if the current node doesn't have a field.
715  * See also [`ts_node_child_by_field_id`], [`ts_language_field_id_for_name`].
716  */
717 TSFieldId ts_tree_cursor_current_field_id(const TSTreeCursor *self);
718 
719 /**
720  * Move the cursor to the parent of its current node.
721  *
722  * This returns `true` if the cursor successfully moved, and returns `false`
723  * if there was no parent node (the cursor was already on the root node).
724  */
725 bool ts_tree_cursor_goto_parent(TSTreeCursor *self);
726 
727 /**
728  * Move the cursor to the next sibling of its current node.
729  *
730  * This returns `true` if the cursor successfully moved, and returns `false`
731  * if there was no next sibling node.
732  */
733 bool ts_tree_cursor_goto_next_sibling(TSTreeCursor *self);
734 
735 /**
736  * Move the cursor to the previous sibling of its current node.
737  *
738  * This returns `true` if the cursor successfully moved, and returns `false` if
739  * there was no previous sibling node.
740  *
741  * Note, that this function may be slower than
742  * [`ts_tree_cursor_goto_next_sibling`] due to how node positions are stored. In
743  * the worst case, this will need to iterate through all the children upto the
744  * previous sibling node to recalculate its position.
745  */
746 bool ts_tree_cursor_goto_previous_sibling(TSTreeCursor *self);
747 
748 /**
749  * Move the cursor to the first child of its current node.
750  *
751  * This returns `true` if the cursor successfully moved, and returns `false`
752  * if there were no children.
753  */
754 bool ts_tree_cursor_goto_first_child(TSTreeCursor *self);
755 
756 /**
757  * Move the cursor to the last child of its current node.
758  *
759  * This returns `true` if the cursor successfully moved, and returns `false` if
760  * there were no children.
761  *
762  * Note that this function may be slower than [`ts_tree_cursor_goto_first_child`]
763  * because it needs to iterate through all the children to compute the child's
764  * position.
765  */
766 bool ts_tree_cursor_goto_last_child(TSTreeCursor *self);
767 
768 /**
769  * Move the cursor to the node that is the nth descendant of
770  * the original node that the cursor was constructed with, where
771  * zero represents the original node itself.
772  */
773 void ts_tree_cursor_goto_descendant(TSTreeCursor *self, uint32_t goal_descendant_index);
774 
775 /**
776  * Get the index of the cursor's current node out of all of the
777  * descendants of the original node that the cursor was constructed with.
778  */
779 uint32_t ts_tree_cursor_current_descendant_index(const TSTreeCursor *self);
780 
781 /**
782  * Get the depth of the cursor's current node relative to the original
783  * node that the cursor was constructed with.
784  */
785 uint32_t ts_tree_cursor_current_depth(const TSTreeCursor *self);
786 
787 /**
788  * Move the cursor to the first child of its current node that extends beyond
789  * the given byte offset or point.
790  *
791  * This returns the index of the child node if one was found, and returns -1
792  * if no such child was found.
793  */
794 int64_t ts_tree_cursor_goto_first_child_for_byte(TSTreeCursor *self, uint32_t goal_byte);
795 int64_t ts_tree_cursor_goto_first_child_for_point(TSTreeCursor *self, TSPoint goal_point);
796 
797 TSTreeCursor ts_tree_cursor_copy(const TSTreeCursor *cursor);
798 
799 /*******************/
800 /* Section - Query */
801 /*******************/
802 
803 /**
804  * Create a new query from a string containing one or more S-expression
805  * patterns. The query is associated with a particular language, and can
806  * only be run on syntax nodes parsed with that language.
807  *
808  * If all of the given patterns are valid, this returns a [`TSQuery`].
809  * If a pattern is invalid, this returns `NULL`, and provides two pieces
810  * of information about the problem:
811  * 1. The byte offset of the error is written to the `error_offset` parameter.
812  * 2. The type of error is written to the `error_type` parameter.
813  */
814 TSQuery *ts_query_new(
815  const TSLanguage *language,
816  const char *source,
817  uint32_t source_len,
818  uint32_t *error_offset,
819  TSQueryError *error_type
820 );
821 
822 /**
823  * Delete a query, freeing all of the memory that it used.
824  */
825 void ts_query_delete(TSQuery *self);
826 
827 /**
828  * Get the number of patterns, captures, or string literals in the query.
829  */
830 uint32_t ts_query_pattern_count(const TSQuery *self);
831 uint32_t ts_query_capture_count(const TSQuery *self);
832 uint32_t ts_query_string_count(const TSQuery *self);
833 
834 /**
835  * Get the byte offset where the given pattern starts in the query's source.
836  *
837  * This can be useful when combining queries by concatenating their source
838  * code strings.
839  */
840 uint32_t ts_query_start_byte_for_pattern(const TSQuery *self, uint32_t pattern_index);
841 
842 /**
843  * Get the byte offset where the given pattern ends in the query's source.
844  *
845  * This can be useful when combining queries by concatenating their source
846  * code strings.
847  */
848 uint32_t ts_query_end_byte_for_pattern(const TSQuery *self, uint32_t pattern_index);
849 
850 /**
851  * Get all of the predicates for the given pattern in the query.
852  *
853  * The predicates are represented as a single array of steps. There are three
854  * types of steps in this array, which correspond to the three legal values for
855  * the `type` field:
856  * - `TSQueryPredicateStepTypeCapture` - Steps with this type represent names
857  * of captures. Their `value_id` can be used with the
858  * [`ts_query_capture_name_for_id`] function to obtain the name of the capture.
859  * - `TSQueryPredicateStepTypeString` - Steps with this type represent literal
860  * strings. Their `value_id` can be used with the
861  * [`ts_query_string_value_for_id`] function to obtain their string value.
862  * - `TSQueryPredicateStepTypeDone` - Steps with this type are *sentinels*
863  * that represent the end of an individual predicate. If a pattern has two
864  * predicates, then there will be two steps with this `type` in the array.
865  */
866 const TSQueryPredicateStep *ts_query_predicates_for_pattern(
867  const TSQuery *self,
868  uint32_t pattern_index,
869  uint32_t *step_count
870 );
871 
872 /*
873  * Check if the given pattern in the query has a single root node.
874  */
875 bool ts_query_is_pattern_rooted(const TSQuery *self, uint32_t pattern_index);
876 
877 /*
878  * Check if the given pattern in the query is 'non local'.
879  *
880  * A non-local pattern has multiple root nodes and can match within a
881  * repeating sequence of nodes, as specified by the grammar. Non-local
882  * patterns disable certain optimizations that would otherwise be possible
883  * when executing a query on a specific range of a syntax tree.
884  */
885 bool ts_query_is_pattern_non_local(const TSQuery *self, uint32_t pattern_index);
886 
887 /*
888  * Check if a given pattern is guaranteed to match once a given step is reached.
889  * The step is specified by its byte offset in the query's source code.
890  */
891 bool ts_query_is_pattern_guaranteed_at_step(const TSQuery *self, uint32_t byte_offset);
892 
893 /**
894  * Get the name and length of one of the query's captures, or one of the
895  * query's string literals. Each capture and string is associated with a
896  * numeric id based on the order that it appeared in the query's source.
897  */
898 const char *ts_query_capture_name_for_id(
899  const TSQuery *self,
900  uint32_t index,
901  uint32_t *length
902 );
903 
904 /**
905  * Get the quantifier of the query's captures. Each capture is * associated
906  * with a numeric id based on the order that it appeared in the query's source.
907  */
908 TSQuantifier ts_query_capture_quantifier_for_id(
909  const TSQuery *self,
910  uint32_t pattern_index,
911  uint32_t capture_index
912 );
913 
914 const char *ts_query_string_value_for_id(
915  const TSQuery *self,
916  uint32_t index,
917  uint32_t *length
918 );
919 
920 /**
921  * Disable a certain capture within a query.
922  *
923  * This prevents the capture from being returned in matches, and also avoids
924  * any resource usage associated with recording the capture. Currently, there
925  * is no way to undo this.
926  */
927 void ts_query_disable_capture(TSQuery *self, const char *name, uint32_t length);
928 
929 /**
930  * Disable a certain pattern within a query.
931  *
932  * This prevents the pattern from matching and removes most of the overhead
933  * associated with the pattern. Currently, there is no way to undo this.
934  */
935 void ts_query_disable_pattern(TSQuery *self, uint32_t pattern_index);
936 
937 /**
938  * Create a new cursor for executing a given query.
939  *
940  * The cursor stores the state that is needed to iteratively search
941  * for matches. To use the query cursor, first call [`ts_query_cursor_exec`]
942  * to start running a given query on a given syntax node. Then, there are
943  * two options for consuming the results of the query:
944  * 1. Repeatedly call [`ts_query_cursor_next_match`] to iterate over all of the
945  * *matches* in the order that they were found. Each match contains the
946  * index of the pattern that matched, and an array of captures. Because
947  * multiple patterns can match the same set of nodes, one match may contain
948  * captures that appear *before* some of the captures from a previous match.
949  * 2. Repeatedly call [`ts_query_cursor_next_capture`] to iterate over all of the
950  * individual *captures* in the order that they appear. This is useful if
951  * don't care about which pattern matched, and just want a single ordered
952  * sequence of captures.
953  *
954  * If you don't care about consuming all of the results, you can stop calling
955  * [`ts_query_cursor_next_match`] or [`ts_query_cursor_next_capture`] at any point.
956  * You can then start executing another query on another node by calling
957  * [`ts_query_cursor_exec`] again.
958  */
959 TSQueryCursor *ts_query_cursor_new(void);
960 
961 /**
962  * Delete a query cursor, freeing all of the memory that it used.
963  */
964 void ts_query_cursor_delete(TSQueryCursor *self);
965 
966 /**
967  * Start running a given query on a given node.
968  */
969 void ts_query_cursor_exec(TSQueryCursor *self, const TSQuery *query, TSNode node);
970 
971 /**
972  * Manage the maximum number of in-progress matches allowed by this query
973  * cursor.
974  *
975  * Query cursors have an optional maximum capacity for storing lists of
976  * in-progress captures. If this capacity is exceeded, then the
977  * earliest-starting match will silently be dropped to make room for further
978  * matches. This maximum capacity is optional — by default, query cursors allow
979  * any number of pending matches, dynamically allocating new space for them as
980  * needed as the query is executed.
981  */
982 bool ts_query_cursor_did_exceed_match_limit(const TSQueryCursor *self);
983 uint32_t ts_query_cursor_match_limit(const TSQueryCursor *self);
984 void ts_query_cursor_set_match_limit(TSQueryCursor *self, uint32_t limit);
985 
986 /**
987  * Set the range of bytes or (row, column) positions in which the query
988  * will be executed.
989  */
990 void ts_query_cursor_set_byte_range(TSQueryCursor *self, uint32_t start_byte, uint32_t end_byte);
991 void ts_query_cursor_set_point_range(TSQueryCursor *self, TSPoint start_point, TSPoint end_point);
992 
993 /**
994  * Advance to the next match of the currently running query.
995  *
996  * If there is a match, write it to `*match` and return `true`.
997  * Otherwise, return `false`.
998  */
999 bool ts_query_cursor_next_match(TSQueryCursor *self, TSQueryMatch *match);
1000 void ts_query_cursor_remove_match(TSQueryCursor *self, uint32_t match_id);
1001 
1002 /**
1003  * Advance to the next capture of the currently running query.
1004  *
1005  * If there is a capture, write its match to `*match` and its index within
1006  * the matche's capture list to `*capture_index`. Otherwise, return `false`.
1007  */
1008 bool ts_query_cursor_next_capture(
1009  TSQueryCursor *self,
1010  TSQueryMatch *match,
1011  uint32_t *capture_index
1012 );
1013 
1014 /**
1015  * Set the maximum start depth for a query cursor.
1016  *
1017  * This prevents cursors from exploring children nodes at a certain depth.
1018  * Note if a pattern includes many children, then they will still be checked.
1019  *
1020  * The zero max start depth value can be used as a special behavior and
1021  * it helps to destructure a subtree by staying on a node and using captures
1022  * for interested parts. Note that the zero max start depth only limit a search
1023  * depth for a pattern's root node but other nodes that are parts of the pattern
1024  * may be searched at any depth what defined by the pattern structure.
1025  *
1026  * Set to `UINT32_MAX` to remove the maximum start depth.
1027  */
1028 void ts_query_cursor_set_max_start_depth(TSQueryCursor *self, uint32_t max_start_depth);
1029 
1030 /**********************/
1031 /* Section - Language */
1032 /**********************/
1033 
1034 /**
1035  * Get another reference to the given language.
1036  */
1037 const TSLanguage *ts_language_copy(const TSLanguage *self);
1038 
1039 /**
1040  * Free any dynamically-allocated resources for this language, if
1041  * this is the last reference.
1042  */
1043 void ts_language_delete(const TSLanguage *self);
1044 
1045 /**
1046  * Get the number of distinct node types in the language.
1047  */
1048 uint32_t ts_language_symbol_count(const TSLanguage *self);
1049 
1050 /**
1051  * Get the number of valid states in this language.
1052 */
1053 uint32_t ts_language_state_count(const TSLanguage *self);
1054 
1055 /**
1056  * Get a node type string for the given numerical id.
1057  */
1058 const char *ts_language_symbol_name(const TSLanguage *self, TSSymbol symbol);
1059 
1060 /**
1061  * Get the numerical id for the given node type string.
1062  */
1063 TSSymbol ts_language_symbol_for_name(
1064  const TSLanguage *self,
1065  const char *string,
1066  uint32_t length,
1067  bool is_named
1068 );
1069 
1070 /**
1071  * Get the number of distinct field names in the language.
1072  */
1073 uint32_t ts_language_field_count(const TSLanguage *self);
1074 
1075 /**
1076  * Get the field name string for the given numerical id.
1077  */
1078 const char *ts_language_field_name_for_id(const TSLanguage *self, TSFieldId id);
1079 
1080 /**
1081  * Get the numerical id for the given field name string.
1082  */
1083 TSFieldId ts_language_field_id_for_name(const TSLanguage *self, const char *name, uint32_t name_length);
1084 
1085 /**
1086  * Check whether the given node type id belongs to named nodes, anonymous nodes,
1087  * or a hidden nodes.
1088  *
1089  * See also [`ts_node_is_named`]. Hidden nodes are never returned from the API.
1090  */
1091 TSSymbolType ts_language_symbol_type(const TSLanguage *self, TSSymbol symbol);
1092 
1093 /**
1094  * Get the ABI version number for this language. This version number is used
1095  * to ensure that languages were generated by a compatible version of
1096  * Tree-sitter.
1097  *
1098  * See also [`ts_parser_set_language`].
1099  */
1100 uint32_t ts_language_version(const TSLanguage *self);
1101 
1102 /**
1103  * Get the next parse state. Combine this with lookahead iterators to generate
1104  * completion suggestions or valid symbols in error nodes. Use
1105  * [`ts_node_grammar_symbol`] for valid symbols.
1106 */
1107 TSStateId ts_language_next_state(const TSLanguage *self, TSStateId state, TSSymbol symbol);
1108 
1109 /********************************/
1110 /* Section - Lookahead Iterator */
1111 /********************************/
1112 
1113 /**
1114  * Create a new lookahead iterator for the given language and parse state.
1115  *
1116  * This returns `NULL` if state is invalid for the language.
1117  *
1118  * Repeatedly using [`ts_lookahead_iterator_next`] and
1119  * [`ts_lookahead_iterator_current_symbol`] will generate valid symbols in the
1120  * given parse state. Newly created lookahead iterators will contain the `ERROR`
1121  * symbol.
1122  *
1123  * Lookahead iterators can be useful to generate suggestions and improve syntax
1124  * error diagnostics. To get symbols valid in an ERROR node, use the lookahead
1125  * iterator on its first leaf node state. For `MISSING` nodes, a lookahead
1126  * iterator created on the previous non-extra leaf node may be appropriate.
1127 */
1128 TSLookaheadIterator *ts_lookahead_iterator_new(const TSLanguage *self, TSStateId state);
1129 
1130 /**
1131  * Delete a lookahead iterator freeing all the memory used.
1132 */
1133 void ts_lookahead_iterator_delete(TSLookaheadIterator *self);
1134 
1135 /**
1136  * Reset the lookahead iterator to another state.
1137  *
1138  * This returns `true` if the iterator was reset to the given state and `false`
1139  * otherwise.
1140 */
1141 bool ts_lookahead_iterator_reset_state(TSLookaheadIterator *self, TSStateId state);
1142 
1143 /**
1144  * Reset the lookahead iterator.
1145  *
1146  * This returns `true` if the language was set successfully and `false`
1147  * otherwise.
1148 */
1149 bool ts_lookahead_iterator_reset(TSLookaheadIterator *self, const TSLanguage *language, TSStateId state);
1150 
1151 /**
1152  * Get the current language of the lookahead iterator.
1153 */
1154 const TSLanguage *ts_lookahead_iterator_language(const TSLookaheadIterator *self);
1155 
1156 /**
1157  * Advance the lookahead iterator to the next symbol.
1158  *
1159  * This returns `true` if there is a new symbol and `false` otherwise.
1160 */
1161 bool ts_lookahead_iterator_next(TSLookaheadIterator *self);
1162 
1163 /**
1164  * Get the current symbol of the lookahead iterator;
1165 */
1166 TSSymbol ts_lookahead_iterator_current_symbol(const TSLookaheadIterator *self);
1167 
1168 /**
1169  * Get the current symbol type of the lookahead iterator as a null terminated
1170  * string.
1171 */
1172 const char *ts_lookahead_iterator_current_symbol_name(const TSLookaheadIterator *self);
1173 
1174 /*************************************/
1175 /* Section - WebAssembly Integration */
1176 /************************************/
1177 
1178 typedef struct wasm_engine_t TSWasmEngine;
1179 typedef struct TSWasmStore TSWasmStore;
1180 
1181 typedef enum {
1182  TSWasmErrorKindNone = 0,
1183  TSWasmErrorKindParse,
1184  TSWasmErrorKindCompile,
1185  TSWasmErrorKindInstantiate,
1186  TSWasmErrorKindAllocate,
1187 } TSWasmErrorKind;
1188 
1189 typedef struct {
1190  TSWasmErrorKind kind;
1191  char *message;
1192 } TSWasmError;
1193 
1194 /**
1195  * Create a Wasm store.
1196  */
1197 TSWasmStore *ts_wasm_store_new(
1198  TSWasmEngine *engine,
1199  TSWasmError *error
1200 );
1201 
1202 /**
1203  * Free the memory associated with the given Wasm store.
1204  */
1205 void ts_wasm_store_delete(TSWasmStore *);
1206 
1207 /**
1208  * Create a language from a buffer of Wasm. The resulting language behaves
1209  * like any other Tree-sitter language, except that in order to use it with
1210  * a parser, that parser must have a Wasm store. Note that the language
1211  * can be used with any Wasm store, it doesn't need to be the same store that
1212  * was used to originally load it.
1213  */
1214 const TSLanguage *ts_wasm_store_load_language(
1215  TSWasmStore *,
1216  const char *name,
1217  const char *wasm,
1218  uint32_t wasm_len,
1219  TSWasmError *error
1220 );
1221 
1222 /**
1223  * Get the number of languages instantiated in the given wasm store.
1224  */
1225 size_t ts_wasm_store_language_count(const TSWasmStore *);
1226 
1227 /**
1228  * Check if the language came from a Wasm module. If so, then in order to use
1229  * this language with a Parser, that parser must have a Wasm store assigned.
1230  */
1231 bool ts_language_is_wasm(const TSLanguage *);
1232 
1233 /**
1234  * Assign the given Wasm store to the parser. A parser must have a Wasm store
1235  * in order to use Wasm languages.
1236  */
1237 void ts_parser_set_wasm_store(TSParser *, TSWasmStore *);
1238 
1239 /**
1240  * Remove the parser's current Wasm store and return it. This returns NULL if
1241  * the parser doesn't have a Wasm store.
1242  */
1243 TSWasmStore *ts_parser_take_wasm_store(TSParser *);
1244 
1245 /**********************************/
1246 /* Section - Global Configuration */
1247 /**********************************/
1248 
1249 /**
1250  * Set the allocation functions used by the library.
1251  *
1252  * By default, Tree-sitter uses the standard libc allocation functions,
1253  * but aborts the process when an allocation fails. This function lets
1254  * you supply alternative allocation functions at runtime.
1255  *
1256  * If you pass `NULL` for any parameter, Tree-sitter will switch back to
1257  * its default implementation of that function.
1258  *
1259  * If you call this function after the library has already been used, then
1260  * you must ensure that either:
1261  * 1. All the existing objects have been freed.
1262  * 2. The new allocator shares its state with the old one, so it is capable
1263  * of freeing memory that was allocated by the old allocator.
1264  */
1265 void ts_set_allocator(
1266  void *(*new_malloc)(size_t),
1267  void *(*new_calloc)(size_t, size_t),
1268  void *(*new_realloc)(void *, size_t),
1269  void (*new_free)(void *)
1270 );
1271 
1272 #ifdef __cplusplus
1273 }
1274 #endif
1275 
1276 #ifndef TREE_SITTER_HIDE_SYMBOLS
1277 #if defined(__GNUC__) || defined(__clang__)
1278 #pragma GCC visibility pop
1279 #endif
1280 #endif
1281 
1282 #endif // TREE_SITTER_API_H_