Skip to main content

aft_parse/parser/
grammar_selection.rs

1use crate::{error::AftError, grammar, lang_id::LangId};
2use tree_sitter::Language;
3
4/// Select a linked grammar or load a stored downloaded grammar, without requesting it.
5pub fn grammar_for(lang: LangId) -> Result<Language, AftError> {
6    if let Some(language) = grammar::extractor_name(lang) {
7        return grammar::registry(language)
8            .ok_or_else(|| AftError::InvalidRequest {
9                message: format!("no manifest entry for grammar: {language}"),
10            })?
11            .stored()
12            .map_err(Into::into);
13    }
14    Ok(match lang {
15        LangId::TypeScript => tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into(),
16        LangId::Tsx => tree_sitter_typescript::LANGUAGE_TSX.into(),
17        LangId::JavaScript => tree_sitter_javascript::LANGUAGE.into(),
18        LangId::Python => tree_sitter_python::LANGUAGE.into(),
19        LangId::Rust => tree_sitter_rust::LANGUAGE.into(),
20        LangId::Go => tree_sitter_go::LANGUAGE.into(),
21        LangId::C => tree_sitter_c::LANGUAGE.into(),
22        LangId::Cpp => tree_sitter_cpp::LANGUAGE.into(),
23        LangId::Cuda => tree_sitter_cuda::LANGUAGE.into(),
24        LangId::Metal => tree_sitter_cpp::LANGUAGE.into(),
25        LangId::Zig => tree_sitter_zig::LANGUAGE.into(),
26        LangId::CSharp => tree_sitter_c_sharp::LANGUAGE.into(),
27        LangId::Bash => tree_sitter_bash::LANGUAGE.into(),
28        LangId::Html => tree_sitter_html::LANGUAGE.into(),
29        LangId::Markdown => tree_sitter_md::LANGUAGE.into(),
30        LangId::Solidity => tree_sitter_solidity::LANGUAGE.into(),
31        LangId::Scss => tree_sitter_scss::language(),
32        LangId::Vue => tree_sitter_vue::LANGUAGE.into(),
33        LangId::Json => tree_sitter_json::LANGUAGE.into(),
34        LangId::Scala => tree_sitter_scala::LANGUAGE.into(),
35        LangId::Java => tree_sitter_java::LANGUAGE.into(),
36        LangId::Ruby => tree_sitter_ruby::LANGUAGE.into(),
37        LangId::Kotlin => tree_sitter_kotlin_sg::LANGUAGE.into(),
38        LangId::Swift => tree_sitter_swift::LANGUAGE.into(),
39        LangId::Php => tree_sitter_php::LANGUAGE_PHP.into(),
40        LangId::Lua => tree_sitter_lua::LANGUAGE.into(),
41        LangId::Perl => tree_sitter_perl::LANGUAGE.into(),
42        LangId::Yaml => tree_sitter_yaml::LANGUAGE.into(),
43        LangId::Pascal => tree_sitter_pascal::LANGUAGE.into(),
44        LangId::R => tree_sitter_r::LANGUAGE.into(),
45        LangId::Groovy => dekobon_tree_sitter_groovy::LANGUAGE.into(),
46        LangId::ObjC => tree_sitter_objc::LANGUAGE.into(),
47        LangId::Toml => tree_sitter_toml::LANGUAGE.into(),
48        _ => unreachable!("downloaded languages use the extractor table"),
49    })
50}