tree_sitter_cpp/
lib.rs

1//! This crate provides C++ language support for the [tree-sitter][] parsing library.
2//!
3//! Typically, you will use the [LANGUAGE][] constant to add this language to a
4//! tree-sitter [Parser][], and then use the parser to parse some code:
5//!
6//! ```
7//! use tree_sitter::Parser;
8//!
9//! let code = r#"
10//! int double(int x) {
11//!     return x * 2;
12//! }
13//! "#;
14//! let mut parser = tree_sitter::Parser::new();
15//! let language = tree_sitter_cpp::LANGUAGE;
16//! parser
17//!     .set_language(&language.into())
18//!     .expect("Error loading C++ parser");
19//! let tree = parser.parse(code, None).unwrap();
20//! assert!(!tree.root_node().has_error());
21//! ```
22//!
23//! [Parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html
24//! [tree-sitter]: https://tree-sitter.github.io/
25
26use tree_sitter_language::LanguageFn;
27
28extern "C" {
29    fn tree_sitter_cpp() -> *const ();
30}
31
32/// The tree-sitter [`LanguageFn`][LanguageFn] for this grammar.
33///
34/// [LanguageFn]: https://docs.rs/tree-sitter-language/*/tree_sitter_language/struct.LanguageFn.html
35pub const LANGUAGE: LanguageFn = unsafe { LanguageFn::from_raw(tree_sitter_cpp) };
36
37/// The content of the [`node-types.json`][] file for this grammar.
38///
39/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
40pub const NODE_TYPES: &str = include_str!("../../src/node-types.json");
41
42/// The syntax highlighting query for this language.
43pub const HIGHLIGHT_QUERY: &str = include_str!("../../queries/highlights.scm");
44
45/// The symbol tagging query for this language.
46pub const TAGS_QUERY: &str = include_str!("../../queries/tags.scm");
47
48#[cfg(test)]
49mod tests {
50    #[test]
51    fn test_can_load_grammar() {
52        let mut parser = tree_sitter::Parser::new();
53        parser
54            .set_language(&super::LANGUAGE.into())
55            .expect("Error loading C++ parser");
56    }
57}