treesitter

Walk through all tree nodes?


I'm using py-tree-sitter and this code to walk the entire node tree in sequence (code is from this github issue: https://github.com/tree-sitter/py-tree-sitter/issues/33#issuecomment-864557166):

def traverse_tree(tree: Tree):
    cursor = tree.walk()

    reached_root = False
    while reached_root == False:
        yield cursor.node

        if cursor.goto_first_child():
            continue

        if cursor.goto_next_sibling():
            continue

        retracing = True
        while retracing:
            if not cursor.goto_parent():
                retracing = False
                reached_root = True

            if cursor.goto_next_sibling():
                retracing = False

for node in traverse_tree(tree.root_node):
  print(node)

I'm using this code for syntax highlighting and this method does not give empty tokens like whitespace and newline which are not part of another AST token.

Is this the correct way to get all the nodes in the tree?

Thanks


Solution

  • Another solution is to use recursion. For example, since tree-sitter does not support query syntax for missing nodes yet, one possible solution to still get them:

    missing_nodes = []
    def traverse_tree(node: Node):
       for n in node.children:
          if n.is_missing: missing_nodes.append(n)
          traverse_tree(n)
            
    traverse_tree(tree.root_node)