ts::Node class

A syntax node in a parsed tree.

Wrapper for a TSNode.

Nodes can be named or anonymous (see Named vs Anonymous Nodes).

Nodes can't be null. If you try to create a null node the constructor will throw a NullNodeException. (But this constructor should only be used internally.)

Supports equality operators.

Features not included (because we currently don't use them):

  • Get child by field:
    • ts_node_child_by_field_name
    • ts_node_child_by_field_id
  • Get child/decendant for byte/point (range):
    • ts_node_first_child_for_byte
    • ts_node_first_named_child_for_byte
    • ts_node_descendant_for_byte_range
    • ts_node_descendant_for_point_range
    • ts_node_named_descendant_for_byte_range
    • ts_node_named_descendant_for_point_range
  • Editing nodes directly (but we support this through the Tree):
    • ts_node_edit

Public static variables

static unsafe_t unsafe constexpr
Type tag for the unsafe constructor.

Public static functions

static auto or_null(TSNode node, const Tree& tree) -> std::optional<Node> noexcept
Create a Node if node is not null.

Constructors, destructors, conversion operators

Node(TSNode node, const Tree& tree)
Node(unsafe_t, TSNode node, const Tree& tree) noexcept

Public functions

auto raw() const -> TSNode
Returns the raw Tree-Sitter node.
auto tree() const -> const Tree&
The Tree this node was created from.
auto type() const -> const char*
The substring of the source code this node represents.
auto type_id() const -> TypeId
The numeric TypeId of the node.
auto is_named() const -> bool
Check if the node is named.
auto is_missing() const -> bool
Check if the node is missing.
auto is_extra() const -> bool
Check if the node is extra.
auto has_changes() const -> bool
Check if the node has been edited.
auto has_error() const -> bool
Check if the node (or any of its children) is a syntax error.
auto parent() const -> std::optional<Node>
The parent of the node.
auto child(std::uint32_t index) const -> std::optional<Node>
The n-th child (0 indexed). Counts named and anonymous nodes.
auto child_count() const -> std::uint32_t
The number of all children (named and anonymous).
auto children() const -> std::vector<Node>
List of all children (named and anonymous).
auto named_child(std::uint32_t index) const -> std::optional<Node>
The n-th named child (0 indexed).
auto named_child_count() const -> std::uint32_t
The number of named children.
auto named_children() const -> std::vector<Node>
List of all named children.
auto next_sibling() const -> std::optional<Node>
The node's next sibling.
auto prev_sibling() const -> std::optional<Node>
The node's previous sibling.
auto next_named_sibling() const -> std::optional<Node>
The node's next named sibling.
auto prev_named_sibling() const -> std::optional<Node>
The node's previous named sibling.
auto start_byte() const -> std::uint32_t
Start position as byte offset.
auto end_byte() const -> std::uint32_t
End position as byte offset.
auto start_point() const -> Point
The start position as Point (row and column).
auto end_point() const -> Point
The end position as Point (row and column).
auto start() const -> Location
The start position as Location (row, column and byte offset).
auto end() const -> Location
The end position as Location (row, column and byte offset).
auto range() const -> Range
The Range of the node (start and end Location).
auto text() const -> std::string
The substring of source code this node represents.
auto as_s_expr() const -> std::string
A string representation of the syntax tree starting from the node represented as an s-expression.

Function documentation

ts::Node::Node(TSNode node, const Tree& tree)

Creates a new node from the given tree-sitter node and the tree.

Will throw NullNodeException if the node is null.

ts::Node::Node(unsafe_t, TSNode node, const Tree& tree) noexcept

Unsafe constructor that does not check for null nodes.

Only call this if you know the node is not null.

TSNode ts::Node::raw() const

Returns the raw Tree-Sitter node.

TypeId ts::Node::type_id() const

The numeric TypeId of the node.

In tree-sitter this is called symbol.

bool ts::Node::is_missing() const

Check if the node is missing.

Missing nodes are used to recover from some kinds of syntax errors.

bool ts::Node::is_extra() const

Check if the node is extra.

Extra nodes represent things like comments.

std::optional<Node> ts::Node::parent() const

The parent of the node.

Returns std::nullopt when called with the root node of a tree.

std::optional<Node> ts::Node::child(std::uint32_t index) const

The n-th child (0 indexed). Counts named and anonymous nodes.

Returns std::nullopt if the child does not exist.

std::optional<Node> ts::Node::named_child(std::uint32_t index) const

The n-th named child (0 indexed).

This will not return anonymous nodes and the index only considers named nodes.

Returns std::nullopt if the child does not exist.

std::optional<Node> ts::Node::next_sibling() const

The node's next sibling.

This will also return anonymous nodes.

Returns std::nullopt if there are no more siblings.

std::optional<Node> ts::Node::prev_sibling() const

The node's previous sibling.

This will also return anonymous nodes.

Returns std::nullopt if this node is already the first sibling.

std::optional<Node> ts::Node::next_named_sibling() const

The node's next named sibling.

This will not return anonymous nodes.

Returns std::nullopt if there are no more named siblings.

std::optional<Node> ts::Node::prev_named_sibling() const

The node's previous named sibling.

This will not return anonymous nodes.

Returns std::nullopt if this node is already the first named sibling.

std::uint32_t ts::Node::end_byte() const

End position as byte offset.

Returns the position after the last character.

Variable documentation

static unsafe_t ts::Node::unsafe constexpr

Type tag for the unsafe constructor.