treesitter_types_cpp/lib.rs
1//! Strongly-typed AST types for C++, auto-generated from
2//! [`tree-sitter-cpp`](https://docs.rs/tree-sitter-cpp)'s `node-types.json`.
3//!
4//! This crate is generated by [`treesitter-types`](https://docs.rs/treesitter-types) and is
5//! automatically kept up to date when a new version of the grammar crate is released.
6//!
7//! These types have been tested by parsing the
8//! [nlohmann/json](https://github.com/nlohmann/json) source code.
9//!
10//! See the [Tree-sitter](https://tree-sitter.github.io/tree-sitter/) project for more
11//! information about the underlying parser framework.
12//!
13//! # Example
14//!
15//! ```
16//! use treesitter_types_cpp::*;
17//!
18//! // A minimal C++ hello-world program.
19//! let src = b"\
20//! #include <iostream>
21//!
22//! int main() {
23//! std::cout << \"Hello, World!\" << std::endl;
24//! return 0;
25//! }
26//! ";
27//!
28//! // Parse the source with tree-sitter and convert into typed AST.
29//! let mut parser = tree_sitter::Parser::new();
30//! parser.set_language(&tree_sitter_cpp::LANGUAGE.into()).unwrap();
31//! let tree = parser.parse(src, None).unwrap();
32//! let tu = TranslationUnit::from_node(tree.root_node(), src).unwrap();
33//!
34//! // The translation unit has two top-level children.
35//! assert_eq!(tu.children.len(), 2);
36//!
37//! // 1) The #include directive.
38//! let TranslationUnitChildren::PreprocInclude(include) = &tu.children[0] else {
39//! panic!("expected a preproc include");
40//! };
41//! assert_eq!(include.span.start.row, 0);
42//!
43//! // 2) The `main` function definition.
44//! let TranslationUnitChildren::FunctionDefinition(func) = &tu.children[1] else {
45//! panic!("expected a function definition");
46//! };
47//! assert!(func.body.is_some()); // has a function body
48//! ```
49
50pub use tree_sitter_cpp;
51pub use treesitter_types::tree_sitter;
52pub use treesitter_types::{FromNode, LeafNode, ParseError, Span, Spanned};
53
54mod generated;
55pub use generated::*;