Rizin
unix-like reverse engineering framework and cli tools
|
Go to the source code of this file.
Classes | |
struct | TSPoint |
struct | TSRange |
struct | TSInput |
struct | TSLogger |
struct | TSInputEdit |
struct | TSNode |
struct | TSTreeCursor |
struct | TSQueryCapture |
struct | TSQueryMatch |
struct | TSQueryPredicateStep |
Macros | |
#define | TREE_SITTER_LANGUAGE_VERSION 14 |
#define | TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION 13 |
Typedefs | |
typedef uint16_t | TSSymbol |
typedef uint16_t | TSFieldId |
typedef struct TSLanguage | TSLanguage |
typedef struct TSParser | TSParser |
typedef struct TSTree | TSTree |
typedef struct TSQuery | TSQuery |
typedef struct TSQueryCursor | TSQueryCursor |
Enumerations | |
enum | TSInputEncoding { TSInputEncodingUTF8 , TSInputEncodingUTF16 } |
enum | TSSymbolType { TSSymbolTypeRegular , TSSymbolTypeAnonymous , TSSymbolTypeAuxiliary } |
enum | TSLogType { TSLogTypeParse , TSLogTypeLex } |
enum | TSQuantifier { TSQuantifierZero = 0 , TSQuantifierZeroOrOne , TSQuantifierZeroOrMore , TSQuantifierOne , TSQuantifierOneOrMore } |
enum | TSQueryPredicateStepType { TSQueryPredicateStepTypeDone , TSQueryPredicateStepTypeCapture , TSQueryPredicateStepTypeString } |
enum | TSQueryError { TSQueryErrorNone = 0 , TSQueryErrorSyntax , TSQueryErrorNodeType , TSQueryErrorField , TSQueryErrorCapture , TSQueryErrorStructure , TSQueryErrorLanguage } |
#define TREE_SITTER_LANGUAGE_VERSION 14 |
The latest ABI version that is supported by the current version of the library. When Languages are generated by the Tree-sitter CLI, they are assigned an ABI version number that corresponds to the current CLI version. The Tree-sitter library is generally backwards-compatible with languages generated using older CLI versions, but is not forwards-compatible.
#define TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION 13 |
typedef struct TSLanguage TSLanguage |
typedef struct TSQueryCursor TSQueryCursor |
enum TSInputEncoding |
enum TSQuantifier |
enum TSQueryError |
enum TSSymbolType |
uint32_t ts_language_field_count | ( | const TSLanguage * | self | ) |
Get the number of distinct field names in the language.
Definition at line 14 of file language.c.
Referenced by ts_language_field_id_for_name(), and ts_language_field_name_for_id().
TSFieldId ts_language_field_id_for_name | ( | const TSLanguage * | self, |
const char * | name, | ||
uint32_t | name_length | ||
) |
Get the numerical id for the given field name string.
Definition at line 119 of file language.c.
References count, i, and ts_language_field_count().
Referenced by ts_node_child_by_field_name(), and ts_query__parse_pattern().
const char* ts_language_field_name_for_id | ( | const TSLanguage * | self, |
TSFieldId | id | ||
) |
Get the field name string for the given numerical id.
Definition at line 107 of file language.c.
References count, id, NULL, and ts_language_field_count().
Referenced by ts_query__analyze_patterns(), and ts_query_cursor__advance().
uint32_t ts_language_symbol_count | ( | const TSLanguage * | self | ) |
Get the number of distinct node types in the language.
Definition at line 6 of file language.c.
Referenced by ts_language_symbol_for_name(), and ts_language_symbol_name().
TSSymbol ts_language_symbol_for_name | ( | const TSLanguage * | self, |
const char * | string, | ||
uint32_t | length, | ||
bool | is_named | ||
) |
Get the numerical id for the given node type string.
Definition at line 74 of file language.c.
References count, i, length, TSSymbolMetadata::named, TSSymbolMetadata::supertype, ts_builtin_sym_error, ts_language_symbol_count(), ts_language_symbol_metadata(), and TSSymbolMetadata::visible.
Referenced by rz_core_cmd_new(), and ts_query__parse_pattern().
const char* ts_language_symbol_name | ( | const TSLanguage * | self, |
TSSymbol | symbol | ||
) |
Get a node type string for the given numerical id.
Definition at line 59 of file language.c.
References NULL, ts_builtin_sym_error, ts_builtin_sym_error_repeat, and ts_language_symbol_count().
Referenced by ts_node_type(), ts_query__analyze_patterns(), ts_stack_print_dot_graph(), ts_subtree__print_dot_graph(), and ts_subtree__write_to_string().
TSSymbolType ts_language_symbol_type | ( | const TSLanguage * | self, |
TSSymbol | symbol | ||
) |
Check whether the given node type id belongs to named nodes, anonymous nodes, or a hidden nodes.
See also ts_node_is_named
. Hidden nodes are never returned from the API.
Definition at line 93 of file language.c.
References TSSymbolMetadata::named, ts_language_symbol_metadata(), TSSymbolTypeAnonymous, TSSymbolTypeAuxiliary, TSSymbolTypeRegular, and TSSymbolMetadata::visible.
Referenced by ts_language_type_is_named_wasm(), and ts_language_type_is_visible_wasm().
uint32_t ts_language_version | ( | const TSLanguage * | self | ) |
Get the ABI version number for this language. This version number is used to ensure that languages were generated by a compatible version of Tree-sitter.
See also ts_parser_set_language
.
Definition at line 10 of file language.c.
Get the node's child at the given index, where zero represents the first child.
Definition at line 492 of file node.c.
References ts_node__child().
Referenced by DEFINE_HANDLE_TS_FCN_AND_SYMBOL(), handle_cmd_substitution_arg(), ts_node_child_by_field_id(), and ts_node_child_wasm().
Get the node's child with the given numerical field id.
You can convert a field name to an id using the ts_language_field_id_for_name
function.
Definition at line 500 of file node.c.
References field_id, TSFieldMapEntry::field_id, TSNode::id, TSFieldMapEntry::inherited, ts_language_field_map(), ts_node__is_relevant(), ts_node__null(), ts_node__subtree(), ts_node_child(), ts_node_child_by_field_id(), ts_node_child_count(), ts_node_child_iterator_next(), ts_node_iterate_children(), and ts_subtree_extra().
Referenced by ts_node_child_by_field_id(), ts_node_child_by_field_id_wasm(), ts_node_child_by_field_name(), and ts_query_cursor__advance().
TSNode ts_node_child_by_field_name | ( | TSNode | self, |
const char * | field_name, | ||
uint32_t | field_name_length | ||
) |
Get the node's child with the given field name.
Definition at line 589 of file node.c.
References field_id, ts_language_field_id_for_name(), and ts_node_child_by_field_id().
Referenced by DEFINE_HANDLE_TS_FCN_AND_SYMBOL(), parse_declaration_node(), parse_enum_node(), parse_parameter_declaration_node(), parse_struct_node(), parse_type_abstract_declarator_node(), parse_type_declarator_node(), parse_type_descriptor_single(), parse_typedef_node(), and parse_union_node().
Get the node's number of children.
Definition at line 602 of file node.c.
References Subtree::ptr, ts_node__subtree(), ts_subtree_child_count(), and SubtreeHeapData::visible_child_count.
Referenced by ts_node__first_child_for_byte(), ts_node_child_by_field_id(), ts_node_child_count_wasm(), and ts_node_children_wasm().
Get the smallest node within this node that spans the given range of bytes or (row, column) positions.
Definition at line 644 of file node.c.
References test_evm::end, start, and ts_node__descendant_for_byte_range().
Referenced by ts_node_descendant_for_index_wasm().
Definition at line 660 of file node.c.
References test_evm::end, start, and ts_node__descendant_for_point_range().
Referenced by ts_node_descendant_for_position_wasm().
void ts_node_edit | ( | TSNode * | self, |
const TSInputEdit * | edit | ||
) |
Edit the node to keep it in-sync with source code that has been edited.
This function is only rarely needed. When you edit a syntax tree with the ts_tree_edit
function, all of the nodes that you retrieve from the tree afterward will already reflect the edit. You only need to use ts_node_edit
when you have a TSNode
instance that you want to keep and continue to use after an edit.
Definition at line 676 of file node.c.
References TSPoint::column, TSInputEdit::new_end_byte, TSInputEdit::new_end_point, TSInputEdit::old_end_byte, TSInputEdit::old_end_point, point_add(), point_sub(), TSPoint::row, TSInputEdit::start_byte, ts_node_start_byte(), and ts_node_start_point().
Get the node's end byte.
Definition at line 406 of file node.c.
References Length::bytes, ts_node__subtree(), ts_node_start_byte(), and ts_subtree_size().
Referenced by create_cmd_edit(), DEFINE_HANDLE_TS_FCN_AND_SYMBOL(), find_autocmplt_type(), find_autocmplt_type_at_stmt(), find_autocmplt_type_at_stmt_op(), find_autocmplt_type_quoted_arg(), get_cd_from_cmdid(), ts_node__first_child_for_byte(), ts_node__next_sibling(), ts_node__prev_sibling(), ts_node_end_index_wasm(), ts_node_parent(), ts_query_cursor__advance(), ts_query_cursor__compare_nodes(), ts_query_cursor__first_in_progress_capture(), ts_query_cursor_next_capture(), and ts_tree_cursor_end_index_wasm().
Get the node's end position in terms of rows and columns.
Definition at line 410 of file node.c.
References point_add(), ts_node__subtree(), ts_node_start_point(), and ts_subtree_size().
Referenced by create_cmd_edit(), ts_node_descendants_of_type_wasm(), ts_node_end_point_wasm(), ts_query_cursor__advance(), ts_query_cursor__first_in_progress_capture(), and ts_tree_cursor_end_position_wasm().
Check if two nodes are identical.
Definition at line 430 of file node.c.
References TSNode::id, and TSNode::tree.
Get the field name for node's child at the given index, where zero represents the first child. Returns NULL, if no field is found.
Definition at line 572 of file node.c.
References i, NULL, ts_language_field_map(), and ts_node__subtree().
Get the node's first child that extends beyond the given byte offset.
Definition at line 636 of file node.c.
References ts_node__first_child_for_byte().
Get the node's first named child that extends beyond the given byte offset.
Definition at line 640 of file node.c.
References ts_node__first_child_for_byte().
Check if a syntax node has been edited.
Definition at line 453 of file node.c.
References ts_node__subtree(), and ts_subtree_has_changes().
Referenced by ts_node_has_changes_wasm().
Check if the node is a syntax error or contains any syntax errors.
Definition at line 457 of file node.c.
References ts_node__subtree(), and ts_subtree_error_cost().
Referenced by core_cmd_tsrzcmd(), find_autocmplt_type_quoted_arg(), substitute_args_do(), and ts_node_has_error_wasm().
Check if the node is extra. Extra nodes represent things like comments, which are not required the grammar, but can appear anywhere.
Definition at line 438 of file node.c.
References ts_node__subtree(), and ts_subtree_extra().
Check if the node is missing. Missing nodes are inserted by the parser in order to recover from certain kinds of syntax errors.
Definition at line 449 of file node.c.
References ts_node__subtree(), and ts_subtree_missing().
Referenced by ts_node_is_missing_wasm(), and ts_tree_cursor_current_node_is_missing_wasm().
Check if the node is named. Named nodes correspond to named rules in the grammar, whereas anonymous nodes correspond to string literals in the grammar.
Definition at line 442 of file node.c.
References TSSymbolMetadata::named, ts_language_symbol_metadata(), ts_node__alias(), ts_node__subtree(), and ts_subtree_named().
Referenced by parse_declaration_node(), parse_enum_node(), parse_parameter_declaration_node(), parse_parameter_list(), parse_primitive_type(), parse_sized_primitive_type(), parse_sole_type_name(), parse_struct_node(), parse_type_abstract_declarator_node(), parse_type_declarator_node(), parse_type_descriptor_single(), parse_type_node_single(), parse_type_nodes_save(), parse_typedef_node(), parse_union_node(), ts_node_is_named_wasm(), ts_node_named_children_wasm(), ts_query_cursor__advance(), and ts_tree_cursor_current_node_is_named_wasm().
Check if the node is null. Functions like ts_node_child
and ts_node_next_sibling
will return a null node to indicate that no such node was found.
Definition at line 434 of file node.c.
Referenced by DEFINE_HANDLE_TS_FCN_AND_SYMBOL(), find_autocmplt_type_arg_identifier(), find_autocmplt_type_at_stmt_op(), get_arg_number(), get_arg_parent(), get_cd_from_arg(), guess_next_autocmplt_token(), handle_cmd_substitution_arg(), is_arg_identifier_in_tmp_stmt(), node_malformed_error(), parse_args(), parse_declaration_node(), parse_enum_node(), parse_parameter_declaration_node(), parse_parameter_list(), parse_primitive_type(), parse_sized_primitive_type(), parse_sole_type_name(), parse_struct_node(), parse_type_abstract_declarator_node(), parse_type_declarator_node(), parse_type_descriptor_single(), parse_type_node_single(), parse_type_nodes_save(), parse_typedef_node(), parse_union_node(), ts_node__next_sibling(), ts_node__prev_sibling(), and ts_query_cursor__advance().
Get the node's named child at the given index.
See also ts_node_is_named
.
Definition at line 496 of file node.c.
References ts_node__child().
Referenced by DEFINE_HANDLE_TS_FCN(), DEFINE_HANDLE_TS_FCN_AND_SYMBOL(), do_handle_ts_unescape_arg(), do_iter_sections(), get_cd_from_arg(), handle_substitution_args(), iter_dbt_commands(), iter_offsets_common(), parse_args(), parse_declaration_node(), parse_enum_node(), parse_parameter_declaration_node(), parse_parameter_list(), parse_struct_node(), parse_type_abstract_declarator_node(), parse_type_declarator_node(), parse_type_descriptor_single(), parse_typedef_node(), parse_union_node(), rz_type_parse_string_declaration_single(), rz_type_parse_string_single(), substitute_args_do(), ts_node_handle_arg_prargs(), ts_node_named_child_wasm(), and type_parse_string().
Get the node's number of named children.
See also ts_node_is_named
.
Definition at line 611 of file node.c.
References SubtreeHeapData::named_child_count, Subtree::ptr, ts_node__subtree(), and ts_subtree_child_count().
Referenced by DEFINE_HANDLE_TS_FCN(), DEFINE_HANDLE_TS_FCN_AND_SYMBOL(), do_handle_ts_unescape_arg(), find_autocmplt_type(), handle_substitution_args(), iter_offsets_common(), parse_args(), parse_declaration_node(), parse_enum_node(), parse_parameter_list(), parse_struct_node(), parse_type_abstract_declarator_node(), parse_type_declarator_node(), parse_type_descriptor_single(), parse_typedef_node(), parse_union_node(), rz_type_parse_string_declaration_single(), rz_type_parse_string_single(), ts_node_named_child_count_wasm(), ts_node_named_children_wasm(), and type_parse_string().
Get the smallest named node within this node that spans the given range of bytes or (row, column) positions.
Definition at line 652 of file node.c.
References test_evm::end, start, and ts_node__descendant_for_byte_range().
Referenced by guess_next_autocmplt_token(), and ts_node_named_descendant_for_index_wasm().
Definition at line 668 of file node.c.
References test_evm::end, start, and ts_node__descendant_for_point_range().
Referenced by ts_node_named_descendant_for_position_wasm().
Get the node's next / previous named sibling.
Definition at line 624 of file node.c.
References ts_node__next_sibling().
Referenced by parse_struct_node(), parse_union_node(), and ts_node_next_named_sibling_wasm().
Get the node's next / previous sibling.
Definition at line 620 of file node.c.
References ts_node__next_sibling().
Referenced by ts_node_next_sibling_wasm().
Get the node's immediate parent.
Definition at line 461 of file node.c.
References TSNode::id, ts_node__is_relevant(), ts_node__null(), ts_node_child_iterator_next(), ts_node_end_byte(), ts_node_iterate_children(), ts_node_start_byte(), and ts_tree_root_node().
Referenced by get_arg_number(), get_arg_parent(), get_cd_from_arg(), handle_cmd_substitution_arg(), ts_node__next_sibling(), ts_node__prev_sibling(), and ts_node_parent_wasm().
Definition at line 632 of file node.c.
References ts_node__prev_sibling().
Referenced by ts_node_prev_named_sibling_wasm().
Definition at line 628 of file node.c.
References ts_node__prev_sibling().
Referenced by get_arg_number(), and ts_node_prev_sibling_wasm().
Get the node's start byte.
Definition at line 36 of file node.c.
Referenced by create_cmd_edit(), DEFINE_HANDLE_TS_FCN_AND_SYMBOL(), find_autocmplt_type(), find_autocmplt_type_at_stmt(), find_autocmplt_type_at_stmt_op(), find_autocmplt_type_newcmd_or_arg(), find_autocmplt_type_quoted_arg(), get_cd_from_cmdid(), ts_node__descendant_for_byte_range(), ts_node__next_sibling(), ts_node_edit(), ts_node_end_byte(), ts_node_iterate_children(), ts_node_parent(), ts_node_start_index_wasm(), ts_node_sub_parent_string(), ts_query_cursor__advance(), ts_query_cursor__compare_nodes(), ts_query_cursor__first_in_progress_capture(), ts_query_cursor_next_capture(), and ts_tree_cursor_start_index_wasm().
Get the node's start position in terms of rows and columns.
Definition at line 40 of file node.c.
Referenced by create_cmd_edit(), ts_node__descendant_for_point_range(), ts_node_descendants_of_type_wasm(), ts_node_edit(), ts_node_end_point(), ts_node_iterate_children(), ts_node_start_point_wasm(), ts_query_cursor__advance(), and ts_tree_cursor_start_position_wasm().
char* ts_node_string | ( | TSNode | self | ) |
Get an S-expression representing the node as a string.
This string is allocated with malloc
and the caller is responsible for freeing it using free
.
Definition at line 426 of file node.c.
References ts_node__subtree(), and ts_subtree_string().
Referenced by core_cmd_tsrzcmd(), node_malformed_error(), parse_enum_node(), parse_struct_node(), parse_typedef_node(), parse_union_node(), rz_type_parse_string_declaration_single(), rz_type_parse_string_single(), ts_node_to_string_wasm(), and type_parse_string().
Get the node's type as a numerical id.
Definition at line 414 of file node.c.
References ts_language_public_symbol(), ts_node__alias(), ts_node__subtree(), and ts_subtree_symbol().
Referenced by handle_ts_stmt(), ts_node_descendants_of_type_wasm(), ts_node_symbol_wasm(), ts_query_cursor__advance(), and ts_tree_cursor_current_node_type_id_wasm().
Get the node's type as a null-terminated string.
Definition at line 420 of file node.c.
References ts_language_symbol_name(), ts_node__alias(), ts_node__subtree(), and ts_subtree_symbol().
Referenced by find_autocmplt_type(), find_autocmplt_type_arg_identifier(), find_autocmplt_type_at_stmt(), find_autocmplt_type_at_stmt_op(), find_autocmplt_type_newcmd_or_arg(), find_autocmplt_type_quoted_arg(), get_arg_number(), get_arg_parent(), get_cd_from_arg(), handle_ts_stmt(), is_arg_identifier_in_tmp_stmt(), parse_declaration_node(), parse_enum_node(), parse_parameter_declaration_node(), parse_parameter_list(), parse_primitive_type(), parse_sized_primitive_type(), parse_sole_type_name(), parse_struct_node(), parse_type_abstract_declarator_node(), parse_type_declarator_node(), parse_type_descriptor_single(), parse_type_node_single(), parse_type_nodes_save(), parse_typedef_node(), parse_union_node(), rz_type_parse_string_declaration_single(), rz_type_parse_string_single(), ts_query_cursor__advance(), ts_query_cursor__capture(), and type_parse_string().
void ts_parser_delete | ( | TSParser * | parser | ) |
Delete the parser, freeing all of the memory that it used.
Definition at line 1725 of file parser.c.
References array_delete, NULL, NULL_SUBTREE, reusable_node_delete(), ts_free, ts_lexer_delete(), ts_parser__set_cached_token(), ts_parser_set_language(), ts_stack_delete(), ts_subtree_pool_delete(), and ts_subtree_release().
Referenced by core_cmd_tsrzcmd(), guess_data_free(), guess_next_autocmplt_token(), rz_type_parse_string_declaration_single(), rz_type_parse_string_single(), and type_parse_string().
Get the ranges of text that the parser will include when parsing.
The returned pointer is owned by the parser. The caller should not free it or write to it. The length of the array will be written to the given length
pointer.
Definition at line 1819 of file parser.c.
References count, and ts_lexer_included_ranges().
const TSLanguage* ts_parser_language | ( | const TSParser * | self | ) |
TSParser* ts_parser_new | ( | void | ) |
Create a new parser.
Definition at line 1704 of file parser.c.
References array_init, array_new, array_reserve, clock_null(), NULL, NULL_SUBTREE, reusable_node_new(), ts_calloc, ts_lexer_init(), ts_parser__set_cached_token(), ts_stack_new(), and ts_subtree_pool_new().
Referenced by core_cmd_tsrzcmd(), guess_next_autocmplt_token(), rz_type_parse_string_declaration_single(), rz_type_parse_string_single(), ts_parser_new_wasm(), and type_parse_string().
Use the parser to parse some source code and create a syntax tree.
If you are parsing this document for the first time, pass NULL
for the old_tree
parameter. Otherwise, if you have already parsed an earlier version of this document and the document has since been edited, pass the previous syntax tree so that the unchanged parts of it can be reused. This will save time and memory. For this to work correctly, you must have already edited the old syntax tree using the ts_tree_edit
function in a way that exactly matches the source code changes.
The TSInput
parameter lets you specify how to read the text. It has the following three fields:
read
: A function to retrieve a chunk of text at a given byte offset and (row, column) position. The function should return a pointer to the text and write its length to the bytes_read
pointer. The parser does not take ownership of this buffer; it just borrows it until it has finished reading it. The function should write a zero value to the bytes_read
pointer to indicate the end of the document.payload
: An arbitrary pointer that will be passed to each invocation of the read
function.encoding
: An indication of how the text is encoded. Either TSInputEncodingUTF8
or TSInputEncodingUTF16
.This function returns a syntax tree on success, and NULL
on failure. There are three possible reasons for failure:
ts_parser_language
function.ts_parser_set_timeout_micros
function. You can resume parsing from where the parser left out by calling ts_parser_parse
again with the same arguments. Or you can start parsing from scratch by first calling ts_parser_reset
.ts_parser_set_cancellation_flag
. You can resume parsing from where the parser left out by calling ts_parser_parse
again with the same arguments. Definition at line 1844 of file parser.c.
References array_clear, Length::bytes, clock_after(), clock_now(), clock_null(), TSPoint::column, Length::extent, i, TSTree::included_range_count, TSTree::included_ranges, input(), LOG, LOG_STACK, LOG_TREE, NULL, NULL_SUBTREE, capstone::range, reusable_node_clear(), reusable_node_reset(), TSTree::root, TSPoint::row, ts_lexer_set_input(), ts_parser__advance(), ts_parser__condense_stack(), ts_parser_has_outstanding_parse(), ts_parser_reset(), ts_range_array_get_changed_ranges(), ts_stack_is_active(), ts_stack_position(), ts_stack_state(), ts_stack_version_count(), ts_subtree_balance(), ts_subtree_error_cost(), ts_subtree_retain(), and ts_tree_new().
Referenced by ts_parser_parse_string_encoding(), and ts_parser_parse_wasm().
TSTree* ts_parser_parse_string | ( | TSParser * | self, |
const TSTree * | old_tree, | ||
const char * | string, | ||
uint32_t | length | ||
) |
Use the parser to parse some source code stored in one contiguous buffer. The first two parameters are the same as in the ts_parser_parse
function above. The second two parameters indicate the location of the buffer and its length in bytes.
Definition at line 1945 of file parser.c.
References length, ts_parser_parse_string_encoding(), and TSInputEncodingUTF8.
Referenced by apply_edits(), core_cmd_tsrzcmd(), guess_next_autocmplt_token(), rz_type_parse_string_declaration_single(), rz_type_parse_string_single(), and type_parse_string().
TSTree* ts_parser_parse_string_encoding | ( | TSParser * | self, |
const TSTree * | old_tree, | ||
const char * | string, | ||
uint32_t | length, | ||
TSInputEncoding | encoding | ||
) |
Use the parser to parse some source code stored in one contiguous buffer with a given encoding. The first four parameters work the same as in the ts_parser_parse_string
method above. The final parameter indicates whether the text is encoded as UTF8 or UTF16.
Definition at line 1954 of file parser.c.
References cmd_descs_generate::encoding, input(), length, ts_parser_parse(), and ts_string_input_read().
Referenced by ts_parser_parse_string().
Set the file descriptor to which the parser should write debugging graphs during parsing. The graphs are formatted in the DOT language. You may want to pipe these graphs directly to a dot(1)
process in order to generate SVG output. You can turn off this logging by passing a negative number.
Definition at line 1783 of file parser.c.
void ts_parser_reset | ( | TSParser * | self | ) |
Instruct the parser to start the next parse from the beginning.
If the parser previously failed because of a timeout or a cancellation, then by default, it will resume where it left off on the next call to ts_parser_parse
or other parsing functions. If you don't want to resume, and instead intend to use this parser to parse some other document, you must call ts_parser_reset
first.
Definition at line 1823 of file parser.c.
References length_zero(), NULL, NULL_SUBTREE, reusable_node_clear(), ts_lexer_reset(), ts_parser__set_cached_token(), ts_stack_clear(), and ts_subtree_release().
Referenced by ts_parser_parse(), and ts_parser_set_language().
Set the parser's current cancellation flag pointer.
If a non-null pointer is assigned, then the parser will periodically read from this pointer during parsing. If it reads a non-zero value, it will halt early, returning NULL. See ts_parser_parse
for more information.
Set the ranges of text that the parser should include when parsing.
By default, the parser will always include entire documents. This function allows you to parse only a portion of a document but still return a syntax tree whose ranges match up with the document as a whole. You can also pass multiple disjoint ranges.
The second and third parameters specify the location and length of an array of ranges. The parser does not take ownership of these ranges; it copies the data, so it doesn't matter how these ranges are allocated.
If length
is zero, then the entire document will be parsed. Otherwise, the given ranges must be ordered from earliest to latest in the document, and they must not overlap. That is, the following must hold for all i
< length - 1
: ranges[i].end_byte <= ranges[i + 1].start_byte
If this requirement is not satisfied, the operation will fail, the ranges will not be assigned, and this function will return false
. On success, this function returns true
Definition at line 1811 of file parser.c.
References count, and ts_lexer_set_included_ranges().
Referenced by ts_parser_parse_wasm().
bool ts_parser_set_language | ( | TSParser * | self, |
const TSLanguage * | language | ||
) |
Set the language that the parser should use for parsing.
Returns a boolean indicating whether or not the language was successfully assigned. True means assignment succeeded. False means there was a version mismatch: the language was generated with an incompatible version of the Tree-sitter CLI. Check the language's version using ts_language_version
and compare it to this library's TREE_SITTER_LANGUAGE_VERSION
and TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION
constants.
Definition at line 1754 of file parser.c.
References TSLanguage::create, TSLanguage::external_scanner, NULL, TREE_SITTER_LANGUAGE_VERSION, TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION, ts_parser_reset(), and TSLanguage::version.
Referenced by core_cmd_tsrzcmd(), guess_next_autocmplt_token(), rz_type_parse_string_declaration_single(), rz_type_parse_string_single(), ts_parser_delete(), and type_parse_string().
Set the logger that a parser should use during parsing.
The parser does not take ownership over the logger payload. If a logger was previously assigned, the caller is responsible for releasing any memory owned by the previous logger.
Definition at line 1779 of file parser.c.
Referenced by ts_parser_enable_logger_wasm().
Set the maximum duration in microseconds that parsing should be allowed to take before halting.
If parsing takes longer than this, it will halt early, returning NULL. See ts_parser_parse
for more information.
Definition at line 1807 of file parser.c.
References duration_from_micros().
Get the duration in microseconds that parsing is allowed to take.
Definition at line 1803 of file parser.c.
References duration_to_micros().
Get the name and length of one of the query's captures, or one of the query's string literals. Each capture and string is associated with a numeric id based on the order that it appeared in the query's source.
Definition at line 2709 of file query.c.
References length, and symbol_table_name_for_id().
TSQuantifier ts_query_capture_quantifier_for_id | ( | const TSQuery * | self, |
uint32_t | pattern_id, | ||
uint32_t | capture_id | ||
) |
Get the quantifier of the query's captures. Each capture is * associated with a numeric id based on the order that it appeared in the query's source.
Definition at line 2717 of file query.c.
References array_get, and capture_quantifier_for_id().
void ts_query_cursor_delete | ( | TSQueryCursor * | self | ) |
Delete a query cursor, freeing all of the memory that it used.
Definition at line 2839 of file query.c.
References array_delete, capture_list_pool_delete(), ts_free, and ts_tree_cursor_delete().
bool ts_query_cursor_did_exceed_match_limit | ( | const TSQueryCursor * | self | ) |
Manage the maximum number of in-progress matches allowed by this query cursor.
Query cursors have an optional maximum capacity for storing lists of in-progress captures. If this capacity is exceeded, then the earliest-starting match will silently be dropped to make room for further matches. This maximum capacity is optional — by default, query cursors allow any number of pending matches, dynamically allocating new space for them as needed as the query is executed.
Definition at line 2847 of file query.c.
Referenced by ts_query_captures_wasm(), and ts_query_matches_wasm().
void ts_query_cursor_exec | ( | TSQueryCursor * | self, |
const TSQuery * | query, | ||
TSNode | node | ||
) |
Start running a given query on a given node.
Definition at line 2859 of file query.c.
References array_clear, capture_list_pool_reset(), and ts_tree_cursor_reset().
Referenced by ts_query_captures_wasm(), and ts_query_matches_wasm().
uint32_t ts_query_cursor_match_limit | ( | const TSQueryCursor * | self | ) |
TSQueryCursor* ts_query_cursor_new | ( | void | ) |
Create a new cursor for executing a given query.
The cursor stores the state that is needed to iteratively search for matches. To use the query cursor, first call ts_query_cursor_exec
to start running a given query on a given syntax node. Then, there are two options for consuming the results of the query:
ts_query_cursor_next_match
to iterate over all of the matches in the order that they were found. Each match contains the index of the pattern that matched, and an array of captures. Because multiple patterns can match the same set of nodes, one match may contain captures that appear before some of the captures from a previous match.ts_query_cursor_next_capture
to iterate over all of the individual captures in the order that they appear. This is useful if don't care about which pattern matched, and just want a single ordered sequence of captures.If you don't care about consuming all of the results, you can stop calling ts_query_cursor_next_match
or ts_query_cursor_next_capture
at any point. You can then start executing another query on another node by calling ts_query_cursor_exec
again.
Definition at line 2820 of file query.c.
References array_new, array_reserve, capture_list_pool_new(), POINT_MAX, ts_malloc, and UINT32_MAX.
Referenced by ts_query_captures_wasm(), and ts_query_matches_wasm().
bool ts_query_cursor_next_capture | ( | TSQueryCursor * | self, |
TSQueryMatch * | match, | ||
uint32_t * | capture_index | ||
) |
Advance to the next capture of the currently running query.
If there is a capture, write its match to *match
and its index within the matche's capture list to *capture_index
. Otherwise, return false
.
Definition at line 3746 of file query.c.
References array_erase, capture_list_pool_get(), capture_list_pool_is_empty(), capture_list_pool_release(), i, LOG, NULL, ts_node_end_byte(), ts_node_start_byte(), ts_query_cursor__advance(), ts_query_cursor__first_in_progress_capture(), and UINT32_MAX.
Referenced by ts_query_captures_wasm().
bool ts_query_cursor_next_match | ( | TSQueryCursor * | self, |
TSQueryMatch * | match | ||
) |
Advance to the next match of the currently running query.
If there is a match, write it to *match
and return true
. Otherwise, return false
.
Definition at line 3690 of file query.c.
References array_erase, capture_list_pool_get(), capture_list_pool_release(), ts_query_cursor__advance(), and UINT32_MAX.
Referenced by ts_query_matches_wasm().
void ts_query_cursor_remove_match | ( | TSQueryCursor * | self, |
uint32_t | id | ||
) |
Definition at line 3715 of file query.c.
References array_erase, capture_list_pool_release(), and i.
void ts_query_cursor_set_byte_range | ( | TSQueryCursor * | self, |
uint32_t | start_byte, | ||
uint32_t | end_byte | ||
) |
Set the range of bytes or (row, column) positions in which the query will be executed.
Definition at line 2876 of file query.c.
References UINT32_MAX.
void ts_query_cursor_set_match_limit | ( | TSQueryCursor * | self, |
uint32_t | limit | ||
) |
Definition at line 2855 of file query.c.
References limit.
Referenced by ts_query_captures_wasm(), and ts_query_matches_wasm().
void ts_query_cursor_set_point_range | ( | TSQueryCursor * | self, |
TSPoint | start_point, | ||
TSPoint | end_point | ||
) |
Definition at line 2888 of file query.c.
References TSPoint::column, POINT_MAX, and TSPoint::row.
Referenced by ts_query_captures_wasm(), and ts_query_matches_wasm().
void ts_query_delete | ( | TSQuery * | self | ) |
Delete a query, freeing all of the memory that it used.
Definition at line 2677 of file query.c.
References array_delete, array_get, capture_quantifiers_delete(), symbol_table_delete(), and ts_free.
Referenced by ts_query_new().
Disable a certain capture within a query.
This prevents the capture from being returned in matches, and also avoids any resource usage associated with recording the capture. Currently, there is no way to undo this.
Definition at line 2785 of file query.c.
References i, length, query_step__remove_capture(), step(), and symbol_table_id_for_name().
Disable a certain pattern within a query.
This prevents the pattern from matching and removes most of the overhead associated with the pattern. Currently, there is no way to undo this.
Definition at line 2801 of file query.c.
References array_erase, i, and PatternEntry.
Definition at line 2754 of file query.c.
References StepOffset::byte_offset, i, StepOffset::step_index, and UINT32_MAX.
TSQuery* ts_query_new | ( | const TSLanguage * | language, |
const char * | source, | ||
uint32_t | source_len, | ||
uint32_t * | error_offset, | ||
TSQueryError * | error_type | ||
) |
Create a new query from a string containing one or more S-expression patterns. The query is associated with a particular language, and can only be run on syntax nodes parsed with that language.
If all of the given patterns are valid, this returns a TSQuery
. If a pattern is invalid, this returns NULL
, and provides two pieces of information about the problem:
error_offset
parameter.error_type
parameter. Definition at line 2546 of file query.c.
References array_back, array_new, array_push, capture_quantifiers_delete(), capture_quantifiers_new(), QueryStep::depth, Slice::length, NONE, NULL, PARENT_DONE, PATTERN_DONE_MARKER, PatternEntry, QueryPattern::predicate_steps, query_step__new(), source, step(), QueryPattern::steps, stream_new(), stream_offset(), stream_skip_whitespace(), QueryStep::symbol, symbol_table_new(), TREE_SITTER_LANGUAGE_VERSION, TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION, ts_malloc, ts_query__parse_pattern(), ts_query__pattern_map_insert(), ts_query_delete(), TSQueryErrorLanguage, TSQueryErrorSyntax, TSLanguage::version, and WILDCARD_SYMBOL.
const TSQueryPredicateStep* ts_query_predicates_for_pattern | ( | const TSQuery * | self, |
uint32_t | pattern_index, | ||
uint32_t * | length | ||
) |
Get all of the predicates for the given pattern in the query.
The predicates are represented as a single array of steps. There are three types of steps in this array, which correspond to the three legal values for the type
field:
TSQueryPredicateStepTypeCapture
- Steps with this type represent names of captures. Their value_id
can be used with the ts_query_capture_name_for_id
function to obtain the name of the capture.TSQueryPredicateStepTypeString
- Steps with this type represent literal strings. Their value_id
can be used with the ts_query_string_value_for_id
function to obtain their string value.TSQueryPredicateStepTypeDone
- Steps with this type are sentinels that represent the end of an individual predicate. If a pattern has two predicates, then there will be two steps with this type
in the array. Definition at line 2734 of file query.c.
References Slice::length, NULL, and Slice::offset.
Definition at line 2726 of file query.c.
References length, and symbol_table_name_for_id().
void ts_set_allocator | ( | void *(*)(size_t) | new_malloc, |
void *(*)(size_t, size_t) | new_calloc, | ||
void *(*)(void *, size_t) | new_realloc, | ||
void(*)(void *) | new_free | ||
) |
Set the allocation functions used by the library.
By default, Tree-sitter uses the standard libc allocation functions, but aborts the process when an allocation fails. This function lets you supply alternative allocation functions at runtime.
If you pass NULL
for any parameter, Tree-sitter will switch back to its default implementation of that function.
If you call this function after the library has already been used, then you must ensure that either:
Definition at line 37 of file alloc.c.
References free(), ts_calloc_default(), ts_current_calloc, ts_current_free, ts_current_malloc, ts_current_realloc, ts_malloc_default(), and ts_realloc_default().
Create a shallow copy of the syntax tree. This is very fast.
You need to copy a syntax tree in order to use it on more than one thread at a time, as syntax trees are not thread safe.
Definition at line 21 of file tree.c.
References ts_subtree_retain(), and ts_tree_new().
TSTreeCursor ts_tree_cursor_copy | ( | const TSTreeCursor * | _cursor | ) |
Definition at line 479 of file tree_cursor.c.
References array_init, array_push_all, NULL, and TreeCursor::tree.
TSFieldId ts_tree_cursor_current_field_id | ( | const TSTreeCursor * | _self | ) |
Get the field id of the tree cursor's current node.
This returns zero if the current node doesn't have a field. See also ts_node_child_by_field_id
, ts_language_field_id_for_name
.
Definition at line 431 of file tree_cursor.c.
References i, SubtreeHeapData::production_id, Subtree::ptr, TreeCursorEntry::subtree, ts_language_alias_at(), ts_language_field_map(), ts_subtree_extra(), and ts_subtree_visible().
Referenced by ts_tree_cursor_current_field_id_wasm(), and ts_tree_cursor_current_field_name().
const char* ts_tree_cursor_current_field_name | ( | const TSTreeCursor * | _self | ) |
Get the field name of the tree cursor's current node.
This returns NULL
if the current node doesn't have a field. See also ts_node_child_by_field_name
.
Definition at line 469 of file tree_cursor.c.
References id, NULL, TSTreeCursor::tree, and ts_tree_cursor_current_field_id().
TSNode ts_tree_cursor_current_node | ( | const TSTreeCursor * | _self | ) |
Get the tree cursor's current node.
Definition at line 262 of file tree_cursor.c.
References array_back, TreeCursorEntry::position, SubtreeHeapData::production_id, Subtree::ptr, TreeCursorEntry::structural_child_index, TreeCursorEntry::subtree, ts_language_alias_at(), ts_node_new(), and ts_subtree_extra().
Referenced by ts_node_children_wasm(), ts_node_descendants_of_type_wasm(), ts_node_named_children_wasm(), ts_query_cursor__advance(), ts_tree_cursor_current_node_id_wasm(), ts_tree_cursor_current_node_is_missing_wasm(), ts_tree_cursor_current_node_is_named_wasm(), ts_tree_cursor_current_node_type_id_wasm(), ts_tree_cursor_current_node_wasm(), ts_tree_cursor_end_index_wasm(), ts_tree_cursor_end_position_wasm(), ts_tree_cursor_start_index_wasm(), and ts_tree_cursor_start_position_wasm().
void ts_tree_cursor_delete | ( | TSTreeCursor * | _self | ) |
Delete a tree cursor, freeing all of the memory that it used.
Definition at line 94 of file tree_cursor.c.
References array_delete.
Referenced by ts_query_cursor_delete(), and ts_tree_cursor_delete_wasm().
bool ts_tree_cursor_goto_first_child | ( | TSTreeCursor * | _self | ) |
Move the cursor to the first child of its current node.
This returns true
if the cursor successfully moved, and returns false
if there were no children.
Definition at line 101 of file tree_cursor.c.
References array_push, ts_subtree_visible_child_count(), ts_tree_cursor_child_iterator_next(), and ts_tree_cursor_iterate_children().
Referenced by ts_node_children_wasm(), ts_node_descendants_of_type_wasm(), ts_node_named_children_wasm(), ts_query_cursor__advance(), ts_tree_cursor_goto_first_child_wasm(), and ts_tree_cursor_goto_next_sibling().
int64_t ts_tree_cursor_goto_first_child_for_byte | ( | TSTreeCursor * | _self, |
uint32_t | goal_byte | ||
) |
Move the cursor to the first child of its current node that extends beyond the given byte offset or point.
This returns the index of the child node if one was found, and returns -1 if no such child was found.
Definition at line 128 of file tree_cursor.c.
References array_push, Length::bytes, ts_subtree_size(), ts_subtree_visible_child_count(), ts_tree_cursor_child_iterator_next(), and ts_tree_cursor_iterate_children().
int64_t ts_tree_cursor_goto_first_child_for_point | ( | TSTreeCursor * | _self, |
TSPoint | goal_point | ||
) |
Definition at line 168 of file tree_cursor.c.
References array_push, Length::extent, point_add(), point_gte(), ts_subtree_size(), ts_subtree_visible_child_count(), ts_tree_cursor_child_iterator_next(), and ts_tree_cursor_iterate_children().
bool ts_tree_cursor_goto_next_sibling | ( | TSTreeCursor * | _self | ) |
Move the cursor to the next sibling of its current node.
This returns true
if the cursor successfully moved, and returns false
if there was no next sibling node.
Definition at line 206 of file tree_cursor.c.
References array_pop, array_push, ts_subtree_visible_child_count(), ts_tree_cursor_child_iterator_next(), ts_tree_cursor_goto_first_child(), and ts_tree_cursor_iterate_children().
Referenced by ts_node_children_wasm(), ts_node_descendants_of_type_wasm(), ts_node_named_children_wasm(), ts_query_cursor__advance(), and ts_tree_cursor_goto_next_sibling_wasm().
bool ts_tree_cursor_goto_parent | ( | TSTreeCursor * | _self | ) |
Move the cursor to the parent of its current node.
This returns true
if the cursor successfully moved, and returns false
if there was no parent node (the cursor was already on the root node).
Definition at line 239 of file tree_cursor.c.
References i, SubtreeHeapData::production_id, Subtree::ptr, entry::size, TreeCursorEntry::subtree, ts_language_alias_at(), ts_subtree_extra(), and ts_subtree_visible().
Referenced by ts_node_descendants_of_type_wasm(), ts_query_cursor__advance(), and ts_tree_cursor_goto_parent_wasm().
TSTreeCursor ts_tree_cursor_new | ( | TSNode | node | ) |
Create a new tree cursor starting from the given node.
A tree cursor allows you to walk a syntax tree more efficiently than is possible using the TSNode
functions. It is a mutable object that is always on a certain syntax node, and can be moved imperatively to different nodes.
Definition at line 70 of file tree_cursor.c.
References NULL, and ts_tree_cursor_init().
Referenced by ts_tree_cursor_new_wasm().
void ts_tree_cursor_reset | ( | TSTreeCursor * | _self, |
TSNode | node | ||
) |
Re-initialize a tree cursor to start at a different node.
Definition at line 76 of file tree_cursor.c.
References ts_tree_cursor_init().
Referenced by ts_node_children_wasm(), ts_node_descendants_of_type_wasm(), ts_node_named_children_wasm(), ts_query_cursor_exec(), and ts_tree_cursor_reset_wasm().
void ts_tree_delete | ( | TSTree * | self | ) |
Delete the syntax tree, freeing all of the memory that it used.
Definition at line 26 of file tree.c.
References ts_free, ts_subtree_pool_delete(), ts_subtree_pool_new(), and ts_subtree_release().
Referenced by core_cmd_tsrzcmd(), guess_data_free(), guess_next_autocmplt_token(), rz_type_parse_string_declaration_single(), rz_type_parse_string_single(), substitute_args_do(), substitute_args_fini(), and type_parse_string().
void ts_tree_edit | ( | TSTree * | self, |
const TSInputEdit * | edit | ||
) |
Edit the syntax tree to keep it in sync with source code that has been edited.
You must describe the edit both in terms of byte offsets and in terms of (row, column) coordinates.
Definition at line 44 of file tree.c.
References i, TSInputEdit::new_end_byte, TSInputEdit::new_end_point, TSInputEdit::old_end_byte, TSInputEdit::old_end_point, point_add(), POINT_MAX, point_sub(), capstone::range, ts_subtree_edit(), ts_subtree_pool_delete(), ts_subtree_pool_new(), and UINT32_MAX.
Referenced by ts_tree_edit_wasm().
TSRange* ts_tree_get_changed_ranges | ( | const TSTree * | old_tree, |
const TSTree * | new_tree, | ||
uint32_t * | length | ||
) |
Compare an old edited syntax tree to a new syntax tree representing the same document, returning an array of ranges whose syntactic structure has changed.
For this to work correctly, the old syntax tree must have been edited such that its ranges match up to the new tree. Generally, you'll want to call this function right after calling one of the ts_parser_parse
functions. You need to pass the old tree that was passed to parse, as well as the new tree that was returned from that function.
The returned array is allocated using malloc
and the caller is responsible for freeing it using free
. The length of the array will be written to the given length
pointer.
Definition at line 78 of file tree.c.
References array_delete, array_new, count, TSTree::included_range_count, TSTree::included_ranges, NULL, TSTree::root, ts_range_array_get_changed_ranges(), ts_subtree_get_changed_ranges(), ts_tree_cursor_init(), and ts_tree_root_node().
Referenced by ts_tree_get_changed_ranges_wasm().
const TSLanguage* ts_tree_language | ( | const TSTree * | self | ) |
Write a DOT graph describing the syntax tree to the given file.
Definition at line 103 of file tree.c.
References ts_subtree_print_dot_graph().
Get the root node of the syntax tree.
Definition at line 36 of file tree.c.
References ts_node_new(), and ts_subtree_padding().
Referenced by core_cmd_tsrzcmd(), guess_next_autocmplt_token(), rz_type_parse_string_declaration_single(), rz_type_parse_string_single(), substitute_args_do(), ts_node_parent(), ts_tree_get_changed_ranges(), ts_tree_root_node_wasm(), and type_parse_string().