tree-sitter-traversal

Traversal of tree-sitter Trees and any arbitrary tree with a TreeCursor-like interface.

build_status Documentation crates.io

Using cursors, iteration over the tree can be implemented in a very efficient manner which performs no additional heap allocation beyond what might be required by the Cursor. The state required for pre-order and post-order traversal is very minimal; for pre-order traversal, all that is required is to maintain whether the traversal is complete, and for post-order traversal, we also maintain if the cursor is currently traversing up or down the tree.

Usage

Add this to your Cargo.toml

toml [dependencies] tree-sitter-traversal = "0.1.1"

Example

```rust use tree_sitter::{Node, Tree};

use treesittertraversal::{traverse, traversetree, Order}; fn gettree() -> Tree { use treesitter::Parser; let mut parser = Parser::new(); let lang = treesitterjavascript::language(); parser.setlanguage(lang).expect("Error loading JavaScript grammar"); return parser.parse("function(x) { return x * 2; }", None).expect("Error parsing provided code"); }

fn main() { use std::collections::HashSet; use std::iter::FromIterator; let tree: Tree = gettree(); let preorder: Vec> = traverse(tree.walk(), Order::Pre).collect::>(); let postorder: Vec> = traversetree(&tree, Order::Post).collect::>(); // For any tree with more than just a root node, // the order of preorder and postorder will be different assertne!(preorder, postorder); // However, they will have the same amount of nodes asserteq!(preorder.len(), postorder.len()); // Specifically, they will have the exact same nodes, just in a different order asserteq!( >::fromiter(preorder.intoiter()), >::fromiter(postorder.into_iter()) );
} ```

Features

Though this library was designed to be used for tree-sitter, that usage is optional, as it can also be used by any struct which implements the Cursor trait. When the tree-sitter feature is disabled, the library is actually #![no_std]. To use without tree-sitter, add this to your Cargo.toml instead:

toml [dependencies.tree-sitter-traversal] version = "0.1.1" default-features = false