ExSQL.Parser (exsql v0.1.5)

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/FALSE literals)
  • {:column, table | nil, name} — column reference

  • {:binary, op, left, right}op in :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

Functions

parse(sql)

@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{}}.