Skip to content

Commit 9901e7e

Browse files
committed
Check in initial diff engine and semantic analysis
1 parent 3b5bf68 commit 9901e7e

File tree

5 files changed

+1028
-1
lines changed

5 files changed

+1028
-1
lines changed

crates/parser/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,7 @@ path = "../../examples/parse_demo.rs"
4141
[[example]]
4242
name = "language_detection_demo"
4343
path = "../../examples/language_detection_demo.rs"
44+
45+
[[example]]
46+
name = "ast_generation_demo"
47+
path = "../../examples/ast_generation_demo.rs"

crates/parser/README.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,80 @@ let detected = LanguageDetector::detect_from_content(mixed_content);
171171
- **Penalty system**: Reduces score when conflicting language patterns are found
172172
- **Fallback**: Returns `Language::Unknown` when confidence is too low
173173

174+
## AST Generation Pipeline
175+
176+
The parser features a sophisticated AST generation pipeline that converts tree-sitter parse trees into normalized, language-agnostic AST representations.
177+
178+
### Configurable AST Building
179+
180+
```rust
181+
use smart_diff_parser::tree_sitter::TreeSitterParser;
182+
183+
// Create parser with custom configuration
184+
let parser = TreeSitterParser::builder()
185+
.include_comments(true) // Include comment nodes
186+
.include_whitespace(false) // Skip whitespace-only nodes
187+
.max_text_length(200) // Limit stored text length
188+
.extract_signatures(true) // Extract function signatures
189+
.build_symbol_table(true) // Build symbol table during parsing
190+
.enable_optimization(true) // Enable AST optimization
191+
.enable_analysis(true) // Enable AST analysis
192+
.build()?;
193+
194+
let result = parser.parse(code, Language::Java)?;
195+
```
196+
197+
### AST Analysis and Metrics
198+
199+
```rust
200+
use smart_diff_parser::ast_processor::ASTProcessor;
201+
202+
let processor = ASTProcessor::new(Language::Java);
203+
let analysis = processor.analyze(&result.ast);
204+
205+
println!("Total nodes: {}", analysis.total_nodes);
206+
println!("Max depth: {}", analysis.max_depth);
207+
println!("Function count: {}", analysis.function_count);
208+
println!("Cyclomatic complexity: {}", analysis.cyclomatic_complexity);
209+
println!("Complexity score: {:.2}", analysis.complexity_score);
210+
```
211+
212+
### Function Signature Extraction
213+
214+
```rust
215+
let signatures = processor.extract_function_signatures(&result.ast);
216+
217+
for signature in signatures {
218+
println!("Function: {} (params: {}, line: {})",
219+
signature.name,
220+
signature.parameter_count,
221+
signature.line);
222+
}
223+
```
224+
225+
### Symbol Table Construction
226+
227+
```rust
228+
let symbol_table = processor.build_symbol_table(&result.ast);
229+
230+
// Find symbols in specific scope
231+
let symbols = symbol_table.get_symbols_in_scope(&["MyClass"]);
232+
233+
// Search for symbol with scope resolution
234+
let symbol = symbol_table.find_symbol("myFunction", &["MyClass"]);
235+
```
236+
237+
### AST Optimization
238+
239+
```rust
240+
let mut ast = result.ast;
241+
let optimization_result = processor.optimize(&mut ast);
242+
243+
println!("Nodes removed: {}", optimization_result.nodes_removed);
244+
println!("Nodes flattened: {}", optimization_result.nodes_flattened);
245+
println!("Nodes merged: {}", optimization_result.nodes_merged);
246+
```
247+
174248
## AST Structure
175249

176250
The parser converts language-specific parse trees into a normalized AST with the following node types:
@@ -272,6 +346,22 @@ This demonstrates the sophisticated language detection capabilities:
272346
- Combined detection strategies
273347
- Edge case handling (mixed content, ambiguous code, etc.)
274348

349+
### AST Generation Pipeline Demo
350+
351+
Run the comprehensive AST generation demo:
352+
353+
```bash
354+
cargo run --example ast_generation_demo
355+
```
356+
357+
This demonstrates the advanced AST generation pipeline:
358+
- Configurable AST building with filtering options
359+
- AST analysis and complexity metrics
360+
- Function signature extraction
361+
- Symbol table construction with scope resolution
362+
- AST optimization and node reduction
363+
- Performance comparisons between different configurations
364+
275365
## Testing
276366

277367
Run the test suite:

0 commit comments

Comments
 (0)