expose a parsing function?
I wondered if it would be possible for modernc/sqlite to expose a parsing function?
Here's some background in case it helps explain why I'm asking.
The SQLite C API doesn't expose a parsing function as far as I can tell, which means for https://github.com/kyleconroy/sqlc we'd need to create a project similar to pg_query_go.
Currently, sqlc use ANTLR as there was an existing ANTLR grammar but it is low level and difficult to use, so I don't think SQLite is going to be improved until we find a better parser.
There was a little investigation of using the internal parser from modernc's sqlite implementation. However, at the time, they weren't able to get anything working that could be used in sqlc, but the progress was shared and might help someone else.
https://gist.github.com/maxhawkins/f162eaacddabbe380c7fb75791d85a56
$ go run *.go 'WITH foo AS (SELECT 1) SELECT * FROM foo'
valid sql
$ go run *.go 'bad sql'
2021/08/02 13:09:53 parse error: near "bad": syntax error
exit status 1
This program uses the sqlite3RunParser function to parse an input sql string and return a syntax error if it fails to parse. I learned that sqlite3RunParser is a higher-level parsing function that relies on information from the database schema and parses directly into bytecode instead of creating an AST. This means my program fails if the input SQL references any tables or tries to create them.
A better approach may be using the tokenizer (sqlite3GetToken) and LALR(1) parser (yyParser) directly and then pulling the parsed data from the yyParser struct. There's an example of that in the source for sqlite3RunParser.
Do you have any thoughts or advice?