Skip to main content

tree_sitter_math/
lib.rs

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