Skip to main content

deagle_parse/
lib.rs

1//! deagle-parse — tree-sitter based code parser.
2//!
3//! Extracts code entities (functions, structs, traits, impls, imports)
4//! from source files using tree-sitter grammars.
5//!
6//! ## Feature Flags
7//!
8//! - `pattern` — structural pattern matching via [ast-grep-core](https://crates.io/crates/ast-grep-core)
9
10pub mod rust_parser;
11pub mod python_parser;
12pub mod go_parser;
13pub mod typescript_parser;
14pub mod java_parser;
15
16#[cfg(feature = "pattern")]
17pub mod pattern;
18
19#[cfg(feature = "text-search")]
20pub mod text_search;
21
22use deagle_core::{Language, Node, Result};
23use std::path::Path;
24
25pub use rust_parser::ParseResult;
26
27/// Parse a source file and extract code entities.
28pub fn parse_file(path: &Path, content: &str, language: Language) -> Result<Vec<Node>> {
29    match language {
30        Language::Rust => rust_parser::parse(path, content),
31        Language::Python => python_parser::parse(path, content),
32        Language::Go => go_parser::parse(path, content),
33        Language::TypeScript | Language::JavaScript => typescript_parser::parse(path, content),
34        Language::Java => java_parser::parse(path, content),
35        _ => Ok(Vec::new()),
36    }
37}
38
39/// Parse with edge extraction — returns nodes and relationship tuples.
40pub fn parse_file_with_edges(path: &Path, content: &str, language: Language) -> Result<ParseResult> {
41    match language {
42        Language::Rust => rust_parser::parse_with_edges(path, content),
43        Language::Python => python_parser::parse_with_edges(path, content),
44        Language::Go => go_parser::parse_with_edges(path, content),
45        Language::TypeScript | Language::JavaScript => typescript_parser::parse_with_edges(path, content),
46        Language::Java => java_parser::parse_with_edges(path, content),
47        _ => Ok(ParseResult { nodes: Vec::new(), edges: Vec::new() }),
48    }
49}
50
51/// Detect language from file path and parse.
52pub fn parse_auto(path: &Path, content: &str) -> Result<Vec<Node>> {
53    let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
54    let lang = Language::from_extension(ext);
55    parse_file(path, content, lang)
56}