-
Notifications
You must be signed in to change notification settings - Fork 3
/
languages.rs
329 lines (316 loc) · 11.8 KB
/
languages.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
use std::path::Path;
use crate::syntax::SyntaxRule;
use SyntaxRule::*;
const C: [SyntaxRule; 3] = [
LineComment(b"//"),
BlockComment(b"/*", b"*/"),
String(b"\""),
];
const PYTHON: [SyntaxRule; 4] = [
LineComment(b"#"),
String(b"\"\"\""),
String(b"\""),
String(b"'"),
];
const RUST: [SyntaxRule; 5] = [
LineComment(b"//!"),
LineComment(b"///"),
LineComment(b"//"),
BlockComment(b"/*", b"*/"),
String(b"\""),
];
#[rustfmt::skip]
const SHELL: [SyntaxRule; 3] = [
LineComment(b"#"),
String(b"\""),
String(b"'"),
];
// The array is sorted by the language name
const SYNTAXES: [(&str, &[SyntaxRule]); 15] = [
("c", &C),
("cpp", &C),
("css", &C),
("glsl", &C),
("java", &C),
("javascript", &C),
("json", &C),
("jsonc", &C),
("python", &PYTHON),
("rust", &RUST),
("scss", &C),
("shell", &SHELL),
("toml", &C),
("typescript", &C),
("yaml", &C),
];
/// Given a language name, get [syntax rules] for a predefined
/// language included in the crate.
/// Returns `None` if the language is not supported.
///
/// In the case of `None`, check the following:
/// - The language `name` must be written in all lower case.
/// - The language `name` must not use special symbols e.g. use `"cpp"` not `"c++"`.
///
/// If [syntax rules] for a language does not exist, then consider
/// trying another language, which has similar syntax rules when
/// it comes to comments and strings. For instance `c` vs `cpp` or
/// `css` vs `scss`.
///
/// Click [here][languages] to see all predefined languages.
///
/// [languages]: ../src/comment_parser/languages.rs.html
///
/// # Example
///
/// ```
/// use comment_parser::get_syntax;
///
/// assert!(get_syntax("rust").is_some());
/// assert!(get_syntax("c").is_some());
/// assert!(get_syntax("cpp").is_some());
/// assert!(get_syntax("python").is_some());
/// ```
///
/// # Custom Syntax Rules
///
/// Go to [`SyntaxRule`][syntax rules] for an example on defining
/// custom syntax rules.
///
/// [syntax rules]: enum.SyntaxRule.html
#[inline]
pub fn get_syntax<S: AsRef<str>>(name: S) -> Option<&'static [SyntaxRule<'static>]> {
SYNTAXES
.binary_search_by_key(&name.as_ref(), |&(name, _)| name)
.ok()
.map(|i| SYNTAXES[i].1)
}
/// Given a [`Path`], get [syntax rules] for a predefined
/// language included in the crate.
/// The language is identified from the [path extension], and
/// the casing of the extension does not affect the result.
///
/// [`Path`]: https://doc.rust-lang.org/stable/std/path/struct.Path.html
/// [path extension]: https://doc.rust-lang.org/stable/std/path/struct.Path.html#method.extension
///
/// Note that `get_syntax_from_path` does not check if the path exists,
/// nor does it attempt to load the file.
///
/// *[See also `get_syntax_from_extension`][get_syntax_from_extension].*
///
/// [get_syntax_from_extension]: fn.get_syntax_from_extension.html
///
/// # Supported Languages
///
/// If [syntax rules] for a language does not exist, then consider
/// trying another language, which has similar syntax rules when
/// it comes to comments and strings. For instance `c` vs `cpp` or
/// `css` vs `scss`.
///
/// Click [here][crate-languages.rs] to see all predefined languages.
///
/// Go to [`SyntaxRule`][syntax rules] for an example on defining
/// custom syntax rules.
///
/// # Example
///
/// ```
/// # use comment_parser::get_syntax_from_path;
/// assert!(get_syntax_from_path("foo.rs").is_ok());
///
/// assert!(get_syntax_from_path("foo.c").is_ok());
/// assert!(get_syntax_from_path("foo.h").is_ok());
///
/// assert!(get_syntax_from_path("foo.cpp").is_ok());
/// assert!(get_syntax_from_path("foo.hpp").is_ok());
///
/// assert!(get_syntax_from_path("foo.py").is_ok());
/// ```
///
/// # Unsupported Syntax Rules
///
/// If you get [`UnsupportedLanguage`] that means
/// the language was identified by [detect-lang], but [syntax rules] are not
/// included and predefined in [comment-parser] for the language.
///
/// If [syntax rules] for a language does not exist then feel free to submit an issue
/// on the [issue tracker][comment-parser-issues], or add it to [languages.rs][comment-parser-languages.rs]
/// and submit a [pull request][comment-parser-pulls].
///
/// # Unknown Language
///
/// If you get [`UnknownLanguage`] that means the language is not supported,
/// by the sister crate [detect-lang].
/// Feel free to submit an issue on the [issue tracker][detect-lang-issues], or add it
/// to [languages.rs][detect-lang-languages.rs] and submit a [pull request][detect-lang-pulls].
///
/// [syntax rules]: enum.SyntaxRule.html
/// [`UnknownLanguage`]: enum.LanguageError.html#variant.UnknownLanguage
/// [`UnsupportedLanguage`]: enum.LanguageError.html#variant.UnsupportedLanguage
///
/// [crate-languages.rs]: ../src/comment_parser/languages.rs.html
///
/// [detect-lang]: https://crates.io/crates/detect-lang
/// [detect-lang-issues]: https://github.com/vallentin/detect-lang/issues
/// [detect-lang-pulls]: https://github.com/vallentin/detect-lang/pulls
/// [detect-lang-languages.rs]: https://github.com/vallentin/detect-lang/blob/master/src/languages.rs
///
/// [comment-parser]: https://crates.io/crates/comment-parser
/// [comment-parser-issues]: https://github.com/vallentin/comment-parser/issues
/// [comment-parser-pulls]: https://github.com/vallentin/comment-parser/pulls
/// [comment-parser-languages.rs]: https://github.com/vallentin/comment-parser/blob/master/src/languages.rs
#[inline]
pub fn get_syntax_from_path<P: AsRef<Path>>(
path: P,
) -> Result<&'static [SyntaxRule<'static>], LanguageError> {
if let Some(language) = detect_lang::from_path(path) {
get_syntax(language.id()).ok_or(LanguageError::UnsupportedLanguage)
} else {
Err(LanguageError::UnknownLanguage)
}
}
/// Given a file `extension`, get [syntax rules] for a predefined
/// language included in the crate.
/// The casing of the `extension` does not affect the result.
///
/// [`Path`]: https://doc.rust-lang.org/stable/std/path/struct.Path.html
/// [path extension]: https://doc.rust-lang.org/stable/std/path/struct.Path.html#method.extension
///
/// *[See also `get_syntax_from_path`][get_syntax_from_path].*
///
/// [get_syntax_from_path]: fn.get_syntax_from_path.html
///
/// # Supported Languages
///
/// If [syntax rules] for a language does not exist, then consider
/// trying another language, which has similar syntax rules when
/// it comes to comments and strings. For instance `c` vs `cpp` or
/// `css` vs `scss`.
///
/// Click [here][crate-languages.rs] to see all predefined languages.
///
/// Go to [`SyntaxRule`][syntax rules] for an example on defining
/// custom syntax rules.
///
/// # Example
///
/// ```
/// # use comment_parser::get_syntax_from_extension;
/// assert!(get_syntax_from_extension("rs").is_ok());
///
/// assert!(get_syntax_from_extension("c").is_ok());
/// assert!(get_syntax_from_extension("h").is_ok());
///
/// assert!(get_syntax_from_extension("cpp").is_ok());
/// assert!(get_syntax_from_extension("hpp").is_ok());
///
/// assert!(get_syntax_from_extension("py").is_ok());
/// ```
///
/// # Unsupported Syntax Rules
///
/// If you get [`UnsupportedLanguage`] that means
/// the language was identified by [detect-lang], but [syntax rules] are not
/// included and predefined in [comment-parser] for the language.
///
/// If [syntax rules] for a language does not exist then feel free to submit an issue
/// on the [issue tracker][comment-parser-issues], or add it to [languages.rs][comment-parser-languages.rs]
/// and submit a [pull request][comment-parser-pulls].
///
/// # Unknown Language
///
/// If you get [`UnknownLanguage`] that means the language is not supported,
/// by the sister crate [detect-lang].
/// Feel free to submit an issue on the [issue tracker][detect-lang-issues], or add it
/// to [languages.rs][detect-lang-languages.rs] and submit a [pull request][detect-lang-pulls].
///
/// [syntax rules]: enum.SyntaxRule.html
/// [`UnknownLanguage`]: enum.LanguageError.html#variant.UnknownLanguage
/// [`UnsupportedLanguage`]: enum.LanguageError.html#variant.UnsupportedLanguage
///
/// [crate-languages.rs]: ../src/comment_parser/languages.rs.html
///
/// [detect-lang]: https://crates.io/crates/detect-lang
/// [detect-lang-issues]: https://github.com/vallentin/detect-lang/issues
/// [detect-lang-pulls]: https://github.com/vallentin/detect-lang/pulls
/// [detect-lang-languages.rs]: https://github.com/vallentin/detect-lang/blob/master/src/languages.rs
///
/// [comment-parser]: https://crates.io/crates/comment-parser
/// [comment-parser-issues]: https://github.com/vallentin/comment-parser/issues
/// [comment-parser-pulls]: https://github.com/vallentin/comment-parser/pulls
/// [comment-parser-languages.rs]: https://github.com/vallentin/comment-parser/blob/master/src/languages.rs
#[inline]
pub fn get_syntax_from_extension<S: AsRef<str>>(
extension: S,
) -> Result<&'static [SyntaxRule<'static>], LanguageError> {
if let Some(language) = detect_lang::from_extension(extension) {
get_syntax(language.id()).ok_or(LanguageError::UnsupportedLanguage)
} else {
Err(LanguageError::UnknownLanguage)
}
}
/// `LanguageError` is an error that can be returned by
/// [`get_syntax_from_path`] and [`get_syntax_from_extension`].
///
/// [`get_syntax_from_path`]: fn.get_syntax_from_path.html
/// [`get_syntax_from_extension`]: fn.get_syntax_from_extension.html
#[derive(Debug)]
pub enum LanguageError {
/// The language could not be identified.
///
/// -----
///
/// If you get `UnknownLanguage` that means the language is not supported,
/// by the sister crate [detect-lang].
/// Feel free to submit an issue on the [issue tracker][detect-lang-issues], or add it
/// to [languages.rs][detect-lang-languages.rs] and submit a [pull request][detect-lang-pulls].
///
/// [detect-lang]: https://crates.io/crates/detect-lang
/// [detect-lang-issues]: https://github.com/vallentin/detect-lang/issues
/// [detect-lang-pulls]: https://github.com/vallentin/detect-lang/pulls
/// [detect-lang-languages.rs]: https://github.com/vallentin/detect-lang/blob/master/src/languages.rs
UnknownLanguage,
/// The language was identified by [detect-lang], but [syntax rules] are not
/// included and predefined in [comment-parser] for the language.
///
/// ### Supported Languages
///
/// If [syntax rules] for a language does not exist, then consider
/// trying another language, which has similar syntax rules when
/// it comes to comments and strings. For instance `c` vs `cpp` or
/// `css` vs `scss`.
///
/// Click [here][crate-languages.rs] to see all predefined languages.
///
/// [crate-languages.rs]: ../src/comment_parser/languages.rs.html
///
/// ### Custom Syntax Rules
///
/// Go to [`SyntaxRule`][syntax rules] for an example on defining
/// custom syntax rules.
///
/// [syntax rules]: enum.SyntaxRule.html
///
/// -----
///
/// If you implement syntax rules for an unsupported language, then feel free to submit
/// your `rules` on the [issue tracker][comment-parser-issues], or add it to
/// [languages.rs][comment-parser-languages.rs] and submit a [pull request][comment-parser-pulls].
///
/// [detect-lang]: https://crates.io/crates/detect-lang
/// [comment-parser]: https://crates.io/crates/comment-parser
/// [comment-parser-issues]: https://github.com/vallentin/comment-parser/issues
/// [comment-parser-pulls]: https://github.com/vallentin/comment-parser/pulls
/// [comment-parser-languages.rs]: https://github.com/vallentin/comment-parser/blob/master/src/languages.rs
UnsupportedLanguage,
}
#[test]
fn check_order() {
for (a, b) in SYNTAXES.iter().zip(SYNTAXES.iter().skip(1)) {
assert!(
a.0 < b.0,
"Syntaxes out of order - {:?} should come after {:?}",
a,
b,
);
}
}