dekobon_tree_sitter_groovy/lib.rs
1//! This crate provides Apache Groovy language support for the [tree-sitter][] parsing library.
2//!
3//! Typically, you will use the [LANGUAGE][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//! def greet(name) {
9//! def msg = name ?: 'World'
10//! println "Hello, ${msg}!"
11//! }
12//! greet 'Groovy'
13//! "#;
14//! let mut parser = tree_sitter::Parser::new();
15//! let language = dekobon_tree_sitter_groovy::LANGUAGE;
16//! parser
17//! .set_language(&language.into())
18//! .expect("Error loading Groovy parser");
19//! let tree = parser.parse(code, None).unwrap();
20//! assert!(!tree.root_node().has_error());
21//! ```
22//!
23//! [LANGUAGE]: crate::LANGUAGE
24//! [Parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html
25//! [tree-sitter]: https://tree-sitter.github.io/
26
27use tree_sitter_language::LanguageFn;
28
29unsafe extern "C" {
30 fn tree_sitter_groovy() -> *const ();
31}
32
33/// The tree-sitter [`LanguageFn`] for this grammar.
34pub const LANGUAGE: LanguageFn = unsafe { LanguageFn::from_raw(tree_sitter_groovy) };
35
36/// The content of the [`node-types.json`][] file for this grammar.
37///
38/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
39pub const NODE_TYPES: &str = include_str!("../../src/node-types.json");
40
41/// The content of the [`highlights.scm`][] query for this grammar.
42///
43/// [`highlights.scm`]: https://tree-sitter.github.io/tree-sitter/syntax-highlighting#highlights
44pub const HIGHLIGHTS_QUERY: &str = include_str!("../../queries/groovy/highlights.scm");
45
46#[cfg(test)]
47mod tests {
48 #[test]
49 fn test_can_load_grammar() {
50 let mut parser = tree_sitter::Parser::new();
51 parser
52 .set_language(&super::LANGUAGE.into())
53 .expect("Error loading Groovy parser");
54 }
55}