parser

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 31, 2026 License: MIT Imports: 8 Imported by: 0

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 LineCol

func LineCol(src string, offset int) (line, col int)

LineCol converts a byte offset into a 1-based line and column.

func Parse

func Parse(ctx context.Context, r io.Reader) ([]ast.Stmt, error)

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 ParseExpr

func ParseExpr(src string) (expr ast.Expr, err error)

ParseExpr parses a single expression. It exists for tests and tooling.

func ParseStatement

func ParseStatement(src string) (ast.Stmt, error)

ParseStatement parses exactly one statement and rejects trailing input.

func ParseString

func ParseString(src string) (stmts []ast.Stmt, err error)

ParseString parses a complete SQL script.

Types

type Error

type Error struct {
	Message string
	Offset  int
	SQL     string
}

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.

func (*Error) Error

func (e *Error) Error() string

Error renders the message with a position when there is one, in the "line:column: message" form editors and compilers expect. Positions are stored as byte offsets and converted only here, on demand.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL