tree_sitter_sfapex/
lib.rs

1//! This crate provides Salesforce Apex, SOQL, SOSL, and Log syntax support for the [tree-sitter] parsing library.
2//!
3//! Typically, you will use the [apex::LANGUAGE] constant 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//! public class Test123 {
11//!   public Test123(){}
12//! }
13//! "#;
14//! let mut parser = Parser::new();
15//! let language_fn = tree_sitter_sfapex::apex::LANGUAGE;
16//! parser
17//!     .set_language(&language_fn.into())
18//!     .expect("Error loading Apex parser");
19//! let tree = parser.parse(code, None).unwrap();
20//! assert!(!tree.root_node().has_error());
21//! ```
22//!
23//! [Parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html
24//! [tree-sitter]: https://tree-sitter.github.io/
25
26pub mod apex {
27    use tree_sitter_language::LanguageFn;
28    extern "C" {
29        fn tree_sitter_apex() -> *const ();
30    }
31
32    /// Returns the tree-sitter [LanguageFn] for this grammar.
33    ///
34    /// [LanguageFn]: https://docs.rs/tree-sitter-language/*/tree_sitter_language/struct.LanguageFn.html
35    pub const LANGUAGE: LanguageFn = unsafe { LanguageFn::from_raw(tree_sitter_apex) };
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
40    pub const NODE_TYPES: &str = include_str!("../../apex/src/node-types.json");
41
42    /// The syntax highlighting query for this language.
43    pub const HIGHLIGHTS_QUERY: &str = include_str!("../../apex/queries/highlights.scm");
44
45    // /// The injections query for this language.
46    // pub const INJECTIONS_QUERY: &str = include_str!("../../apex/queries/injections.scm");
47
48    /// The symbol tagging query for this language.
49    pub const TAGS_QUERY: &str = include_str!("../../apex/queries/tags.scm");
50}
51
52pub mod soql {
53    use tree_sitter_language::LanguageFn;
54    extern "C" {
55        fn tree_sitter_soql() -> *const ();
56    }
57
58    /// Returns the tree-sitter [LanguageFn] for this grammar.
59    ///
60    /// [LanguageFn]: https://docs.rs/tree-sitter-language/*/tree_sitter_language/struct.LanguageFn.html
61    pub const LANGUAGE: LanguageFn = unsafe { LanguageFn::from_raw(tree_sitter_soql) };
62
63    /// The content of the [`node-types.json`] file for this grammar.
64    ///
65    /// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
66    pub const NODE_TYPES: &str = include_str!("../../soql/src/node-types.json");
67
68    /// The syntax highlighting query for this language.
69    pub const HIGHLIGHTS_QUERY: &str = include_str!("../../soql/queries/highlights.scm");
70
71    // /// The injections query for this language.
72    // pub const INJECTIONS_QUERY: &str = include_str!("../../soql/queries/injections.scm");
73
74    // /// The symbol tagging query for this language.
75    // pub const TAGS_QUERY: &str = include_str!("../../soql/queries/tags.scm");
76}
77
78pub mod sosl {
79    use tree_sitter_language::LanguageFn;
80    extern "C" {
81        fn tree_sitter_sosl() -> *const ();
82    }
83
84    /// Returns the tree-sitter [LanguageFn] for this grammar.
85    ///
86    /// [LanguageFn]: https://docs.rs/tree-sitter-language/*/tree_sitter_language/struct.LanguageFn.html
87    pub const LANGUAGE: LanguageFn = unsafe { LanguageFn::from_raw(tree_sitter_sosl) };
88
89    /// The content of the [`node-types.json`] file for this grammar.
90    ///
91    /// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
92    pub const NODE_TYPES: &str = include_str!("../../sosl/src/node-types.json");
93
94    /// The syntax highlighting query for this language.
95    pub const HIGHLIGHTS_QUERY: &str = include_str!("../../sosl/queries/highlights.scm");
96
97    // /// The injections query for this language.
98    // pub const INJECTIONS_QUERY: &str = include_str!("../../sosl/queries/injections.scm");
99
100    // /// The symbol tagging query for this language.
101    // pub const TAGS_QUERY: &str = include_str!("../../sosl/queries/tags.scm");
102}
103
104pub mod sflog {
105    use tree_sitter_language::LanguageFn;
106    extern "C" {
107        fn tree_sitter_sflog() -> *const ();
108    }
109
110    /// Returns the tree-sitter [LanguageFn] for this grammar.
111    ///
112    /// [LanguageFn]: https://docs.rs/tree-sitter-language/*/tree_sitter_language/struct.LanguageFn.html
113    pub const LANGUAGE: LanguageFn = unsafe { LanguageFn::from_raw(tree_sitter_sflog) };
114
115    /// The content of the [`node-types.json`] file for this grammar.
116    ///
117    /// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
118    pub const NODE_TYPES: &str = include_str!("../../sflog/src/node-types.json");
119
120    /// The syntax highlighting query for this language.
121    pub const HIGHLIGHTS_QUERY: &str = include_str!("../../sflog/queries/highlights.scm");
122
123    // /// The injections query for this language.
124    // pub const INJECTIONS_QUERY: &str = include_str!("../../sflog/queries/injections.scm");
125
126    // /// The symbol tagging query for this language.
127    // pub const TAGS_QUERY: &str = include_str!("../../sflog/queries/tags.scm");
128}
129
130#[cfg(test)]
131mod tests {
132    use tree_sitter::Parser;
133
134    // Helper function to test language loading
135    fn test_language_loading(language_fn: tree_sitter_language::LanguageFn) {
136        let mut parser = Parser::new();
137        parser
138            .set_language(&language_fn.into())
139            .expect("Failed to load language parser");
140    }
141
142    #[test]
143    fn test_can_load_apex_grammar() {
144        test_language_loading(super::apex::LANGUAGE);
145    }
146
147    #[test]
148    fn test_can_load_soql_grammar() {
149        test_language_loading(super::soql::LANGUAGE);
150    }
151
152    #[test]
153    fn test_can_load_sosl_grammar() {
154        test_language_loading(super::sosl::LANGUAGE);
155    }
156
157    #[test]
158    fn test_can_load_sflog_grammar() {
159        test_language_loading(super::sflog::LANGUAGE);
160    }
161}