Skip to main content
A Node represents a single node within a syntax tree. Nodes are lightweight and can be copied freely.

Node Identity

id

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.

is_extra

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.
u32
required
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.
u32
required
The index of the named child to retrieve

children

Iterate over this node’s children. A TreeCursor is used to retrieve the children efficiently.
&mut TreeCursor
required
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.
impl AsRef<[u8]>
required
The name of the field

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.
u16
required
The numerical id of the field

children_by_field_name

Iterate over this node’s children with a given field name.
&str
required
The name of the field
&mut TreeCursor
required
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.
u32
required
The index of the child

field_name_for_named_child

Get the field name of this node’s named child at the given index.
u32
required
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.
usize
required
The start byte of the range
usize
required
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.
Point
required
The start point of the range
Point
required
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.
Node
required
The descendant node

first_child_for_byte

Get this node’s first child that contains or starts after the given byte offset.
usize
required
The byte offset

first_named_child_for_byte

Get this node’s first named child that contains or starts after the given byte offset.

Text Extraction

utf8_text

Get the text of this node from UTF-8 encoded source code.
&[u8]
required
The source code bytes

utf16_text

Get the text of this node from UTF-16 encoded source code.
&[u16]
required
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.
&InputEdit
required
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.
usize
The zero-based row number
usize
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.
usize
The byte offset where the range starts
usize
The byte offset where the range ends
Point
The row/column position where the range starts
Point
The row/column position where the range ends

Examples

Finding a Specific Node

Iterating Over Children

Checking for Errors

Finding Node at Position