11//! Tree-sitter integration for multi-language parsing
22
33use crate :: ast:: { ASTNode , NodeType , NodeMetadata } ;
4+ use crate :: ast_builder:: { ASTBuilder , ASTBuilderBuilder , ASTBuilderConfig } ;
5+ use crate :: ast_processor:: { ASTProcessor , ASTAnalysis } ;
46use crate :: language:: Language ;
57use crate :: language_config:: { LanguageConfig , LANGUAGE_CONFIGS } ;
68use crate :: parser:: { ParseError , ParseResult , Parser } ;
@@ -10,6 +12,9 @@ use once_cell::sync::Lazy;
1012/// Tree-sitter based parser implementation
1113pub 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
2631impl 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