Files

48 lines
1.4 KiB
Rust
Raw Permalink Normal View History

2025-05-09 10:06:33 +03:00
//! This crate provides HCL language support for the [tree-sitter] parsing library.
2022-06-18 04:40:10 -06:00
//!
2025-05-09 10:06:33 +03:00
//! Typically, you will use the [`LANGUAGE`] constant to add this language to a
//! tree-sitter [`Parser`], and then use the parser to parse some code:
2022-06-18 04:40:10 -06:00
//!
//! ```
//! let code = r#"
2025-05-09 10:06:33 +03:00
//! example "test" {
//! foo = "bar"
//! }
//! "#;
2022-06-18 04:40:10 -06:00
//! let mut parser = tree_sitter::Parser::new();
2025-05-09 10:06:33 +03:00
//! let language = tree_sitter_hcl::LANGUAGE;
//! parser
//! .set_language(&language.into())
2025-05-09 10:06:33 +03:00
//! .expect("Error loading HCL parser");
2022-06-18 04:40:10 -06:00
//! let tree = parser.parse(code, None).unwrap();
//! assert!(!tree.root_node().has_error());
2022-06-18 04:40:10 -06:00
//! ```
//!
2025-05-09 10:06:33 +03:00
//! [`Parser`]: https://docs.rs/tree-sitter/0.25.3/tree_sitter/struct.Parser.html
2022-06-18 04:40:10 -06:00
//! [tree-sitter]: https://tree-sitter.github.io/
use tree_sitter_language::LanguageFn;
2022-06-18 04:40:10 -06:00
extern "C" {
fn tree_sitter_hcl() -> *const ();
2022-06-18 04:40:10 -06:00
}
2025-05-09 10:06:33 +03:00
/// The tree-sitter [`LanguageFn`] for this grammar.
pub const LANGUAGE: LanguageFn = unsafe { LanguageFn::from_raw(tree_sitter_hcl) };
2022-06-18 04:40:10 -06:00
2025-05-09 10:06:33 +03:00
/// The content of the [`node-types.json`] file for this grammar.
2022-06-18 04:40:10 -06:00
///
/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers/6-static-node-types
pub const NODE_TYPES: &str = include_str!("../../src/node-types.json");
2022-06-18 04:40:10 -06:00
#[cfg(test)]
mod tests {
#[test]
fn test_can_load_grammar() {
let mut parser = tree_sitter::Parser::new();
parser
.set_language(&super::LANGUAGE.into())
2025-05-09 10:06:33 +03:00
.expect("Error loading HCL parser");
2022-06-18 04:40:10 -06:00
}
}