tree_sitter_beancount/lib.rs
1#![allow(dead_code)]
2//! This crate provides beancount language support for the [tree-sitter][] parsing library.
3//!
4//! Typically, you will use the [language][language func] function to add this language to a
5//! tree-sitter [Parser][], and then use the parser to parse some code:
6//!
7//! ```
8//! let code = "";
9//! let mut parser = tree_sitter_beancount::tree_sitter::Parser::new();
10//! parser.set_language(&tree_sitter_beancount::language()).expect("Error loading beancount grammar");
11//! let tree = parser.parse(code, None).unwrap();
12//! ```
13//!
14//! This crate re-exports `tree-sitter` for convenience, so you don't need to add it as a separate dependency.
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::Language;
22
23// Re-export tree-sitter for user convenience
24pub use tree_sitter;
25
26extern "C" {
27 fn tree_sitter_beancount() -> Language;
28}
29
30/// Get the tree-sitter [Language][] for this grammar.
31///
32/// [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
33pub fn language() -> Language {
34 unsafe { tree_sitter_beancount() }
35}
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
42mod node_kind;
43pub use node_kind::LeafNodeKind;
44pub use node_kind::NodeKind;
45
46// Uncomment these to include any queries that this grammar contains
47
48// pub const HIGHLIGHTS_QUERY: &'static str = include_str!("../../queries/highlights.scm");
49// pub const INJECTIONS_QUERY: &'static str = include_str!("../../queries/injections.scm");
50// pub const LOCALS_QUERY: &'static str = include_str!("../../queries/locals.scm");
51// pub const TAGS_QUERY: &'static str = include_str!("../../queries/tags.scm");
52
53#[cfg(test)]
54mod tests {
55 #[test]
56 fn test_can_load_grammar() {
57 let mut parser = super::tree_sitter::Parser::new();
58 parser
59 .set_language(&super::language())
60 .expect("Error loading beancount language");
61 }
62
63 #[test]
64 fn test_leaf_node_kind_from_str() {
65 use super::LeafNodeKind;
66
67 assert_eq!(LeafNodeKind::from("account"), LeafNodeKind::Account);
68 assert_eq!(LeafNodeKind::from("string"), LeafNodeKind::String);
69 assert_eq!(LeafNodeKind::from("number"), LeafNodeKind::Number);
70 assert_eq!(LeafNodeKind::from("flag"), LeafNodeKind::Flag);
71 assert_eq!(LeafNodeKind::from("does_not_exist"), LeafNodeKind::Unknown);
72 }
73}