Iterating through a syntax tree with TreeCursor


I’m trying to iterate through the syntax tree generated by a the lezer parser using the .next() method and process all of the leaf nodes. I’ve been looking through the lezer reference manual and I’m curious about the best way to do the following:

  1. Determine whether a TreeCursor is pointing to a leaf node.
  2. Determine whether a TreeCursor is pointing at the last node in the tree (in terms of a DFS pre-order). Alternatively, it seems that the .next() method brings the cursor back to the top of the tree after the last node, so
  3. Determine whether the TreeCursor is pointing at the top of the tree.

The iterator is definitely a bit awkward to use in cases like this. If you can integrate iteration and the actions that are conditional on these checks, you could call, for example, .firstChild() and run the is-a-leaf logic when it returns false. Or you could use SyntaxNodes instead, which make this easier.