tree_sitter_cpp/lib.rs
1//! This crate provides C++ language support for the [tree-sitter] parsing library.
2//!
3//! Use [willbooster-tree-sitter] 19.1.1 or later in the 19.x series. The parser carries
4//! WillBooster/tree-sitter ABI `0x5742000c`, which upstream Tree-sitter runtimes reject, and omits
5//! unused `TSLanguage` tables that pre-19.1.0 WillBooster runtimes (`17.x` and `18.x`) dereference
6//! despite sharing ABI `0x5742000c`.
7//!
8//! Typically, you will use the [`LANGUAGE`] constant to add this language to a
9//! tree-sitter [`Parser`], and then use the parser to parse some code:
10//!
11//! ```
12//! let code = "#include <iostream>\n\nint main() {\n std::cout << \"Hello\\n\";\n}\n";
13//! let mut parser = tree_sitter::Parser::new();
14//! let language = tree_sitter_cpp::LANGUAGE;
15//! parser
16//! .set_language(&language.into())
17//! .expect("Error loading C++ parser");
18//! let tree = parser.parse(code, None).unwrap();
19//! assert!(!tree.root_node().has_error());
20//! ```
21//!
22//! [`Parser`]: https://docs.rs/willbooster-tree-sitter/19.1.1/tree_sitter/struct.Parser.html
23//! [willbooster-tree-sitter]: https://crates.io/crates/willbooster-tree-sitter
24//! [tree-sitter]: https://tree-sitter.github.io/
25
26use tree_sitter_language::LanguageFn;
27
28unsafe extern "C" {
29 fn tree_sitter_cpp() -> *const ();
30}
31
32/// The tree-sitter [`LanguageFn`] for this grammar.
33pub const LANGUAGE: LanguageFn = unsafe { LanguageFn::from_raw(tree_sitter_cpp) };
34
35/// The content of the [`node-types.json`] file for this grammar.
36///
37/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers/6-static-node-types
38pub const NODE_TYPES: &str = include_str!("../../src/node-types.json");
39
40#[cfg(test)]
41mod tests {
42 #[test]
43 fn test_can_load_grammar() {
44 let mut parser = tree_sitter::Parser::new();
45 parser
46 .set_language(&super::LANGUAGE.into())
47 .expect("Error loading C++ parser");
48 }
49}