tree_sitter_fsharp/
lib.rs

1//! This crate provides Fsharp 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//! let code = r#"
8//! "#;
9//! let mut parser = tree_sitter::Parser::new();
10//! let language = tree_sitter_fsharp::LANGUAGE_FSHARP;
11//! parser
12//!     .set_language(&language.into())
13//!     .expect("Error loading Fsharp parser");
14//! let tree = parser.parse(code, None).unwrap();
15//! assert!(!tree.root_node().has_error());
16//! ```
17//!
18//! [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
19//! [language func]: fn.language.html
20//! [Parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html
21//! [tree-sitter]: https://tree-sitter.github.io/
22
23use tree_sitter_language::LanguageFn;
24
25extern "C" {
26    fn tree_sitter_fsharp() -> *const ();
27    fn tree_sitter_fsharp_signature() -> *const ();
28}
29
30/// The tree-sitter [`LanguageFn`] for this grammar.
31pub const LANGUAGE_FSHARP: LanguageFn = unsafe { LanguageFn::from_raw(tree_sitter_fsharp) };
32pub const LANGUAGE_SIGNATURE: LanguageFn = unsafe { LanguageFn::from_raw(tree_sitter_fsharp_signature) };
33
34/// The content of the [`node-types.json`][] file for this grammar.
35///
36/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
37pub const FSHARP_NODE_TYPES: &str = include_str!("../../fsharp/src/node-types.json");
38pub const SIGNATURE_NODE_TYPES: &str = include_str!("../../fsharp_signature/src/node-types.json");
39
40// NOTE: uncomment these to include any queries that this grammar contains:
41
42pub const HIGHLIGHTS_QUERY: &str = include_str!("../../queries/highlights.scm");
43pub const INJECTIONS_QUERY: &str = include_str!("../../queries/injections.scm");
44pub const LOCALS_QUERY: &str = include_str!("../../queries/locals.scm");
45pub const TAGS_QUERY: &str = include_str!("../../queries/tags.scm");
46
47#[cfg(test)]
48mod tests {
49    #[test]
50    fn test_fsharp() {
51        let mut parser = tree_sitter::Parser::new();
52        parser
53            .set_language(&super::LANGUAGE_FSHARP.into())
54            .expect("Error loading Fsharp parser");
55
56        let code = r#"
57            module M =
58              let x = 0
59        "#;
60
61        let tree = parser.parse(code, None).unwrap();
62        let root = tree.root_node();
63        assert!(!root.has_error());
64    }
65
66    #[test]
67    fn test_fsharp_signature() {
68        let mut parser = tree_sitter::Parser::new();
69        parser
70            .set_language(&super::LANGUAGE_SIGNATURE.into())
71            .expect("Error loading Fsharp parser");
72
73        let code = r#"
74            module M =
75              val x : int -> int
76        "#;
77
78        let tree = parser.parse(code, None).unwrap();
79        let root = tree.root_node();
80        assert!(!root.has_error());
81    }
82}