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.
- Syntax
Error - 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
textas 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.hhas it. - keywords
- Every keyword, in the order of
kwlist.h. - parse
- The raw parse tree of
textand the notices of the parse, asraw_parserof PostgreSQL gives them: a list with aRawStmtfor each statement. An error is the first error of the lexer, the grammar or an action. - parse_
plpgsql_ assign - The
PLAssignStmtof a PL/pgSQL assignmenttext, asraw_parsergives it in the modesRAW_PARSE_PLPGSQL_ASSIGN1toRAW_PARSE_PLPGSQL_ASSIGN3: the target withnamesnames, 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 intext. - parse_
plpgsql_ expr - The
SelectStmtof a PL/pgSQL expressiontext, asraw_parsergives it in the modeRAW_PARSE_PLPGSQL_EXPR: the targets and the clauses of aSELECTwithout the wordSELECT. An error is the first error of the lexer, the grammar or an action, at a byte offset intext. - parse_
type_ name typeStringToTypeNamewithout its checks: theTypeNameoftext, asraw_parsergives it in the modeRAW_PARSE_TYPE_NAME. An error is the first error of the lexer, the grammar or an action, at a byte offset intext.- 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.txthas it, for examplea_expr.17, orNonewhen there is no such rule. - symbol_
name - The name of a symbol as
gram.yhas it, for exampleIDENT,'+'ora_expr, orNonewhen there is no such symbol. The tokens come first.