ts::Cursor class

Allows efficient walking of a Tree.

This is more efficient than using the methods on Node because we don't create a new Node after every navigation step.

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

  • ts_tree_cursor_goto_first_child_for_byte

Constructors, destructors, conversion operators

Cursor(Node) explicit noexcept
Create a cursor starting at the given node.
Cursor(const Tree&) explicit noexcept
Create a cursor starting at the root node of the given tree.
Cursor(const Cursor&) noexcept
Cursor(Cursor&&) defaulted

Public functions

auto operator=(const Cursor&) -> Cursor& noexcept
auto operator=(Cursor&&) -> Cursor& defaulted
void reset(Node)
Reset the cursor to the given node.
void reset(const Tree&)
Reset the cursor to the root node of the given tree.
auto current_node() const -> Node
The node the cursor is currently pointing at.
auto current_field_name() const -> const char*
The field name of the node the cursor is currently pointing at.
auto current_field_id() const -> FieldId
The FieldId of the node the cursor is currently pointing at.
auto goto_parent() -> bool
Move the cursor to the parent of the current node.
auto goto_next_sibling() -> bool
Move the cursor to the next sibling of the current node.
auto skip_n_siblings(int n) -> int
Similar to calling Cursor::goto_next_sibling n times.
auto goto_first_child() -> bool
Move the cursor to the first child of the current node.
auto goto_next_named_sibling() -> bool
Move the cursor to the next named sibling of the current node.
auto goto_first_named_child() -> bool
Move the cursor to the next named sibling of the current node.
template<typename Fn>
auto skip_siblings_while(Fn fn) -> bool
Skips over nodes while the given callback returns true.
template<typename Fn>
void foreach_remaining_siblings(Fn fn)
Calls the provided callback for every sibling and moves the cursor.
auto children() -> std::vector<Node>
List of all child nodes of the current node.
auto named_children() -> std::vector<Node>
List of all named child nodes of the current node.

Friends

void swap(Cursor&, Cursor&) noexcept

Function documentation

ts::Cursor::Cursor(const Cursor&) noexcept

Copy constructor.

ts::Cursor::Cursor(Cursor&&) defaulted

Move constructor.

Cursor& ts::Cursor::operator=(const Cursor&) noexcept

Copy assignment operator.

Cursor& ts::Cursor::operator=(Cursor&&) defaulted

Move assignment operator.

bool ts::Cursor::goto_parent()

Move the cursor to the parent of the current node.

Returns only false if the cursor is already at the root node.

bool ts::Cursor::goto_next_sibling()

Move the cursor to the next sibling of the current node.

Returns false if there was no next sibling.

int ts::Cursor::skip_n_siblings(int n)

Similar to calling Cursor::goto_next_sibling n times.

Returns the number of siblings skipped.

bool ts::Cursor::goto_first_child()

Move the cursor to the first child of the current node.

Returns false if there were no children.

bool ts::Cursor::goto_next_named_sibling()

Move the cursor to the next named sibling of the current node.

Returns false if there was no next sibling.

bool ts::Cursor::goto_first_named_child()

Move the cursor to the next named sibling of the current node.

Returns false if there was no next sibling.

NOTE: This method might move the cursor to another unnamed node and then still return false if there is no named node.

template<typename Fn>
bool ts::Cursor::skip_siblings_while(Fn fn)

Skips over nodes while the given callback returns true.

The method returns false if there were no more siblings to skip while the callback still returned true.

template<typename Fn>
void ts::Cursor::foreach_remaining_siblings(Fn fn)

Calls the provided callback for every sibling and moves the cursor.

The callback will also be called on the current node. So it will always be called at least once.

std::vector<Node> ts::Cursor::children()

List of all child nodes of the current node.

This will also move the cursor to the last child but you can Cursor::reset the cursor to point at any of the returned children or call 'parent' to get back to the current node.

std::vector<Node> ts::Cursor::named_children()

List of all named child nodes of the current node.

This will also move the cursor to the last child but you can Cursor::reset the cursor to point at any of the returned children or call Cursor::parent to get back to the current node.

void swap(Cursor&, Cursor&) noexcept

Swap function.