tree_sitter_cuda/lib.rs
1//! This crate provides CUDA 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//! __global__ void cuda_hello(){
9//! printf("Hello World from GPU!\n");
10//! }
11//!
12//! int main() {
13//! cuda_hello<<<1,1>>>();
14//! return 0;
15//! }
16//! "#;
17//! let mut parser = tree_sitter::Parser::new();
18//! let language = tree_sitter_cuda::LANGUAGE;
19//! parser
20//! .set_language(&language.into())
21//! .expect("Error loading CUDA parser");
22//! let tree = parser.parse(code, None).unwrap();
23//! assert!(!tree.root_node().has_error());
24//! ```
25//!
26//! [`Parser`]: https://docs.rs/tree-sitter/0.25.4/tree_sitter/struct.Parser.html
27//! [tree-sitter]: https://tree-sitter.github.io/
28
29use tree_sitter_language::LanguageFn;
30
31extern "C" {
32 fn tree_sitter_cuda() -> *const ();
33}
34
35/// The tree-sitter [`LanguageFn`] for this grammar.
36pub const LANGUAGE: LanguageFn = unsafe { LanguageFn::from_raw(tree_sitter_cuda) };
37
38/// The content of the [`node-types.json`] file for this grammar.
39///
40/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers/6-static-node-types
41pub const NODE_TYPES: &str = include_str!("../../src/node-types.json");
42
43/// The highlights queries for this grammar.
44pub const HIGHLIGHTS_QUERY: &str = include_str!("../../queries/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 CUDA parser");
54 }
55}