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 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
17/// Result of parsing a single source file.
18pub struct ParseResult {
19    pub nodes: Vec<Node>,
20    pub edges: Vec<Edge>,
21    /// Unresolved call sites — resolved cross-file by the indexer.
22    pub deferred_calls: Vec<(NodeId, String)>,
23    /// Unresolved parameter/return-type references: (fn_id, type_name).
24    pub deferred_uses: Vec<(NodeId, String)>,
25    /// Unresolved trait implementations: (struct_id, trait_name).
26    pub deferred_implements: Vec<(NodeId, String)>,
27    /// Unresolved class extends / structural inheritance: (subtype_id, supertype_name).
28    pub deferred_inherits: Vec<(NodeId, String)>,
29    /// Unresolved exception throws: (method_id, exception_type_name).
30    pub deferred_throws: Vec<(NodeId, String)>,
31    /// Unresolved decorator/annotation references: (target_id, annotation_name).
32    pub deferred_annotated: Vec<(NodeId, String)>,
33    /// Unresolved use-declaration imports: (src_node_id, imported_leaf_name).
34    pub deferred_imports: Vec<(NodeId, String)>,
35}
36
37/// Contract every language parser must satisfy.
38///
39/// Implementations are stateless — a parser value is cheap to create and safe
40/// to reuse across files. Parsing is purely functional: source text in, graph
41/// nodes + edges out. Cross-file edges are resolved by the indexer after all
42/// files in the diff have been parsed.
43pub trait LanguageParser: Send + Sync {
44    /// File extensions this parser handles (lower-case, without the dot).
45    fn extensions(&self) -> &[&str];
46
47    /// Parse `source` (content of `path`) and return all nodes, edges, and
48    /// unresolved call references found in that file.
49    fn parse(&self, path: &Path, source: &str) -> Result<ParseResult>;
50}
51
52/// Return the appropriate parser for `path`, keyed on file extension.
53/// Returns `None` when the extension is unsupported.
54pub 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}