1pub mod rust;
10pub mod typescript;
11pub mod tsconfig;
12pub mod vue;
13pub mod svelte;
14pub mod php;
15pub mod python;
16pub mod go;
17pub mod java;
18pub mod c;
19pub mod cpp;
20pub mod csharp;
21pub mod ruby;
22pub mod kotlin;
23pub mod zig;
25
26use anyhow::{anyhow, Result};
27use crate::models::{Language, SearchResult};
28
29pub struct ParserFactory;
31
32#[derive(Debug, Clone)]
34pub struct ImportInfo {
35 pub imported_path: String,
37 pub import_type: crate::models::ImportType,
39 pub line_number: usize,
41 pub imported_symbols: Option<Vec<String>>,
43}
44
45#[derive(Debug, Clone)]
47pub struct ExportInfo {
48 pub exported_symbol: Option<String>,
50 pub source_path: String,
52 pub line_number: usize,
54}
55
56pub trait DependencyExtractor {
61 fn extract_dependencies(source: &str) -> Result<Vec<ImportInfo>>;
74}
75
76impl ParserFactory {
77 pub fn get_language_grammar(language: Language) -> Result<tree_sitter::Language> {
87 match language {
88 Language::Rust => Ok(tree_sitter_rust::LANGUAGE.into()),
89 Language::Python => Ok(tree_sitter_python::LANGUAGE.into()),
90 Language::TypeScript => Ok(tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into()),
91 Language::JavaScript => Ok(tree_sitter_typescript::LANGUAGE_TSX.into()),
92 Language::Go => Ok(tree_sitter_go::LANGUAGE.into()),
93 Language::Java => Ok(tree_sitter_java::LANGUAGE.into()),
94 Language::C => Ok(tree_sitter_c::LANGUAGE.into()),
95 Language::Cpp => Ok(tree_sitter_cpp::LANGUAGE.into()),
96 Language::CSharp => Ok(tree_sitter_c_sharp::LANGUAGE.into()),
97 Language::PHP => Ok(tree_sitter_php::LANGUAGE_PHP.into()),
98 Language::Ruby => Ok(tree_sitter_ruby::LANGUAGE.into()),
99 Language::Kotlin => Ok(tree_sitter_kotlin_ng::LANGUAGE.into()),
100 Language::Zig => Ok(tree_sitter_zig::LANGUAGE.into()),
101 Language::Swift => Err(anyhow!(
102 "Swift support temporarily disabled (requires tree-sitter 0.23)"
103 )),
104 Language::Vue => Err(anyhow!(
105 "Vue uses line-based parsing, not tree-sitter (tree-sitter-vue incompatible with tree-sitter 0.24+)"
106 )),
107 Language::Svelte => Err(anyhow!(
108 "Svelte uses line-based parsing, not tree-sitter (tree-sitter-svelte incompatible with tree-sitter 0.24+)"
109 )),
110 Language::Unknown => Err(anyhow!("Unknown language")),
111 }
112 }
113
114 pub fn get_keywords(language: Language) -> &'static [&'static str] {
122 match language {
123 Language::Rust => &["fn", "struct", "enum", "trait", "impl", "mod", "const", "static", "type", "macro"],
124 Language::PHP => &["class", "function", "trait", "interface", "enum"],
125 Language::Python => &["class", "def", "async"],
126 Language::TypeScript | Language::JavaScript => &["class", "function", "interface", "type", "enum", "const", "let", "var"],
127 Language::Go => &["func", "struct", "interface", "type", "const", "var"],
128 Language::Java => &["class", "interface", "enum", "@interface"],
129 Language::C => &["struct", "enum", "union", "typedef"],
130 Language::Cpp => &["class", "struct", "enum", "union", "typedef", "namespace", "template"],
131 Language::CSharp => &["class", "struct", "interface", "enum", "delegate", "record", "namespace"],
132 Language::Ruby => &["class", "module", "def"],
133 Language::Kotlin => &["class", "fun", "interface", "object", "enum", "annotation"],
134 Language::Zig => &["fn", "struct", "enum", "const", "var", "type"],
135 Language::Swift => &["class", "struct", "enum", "protocol", "func", "var", "let"],
136 Language::Vue | Language::Svelte => &["function", "const", "let", "var"],
137 Language::Unknown => &[],
138 }
139 }
140
141 pub fn get_all_keywords() -> &'static [&'static str] {
149 &[
150 "fn", "function", "def", "func",
152 "class", "struct", "enum", "interface", "trait", "type", "record",
154 "mod", "module", "namespace",
156 "const", "static", "let", "var",
158 "impl", "async", "object", "annotation", "protocol",
160 "union", "typedef", "delegate", "template",
161 "@interface",
163 ]
164 }
165
166 pub fn parse(
168 path: &str,
169 source: &str,
170 language: Language,
171 ) -> Result<Vec<SearchResult>> {
172 match language {
173 Language::Rust => rust::parse(path, source),
174 Language::TypeScript => typescript::parse(path, source, language),
175 Language::JavaScript => typescript::parse(path, source, language),
176 Language::Vue => vue::parse(path, source),
177 Language::Svelte => svelte::parse(path, source),
178 Language::Python => python::parse(path, source),
179 Language::Go => go::parse(path, source),
180 Language::Java => java::parse(path, source),
181 Language::PHP => php::parse(path, source),
182 Language::C => c::parse(path, source),
183 Language::Cpp => cpp::parse(path, source),
184 Language::CSharp => csharp::parse(path, source),
185 Language::Ruby => ruby::parse(path, source),
186 Language::Kotlin => kotlin::parse(path, source),
187 Language::Swift => {
188 log::warn!("Swift support temporarily disabled (requires tree-sitter 0.23): {}", path);
189 Ok(vec![])
190 }
191 Language::Zig => zig::parse(path, source),
192 Language::Unknown => {
193 log::warn!("Unknown language for file: {}", path);
194 Ok(vec![])
195 }
196 }
197 }
198}
199
200#[cfg(test)]
201mod tests {
202 use super::*;
203
204 #[test]
205 fn test_parser_factory() {
206 let _factory = ParserFactory;
208 }
209}