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