Skip to main content

rupg_sql/
lib.rs

1//! The lexer, the parser from the vendored `gram.y`, the parse tree and its printer.
2//!
3//! This crate first ships in milestone M2. See `spec/07-sql-types-and-catalog.md` section 7.2, `spec/22-crate-layout.md` section 22.4 and `spec/23-milestones.md` section 23.5.
4//!
5//! Lifted from `crates/rudb-pgparse/src/lib.rs` of tamnd/rudb at f5f7065a (spec/04 section 4.9).
6//!
7//! The grammar is not written by hand. `cargo xtask vendor postgres-19` copies `gram.y`, `scan.l`, `parser.c`, `kwlist.h` and the node headers from PostgreSQL at the pin into `vendor/postgres-19`. `cargo xtask sql` makes the LALR(1) tables in `src/generated/` from them, with the state numbers that `bison -v` gives for the same grammar. So a statement that PostgreSQL parses is a statement that this crate parses, and a syntax error stops at the same token.
8//!
9//! # What is here
10//!
11//! [`keyword`], which says whether a word is a keyword and of which [`Category`], and [`token`], the token numbers.
12//!
13//! [`Lexer`], a port of `scan.l`, and [`Tokens`], the filter of `parser.c` after it, which looks one token further for the few places where the grammar needs two tokens.
14//!
15//! [`check`], which lexes and parses a text and gives the same first error as PostgreSQL, with the same message and position. [`recognize`] runs the tables over a list of tokens.
16//!
17//! [`nodes`], the node types of the raw parse tree of PostgreSQL, generated from the vendored headers with the names and the fields of C. Each node writes the text of `nodeToString`, which PostgreSQL logs for a statement when `debug_print_raw_parse` is on, so a test compares the two trees. [`parse`] runs the actions of `gram.y` and gives this tree.
18
19#![forbid(unsafe_code)]
20
21mod actions;
22mod error;
23mod filter;
24mod generated;
25mod lexer;
26pub mod nodes;
27mod parser;
28
29pub use error::{Error, Notice, Severity};
30pub use filter::Tokens;
31pub use generated::keywords::{Category, Keyword};
32pub use generated::tables::{RULES, STATES, TOKENS, token};
33pub use lexer::{Lexer, Token, Value};
34pub use parser::{
35    SyntaxError, character, check, keyword, keywords, parse, parse_plpgsql_assign,
36    parse_plpgsql_expr, parse_type_name, recognize, rule_name, symbol_name,
37};