-
Notifications
You must be signed in to change notification settings - Fork 20
/
language.rs
164 lines (151 loc) · 5.29 KB
/
language.rs
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use anyhow::{anyhow, Error, Result};
use std::str::FromStr;
use strum::{IntoEnumIterator, VariantNames};
use strum_macros::{Display, EnumIter, FromRepr, VariantNames};
#[derive(Display, FromRepr, EnumIter, VariantNames, PartialEq, Eq, Hash, Debug)]
#[strum(serialize_all = "lowercase")]
pub enum Language {
C,
Cpp,
Cuda,
Elixir,
Elm,
Go,
Haskell,
Java,
JavaScript,
Markdown,
Nix,
Php,
Python,
Ruby,
Rust,
Sass,
TypeScript,
}
impl Language {
pub fn all() -> Vec<Language> {
Language::iter().collect()
}
pub fn language(&self) -> tree_sitter::Language {
unsafe {
match self {
Language::C => tree_sitter_c(),
Language::Cpp => tree_sitter_cpp(),
Language::Cuda => tree_sitter_cuda(),
Language::Elixir => tree_sitter_elixir(),
Language::Elm => tree_sitter_elm(),
Language::Go => tree_sitter_go(),
Language::Haskell => tree_sitter_haskell(),
Language::Java => tree_sitter_java(),
Language::JavaScript => tree_sitter_javascript(),
Language::Markdown => tree_sitter_markdown(),
Language::Nix => tree_sitter_nix(),
Language::Php => tree_sitter_php(),
Language::Python => tree_sitter_python(),
Language::Ruby => tree_sitter_ruby(),
Language::Rust => tree_sitter_rust(),
Language::Sass => tree_sitter_scss(),
Language::TypeScript => tree_sitter_typescript(),
}
}
}
pub fn parse_query(&self, raw: &str) -> Result<tree_sitter::Query> {
tree_sitter::Query::new(&self.language(), raw).map_err(|err| anyhow!("{}", err))
}
pub fn name_for_types_builder(&self) -> &str {
match self {
Language::C => "c",
Language::Cpp => "cpp",
Language::Cuda => "cuda",
Language::Elixir => "elixir",
Language::Elm => "elm",
Language::Go => "go",
Language::Haskell => "haskell",
Language::Java => "java",
Language::JavaScript => "js",
Language::Markdown => "markdown",
Language::Nix => "nix",
Language::Php => "php",
Language::Python => "py",
Language::Ruby => "ruby",
Language::Rust => "rust",
Language::Sass => "sass",
Language::TypeScript => "ts",
}
}
}
impl FromStr for Language {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
let languages = Language::VARIANTS;
languages
.binary_search(&s)
.map(|idx| Language::from_repr(idx).unwrap())
.map_err(|_| {
anyhow!(
"unknown language {}. Try one of: {}",
s,
languages.join(", ")
)
})
}
}
extern "C" {
fn tree_sitter_c() -> tree_sitter::Language;
fn tree_sitter_cpp() -> tree_sitter::Language;
fn tree_sitter_cuda() -> tree_sitter::Language;
fn tree_sitter_elixir() -> tree_sitter::Language;
fn tree_sitter_elm() -> tree_sitter::Language;
fn tree_sitter_go() -> tree_sitter::Language;
fn tree_sitter_haskell() -> tree_sitter::Language;
fn tree_sitter_java() -> tree_sitter::Language;
fn tree_sitter_javascript() -> tree_sitter::Language;
fn tree_sitter_markdown() -> tree_sitter::Language;
fn tree_sitter_nix() -> tree_sitter::Language;
fn tree_sitter_php() -> tree_sitter::Language;
fn tree_sitter_python() -> tree_sitter::Language;
fn tree_sitter_ruby() -> tree_sitter::Language;
fn tree_sitter_rust() -> tree_sitter::Language;
fn tree_sitter_scss() -> tree_sitter::Language;
fn tree_sitter_typescript() -> tree_sitter::Language;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn to_str_reflects_from_str() {
// Note: this will hide results if there are multiple failures. It's
// something that could be worked around but I don't think it is right
// now. If it bothers you in the future, feel free to take a stab at it!
Language::iter()
.for_each(|lang| assert_eq!(Language::from_str(&lang.to_string()).unwrap(), lang))
}
#[test]
fn parse_query_smoke_test() {
Language::iter().for_each(|lang| assert!(lang.parse_query("(_)").is_ok()));
}
#[test]
fn language_list_should_be_sorted() {
use itertools::Itertools;
// sorted elements needed by binary_search in FromStr
// TODO: use is_sorted: https://github.com/rust-lang/rust/issues/53485
assert!(Language::VARIANTS
.iter()
.tuple_windows()
.all(|(a, b)| a <= b));
}
#[test]
fn parse_query_problem() {
// tree-grepper 1.0 just printed the error struct when problems like
// this happened. This test is just here to make sure we take a slightly
// friendlier approach for 2.0.
assert_eq!(
String::from("Query error at 1:2. Invalid node type node_that_doesnt_exist"),
Language::Elm
.parse_query("(node_that_doesnt_exist)")
.unwrap_err()
.to_string(),
)
}
}