neural_shared/parser/
mod.rs

1//! Language detection and AST parsing using tree-sitter
2
3use crate::Result;
4use anyhow::anyhow;
5use std::path::Path;
6
7mod python;
8mod typescript;
9
10pub use python::PythonParser;
11pub use typescript::TypeScriptParser;
12
13/// Supported languages
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15pub enum Language {
16    Python,
17    TypeScript,
18    JavaScript,
19}
20
21impl Language {
22    /// Detect language from file extension
23    pub fn from_path(path: &Path) -> Result<Self> {
24        let ext = path
25            .extension()
26            .and_then(|e| e.to_str())
27            .ok_or_else(|| anyhow!("No file extension found"))?;
28
29        match ext {
30            "py" => Ok(Language::Python),
31            "ts" | "tsx" => Ok(Language::TypeScript),
32            "js" | "jsx" => Ok(Language::JavaScript),
33            _ => Err(anyhow!("Unsupported file extension: {}", ext)),
34        }
35    }
36}
37
38/// Parser trait for language-specific parsing
39pub trait Parser {
40    /// Parse source code and extract symbols
41    fn parse(&self, source: &str, file_path: &Path) -> Result<ParsedFile>;
42}
43
44/// Parsed file containing symbols
45#[derive(Debug, Clone)]
46pub struct ParsedFile {
47    pub path: String,
48    pub definitions: Vec<Symbol>,
49    pub usages: Vec<Symbol>,
50    pub entry_points: Vec<String>,
51}
52
53/// Symbol represents a function, class, method, or variable
54#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
55pub struct Symbol {
56    pub name: String,
57    pub kind: SymbolKind,
58    pub location: Location,
59}
60
61#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
62pub enum SymbolKind {
63    Function,
64    Class,
65    Method { class_name: String },
66    Variable,
67}
68
69#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
70pub struct Location {
71    pub file: String,
72    pub line: usize,
73    pub column: usize,
74}
75
76impl Symbol {
77    pub fn new(name: String, kind: SymbolKind, location: Location) -> Self {
78        Self {
79            name,
80            kind,
81            location,
82        }
83    }
84}