Skip to main content
The Node API provides functions for inspecting node properties and traversing syntax trees. Unlike other Tree-sitter types, TSNode is passed by value rather than by pointer.

Node properties

ts_node_type

Get the node’s type as a null-terminated string.
TSNode
The node to inspect
Returns: The node’s type name (do not free). Example:

ts_node_symbol

Get the node’s type as a numerical id.
TSNode
The node to inspect
Returns: The node’s symbol ID.

ts_node_language

Get the node’s language.
TSNode
The node to inspect
Returns: The language of the node’s tree.

ts_node_grammar_type

Get the node’s type as it appears in the grammar ignoring aliases as a null-terminated string.
TSNode
The node to inspect
Returns: The node’s grammar type name (do not free).

ts_node_grammar_symbol

Get the node’s type as a numerical id as it appears in the grammar ignoring aliases.
TSNode
The node to inspect
Returns: The node’s grammar symbol ID. This should be used in ts_language_next_state instead of ts_node_symbol.

Node positions

ts_node_start_byte

Get the node’s start byte.
TSNode
The node to inspect
Returns: The byte offset where the node starts.

ts_node_start_point

Get the node’s start position in terms of rows and columns.
TSNode
The node to inspect
Returns: The node’s start position. Example:

ts_node_end_byte

Get the node’s end byte.
TSNode
The node to inspect
Returns: The byte offset where the node ends.

ts_node_end_point

Get the node’s end position in terms of rows and columns.
TSNode
The node to inspect
Returns: The node’s end position.

Node state

ts_node_string

Get an S-expression representing the node as a string.
TSNode
The node to convert to a string
Returns: String representation (caller must free with free). Example:

ts_node_is_null

Check if the node is null.
TSNode
The node to check
Returns: true if the node is null. Functions like ts_node_child and ts_node_next_sibling return a null node to indicate that no such node was found.

ts_node_is_named

Check if the node is named.
TSNode
The node to check
Returns: true if the node is named. Named nodes correspond to named rules in the grammar, whereas anonymous nodes correspond to string literals in the grammar.

ts_node_is_missing

Check if the node is missing.
TSNode
The node to check
Returns: true if the node is missing. Missing nodes are inserted by the parser to recover from certain kinds of syntax errors.

ts_node_is_extra

Check if the node is extra.
TSNode
The node to check
Returns: true if the node is extra. Extra nodes represent things like comments, which are not required by the grammar but can appear anywhere.

ts_node_has_changes

Check if a syntax node has been edited.
TSNode
The node to check
Returns: true if the node has been edited.

ts_node_has_error

Check if the node is a syntax error or contains any syntax errors.
TSNode
The node to check
Returns: true if the node is or contains an error.

ts_node_is_error

Check if the node is a syntax error.
TSNode
The node to check
Returns: true if the node is an error.

ts_node_parse_state

Get this node’s parse state.
TSNode
The node to inspect
Returns: The parse state at the start of the node.

ts_node_next_parse_state

Get the parse state after this node.
TSNode
The node to inspect
Returns: The parse state after the node.

Node relationships

ts_node_parent

Get the node’s immediate parent.
TSNode
The node to inspect
Returns: The parent node, or a null node if there is no parent. Prefer ts_node_child_with_descendant for iterating over the node’s ancestors.

ts_node_child_with_descendant

Get the node that contains the given descendant.
TSNode
The ancestor node
TSNode
The descendant node
Returns: The direct child of self that contains descendant. Note that this can return descendant itself if it’s a direct child of self.

ts_node_child

Get the node’s child at the given index.
TSNode
The parent node
uint32_t
The child index, where zero represents the first child
Returns: The child node, or a null node if the index is out of bounds.

ts_node_field_name_for_child

Get the field name for the node’s child at the given index.
TSNode
The parent node
uint32_t
The child index
Returns: The field name, or NULL if no field is found.

ts_node_field_name_for_named_child

Get the field name for the node’s named child at the given index.
TSNode
The parent node
uint32_t
The named child index
Returns: The field name, or NULL if no field is found.

