tree_sitter_solidity_unofficial/lib.rs
1//! This crate provides Solidity language support for the [tree-sitter][] parsing library.
2//!
3//! The bindings in https://github.com/tree-sitter/tree-sitter-javascript/blob/master/bindings/rust were used
4//! as a template for the tree-sitter-solidity rust bindings.
5//!
6//! Typically, you will use the [language][language func] function to add this language to a
7//! tree-sitter [Parser][], and then use the parser to parse some code:
8//!
9//! ```
10//! let code = "";
11//! let mut parser = tree_sitter::Parser::new();
12//! parser.set_language(tree_sitter_solidity::language()).expect("Error loading Solidity grammar");
13//! let tree = parser.parse(code, None).unwrap();
14//! ```
15//!
16//! [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
17//! [language func]: fn.language.html
18//! [Parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html
19//! [tree-sitter]: https://tree-sitter.github.io/
20
21use tree_sitter_unofficial::Language;
22
23extern "C" {
24 fn tree_sitter_solidity() -> Language;
25}
26
27/// Get the tree-sitter [Language][] for this grammar.
28///
29/// [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
30pub fn language() -> Language {
31 unsafe { tree_sitter_solidity() }
32}
33
34/// The source of the Solidity tree-sitter grammar description.
35pub const GRAMMAR: &'static str = include_str!("../../grammar.js");
36
37/// The content of the [`node-types.json`][] file for this grammar.
38///
39/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
40pub const NODE_TYPES: &'static str = include_str!("../../src/node-types.json");
41
42// Uncomment these to include any queries that this grammar contains
43
44pub const HIGHLIGHTS_QUERY: &'static str = include_str!("../../queries/highlights.scm");
45// pub const INJECTIONS_QUERY: &'static str = include_str!("../../queries/injections.scm");
46// pub const LOCALS_QUERY: &'static str = include_str!("../../queries/locals.scm");
47// pub const TAGS_QUERY: &'static str = include_str!("../../queries/tags.scm");
48
49#[cfg(test)]
50mod tests {
51 #[test]
52 fn test_can_load_grammar() {
53 let mut parser = tree_sitter::Parser::new();
54 parser
55 .set_language(super::language())
56 .expect("Error loading Solidity language");
57 }
58}