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