Skip to main content

Crate rupg_sql

Crate rupg_sql 

Source
Expand description

The lexer, the parser from the vendored gram.y, the parse tree and its printer.

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.

Lifted from crates/rudb-pgparse/src/lib.rs of tamnd/rudb at f5f7065a (spec/04 section 4.9).

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.

§What is here

keyword, which says whether a word is a keyword and of which Category, and token, the token numbers.

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.

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.

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.

Modules§

nodes
The raw parse tree: the node types of PostgreSQL and their text.
token
The token numbers. A character token is in CHARACTER.

Structs§

Error
An error of the lexer or the parser.
Keyword
One keyword.
Lexer
The lexer. It gives one token for each call of Lexer::next_token.
Notice
A notice or a warning of the lexer or the parser, for example for an identifier that is too long.
SyntaxError
The place where the grammar stops accepting the input.
Token
One token.
Tokens
The tokens that the parser reads.

Enums§

Category
The category that decides where a keyword can be a name.
Severity
The severity of a Notice.
Value
The value of a token.

Constants§

RULES
The number of rules. Rule 0 is $accept, which never reduces.
STATES
The number of states.
TOKENS
The number of tokens. A symbol below this number is a token.

Functions§

character
The token of an ASCII character that is a token by itself, such as ; or +.
check
Lexes and parses text as PostgreSQL does, and gives the first error of the lexer or the grammar, or the notices when there is no error.
keyword
The keyword that a word spells, with ASCII letters in any case, as kwlist.h has it.
keywords
Every keyword, in the order of kwlist.h.
parse
The raw parse tree of text and the notices of the parse, as raw_parser of PostgreSQL gives them: a list with a RawStmt for each statement. An error is the first error of the lexer, the grammar or an action.
parse_plpgsql_assign
The PLAssignStmt of a PL/pgSQL assignment text, as raw_parser gives it in the modes RAW_PARSE_PLPGSQL_ASSIGN1 to RAW_PARSE_PLPGSQL_ASSIGN3: the target with names names, which is 1, 2 or 3, its indirection, := and the value. An error is the first error of the lexer, the grammar or an action, at a byte offset in text.
parse_plpgsql_expr
The SelectStmt of a PL/pgSQL expression text, as raw_parser gives it in the mode RAW_PARSE_PLPGSQL_EXPR: the targets and the clauses of a SELECT without the word SELECT. An error is the first error of the lexer, the grammar or an action, at a byte offset in text.
parse_type_name
typeStringToTypeName without its checks: the TypeName of text, as raw_parser gives it in the mode RAW_PARSE_TYPE_NAME. An error is the first error of the lexer, the grammar or an action, at a byte offset in text.
recognize
Runs the grammar over a list of tokens. The list does not end with the end token; the end of the slice is the end of the input.
rule_name
The name of a rule as productions.txt has it, for example a_expr.17, or None when there is no such rule.
symbol_name
The name of a symbol as gram.y has it, for example IDENT, '+' or a_expr, or None when there is no such symbol. The tokens come first.