Skip to main content

gitcortex_indexer/parser/
mod.rs

1use std::path::Path;
2
3use gitcortex_core::{
4    error::Result,
5    graph::{Edge, Node, NodeId},
6};
7
8pub mod go;
9pub mod java;
10pub mod python;
11pub mod rust;
12pub mod typescript;
13
14/// Result of parsing a single source file.
15pub struct ParseResult {
16    pub nodes: Vec<Node>,
17    pub edges: Vec<Edge>,
18    /// Unresolved call sites — resolved cross-file by the indexer.
19    pub deferred_calls: Vec<(NodeId, String)>,
20    /// Unresolved parameter/return-type references: (fn_id, type_name).
21    pub deferred_uses: Vec<(NodeId, String)>,
22    /// Unresolved trait implementations: (struct_id, trait_name).
23    pub deferred_implements: Vec<(NodeId, String)>,
24    /// Unresolved class extends / structural inheritance: (subtype_id, supertype_name).
25    pub deferred_inherits: Vec<(NodeId, String)>,
26    /// Unresolved exception throws: (method_id, exception_type_name).
27    pub deferred_throws: Vec<(NodeId, String)>,
28    /// Unresolved decorator/annotation references: (target_id, annotation_name).
29    pub deferred_annotated: Vec<(NodeId, String)>,
30    /// Unresolved use-declaration imports: (src_node_id, imported_leaf_name).
31    pub deferred_imports: Vec<(NodeId, String)>,
32}
33
34/// Contract every language parser must satisfy.
35///
36/// Implementations are stateless — a parser value is cheap to create and safe
37/// to reuse across files. Parsing is purely functional: source text in, graph
38/// nodes + edges out. Cross-file edges are resolved by the indexer after all
39/// files in the diff have been parsed.
40pub trait LanguageParser: Send + Sync {
41    /// File extensions this parser handles (lower-case, without the dot).
42    fn extensions(&self) -> &[&str];
43
44    /// Parse `source` (content of `path`) and return all nodes, edges, and
45    /// unresolved call references found in that file.
46    fn parse(&self, path: &Path, source: &str) -> Result<ParseResult>;
47}
48
49/// Return the appropriate parser for `path`, keyed on file extension.
50/// Returns `None` when the extension is unsupported.
51pub fn parser_for_path(path: &Path) -> Option<Box<dyn LanguageParser>> {
52    let ext = path.extension()?.to_str()?;
53    match ext {
54        "rs" => Some(Box::new(rust::RustParser::new())),
55        "py" => Some(Box::new(python::PythonParser::new())),
56        "ts" => Some(Box::new(typescript::TypeScriptParser::new_ts())),
57        "tsx" => Some(Box::new(typescript::TypeScriptParser::new_tsx())),
58        "js" | "mjs" | "cjs" => Some(Box::new(typescript::JavaScriptParser::new())),
59        "jsx" => Some(Box::new(typescript::JavaScriptParser::new())),
60        "go" => Some(Box::new(go::GoParser::new())),
61        "java" => Some(Box::new(java::JavaParser::new())),
62        _ => None,
63    }
64}