1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//! This crate provides CSV & TSV language support for the [tree-sitter][] parsing library.
//!
//! Typically, you will use the [language][language func] function to add this language to a
//! tree-sitter [Parser][], and then use the parser to parse some code:
//!
//! ```
//! let code = r#"
//! "Name","Age","Salary"
//! "John Doe",30,120000
//! "#;
//! let mut parser = tree_sitter::Parser::new();
//! parser.set_language(tree_sitter_csv::language_csv()).expect("Error loading CSV grammar");
//! let tree = parser.parse(code, None).unwrap();
//!
//! let code = r"
//! Name\tAge\tSalary
//! John Doe\t30\t120000
//! ";
//! let mut parser = tree_sitter::Parser::new();
//! parser.set_language(tree_sitter_csv::language_tsv()).expect("Error loading TSV grammar");
//! let tree = parser.parse(code, None).unwrap();
//! ```
//!
//! [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
//! [language func]: fn.language.html
//! [Parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html
//! [tree-sitter]: https://tree-sitter.github.io/

use tree_sitter::Language;

extern "C" {
    fn tree_sitter_csv() -> Language;
    fn tree_sitter_tsv() -> Language;
}

/// Get the tree-sitter [Language][] for this grammar.
///
/// [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
pub fn language_csv() -> Language {
    unsafe { tree_sitter_csv() }
}

pub fn language_tsv() -> Language {
    unsafe { tree_sitter_tsv() }
}

/// The content of the [`grammar.json`][] file for CSV.
pub const GRAMMAR_JSON_CSV: &str = include_str!("../../csv/src/grammar.json");

/// The content of the [`grammar.json`][] file for TSV.
pub const GRAMMAR_JSON_TSV: &str = include_str!("../../tsv/src/grammar.json");

/// The content of the [`node-types.json`][] file for CSV.
///
/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
pub const NODE_TYPES_CSV: &str = include_str!("../../csv/src/node-types.json");

/// The content of the [`node-types.json`][] file for TSV.
///
/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
pub const NODE_TYPES_TSV: &str = include_str!("../../tsv/src/node-types.json");

/// The syntax highlighting query for CSV.
pub const HIGHLIGHT_QUERY_CSV: &str = include_str!("../../csv/queries/highlights.scm");

/// The syntax highlighting query for TSV.
pub const HIGHLIGHT_QUERY_TSV: &str = include_str!("../../tsv/queries/highlights.scm");

#[cfg(test)]
mod tests {
    #[test]
    fn test_can_load_grammar() {
        let mut parser = tree_sitter::Parser::new();
        parser
            .set_language(super::language_csv())
            .expect("Error loading CSV grammar");

        let mut parser = tree_sitter::Parser::new();
        parser
            .set_language(super::language_tsv())
            .expect("Error loading TSV grammar");

        let code = r"
        Name\tAge\tSalary
        John Doe\t30\t120000
        ";
        let tree = parser.parse(code, None).unwrap();
        println!("{:#?}", tree.root_node().to_sexp());
    }
}