Walking Trees with Tree Cursors

You can access every node in a syntax tree using the TSNode APIs described earlier, but if you need to access a large number of nodes, the fastest way to do so is with a tree cursor. A cursor is a stateful object that allows you to walk a syntax tree with maximum efficiency.

Note that the given input node is considered the root of the cursor, and the cursor cannot walk outside this node. Going to the parent or any sibling of the root node will always return false.

This has no unexpected effects if the given input node is the actual root node of the tree, but is something to keep in mind when using cursors constructed with a node that is not the root node.

You can initialize a cursor from any node:

TSTreeCursor ts_tree_cursor_new(TSNode);

You can move the cursor around the tree:

bool ts_tree_cursor_goto_first_child(TSTreeCursor *);
bool ts_tree_cursor_goto_next_sibling(TSTreeCursor *);
bool ts_tree_cursor_goto_parent(TSTreeCursor *);

These methods return true if the cursor successfully moved and false if there was no node to move to.

You can always retrieve the cursor's current node, as well as the field name that is associated with the current node.

TSNode ts_tree_cursor_current_node(const TSTreeCursor *);
const char *ts_tree_cursor_current_field_name(const TSTreeCursor *);
TSFieldId ts_tree_cursor_current_field_id(const TSTreeCursor *);