Skip to main content

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 fsharp 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/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers/6-static-node-types.html
36pub const FSHARP_NODE_TYPES: &str = include_str!("../../fsharp/src/node-types.json");
37
38/// The content of the [`node-types.json`][] file for the signature grammar.
39/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers/6-static-node-types.html
40pub const SIGNATURE_NODE_TYPES: &str = include_str!("../../fsharp_signature/src/node-types.json");
41
42// NOTE: uncomment these to include any queries that this grammar contains:
43
44pub const HIGHLIGHTS_QUERY: &str = include_str!("../../queries/highlights.scm");
45pub const INJECTIONS_QUERY: &str = include_str!("../../queries/injections.scm");
46pub const LOCALS_QUERY: &str = include_str!("../../queries/locals.scm");
47pub const TAGS_QUERY: &str = include_str!("../../queries/tags.scm");
48
49#[cfg(test)]
50mod tests {
51    #[test]
52    fn test_fsharp() {
53        let mut parser = tree_sitter::Parser::new();
54        parser
55            .set_language(&super::LANGUAGE_FSHARP.into())
56            .expect("Error loading Fsharp parser");
57
58        let code = r#"
59            module M =
60              let x = 0
61        "#;
62
63        let tree = parser.parse(code, None).unwrap();
64        let root = tree.root_node();
65        assert!(!root.has_error());
66    }
67
68    #[test]
69    fn test_fsharp_signature() {
70        let mut parser = tree_sitter::Parser::new();
71        parser
72            .set_language(&super::LANGUAGE_SIGNATURE.into())
73            .expect("Error loading Fsharp parser");
74
75        let code = r#"
76            module M =
77              val x : int -> int
78        "#;
79
80        let tree = parser.parse(code, None).unwrap();
81        let root = tree.root_node();
82        assert!(!root.has_error());
83    }
84}