Documentation
¶
Overview ¶
Package parser implements a hand-written recursive descent parser for the SQLite SQL dialect.
The grammar is SQLite's src/parse.y, a Lemon LALR(1) grammar. Every non-trivial function here names the rule or rules it implements. Where parse.y resolves an ambiguity with a precedence annotation or with rule ordering, the equivalent decision is spelled out and commented.
Errors match SQLite byte for byte. There are exactly three parser-level messages:
near "X": syntax error the token X could not be shifted
unrecognized token: "X" the tokenizer produced TK_ILLEGAL at X
incomplete input the error fell on the end of the input,
where SQLite feeds a synthetic empty token
Parsing is fail-fast: the first error aborts, as it does in SQLite, which reports exactly one parse error per statement.
One habit recurs throughout and is worth stating once. SQLite's LALR parser shifts a token as soon as some rule can begin with it, and only discovers that the rule does not continue on the token after. So where a keyword can only introduce one thing -- NOT before INDEXED, DEFERRABLE or MATERIALIZED, say -- meyer consumes it before checking what follows, rather than peeking. Peeking would put the error on the keyword; SQLite puts it on what came next.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Parse ¶
Parse reads SQL from r and returns one ast.Stmt per statement.
The context is accepted so the signature matches the sibling parsers sqlc uses, and is not consulted: parsing is a bounded, allocation-light pass over the input, and the recursion limit keeps even hostile input from taking long enough to be worth cancelling.
func ParseStatement ¶
ParseStatement parses exactly one statement and rejects trailing input.
Types ¶
type Error ¶
Error is the error type returned for inputs meyer rejects.
Message matches SQLite's parser wording byte for byte (`near "FROM": syntax error`), and is the field to compare against when conformance is what matters. Offset is the byte offset of the error, the analogue of sqlite3_error_offset, or -1 when the position is unknown. SQL is the input that failed, so that Error can turn the offset into a line and column.