Skip to main content

lynx_parser/
lib.rs

1pub mod languages;
2pub mod symbol_extraction;
3pub mod tree_sitter;
4
5use anyhow::Result;
6use lynx_protocol::{CodeChunk, SymbolRecord};
7use std::path::Path;
8
9pub struct Parser {
10    // We can add state here if needed, like a pool of tree-sitter parsers
11}
12
13impl Parser {
14    pub fn new() -> Self {
15        Self {}
16    }
17}
18
19impl Default for Parser {
20    fn default() -> Self {
21        Self::new()
22    }
23}
24
25impl Parser {
26    pub fn parse_file(
27        &self,
28        path: &Path,
29        content: &str,
30    ) -> Result<(Vec<CodeChunk>, Vec<SymbolRecord>)> {
31        let extension = path.extension().and_then(|s| s.to_str()).unwrap_or("");
32
33        match extension {
34            "rs" => self.parse_rust(path, content),
35            "go" => self.parse_go(path, content),
36            "ts" | "tsx" => self.parse_typescript(path, content),
37            "js" | "jsx" => self.parse_javascript(path, content),
38            "py" => self.parse_python(path, content),
39            _ => {
40                // For unsupported languages, we might want to do basic line-based chunking
41                // or just return empty for now
42                Ok((vec![], vec![]))
43            }
44        }
45    }
46
47    fn parse_rust(
48        &self,
49        path: &Path,
50        content: &str,
51    ) -> Result<(Vec<CodeChunk>, Vec<SymbolRecord>)> {
52        symbol_extraction::rust::extract(path, content)
53    }
54
55    fn parse_go(&self, path: &Path, content: &str) -> Result<(Vec<CodeChunk>, Vec<SymbolRecord>)> {
56        symbol_extraction::go::extract(path, content)
57    }
58
59    fn parse_typescript(
60        &self,
61        path: &Path,
62        content: &str,
63    ) -> Result<(Vec<CodeChunk>, Vec<SymbolRecord>)> {
64        symbol_extraction::typescript::extract(path, content)
65    }
66
67    fn parse_javascript(
68        &self,
69        path: &Path,
70        content: &str,
71    ) -> Result<(Vec<CodeChunk>, Vec<SymbolRecord>)> {
72        symbol_extraction::javascript::extract(path, content)
73    }
74
75    fn parse_python(
76        &self,
77        path: &Path,
78        content: &str,
79    ) -> Result<(Vec<CodeChunk>, Vec<SymbolRecord>)> {
80        symbol_extraction::python::extract(path, content)
81    }
82}