tree_sitter_csv/
lib.rs

1//! This crate provides CSV, PSV, & TSV 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//! "Name","Age","Salary"
9//! "John Doe",30,120000
10//! "#;
11//! let mut parser = tree_sitter::Parser::new();
12//! parser.set_language(tree_sitter_csv::language_csv()).expect("Error loading CSV grammar");
13//! let tree = parser.parse(code, None).unwrap();
14//!
15//! let code = r"
16//! Name|Age|Salary
17//! John Doe|30|120000
18//! ";
19//! let mut parser = tree_sitter::Parser::new();
20//! parser.set_language(tree_sitter_csv::language_tsv()).expect("Error loading PSV grammar");
21//! let tree = parser.parse(code, None).unwrap();
22//!
23//! let code = r"
24//! Name\tAge\tSalary
25//! John Doe\t30\t120000
26//! ";
27//! let mut parser = tree_sitter::Parser::new();
28//! parser.set_language(tree_sitter_csv::language_tsv()).expect("Error loading TSV grammar");
29//! let tree = parser.parse(code, None).unwrap();
30//! ```
31//!
32//! [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
33//! [language func]: fn.language.html
34//! [Parser]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Parser.html
35//! [tree-sitter]: https://tree-sitter.github.io/
36
37use tree_sitter::Language;
38
39extern "C" {
40    fn tree_sitter_csv() -> Language;
41    fn tree_sitter_psv() -> Language;
42    fn tree_sitter_tsv() -> Language;
43}
44
45/// Get the tree-sitter [Language][] for this grammar.
46///
47/// [Language]: https://docs.rs/tree-sitter/*/tree_sitter/struct.Language.html
48pub fn language_csv() -> Language {
49    unsafe { tree_sitter_csv() }
50}
51
52pub fn language_psv() -> Language {
53    unsafe { tree_sitter_psv() }
54}
55
56pub fn language_tsv() -> Language {
57    unsafe { tree_sitter_tsv() }
58}
59
60/// The content of the [`grammar.json`][] file for CSV.
61pub const GRAMMAR_JSON_CSV: &str = include_str!("../../csv/src/grammar.json");
62
63/// The content of the [`grammar.json`][] file for PSV.
64pub const GRAMMAR_JSON_PSV: &str = include_str!("../../psv/src/grammar.json");
65
66/// The content of the [`grammar.json`][] file for TSV.
67pub const GRAMMAR_JSON_TSV: &str = include_str!("../../tsv/src/grammar.json");
68
69/// The content of the [`node-types.json`][] file for CSV.
70///
71/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
72pub const NODE_TYPES_CSV: &str = include_str!("../../csv/src/node-types.json");
73
74/// The content of the [`node-types.json`][] file for PSV.
75///
76/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
77pub const NODE_TYPES_PSV: &str = include_str!("../../psv/src/node-types.json");
78
79/// The content of the [`node-types.json`][] file for TSV.
80///
81/// [`node-types.json`]: https://tree-sitter.github.io/tree-sitter/using-parsers#static-node-types
82pub const NODE_TYPES_TSV: &str = include_str!("../../tsv/src/node-types.json");
83
84/// The syntax highlighting query for CSV.
85pub const HIGHLIGHT_QUERY_CSV: &str = include_str!("../../csv/queries/highlights.scm");
86
87/// The syntax highlighting query for PSV.
88pub const HIGHLIGHT_QUERY_PSV: &str = include_str!("../../psv/queries/highlights.scm");
89
90/// The syntax highlighting query for TSV.
91pub const HIGHLIGHT_QUERY_TSV: &str = include_str!("../../tsv/queries/highlights.scm");
92
93#[cfg(test)]
94mod tests {
95    #[test]
96    fn test_can_load_grammar() {
97        let mut parser = tree_sitter::Parser::new();
98        parser
99            .set_language(super::language_csv())
100            .expect("Error loading CSV grammar");
101
102        let mut parser = tree_sitter::Parser::new();
103        parser
104            .set_language(super::language_psv())
105            .expect("Error loading PSV grammar");
106
107        let mut parser = tree_sitter::Parser::new();
108        parser
109            .set_language(super::language_tsv())
110            .expect("Error loading TSV grammar");
111    }
112}