databas_sql_parser crate provides a complete SQL parsing solution for Databas, implementing lexical analysis and syntax parsing for SQL statements.
Architecture
The parser is organized into two main components:- Lexer: Tokenizes SQL source text into a stream of tokens
- Parser: Builds an Abstract Syntax Tree (AST) from the token stream
Module Structure
Lexer Module
The lexer module handles tokenization of SQL source code:token: Defines theTokenstruct representing a token with its positiontoken_kind: Enumerates all token types including keywords, operators, and literalsLexer: Iterator that produces tokens from input text
Parser Module
The parser module constructs AST nodes from tokens:expr: Expression types including literals, identifiers, operators, and aggregate functionsop: Operator types and precedence rulesstmt: SQL statement types (SELECT, INSERT, CREATE TABLE)Parser: Iterator that produces statements from the lexer
Error Module
The error module definesSQLError and SQLErrorKind for detailed error reporting with position information.
Supported SQL Statements
The parser currently supports:- SELECT: Query data with WHERE, ORDER BY, LIMIT, and OFFSET clauses
- INSERT: Insert rows with multiple value tuples
- CREATE TABLE: Define tables with typed columns and constraints
Usage Example
Expression Parsing
The parser uses Pratt parsing (operator precedence parsing) to handle expressions with correct precedence and associativity:Error Handling
All parsing operations returnResult types with detailed error information:
Next Steps
- Lexer API Reference - Detailed lexer API documentation
- Parser API Reference - Detailed parser API documentation