Node#

class tree_sitter.Node#

A single node within a syntax Tree.

Methods#

child(index, /)#

Get this node’s child at the given index, where 0 represents the first child.

Caution

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 or walk() instead.

child_by_field_id(id, /)#

Get the first child with the given numerical field id.

Hint

You can convert a field name to an id using Language.field_id_for_name().

child_by_field_name(name, /)#

Get the first child with the given field name.

child_containing_descendant(descendant, /)#

Get the child of the node that contains the given descendant.

Attention

This will not return the descendant if it is a direct child of this node.

Deprecated since version 0.24.0: Use child_with_descendant() instead

child_with_descendant(descendant, /)#

Get the node that contains the given descendant.

Added in version 0.24.0.

children_by_field_id(id, /)#

Get a list of children with the given numerical field id.

children_by_field_name(name, /)#

Get a list of children with the given field name.

descendant_for_byte_range(start_byte, end_byte, /)#

Get the smallest node within this node that spans the given byte range.

descendant_for_point_range(start_point, end_point, /)#

Get the smallest node within this node that spans the given point range.

edit(start_byte, old_end_byte, new_end_byte, start_point, old_end_point, new_end_point)#

Edit this node to keep it in-sync with source code that has been edited.

Note

This method is only rarely needed. When you edit a syntax tree via Tree.edit(), all of the nodes that you retrieve from the tree afterwards will already reflect the edit. You only need to use this when you have a specific Node instance that you want to keep and continue to use after an edit.

field_name_for_child(child_index, /)#

Get the field name of this node’s child at the given index.

field_name_for_named_child()#

field_name_for_child(self, child_index, /) –

Get the field name of this node’s named child at the given index.

Added in version 0.24.0.

named_child(index, /)#

Get this node’s named child at the given index, where 0 represents the first child.

Caution

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 or walk() instead.

named_descendant_for_byte_range(start_byte, end_byte, /)#

Get the smallest named node within this node that spans the given byte range.

named_descendant_for_point_range(start_point, end_point, /)#

Get the smallest named node within this node that spans the given point range.

walk()#

Create a new TreeCursor starting from this node.

Special Methods#

__eq__(value, /)#

Implements self==value.

__hash__()#

Implements hash(self).

__ne__(value, /)#

Implements self!=value.

__repr__()#

Implements repr(self).

__str__()#

Implements str(self).

Attributes#

byte_range#

The byte range of source code that this node represents, in terms of bytes.

child_count#

This node’s number of children.

children#

This node’s children.

Note

If you’re walking the tree recursively, you may want to use walk() instead.

descendant_count#

This node’s number of descendants, including the node itself.

end_byte#

The byte offset where this node ends.

end_point#

This node’s end point.

grammar_id#

This node’s type as a numerical id as it appears in the grammar ignoring aliases.

grammar_name#

This node’s symbol name as it appears in the grammar ignoring aliases.

has_changes#

Check if this node has been edited.

has_error#

Check if this node represents a syntax error or contains any syntax errors anywhere within it.

id#

This node’s numerical id.

Note

Within a given syntax tree, no two nodes have the same id. However, 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.

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.

is_extra#

Check if this node is _extra_.

Extra nodes represent things which are not required by the grammar but can appear anywhere (e.g. whitespace).

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_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.

kind_id#

This node’s type as a numerical id.

named_child_count#

This node’s number of _named_ children.

named_children#

This node’s _named_ children.

next_named_sibling#

This node’s next named sibling.

next_parse_state#

The parse state after this node.

next_sibling#

This node’s next sibling.

parent#

This node’s immediate parent.

parse_state#

This node’s parse state.

prev_named_sibling#

This node’s previous named sibling.

prev_sibling#

This node’s previous sibling.

range#

The range of source code that this node represents.

start_byte#

The byte offset where this node starts.

start_point#

This node’s start point

text#

The text of the node, if the tree has not been edited

type#

This node’s type as a string.