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_ 4 #ifndef TREE_SITTER_HIDE_SYMBOLS 5 #if defined(__GNUC__) || defined(__clang__) 6 #pragma GCC visibility push(default) 18 /****************************/ 19 /* Section - ABI Versioning */ 20 /****************************/ 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. 29 #define TREE_SITTER_LANGUAGE_VERSION 14 32 * The earliest ABI version that is supported by the current version of the 35 #define TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION 13 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; 51 typedef enum TSInputEncoding { 56 typedef enum TSSymbolType { 58 TSSymbolTypeAnonymous, 59 TSSymbolTypeAuxiliary, 62 typedef struct TSPoint { 67 typedef struct TSRange { 74 typedef struct TSInput { 76 const char *(*read)(void *payload, uint32_t byte_index, TSPoint position, uint32_t *bytes_read); 77 TSInputEncoding encoding; 80 typedef enum TSLogType { 85 typedef struct TSLogger { 87 void (*log)(void *payload, TSLogType log_type, const char *buffer); 90 typedef struct TSInputEdit { 92 uint32_t old_end_byte; 93 uint32_t new_end_byte; 95 TSPoint old_end_point; 96 TSPoint new_end_point; 99 typedef struct TSNode { 105 typedef struct TSTreeCursor { 111 typedef struct TSQueryCapture { 116 typedef enum TSQuantifier { 117 TSQuantifierZero = 0, // must match the array initialization value 118 TSQuantifierZeroOrOne, 119 TSQuantifierZeroOrMore, 121 TSQuantifierOneOrMore, 124 typedef struct TSQueryMatch { 126 uint16_t pattern_index; 127 uint16_t capture_count; 128 const TSQueryCapture *captures; 131 typedef enum TSQueryPredicateStepType { 132 TSQueryPredicateStepTypeDone, 133 TSQueryPredicateStepTypeCapture, 134 TSQueryPredicateStepTypeString, 135 } TSQueryPredicateStepType; 137 typedef struct TSQueryPredicateStep { 138 TSQueryPredicateStepType type; 140 } TSQueryPredicateStep; 142 typedef enum TSQueryError { 143 TSQueryErrorNone = 0, 145 TSQueryErrorNodeType, 148 TSQueryErrorStructure, 149 TSQueryErrorLanguage, 152 /********************/ 153 /* Section - Parser */ 154 /********************/ 157 * Create a new parser. 159 TSParser *ts_parser_new(void); 162 * Delete the parser, freeing all of the memory that it used. 164 void ts_parser_delete(TSParser *self); 167 * Get the parser's current language. 169 const TSLanguage *ts_parser_language(const TSParser *self); 172 * Set the language that the parser should use for parsing. 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. 181 bool ts_parser_set_language(TSParser *self, const TSLanguage *language); 184 * Set the ranges of text that the parser should include when parsing. 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. 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. 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: 199 * `i < count - 1`: `ranges[i].end_byte <= ranges[i + 1].start_byte` 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` 205 bool ts_parser_set_included_ranges( 207 const TSRange *ranges, 212 * Get the ranges of text that the parser will include when parsing. 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 218 const TSRange *ts_parser_included_ranges( 219 const TSParser *self, 224 * Use the parser to parse some source code and create a syntax tree. 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. 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`. 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. 261 * [`read`]: TSInput::read 262 * [`payload`]: TSInput::payload 263 * [`encoding`]: TSInput::encoding 264 * [`bytes_read`]: TSInput::read 266 TSTree *ts_parser_parse( 268 const TSTree *old_tree, 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 278 TSTree *ts_parser_parse_string( 280 const TSTree *old_tree, 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. 291 TSTree *ts_parser_parse_string_encoding( 293 const TSTree *old_tree, 296 TSInputEncoding encoding 300 * Instruct the parser to start the next parse from the beginning. 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. 308 void ts_parser_reset(TSParser *self); 311 * Set the maximum duration in microseconds that parsing should be allowed to 312 * take before halting. 314 * If parsing takes longer than this, it will halt early, returning NULL. 315 * See [`ts_parser_parse`] for more information. 317 void ts_parser_set_timeout_micros(TSParser *self, uint64_t timeout_micros); 320 * Get the duration in microseconds that parsing is allowed to take. 322 uint64_t ts_parser_timeout_micros(const TSParser *self); 325 * Set the parser's current cancellation flag pointer. 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. 331 void ts_parser_set_cancellation_flag(TSParser *self, const size_t *flag); 334 * Get the parser's current cancellation flag pointer. 336 const size_t *ts_parser_cancellation_flag(const TSParser *self); 339 * Set the logger that a parser should use during parsing. 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. 345 void ts_parser_set_logger(TSParser *self, TSLogger logger); 348 * Get the parser's current logger. 350 TSLogger ts_parser_logger(const TSParser *self); 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. 358 void ts_parser_print_dot_graphs(TSParser *self, int fd); 365 * Create a shallow copy of the syntax tree. This is very fast. 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. 370 TSTree *ts_tree_copy(const TSTree *self); 373 * Delete the syntax tree, freeing all of the memory that it used. 375 void ts_tree_delete(TSTree *self); 378 * Get the root node of the syntax tree. 380 TSNode ts_tree_root_node(const TSTree *self); 383 * Get the root node of the syntax tree, but with its position 384 * shifted forward by the given offset. 386 TSNode ts_tree_root_node_with_offset( 388 uint32_t offset_bytes, 389 TSPoint offset_extent 393 * Get the language that was used to parse the syntax tree. 395 const TSLanguage *ts_tree_language(const TSTree *self); 398 * Get the array of included ranges that was used to parse the syntax tree. 400 * The returned pointer must be freed by the caller. 402 TSRange *ts_tree_included_ranges(const TSTree *self, uint32_t *length); 405 * Edit the syntax tree to keep it in sync with source code that has been 408 * You must describe the edit both in terms of byte offsets and in terms of 409 * (row, column) coordinates. 411 void ts_tree_edit(TSTree *self, const TSInputEdit *edit); 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. 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. 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. 427 TSRange *ts_tree_get_changed_ranges( 428 const TSTree *old_tree, 429 const TSTree *new_tree, 434 * Write a DOT graph describing the syntax tree to the given file. 436 void ts_tree_print_dot_graph(const TSTree *self, int file_descriptor); 443 * Get the node's type as a null-terminated string. 445 const char *ts_node_type(TSNode self); 448 * Get the node's type as a numerical id. 450 TSSymbol ts_node_symbol(TSNode self); 453 * Get the node's language. 455 const TSLanguage *ts_node_language(TSNode self); 458 * Get the node's type as it appears in the grammar ignoring aliases as a 459 * null-terminated string. 461 const char *ts_node_grammar_type(TSNode self); 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`]. 468 TSSymbol ts_node_grammar_symbol(TSNode self); 471 * Get the node's start byte. 473 uint32_t ts_node_start_byte(TSNode self); 476 * Get the node's start position in terms of rows and columns. 478 TSPoint ts_node_start_point(TSNode self); 481 * Get the node's end byte. 483 uint32_t ts_node_end_byte(TSNode self); 486 * Get the node's end position in terms of rows and columns. 488 TSPoint ts_node_end_point(TSNode self); 491 * Get an S-expression representing the node as a string. 493 * This string is allocated with `malloc` and the caller is responsible for 494 * freeing it using `free`. 496 char *ts_node_string(TSNode self); 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 503 bool ts_node_is_null(TSNode self); 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 510 bool ts_node_is_named(TSNode self); 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. 516 bool ts_node_is_missing(TSNode self); 519 * Check if the node is *extra*. Extra nodes represent things like comments, 520 * which are not required the grammar, but can appear anywhere. 522 bool ts_node_is_extra(TSNode self); 525 * Check if a syntax node has been edited. 527 bool ts_node_has_changes(TSNode self); 530 * Check if the node is a syntax error or contains any syntax errors. 532 bool ts_node_has_error(TSNode self); 535 * Check if the node is a syntax error. 537 bool ts_node_is_error(TSNode self); 540 * Get this node's parse state. 542 TSStateId ts_node_parse_state(TSNode self); 545 * Get the parse state after this node. 547 TSStateId ts_node_next_parse_state(TSNode self); 550 * Get the node's immediate parent. 551 * Prefer [`ts_node_child_containing_descendant`] for 552 * iterating over the node's ancestors. 554 TSNode ts_node_parent(TSNode self); 557 * Get the node's child that contains `descendant`. 559 TSNode ts_node_child_containing_descendant(TSNode self, TSNode descendant); 562 * Get the node's child at the given index, where zero represents the first 565 TSNode ts_node_child(TSNode self, uint32_t child_index); 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. 571 const char *ts_node_field_name_for_child(TSNode self, uint32_t child_index); 574 * Get the node's number of children. 576 uint32_t ts_node_child_count(TSNode self); 579 * Get the node's *named* child at the given index. 581 * See also [`ts_node_is_named`]. 583 TSNode ts_node_named_child(TSNode self, uint32_t child_index); 586 * Get the node's number of *named* children. 588 * See also [`ts_node_is_named`]. 590 uint32_t ts_node_named_child_count(TSNode self); 593 * Get the node's child with the given field name. 595 TSNode ts_node_child_by_field_name( 602 * Get the node's child with the given numerical field id. 604 * You can convert a field name to an id using the 605 * [`ts_language_field_id_for_name`] function. 607 TSNode ts_node_child_by_field_id(TSNode self, TSFieldId field_id); 610 * Get the node's next / previous sibling. 612 TSNode ts_node_next_sibling(TSNode self); 613 TSNode ts_node_prev_sibling(TSNode self); 616 * Get the node's next / previous *named* sibling. 618 TSNode ts_node_next_named_sibling(TSNode self); 619 TSNode ts_node_prev_named_sibling(TSNode self); 622 * Get the node's first child that extends beyond the given byte offset. 624 TSNode ts_node_first_child_for_byte(TSNode self, uint32_t byte); 627 * Get the node's first named child that extends beyond the given byte offset. 629 TSNode ts_node_first_named_child_for_byte(TSNode self, uint32_t byte); 632 * Get the node's number of descendants, including one for the node itself. 634 uint32_t ts_node_descendant_count(TSNode self); 637 * Get the smallest node within this node that spans the given range of bytes 638 * or (row, column) positions. 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); 644 * Get the smallest named node within this node that spans the given range of 645 * bytes or (row, column) positions. 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); 651 * Edit the node to keep it in-sync with source code that has been edited. 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 659 void ts_node_edit(TSNode *self, const TSInputEdit *edit); 662 * Check if two nodes are identical. 664 bool ts_node_eq(TSNode self, TSNode other); 666 /************************/ 667 /* Section - TreeCursor */ 668 /************************/ 671 * Create a new tree cursor starting from the given node. 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. 677 TSTreeCursor ts_tree_cursor_new(TSNode node); 680 * Delete a tree cursor, freeing all of the memory that it used. 682 void ts_tree_cursor_delete(TSTreeCursor *self); 685 * Re-initialize a tree cursor to start at the original node that the cursor was 688 void ts_tree_cursor_reset(TSTreeCursor *self, TSNode node); 691 * Re-initialize a tree cursor to the same position as another cursor. 693 * Unlike [`ts_tree_cursor_reset`], this will not lose parent information and 694 * allows reusing already created cursors. 696 void ts_tree_cursor_reset_to(TSTreeCursor *dst, const TSTreeCursor *src); 699 * Get the tree cursor's current node. 701 TSNode ts_tree_cursor_current_node(const TSTreeCursor *self); 704 * Get the field name of the tree cursor's current node. 706 * This returns `NULL` if the current node doesn't have a field. 707 * See also [`ts_node_child_by_field_name`]. 709 const char *ts_tree_cursor_current_field_name(const TSTreeCursor *self); 712 * Get the field id of the tree cursor's current node. 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`]. 717 TSFieldId ts_tree_cursor_current_field_id(const TSTreeCursor *self); 720 * Move the cursor to the parent of its current node. 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). 725 bool ts_tree_cursor_goto_parent(TSTreeCursor *self); 728 * Move the cursor to the next sibling of its current node. 730 * This returns `true` if the cursor successfully moved, and returns `false` 731 * if there was no next sibling node. 733 bool ts_tree_cursor_goto_next_sibling(TSTreeCursor *self); 736 * Move the cursor to the previous sibling of its current node. 738 * This returns `true` if the cursor successfully moved, and returns `false` if 739 * there was no previous sibling node. 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. 746 bool ts_tree_cursor_goto_previous_sibling(TSTreeCursor *self); 749 * Move the cursor to the first child of its current node. 751 * This returns `true` if the cursor successfully moved, and returns `false` 752 * if there were no children. 754 bool ts_tree_cursor_goto_first_child(TSTreeCursor *self); 757 * Move the cursor to the last child of its current node. 759 * This returns `true` if the cursor successfully moved, and returns `false` if 760 * there were no children. 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 766 bool ts_tree_cursor_goto_last_child(TSTreeCursor *self); 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. 773 void ts_tree_cursor_goto_descendant(TSTreeCursor *self, uint32_t goal_descendant_index); 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. 779 uint32_t ts_tree_cursor_current_descendant_index(const TSTreeCursor *self); 782 * Get the depth of the cursor's current node relative to the original 783 * node that the cursor was constructed with. 785 uint32_t ts_tree_cursor_current_depth(const TSTreeCursor *self); 788 * Move the cursor to the first child of its current node that extends beyond 789 * the given byte offset or point. 791 * This returns the index of the child node if one was found, and returns -1 792 * if no such child was found. 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); 797 TSTreeCursor ts_tree_cursor_copy(const TSTreeCursor *cursor); 799 /*******************/ 800 /* Section - Query */ 801 /*******************/ 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. 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. 814 TSQuery *ts_query_new( 815 const TSLanguage *language, 818 uint32_t *error_offset, 819 TSQueryError *error_type 823 * Delete a query, freeing all of the memory that it used. 825 void ts_query_delete(TSQuery *self); 828 * Get the number of patterns, captures, or string literals in the query. 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); 835 * Get the byte offset where the given pattern starts in the query's source. 837 * This can be useful when combining queries by concatenating their source 840 uint32_t ts_query_start_byte_for_pattern(const TSQuery *self, uint32_t pattern_index); 843 * Get the byte offset where the given pattern ends in the query's source. 845 * This can be useful when combining queries by concatenating their source 848 uint32_t ts_query_end_byte_for_pattern(const TSQuery *self, uint32_t pattern_index); 851 * Get all of the predicates for the given pattern in the query. 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 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. 866 const TSQueryPredicateStep *ts_query_predicates_for_pattern( 868 uint32_t pattern_index, 873 * Check if the given pattern in the query has a single root node. 875 bool ts_query_is_pattern_rooted(const TSQuery *self, uint32_t pattern_index); 878 * Check if the given pattern in the query is 'non local'. 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. 885 bool ts_query_is_pattern_non_local(const TSQuery *self, uint32_t pattern_index); 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. 891 bool ts_query_is_pattern_guaranteed_at_step(const TSQuery *self, uint32_t byte_offset); 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. 898 const char *ts_query_capture_name_for_id( 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. 908 TSQuantifier ts_query_capture_quantifier_for_id( 910 uint32_t pattern_index, 911 uint32_t capture_index 914 const char *ts_query_string_value_for_id( 921 * Disable a certain capture within a query. 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. 927 void ts_query_disable_capture(TSQuery *self, const char *name, uint32_t length); 930 * Disable a certain pattern within a query. 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. 935 void ts_query_disable_pattern(TSQuery *self, uint32_t pattern_index); 938 * Create a new cursor for executing a given query. 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. 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. 959 TSQueryCursor *ts_query_cursor_new(void); 962 * Delete a query cursor, freeing all of the memory that it used. 964 void ts_query_cursor_delete(TSQueryCursor *self); 967 * Start running a given query on a given node. 969 void ts_query_cursor_exec(TSQueryCursor *self, const TSQuery *query, TSNode node); 972 * Manage the maximum number of in-progress matches allowed by this query 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. 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); 987 * Set the range of bytes or (row, column) positions in which the query 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); 994 * Advance to the next match of the currently running query. 996 * If there is a match, write it to `*match` and return `true`. 997 * Otherwise, return `false`. 999 bool ts_query_cursor_next_match(TSQueryCursor *self, TSQueryMatch *match); 1000 void ts_query_cursor_remove_match(TSQueryCursor *self, uint32_t match_id); 1003 * Advance to the next capture of the currently running query. 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`. 1008 bool ts_query_cursor_next_capture( 1009 TSQueryCursor *self, 1010 TSQueryMatch *match, 1011 uint32_t *capture_index 1015 * Set the maximum start depth for a query cursor. 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. 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. 1026 * Set to `UINT32_MAX` to remove the maximum start depth. 1028 void ts_query_cursor_set_max_start_depth(TSQueryCursor *self, uint32_t max_start_depth); 1030 /**********************/ 1031 /* Section - Language */ 1032 /**********************/ 1035 * Get another reference to the given language. 1037 const TSLanguage *ts_language_copy(const TSLanguage *self); 1040 * Free any dynamically-allocated resources for this language, if 1041 * this is the last reference. 1043 void ts_language_delete(const TSLanguage *self); 1046 * Get the number of distinct node types in the language. 1048 uint32_t ts_language_symbol_count(const TSLanguage *self); 1051 * Get the number of valid states in this language. 1053 uint32_t ts_language_state_count(const TSLanguage *self); 1056 * Get a node type string for the given numerical id. 1058 const char *ts_language_symbol_name(const TSLanguage *self, TSSymbol symbol); 1061 * Get the numerical id for the given node type string. 1063 TSSymbol ts_language_symbol_for_name( 1064 const TSLanguage *self, 1071 * Get the number of distinct field names in the language. 1073 uint32_t ts_language_field_count(const TSLanguage *self); 1076 * Get the field name string for the given numerical id. 1078 const char *ts_language_field_name_for_id(const TSLanguage *self, TSFieldId id); 1081 * Get the numerical id for the given field name string. 1083 TSFieldId ts_language_field_id_for_name(const TSLanguage *self, const char *name, uint32_t name_length); 1086 * Check whether the given node type id belongs to named nodes, anonymous nodes, 1087 * or a hidden nodes. 1089 * See also [`ts_node_is_named`]. Hidden nodes are never returned from the API. 1091 TSSymbolType ts_language_symbol_type(const TSLanguage *self, TSSymbol symbol); 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 1098 * See also [`ts_parser_set_language`]. 1100 uint32_t ts_language_version(const TSLanguage *self); 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. 1107 TSStateId ts_language_next_state(const TSLanguage *self, TSStateId state, TSSymbol symbol); 1109 /********************************/ 1110 /* Section - Lookahead Iterator */ 1111 /********************************/ 1114 * Create a new lookahead iterator for the given language and parse state. 1116 * This returns `NULL` if state is invalid for the language. 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` 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. 1128 TSLookaheadIterator *ts_lookahead_iterator_new(const TSLanguage *self, TSStateId state); 1131 * Delete a lookahead iterator freeing all the memory used. 1133 void ts_lookahead_iterator_delete(TSLookaheadIterator *self); 1136 * Reset the lookahead iterator to another state. 1138 * This returns `true` if the iterator was reset to the given state and `false` 1141 bool ts_lookahead_iterator_reset_state(TSLookaheadIterator *self, TSStateId state); 1144 * Reset the lookahead iterator. 1146 * This returns `true` if the language was set successfully and `false` 1149 bool ts_lookahead_iterator_reset(TSLookaheadIterator *self, const TSLanguage *language, TSStateId state); 1152 * Get the current language of the lookahead iterator. 1154 const TSLanguage *ts_lookahead_iterator_language(const TSLookaheadIterator *self); 1157 * Advance the lookahead iterator to the next symbol. 1159 * This returns `true` if there is a new symbol and `false` otherwise. 1161 bool ts_lookahead_iterator_next(TSLookaheadIterator *self); 1164 * Get the current symbol of the lookahead iterator; 1166 TSSymbol ts_lookahead_iterator_current_symbol(const TSLookaheadIterator *self); 1169 * Get the current symbol type of the lookahead iterator as a null terminated 1172 const char *ts_lookahead_iterator_current_symbol_name(const TSLookaheadIterator *self); 1174 /*************************************/ 1175 /* Section - WebAssembly Integration */ 1176 /************************************/ 1178 typedef struct wasm_engine_t TSWasmEngine; 1179 typedef struct TSWasmStore TSWasmStore; 1182 TSWasmErrorKindNone = 0, 1183 TSWasmErrorKindParse, 1184 TSWasmErrorKindCompile, 1185 TSWasmErrorKindInstantiate, 1186 TSWasmErrorKindAllocate, 1190 TSWasmErrorKind kind; 1195 * Create a Wasm store. 1197 TSWasmStore *ts_wasm_store_new( 1198 TSWasmEngine *engine, 1203 * Free the memory associated with the given Wasm store. 1205 void ts_wasm_store_delete(TSWasmStore *); 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. 1214 const TSLanguage *ts_wasm_store_load_language( 1223 * Get the number of languages instantiated in the given wasm store. 1225 size_t ts_wasm_store_language_count(const TSWasmStore *); 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. 1231 bool ts_language_is_wasm(const TSLanguage *); 1234 * Assign the given Wasm store to the parser. A parser must have a Wasm store 1235 * in order to use Wasm languages. 1237 void ts_parser_set_wasm_store(TSParser *, TSWasmStore *); 1240 * Remove the parser's current Wasm store and return it. This returns NULL if 1241 * the parser doesn't have a Wasm store. 1243 TSWasmStore *ts_parser_take_wasm_store(TSParser *); 1245 /**********************************/ 1246 /* Section - Global Configuration */ 1247 /**********************************/ 1250 * Set the allocation functions used by the library. 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. 1256 * If you pass `NULL` for any parameter, Tree-sitter will switch back to 1257 * its default implementation of that function. 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. 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 *) 1276 #ifndef TREE_SITTER_HIDE_SYMBOLS 1277 #if defined(__GNUC__) || defined(__clang__) 1278 #pragma GCC visibility pop 1282 #endif // TREE_SITTER_API_H_