tree-sitter-iter-1.25.2/.cargo_vcs_info.json0000644000000001651046102023000143570ustar { "git": { "sha1": "b50d8f60e27e0084aa3a5f5dff46054a8253ac2a" }, "path_in_vcs": "crates/tree-sitter-iter" }tree-sitter-iter-1.25.2/Cargo.lock0000644000000133471046102023000123400ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 4 [[package]] name = "aho-corasick" version = "1.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" dependencies = [ "memchr", ] [[package]] name = "cc" version = "1.2.49" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90583009037521a116abf44494efecd645ba48b6622457080f080b85544e2215" dependencies = [ "find-msvc-tools", "shlex", ] [[package]] name = "equivalent" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" [[package]] name = "find-msvc-tools" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3a3076410a55c90011c298b04d0cfa770b00fa04e1e3c97d3f6c9de105a03844" [[package]] name = "hashbrown" version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4f467dd6dccf739c208452f8014c75c18bb8301b050ad1cfb27153803edb0f51" [[package]] name = "indexmap" version = "2.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d466e9454f08e4a911e14806c24e16fba1b4c121d1ea474396f396069cf949d9" dependencies = [ "equivalent", "hashbrown", ] [[package]] name = "itoa" version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" [[package]] name = "memchr" version = "2.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79" [[package]] name = "proc-macro2" version = "1.0.106" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" dependencies = [ "unicode-ident", ] [[package]] name = "quote" version = "1.0.45" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924" dependencies = [ "proc-macro2", ] [[package]] name = "regex" version = "1.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e10754a14b9137dd7b1e3e5b0493cc9171fdd105e0ab477f51b72e7f3ac0e276" dependencies = [ "aho-corasick", "memchr", "regex-automata", "regex-syntax", ] [[package]] name = "regex-automata" version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e1dd4122fc1595e8162618945476892eefca7b88c52820e74af6262213cae8f" dependencies = [ "aho-corasick", "memchr", "regex-syntax", ] [[package]] name = "regex-syntax" version = "0.8.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a2d987857b319362043e95f5353c0535c1f58eec5336fdfcf626430af7def58" [[package]] name = "serde" version = "1.0.228" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" dependencies = [ "serde_core", ] [[package]] name = "serde_core" version = "1.0.228" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" version = "1.0.228" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" dependencies = [ "proc-macro2", "quote", "syn", ] [[package]] name = "serde_json" version = "1.0.149" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "83fc039473c5595ace860d8c4fafa220ff474b3fc6bfdb4293327f1a37e94d86" dependencies = [ "indexmap", "itoa", "memchr", "serde", "serde_core", "zmij", ] [[package]] name = "shlex" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" [[package]] name = "streaming-iterator" version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b2231b7c3057d5e4ad0156fb3dc807d900806020c5ffa3ee6ff2c8c76fb8520" [[package]] name = "syn" version = "2.0.117" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] [[package]] name = "tree-sitter" version = "0.26.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "887bd495d0582c5e3e0d8ece2233666169fa56a9644d172fc22ad179ab2d0538" dependencies = [ "cc", "regex", "regex-syntax", "serde_json", "streaming-iterator", "tree-sitter-language", ] [[package]] name = "tree-sitter-iter" version = "1.25.2" dependencies = [ "tree-sitter", "tree-sitter-yaml", ] [[package]] name = "tree-sitter-language" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4013970217383f67b18aef68f6fb2e8d409bc5755227092d32efb0422ba24b8" [[package]] name = "tree-sitter-yaml" version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "53c223db85f05e34794f065454843b0668ebc15d240ada63e2b5939f43ce7c97" dependencies = [ "cc", "tree-sitter-language", ] [[package]] name = "unicode-ident" version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "462eeb75aeb73aea900253ce739c8e18a67423fadf006037cd3ff27e82748a06" [[package]] name = "zmij" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0f4a4e8e9dc5c62d159f04fcdbe07f4c3fb710415aab4754bf11505501e3251d" tree-sitter-iter-1.25.2/Cargo.toml0000644000000023051046102023000123530ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2024" rust-version = "1.88.0" name = "tree-sitter-iter" version = "1.25.2" authors = ["William Woodruff "] build = false autolib = false autobins = false autoexamples = false autotests = false autobenches = false description = "A very simple pre-order iterator for tree-sitter CSTs" homepage = "https://docs.zizmor.sh" readme = "README.md" license = "MIT" resolver = "2" [lib] name = "tree_sitter_iter" path = "src/lib.rs" [dependencies.tree-sitter] version = "0.26.8" [dev-dependencies.tree-sitter-yaml] version = "0.7.2" [lints.clippy] dbg_macro = "warn" needless_lifetimes = "warn" print_stderr = "warn" print_stdout = "warn" todo = "warn" unimplemented = "warn" unwrap_used = "warn" use_debug = "warn" tree-sitter-iter-1.25.2/Cargo.toml.orig000064400000000000000000000006121046102023000160110ustar 00000000000000[package] name = "tree-sitter-iter" description = "A very simple pre-order iterator for tree-sitter CSTs" version.workspace = true authors.workspace = true homepage.workspace = true edition.workspace = true license.workspace = true rust-version.workspace = true [dependencies] tree-sitter.workspace = true [dev-dependencies] tree-sitter-yaml = { workspace = true } [lints] workspace = true tree-sitter-iter-1.25.2/README.md000064400000000000000000000034411046102023000144040ustar 00000000000000# tree-sitter-iter [![zizmor](https://img.shields.io/badge/%F0%9F%8C%88-zizmor-white?labelColor=white)](https://zizmor.sh/) [![CI](https://github.com/zizmorcore/zizmor/actions/workflows/ci.yml/badge.svg)](https://github.com/zizmorcore/zizmor/actions/workflows/ci.yml) [![Crates.io](https://img.shields.io/crates/v/tree-sitter-iter)](https://crates.io/crates/tree-sitter-iter) [![docs.rs](https://img.shields.io/docsrs/tree-sitter-iter)](https://docs.rs/tree-sitter-iter) [![GitHub Sponsors](https://img.shields.io/github/sponsors/woodruffw?style=flat&logo=githubsponsors&labelColor=white&color=white)](https://github.com/sponsors/woodruffw) [![Discord](https://img.shields.io/badge/Discord-%235865F2.svg?logo=discord&logoColor=white)](https://discord.com/invite/PGU3zGZuGG) A very simple pre-order iterator for tree-sitter CSTs. This library is part of [zizmor]. ## Usage Given a `tree_sitter::Tree`, you can create a `TreeIter` to iterate over its nodes in pre-order: ```rust use tree_sitter_iter::TreeIter; let tree: tree_sitter::Tree = parse(); // Your parsing logic here. for node in TreeIter::new(&tree) { println!("Node kind: {}", node.kind()); } ``` `TreeIter` implements the standard `Iterator` trait, meaning that you can use any of the normal iterator combinators. For example, to filter only to nodes of a specific kind: ```rust for node in TreeIter::new(&tree).filter(|n| n.kind() == "call") { // Do something with each "call" node. } ``` `tree-sitter-iter`'s space and time performance is equivalent to a walk of the tree using the `TreeCursor` APIs. In other words, it's exactly the same as using a `TreeCursor` manually, but with a more ergonomic iterator interface. See the [documentation] for more details. [documentation]: https://docs.rs/tree-sitter-iter [zizmor]: https://zizmor.sh tree-sitter-iter-1.25.2/src/lib.rs000064400000000000000000000053641046102023000150360ustar 00000000000000//! A very simple pre-order iterator for tree-sitter CSTs. #![deny(rustdoc::broken_intra_doc_links)] #![deny(missing_docs)] #![allow(clippy::redundant_field_names)] #![forbid(unsafe_code)] use tree_sitter::{Node, Tree, TreeCursor}; /// A pre-order iterator over the nodes of a tree-sitter syntax tree. pub struct TreeIter<'tree> { cursor: Option>, } impl<'tree> TreeIter<'tree> { /// Creates a new `TreeSitterIter` for the given syntax tree. pub fn new(tree: &'tree Tree) -> Self { Self { cursor: Some(tree.root_node().walk()), } } } impl<'tree> Iterator for TreeIter<'tree> { type Item = Node<'tree>; fn next(&mut self) -> Option { let cursor = match &mut self.cursor { Some(cursor) => cursor, None => return None, }; let node = cursor.node(); if cursor.goto_first_child() || cursor.goto_next_sibling() { return Some(node); } loop { if !cursor.goto_parent() { // If we can't go to the parent, the walk will be // complete *after* the current node. self.cursor = None; break; } if cursor.goto_next_sibling() { break; } } Some(node) } } #[cfg(test)] mod tests { #[test] fn test_iter_is_total() { let anchors = r#" jobs: job1: env: &env_vars # Define the anchor on first use NODE_ENV: production DATABASE_URL: ${{ secrets.DATABASE_URL }} steps: - run: echo "Using production settings" job2: env: *env_vars # Reuse the environment variables steps: - run: echo "Same environment variables here" "#; // NOTE(ww): These node counts will probably change if // tree-sitter-yaml changes its node structure. Hopefully // that doesn't happen often. let testcases = &[ ("foo:", 9), ("foo: # comment", 10), ("foo: bar", 12), ("foo: bar # comment", 13), ("foo: []", 13), ("foo: [] # comment", 14), (anchors, 100), ]; for (src, expected_count) in testcases { let mut parser = tree_sitter::Parser::new(); parser .set_language(&tree_sitter_yaml::LANGUAGE.into()) .expect("Error loading YAML grammar"); let tree = parser.parse(src, None).expect("Failed to parse source"); let node_count = tree.root_node().descendant_count(); let iter_count = super::TreeIter::new(&tree).count(); assert_eq!(node_count, *expected_count); assert_eq!(node_count, iter_count); } } }