ts_node_child_count

Get the node’s number of children.
TSNode
The node to inspect
Returns: The number of children.

ts_node_named_child

Get the node’s named child at the given index.
TSNode
The parent node
uint32_t
The named child index
Returns: The named child node, or a null node if the index is out of bounds. See also ts_node_is_named.

ts_node_named_child_count

Get the node’s number of named children.
TSNode
The node to inspect
Returns: The number of named children. See also ts_node_is_named.

ts_node_child_by_field_name

Get the node’s child with the given field name.
TSNode
The parent node
const char *
The field name
uint32_t
The length of the field name
Returns: The child node with the given field, or a null node if not found. Example:

ts_node_child_by_field_id

Get the node’s child with the given numerical field id.
TSNode
The parent node
TSFieldId
The numerical field ID
Returns: The child node with the given field, or a null node if not found. You can convert a field name to an id using ts_language_field_id_for_name.

Siblings

ts_node_next_sibling

Get the node’s next sibling.
TSNode
The node to inspect
Returns: The next sibling, or a null node if there is none.

ts_node_prev_sibling

Get the node’s previous sibling.
TSNode
The node to inspect
Returns: The previous sibling, or a null node if there is none.

ts_node_next_named_sibling

Get the node’s next named sibling.
TSNode
The node to inspect
Returns: The next named sibling, or a null node if there is none.

ts_node_prev_named_sibling

Get the node’s previous named sibling.
TSNode
The node to inspect
Returns: The previous named sibling, or a null node if there is none.

Searching

ts_node_first_child_for_byte

Get the node’s first child that contains or starts after the given byte offset.
TSNode
The parent node
uint32_t
The byte offset to search for
Returns: The child node, or a null node if not found.

ts_node_first_named_child_for_byte

Get the node’s first named child that contains or starts after the given byte offset.
TSNode
The parent node
uint32_t
The byte offset to search for
Returns: The named child node, or a null node if not found.

ts_node_descendant_count

Get the node’s number of descendants, including one for the node itself.
TSNode
The node to inspect
Returns: The total number of descendants.

ts_node_descendant_for_byte_range

Get the smallest node within this node that spans the given range of bytes.
TSNode
The node to search within
uint32_t
The start byte offset
uint32_t
The end byte offset
Returns: The smallest descendant spanning the range.

ts_node_descendant_for_point_range

Get the smallest node within this node that spans the given range of positions.
TSNode
The node to search within
TSPoint
The start position
TSPoint
The end position
Returns: The smallest descendant spanning the range.

ts_node_named_descendant_for_byte_range

Get the smallest named node within this node that spans the given range of bytes.
TSNode
The node to search within
uint32_t
The start byte offset
uint32_t
The end byte offset
Returns: The smallest named descendant spanning the range.

ts_node_named_descendant_for_point_range

Get the smallest named node within this node that spans the given range of positions.
TSNode
The node to search within
TSPoint
The start position
TSPoint
The end position
Returns: The smallest named descendant spanning the range.

Editing

ts_node_edit

Edit the node to keep it in-sync with source code that has been edited.
TSNode *
Pointer to the node to edit
const TSInputEdit *
Description of the edit
This function is only rarely needed. When you edit a syntax tree with ts_tree_edit, 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.

ts_point_edit

Edit a point to keep it in-sync with source code that has been edited.
TSPoint *
Pointer to the point to edit
uint32_t *
Pointer to the byte offset to edit
const TSInputEdit *
Description of the edit
This function updates a single point’s byte offset and row/column position based on an edit operation. This is useful for editing points without requiring a tree or node instance.

ts_range_edit

Edit a range to keep it in-sync with source code that has been edited.
TSRange *
Pointer to the range to edit
const TSInputEdit *
Description of the edit
This function updates a range’s start and end positions based on an edit operation. This is useful for editing ranges without requiring a tree or node instance.

Comparison

ts_node_eq

Check if two nodes are identical.
TSNode
The first node
TSNode
The second node
Returns: true if the nodes are identical.