Parse parses the given SQL input (file or direct SQL)
(input string)
| 64 | |
| 65 | // Parse parses the given SQL input (file or direct SQL) |
| 66 | func (p *Parser) Parse(input string) (*ParserResult, error) { |
| 67 | result := &ParserResult{} |
| 68 | |
| 69 | // Use robust input detection with security checks |
| 70 | inputResult, err := DetectAndReadInput(input) |
| 71 | if err != nil { |
| 72 | result.Error = fmt.Errorf("input processing failed: %w", err) |
| 73 | return result, result.Error |
| 74 | } |
| 75 | |
| 76 | // Use pooled tokenizer |
| 77 | tkz := tokenizer.GetTokenizer() |
| 78 | defer tokenizer.PutTokenizer(tkz) |
| 79 | |
| 80 | // Tokenize |
| 81 | tokens, err := tkz.Tokenize(inputResult.Content) |
| 82 | if err != nil { |
| 83 | result.Error = fmt.Errorf("tokenization failed: %w", err) |
| 84 | return result, result.Error |
| 85 | } |
| 86 | |
| 87 | result.Tokens = tokens |
| 88 | |
| 89 | // If only tokens requested, return early |
| 90 | if p.Opts.ShowTokens { |
| 91 | return result, nil |
| 92 | } |
| 93 | |
| 94 | // Parse to AST with proper error handling for memory management |
| 95 | pr := parser.NewParser() |
| 96 | defer pr.Release() |
| 97 | astObj, err := pr.ParseFromModelTokens(result.Tokens) |
| 98 | if err != nil { |
| 99 | // Parser failed, no AST to release |
| 100 | result.Error = fmt.Errorf("parsing failed: %w", err) |
| 101 | return result, result.Error |
| 102 | } |
| 103 | |
| 104 | result.AST = astObj |
| 105 | return result, nil |
| 106 | } |
| 107 | |
| 108 | // Display displays the parsing result based on configuration |
| 109 | func (p *Parser) Display(result *ParserResult) error { |