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