MCPcopy Create free account
hub / github.com/ajitpratap0/GoSQLX / Parse

Method Parse

cmd/gosqlx/cmd/parser_cmd.go:66–106  ·  view source on GitHub ↗

Parse parses the given SQL input (file or direct SQL)

(input string)

Source from the content-addressed store, hash-verified

64
65// Parse parses the given SQL input (file or direct SQL)
66func (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
109func (p *Parser) Display(result *ParserResult) error {

Callers 10

TestParser_ParseFunction · 0.95
TestParser_DisplayTokensFunction · 0.95
TestParser_DisplayTreeFunction · 0.95
TestParser_ComplexQueryFunction · 0.95
TestConvertStatementFunction · 0.95
parseRunFunction · 0.95
parseFromStdinFunction · 0.95

Calls 8

ReleaseMethod · 0.95
ParseFromModelTokensMethod · 0.95
GetTokenizerFunction · 0.92
PutTokenizerFunction · 0.92
NewParserFunction · 0.92
TokenizeMethod · 0.80
DetectAndReadInputFunction · 0.70
ErrorfMethod · 0.65

Tested by 8

TestParser_ParseFunction · 0.76
TestParser_DisplayTokensFunction · 0.76
TestParser_DisplayTreeFunction · 0.76
TestParser_ComplexQueryFunction · 0.76
TestConvertStatementFunction · 0.76