A Node represents a single node within a syntax tree. Nodes are lightweight and can be copied freely.
Node Identity
Get a numeric id for this node that is unique within a given syntax tree. If a new tree is created based on an older tree, and a node from the old tree is reused in the process, then that node will have the same id in both trees.
kind
Get this node’s type as a string.
kind_id
Get this node’s type as a numerical id.
grammar_id
Get the node’s type as a numerical id as it appears in the grammar, ignoring aliases.
grammar_name
Get this node’s symbol name as it appears in the grammar, ignoring aliases.
Node Properties
is_named
Check if this node is named. Named nodes correspond to named rules in the grammar, whereas anonymous nodes correspond to string literals in the grammar.
Check if this node is extra. Extra nodes represent things like comments, which are not required by the grammar but can appear anywhere.
is_missing
Check if this node is missing. Missing nodes are inserted by the parser in order to recover from certain kinds of syntax errors.
is_error
Check if this node represents a syntax error. Syntax errors represent parts of the code that could not be incorporated into a valid syntax tree.
has_error
Check if this node represents a syntax error or contains any syntax errors anywhere within it.
has_changes
Check if this node has been edited.
Position and Range
start_byte
Get the byte offset where this node starts.
end_byte
Get the byte offset where this node ends.
byte_range
Get the byte range of source code that this node represents.
start_position
Get this node’s start position in terms of rows and columns.
end_position
Get this node’s end position in terms of rows and columns.
range
Get the range of source code that this node represents, both in terms of raw bytes and of row/column coordinates.
Children and Siblings
child_count
Get this node’s number of children.
child
Get the node’s child at the given index, where zero represents the first child. This method is fairly fast, but its cost is technically log(i), so if you might be iterating over a long list of children, you should use children instead.
The index of the child to retrieve
named_child_count
Get this node’s number of named children.
named_child
Get this node’s named child at the given index.
The index of the named child to retrieve
children
Iterate over this node’s children. A TreeCursor is used to retrieve the children efficiently.
A tree cursor to use for iteration. To avoid unnecessary allocations, you should reuse the same cursor for subsequent calls.
named_children
Iterate over this node’s named children.
parent
Get this node’s immediate parent. Prefer child_with_descendant for iterating over this node’s ancestors.
next_sibling
Get this node’s next sibling.
prev_sibling
Get this node’s previous sibling.
next_named_sibling
Get this node’s next named sibling.
prev_named_sibling
Get this node’s previous named sibling.
Field Access
child_by_field_name
Get the first child with the given field name. If multiple children may have the same field name, access them using children_by_field_name.
child_by_field_id
Get this node’s child with the given numerical field id. You can convert a field name to an id using Language::field_id_for_name.
The numerical id of the field
children_by_field_name
Iterate over this node’s children with a given field name.
A tree cursor to use for iteration
children_by_field_id
Iterate over this node’s children with a given field id.
field_name_for_child
Get the field name of this node’s child at the given index.
field_name_for_named_child
Get the field name of this node’s named child at the given index.
The index of the named child
Descendants
descendant_count
Get the node’s number of descendants, including one for the node itself.
descendant_for_byte_range
Get the smallest node within this node that spans the given byte range.
The start byte of the range
The end byte of the range
named_descendant_for_byte_range
Get the smallest named node within this node that spans the given byte range.
descendant_for_point_range
Get the smallest node within this node that spans the given point range.
The start point of the range
The end point of the range
named_descendant_for_point_range
Get the smallest named node within this node that spans the given point range.
child_with_descendant
Get the node that contains descendant. Note that this can return descendant itself.
first_child_for_byte
Get this node’s first child that contains or starts after the given byte offset.
first_named_child_for_byte
Get this node’s first named child that contains or starts after the given byte offset.
utf8_text
Get the text of this node from UTF-8 encoded source code.
utf16_text
Get the text of this node from UTF-16 encoded source code.
The source code as UTF-16 code units
Other Methods
language
Get the Language that was used to parse this node’s syntax tree.
parse_state
Get this node’s parse state.
next_parse_state
Get the parse state after this node.
to_sexp
Get an S-expression representing the node.
walk
Create a new TreeCursor starting from this node. Note that the given node is considered the root of the cursor, and the cursor cannot walk outside this node.
edit
Edit this 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 Tree::edit method, all of the nodes that you retrieve from the tree afterward will already reflect the edit.
A description of the edit
Point
A position in a multi-line text document, in terms of rows and columns. Rows and columns are zero-based.
The zero-based row number
The zero-based column number (in bytes)
new
Range
A range of positions in a multi-line text document, both in terms of bytes and of rows and columns.
The byte offset where the range starts
The byte offset where the range ends
The row/column position where the range starts
The row/column position where the range ends
Examples
Finding a Specific Node
Iterating Over Children
Checking for Errors
Finding Node at Position