ExSQL. Parser
(exsql v0.1.1)
Copy Markdown
Recursive-descent parser producing ExSQL.AST structs.
SQLite generates its parser from a Lemon LALR grammar (parse.y); a
hand-written recursive descent with precedence climbing is the idiomatic
Elixir equivalent and keeps the grammar readable as plain functions, one
per production.
Expression representation
Expressions are tagged tuples:
{:literal, value}—nil, integer, float, UTF-8 binary,{:blob, bin}, or boolean (TRUE/FALSEliterals){:column, table | nil, name}— column reference{:binary, op, left, right}—opin:or | :and | :eq | :ne | :lt | :le | :gt | :ge | :add | :sub | :mul | :div | :mod | :concat{:not, expr}/{:negate, expr}{:is, expr, expr}/{:is_not, expr, expr}— NULL-safe comparison{:in, expr, [expr] | {:select, query}, negated?}{:between, expr, low, high, negated?}{:like, expr, pattern, negated?}/{:glob, expr, pattern, negated?}{:function, name, :star | [expr]}— e.g.count(*){:case, operand | nil, [{when_expr, then_expr}], else_expr | nil}{:subquery, %Select{}}— scalar subquery{:exists, %Select{}}—EXISTS (SELECT ...)
Summary
Functions
Parses sql into a list of statements.
Types
@type expr() :: tuple()
@type statement() :: ExSQL.AST.Select.t() | ExSQL.AST.Compound.t() | ExSQL.AST.Values.t() | ExSQL.AST.With.t() | ExSQL.AST.CreateTable.t() | ExSQL.AST.CreateIndex.t() | ExSQL.AST.CreateView.t() | ExSQL.AST.AlterTable.t() | ExSQL.AST.Insert.t() | ExSQL.AST.Pragma.t() | ExSQL.AST.Update.t() | ExSQL.AST.Delete.t() | ExSQL.AST.DropTable.t() | ExSQL.AST.DropIndex.t() | ExSQL.AST.DropView.t() | {:attach, expr(), String.t()} | {:detach, String.t()} | {:explain, statement()} | {:explain_query_plan, statement()}
Functions
@spec parse(String.t()) :: {:ok, [statement()]} | {:error, ExSQL.Error.t()}
Parses sql into a list of statements.
Accepts multiple statements separated by ;. Returns {:ok, statements}
or {:error, %ExSQL.Error{}}.