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