MCPcopy
hub / github.com/sqlc-dev/sqlc / Parse

Method Parse

internal/engine/sqlite/parse.go:40–87  ·  view source on GitHub ↗
(r io.Reader)

Source from the content-addressed store, hash-verified

38}
39
40func (p *Parser) Parse(r io.Reader) ([]ast.Statement, error) {
41 blob, err := io.ReadAll(r)
42 if err != nil {
43 return nil, err
44 }
45 input := antlr.NewInputStream(string(blob))
46 lexer := parser.NewSQLiteLexer(input)
47 stream := antlr.NewCommonTokenStream(lexer, 0)
48 pp := parser.NewSQLiteParser(stream)
49 el := &errorListener{}
50 pp.AddErrorListener(el)
51 // pp.BuildParseTrees = true
52 tree := pp.Parse()
53 if el.err != "" {
54 return nil, errors.New(el.err)
55 }
56 pctx, ok := tree.(*parser.ParseContext)
57 if !ok {
58 return nil, fmt.Errorf("expected ParserContext; got %T\n", tree)
59 }
60 var stmts []ast.Statement
61 for _, istmt := range pctx.AllSql_stmt_list() {
62 list, ok := istmt.(*parser.Sql_stmt_listContext)
63 if !ok {
64 return nil, fmt.Errorf("expected Sql_stmt_listContext; got %T\n", istmt)
65 }
66 loc := 0
67
68 for _, stmt := range list.AllSql_stmt() {
69 converter := &cc{}
70 out := converter.convert(stmt)
71 if _, ok := out.(*ast.TODO); ok {
72 loc = stmt.GetStop().GetStop() + 2
73 continue
74 }
75 len := (stmt.GetStop().GetStop() + 1) - loc
76 stmts = append(stmts, ast.Statement{
77 Raw: &ast.RawStmt{
78 Stmt: out,
79 StmtLocation: loc,
80 StmtLen: len,
81 },
82 })
83 loc = stmt.GetStop().GetStop() + 2
84 }
85 }
86 return stmts, nil
87}
88
89func (p *Parser) CommentSyntax() source.CommentSyntax {
90 return source.CommentSyntax{

Callers 2

TestUpdateFunction · 0.95
TestFormatFunction · 0.95

Implementers 4

Parserinternal/engine/postgresql/parse.go
Parserinternal/engine/dolphin/parse.go
Parserinternal/engine/clickhouse/parse.go
Parserinternal/engine/sqlite/parse.go

Calls 6

ParseMethod · 0.95
convertMethod · 0.95
NewSQLiteLexerFunction · 0.92
NewSQLiteParserFunction · 0.92
AllSql_stmt_listMethod · 0.65
AllSql_stmtMethod · 0.65

Tested by 2

TestUpdateFunction · 0.76
TestFormatFunction · 0.76