Skip to content

Commit 70d301c

Browse files
committed
Check in initial diff engine and semantic analysis
1 parent 72c264c commit 70d301c

File tree

2 files changed

+119
-3
lines changed

2 files changed

+119
-3
lines changed

crates/parser/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
//! for structural and semantic comparison.
55
66
pub mod ast;
7+
pub mod ast_builder;
8+
pub mod ast_processor;
79
pub mod function;
810
pub mod language;
911
pub mod language_config;
@@ -12,6 +14,8 @@ pub mod parser;
1214
pub mod tree_sitter;
1315

1416
pub use ast::{ASTNode, NodeType, NodeMetadata};
17+
pub use ast_builder::{ASTBuilder, ASTBuilderBuilder, ASTBuilderConfig};
18+
pub use ast_processor::{ASTProcessor, ASTAnalysis, FunctionSignatureInfo, SymbolTable, Symbol, SymbolType};
1519
pub use function::{Function, FunctionSignature, Parameter, Type, FunctionLocation};
1620
pub use language::{Language, LanguageDetector};
1721
pub use matching::{MatchResult, Change, ChangeType, CodeElement, ElementType, ChangeDetail, RefactoringType};

crates/parser/src/tree_sitter.rs

Lines changed: 115 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Tree-sitter integration for multi-language parsing
22
33
use crate::ast::{ASTNode, NodeType, NodeMetadata};
4+
use crate::ast_builder::{ASTBuilder, ASTBuilderBuilder, ASTBuilderConfig};
5+
use crate::ast_processor::{ASTProcessor, ASTAnalysis};
46
use crate::language::Language;
57
use crate::language_config::{LanguageConfig, LANGUAGE_CONFIGS};
68
use crate::parser::{ParseError, ParseResult, Parser};
@@ -10,6 +12,9 @@ use once_cell::sync::Lazy;
1012
/// Tree-sitter based parser implementation
1113
pub struct TreeSitterParser {
1214
parsers: HashMap<Language, tree_sitter::Parser>,
15+
builder_config: ASTBuilderConfig,
16+
enable_optimization: bool,
17+
enable_analysis: bool,
1318
}
1419

1520
/// Global language configurations
@@ -25,6 +30,10 @@ static LANGUAGE_CONFIGS: Lazy<HashMap<Language, fn() -> tree_sitter::Language>>
2530

2631
impl TreeSitterParser {
2732
pub fn new() -> Result<Self, ParseError> {
33+
Self::with_config(ASTBuilderConfig::default())
34+
}
35+
36+
pub fn with_config(builder_config: ASTBuilderConfig) -> Result<Self, ParseError> {
2837
let mut parsers = HashMap::new();
2938

3039
// Initialize parsers for supported languages
@@ -35,7 +44,27 @@ impl TreeSitterParser {
3544
parsers.insert(language, parser);
3645
}
3746

38-
Ok(Self { parsers })
47+
Ok(Self {
48+
parsers,
49+
builder_config,
50+
enable_optimization: true,
51+
enable_analysis: true,
52+
})
53+
}
54+
55+
/// Create parser with builder pattern
56+
pub fn builder() -> TreeSitterParserBuilder {
57+
TreeSitterParserBuilder::new()
58+
}
59+
60+
/// Enable or disable AST optimization
61+
pub fn set_optimization_enabled(&mut self, enabled: bool) {
62+
self.enable_optimization = enabled;
63+
}
64+
65+
/// Enable or disable AST analysis
66+
pub fn set_analysis_enabled(&mut self, enabled: bool) {
67+
self.enable_analysis = enabled;
3968
}
4069

4170
/// Get available languages
@@ -206,8 +235,15 @@ impl Parser for TreeSitterParser {
206235

207236
let root_node = tree.root_node();
208237

209-
// Convert tree-sitter tree to our AST
210-
let ast = self.convert_tree_sitter_node(&root_node, content);
238+
// Build AST using the enhanced AST builder
239+
let mut ast_builder = ASTBuilder::new(language.clone(), self.builder_config.clone());
240+
let mut ast = ast_builder.build_ast(&tree, content);
241+
242+
// Optimize AST if enabled
243+
if self.enable_optimization {
244+
let processor = ASTProcessor::new(language.clone());
245+
let _optimization_result = processor.optimize(&mut ast);
246+
}
211247

212248
// Collect any parse errors
213249
let mut errors = Vec::new();
@@ -217,6 +253,17 @@ impl Parser for TreeSitterParser {
217253
self.collect_parse_errors(&root_node, content, &mut errors);
218254
}
219255

256+
// Add build statistics as warnings if analysis is enabled
257+
if self.enable_analysis {
258+
let stats = ast_builder.get_stats();
259+
if stats.skipped_nodes > 0 {
260+
warnings.push(format!("Skipped {} noise nodes during AST construction", stats.skipped_nodes));
261+
}
262+
if stats.total_nodes > 1000 {
263+
warnings.push(format!("Large AST with {} nodes may impact performance", stats.total_nodes));
264+
}
265+
}
266+
220267
Ok(ParseResult {
221268
ast,
222269
language,
@@ -235,3 +282,68 @@ impl Parser for TreeSitterParser {
235282
Self::supported_languages()
236283
}
237284
}
285+
286+
/// Builder for TreeSitterParser with fluent configuration
287+
pub struct TreeSitterParserBuilder {
288+
builder_config: ASTBuilderConfig,
289+
enable_optimization: bool,
290+
enable_analysis: bool,
291+
}
292+
293+
impl TreeSitterParserBuilder {
294+
pub fn new() -> Self {
295+
Self {
296+
builder_config: ASTBuilderConfig::default(),
297+
enable_optimization: true,
298+
enable_analysis: true,
299+
}
300+
}
301+
302+
pub fn include_comments(mut self, include: bool) -> Self {
303+
self.builder_config.include_comments = include;
304+
self
305+
}
306+
307+
pub fn include_whitespace(mut self, include: bool) -> Self {
308+
self.builder_config.include_whitespace = include;
309+
self
310+
}
311+
312+
pub fn max_text_length(mut self, length: usize) -> Self {
313+
self.builder_config.max_text_length = length;
314+
self
315+
}
316+
317+
pub fn extract_signatures(mut self, extract: bool) -> Self {
318+
self.builder_config.extract_signatures = extract;
319+
self
320+
}
321+
322+
pub fn build_symbol_table(mut self, build: bool) -> Self {
323+
self.builder_config.build_symbol_table = build;
324+
self
325+
}
326+
327+
pub fn enable_optimization(mut self, enable: bool) -> Self {
328+
self.enable_optimization = enable;
329+
self
330+
}
331+
332+
pub fn enable_analysis(mut self, enable: bool) -> Self {
333+
self.enable_analysis = enable;
334+
self
335+
}
336+
337+
pub fn build(self) -> Result<TreeSitterParser, ParseError> {
338+
let mut parser = TreeSitterParser::with_config(self.builder_config)?;
339+
parser.enable_optimization = self.enable_optimization;
340+
parser.enable_analysis = self.enable_analysis;
341+
Ok(parser)
342+
}
343+
}
344+
345+
impl Default for TreeSitterParserBuilder {
346+
fn default() -> Self {
347+
Self::new()
348+
}
349+
}

0 commit comments

Comments
 (0)