Crate tree_sitter_traversal[][src]

Expand description

Iterators to traverse tree-sitter Trees using a TreeCursor, with a Cursor trait to allow for traversing arbitrary n-ary trees.

Examples

Basic usage:

use tree_sitter::{Node, Tree};
use std::collections::HashSet;
use std::iter::FromIterator;

use tree_sitter_traversal::{traverse, traverse_tree, Order};

// Non-existent method, imagine it gets a valid Tree with >1 node
let tree: Tree = get_tree();
let preorder: Vec<Node<'_>> = traverse(tree.walk(), Order::Pre).collect::<Vec<_>>();
let postorder: Vec<Node<'_>> = traverse_tree(&tree, Order::Post).collect::<Vec<_>>();
// For any tree with more than just a root node,
// the order of preorder and postorder will be different
assert_ne!(preorder, postorder);
// However, they will have the same amount of nodes
assert_eq!(preorder.len(), postorder.len());
// Specifically, they will have the exact same nodes, just in a different order
assert_eq!(
    <HashSet<_>>::from_iter(preorder.into_iter()),
    <HashSet<_>>::from_iter(postorder.into_iter())
);

Enums

Order to iterate through a n-ary tree; for n-ary trees only Pre-order and Post-order make sense.

Traits

Trait which represents a stateful cursor in a n-ary tree. The cursor can be moved between nodes in the tree by the given methods, and the node which the cursor is currently pointing at can be read as well.

Functions

Traverse an n-ary tree using cursor, returning the nodes of the tree through an iterator in an order according to order.

Convenience method to traverse a tree-sitter Tree in an order according to order.