gitcortex_indexer/parser/
mod.rs1use std::path::Path;
2
3use gitcortex_core::{
4 error::Result,
5 graph::{Edge, Node, NodeId},
6};
7
8mod deftext;
9pub mod go;
10pub mod java;
11pub mod python;
12pub mod rust;
13pub mod typescript;
14
15pub(crate) use deftext::capture as capture_definition;
16
17pub struct ParseResult {
19 pub nodes: Vec<Node>,
20 pub edges: Vec<Edge>,
21 pub deferred_calls: Vec<(NodeId, String)>,
23 pub deferred_uses: Vec<(NodeId, String)>,
25 pub deferred_implements: Vec<(NodeId, String)>,
27 pub deferred_inherits: Vec<(NodeId, String)>,
29 pub deferred_throws: Vec<(NodeId, String)>,
31 pub deferred_annotated: Vec<(NodeId, String)>,
33 pub deferred_imports: Vec<(NodeId, String)>,
35}
36
37pub trait LanguageParser: Send + Sync {
44 fn extensions(&self) -> &[&str];
46
47 fn parse(&self, path: &Path, source: &str) -> Result<ParseResult>;
50}
51
52pub fn parser_for_path(path: &Path) -> Option<Box<dyn LanguageParser>> {
55 let ext = path.extension()?.to_str()?;
56 match ext {
57 "rs" => Some(Box::new(rust::RustParser::new())),
58 "py" => Some(Box::new(python::PythonParser::new())),
59 "ts" => Some(Box::new(typescript::TypeScriptParser::new_ts())),
60 "tsx" => Some(Box::new(typescript::TypeScriptParser::new_tsx())),
61 "js" | "mjs" | "cjs" => Some(Box::new(typescript::JavaScriptParser::new())),
62 "jsx" => Some(Box::new(typescript::JavaScriptParser::new())),
63 "go" => Some(Box::new(go::GoParser::new())),
64 "java" => Some(Box::new(java::JavaParser::new())),
65 _ => None,
66 }
67}