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;
14
15#[cfg(feature = "pattern")]
16pub mod pattern;
17
18#[cfg(feature = "text-search")]
19pub mod text_search;
20
21use deagle_core::{Language, Node, Result};
22use std::path::Path;
23
24pub use rust_parser::ParseResult;
25
26/// Parse a source file and extract code entities.
27pub fn parse_file(path: &Path, content: &str, language: Language) -> Result<Vec<Node>> {
28    match language {
29        Language::Rust => rust_parser::parse(path, content),
30        Language::Python => python_parser::parse(path, content),
31        Language::Go => go_parser::parse(path, content),
32        Language::TypeScript | Language::JavaScript => typescript_parser::parse(path, content),
33        _ => Ok(Vec::new()),
34    }
35}
36
37/// Parse with edge extraction — returns nodes and relationship tuples.
38pub fn parse_file_with_edges(path: &Path, content: &str, language: Language) -> Result<ParseResult> {
39    match language {
40        Language::Rust => rust_parser::parse_with_edges(path, content),
41        Language::Python => python_parser::parse_with_edges(path, content),
42        Language::Go => go_parser::parse_with_edges(path, content),
43        Language::TypeScript | Language::JavaScript => typescript_parser::parse_with_edges(path, content),
44        _ => Ok(ParseResult { nodes: Vec::new(), edges: Vec::new() }),
45    }
46}
47
48/// Detect language from file path and parse.
49pub fn parse_auto(path: &Path, content: &str) -> Result<Vec<Node>> {
50    let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
51    let lang = Language::from_extension(ext);
52    parse_file(path, content, lang)
53}