RookDb SQL Compiler (lexical_parser)
Lightweight SQL lexer + parser in Rust that converts SQL queries into an Abstract Syntax Tree (AST).
๐ Overview
This project is a small SQL front-end designed for learning, experimentation, and compiler-course style work.
It helps you:
- tokenize SQL input (lexical analysis),
- parse supported SQL grammar (syntactic analysis),
- inspect AST output in debug and readable formats.
Problem solved: it provides a clean way to understand how SQL text becomes structured syntax trees.
Who it is for:
- students learning compiler construction,
- beginners learning Rust parsing patterns,
- developers prototyping SQL tooling.
โจ Features
- SQL tokenization using
sqlparsertokenizer + custom token classification. - Recursive-descent parser with expression precedence handling.
- AST generation for common statement families.
- Supports
SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,ALTER. - Join support:
INNER,LEFT,RIGHT,FULL,CROSS. - Multiple runtime input modes:
- command argument SQL,
--stdinpiped SQL,- interactive prompt,
- demo-query fallback.
- Separate integration test suites for lexer and parser.
๐ Tech Stack
| Category | Stack |
|---|---|
| Language | Rust (Edition 2021) |
| Parsing helper library | sqlparser = 0.48 (tokenizer) |
| Logging | log, env_logger |
| Build/Test tooling | Cargo (cargo build, cargo run, cargo test, cargo clippy) |
๐ฆ Prerequisites / Required Installations
System requirements
- Windows, Linux, or macOS
- Terminal/command prompt
Required software
- Rust toolchain (recommended stable)
- Cargo (bundled with Rust)
Install Rust (if needed)
# Linux/macOS
|
# Windows
# Install from: https://rustup.rs/
Verify installation:
Assumption: Rust stable
1.75+is sufficient (exact minimum version is not pinned in this repo).
๐ Installation
- Clone the repository:
- Build once to fetch dependencies:
- (Optional) Run tests to verify setup:
Environment variables
No .env file is required.
Assumption: Default logger configuration is sufficient for local usage.
โถ๏ธ How to Run
Development run
Behavior with no arguments:
- prompts for one SQL query,
- if you press Enter without input, it runs built-in demo queries.
Run a single SQL query (argument mode)
Run from stdin (recommended for pipes)
|
Production-style run
โ๏ธ Configuration
This project currently has minimal runtime configuration.
| Config | Where | Description |
|---|---|---|
| Logging | env_logger |
Uses default environment behavior; no required variables |
| Input mode | CLI args / --stdin / interactive |
Determines where SQL is read from |
No custom config file is required.
๐ง How It Works (Architecture / Code Flow)
High-level flow:
- Input collection (
src/main.rs)
- read SQL from argument,
--stdin, interactive input, or demo list.
- Lexical analysis (
src/lib.rs)
LexicalParsertokenizes raw SQL and maps tokens toTokenType.
- Syntactic analysis (
src/parser.rs)
SyntacticParserconsumes tokens and builds AST nodes.
- AST output (
src/main.rs)
- prints both debug AST and display-formatted AST.
Key modules:
src/lib.rs: exported lexer/token API + module exports.src/parser.rs: recursive-descent SQL parser.src/ast.rs: AST types for statements/expressions/clauses.src/main.rs: CLI runtime entry point.
๐ Project Structure
compiler/
โโ Cargo.toml
โโ README.md
โโ src/
โ โโ main.rs # CLI/runtime entry
โ โโ lib.rs # Public lexer/token API
โ โโ parser.rs # Syntactic parser
โ โโ ast.rs # AST definitions
โโ tests/
โ โโ lexer_tests.rs
โ โโ parser_tests.rs
โโ target/ # Build artifacts (generated)
๐งช Testing
Test framework: Rust built-in test framework (cargo test).
Run all tests:
Run with strict linting:
๐ง Known Limitations / Future Improvements
- Supports a practical SQL subset, not full SQL standard compliance.
- Error diagnostics can be improved (friendlier messages, richer context).
- No benchmark/performance profiling pipeline yet.
- No CI workflow file included yet.
- No fuzz/property-based parser robustness tests yet.