tree_sitter_nix/lib.rs
1//! This crate provides nix language support for the [tree-sitter][] parsing library.
2//!
3//! Typically, you will use the [language][language func] function to add this language to a
4//! tree-sitter [Parser][], and then use the parser to parse some code:
5//!
6//! ```
7//! use tree_sitter::Parser;
8//!
9//! let code = r#"
10//! let
11//! b = a + 1;
12//! a = 1;
13//! in
14//! a + b
15//! "#;
16//! let mut parser = Parser::new();
17//! let language = tree_sitter_nix::LANGUAGE;
18//! parser
19//! .set_language(&language.into())
20//! .expect("Error loading nix parser");
21//! let tree = parser.parse(code, None).unwrap();
22//! assert!(!tree.root_node().has_error());
23//! ```
24//!
25//! [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
26//! [language func]: fn.language.html
27//! [Parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html
28//! [tree-sitter]: https://tree-sitter.github.io/
29
30use tree_sitter_language::LanguageFn;
31
32extern "C" {
33 fn tree_sitter_nix() -> *const ();
34}
35
36/// The tree-sitter [`LanguageFn`] for this grammar.
37pub const LANGUAGE: LanguageFn = unsafe { LanguageFn::from_raw(tree_sitter_nix) };
38
39/// The content of the [`node-types.json`][] file for this grammar.
40///
41/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
42pub const NODE_TYPES: &'static str = include_str!("../../src/node-types.json");
43
44// Uncomment these to include any queries that this grammar contains
45
46/// The syntax highlighting query for this language.
47pub const HIGHLIGHTS_QUERY: &'static str = include_str!("../../queries/highlights.scm");
48
49/// The injections query for this language.
50pub const INJECTIONS_QUERY: &'static str = include_str!("../../queries/injections.scm");
51
52// The locals tagging query for this language.
53// pub const LOCALS_QUERY: &'static str = include_str!("../../queries/locals.scm");
54
55/// The symbol tagging query for this language.
56// pub const TAGS_QUERY: &'static str = include_str!("../../queries/tags.scm");
57
58#[cfg(test)]
59mod tests {
60 #[test]
61 fn test_can_load_grammar() {
62 let mut parser = tree_sitter::Parser::new();
63 parser
64 .set_language(&super::LANGUAGE.into())
65 .expect("Error loading nix parser");
66 }
67}