@@ -37,6 +37,28 @@ $tokens = ( new WP_MySQL_Lexer( 'SELECT 1 + 2' ) )->remaining_tokens();
3737$ast = $parser->parse( $tokens );
3838```
3939
40+ ## Tokens and AST nodes
41+ AST nodes and tokens use the names from the official MySQL grammar (`sql/sql_yacc.yy`):
42+
43+ - **Nodes** are named after grammar rules, such as `select_item` or `simple_expr`.
44+ - **Keywords** are looked up by their SQL spelling in `WP_MySQL_Lexer::KEYWORDS`,
45+ which is generated from the MySQL keyword list (`sql/lex.h`).
46+ - **Other tokens** are `WP_MySQL_Lexer` constants named after their grammar terminals,
47+ such as `IDENT`, `TEXT_STRING`, or `NUM`.
48+ - **Single-character tokens** are named after the character, such as `LEFT_PAREN`,
49+ `COMMA`, or `ASTERISK`.
50+
51+ ```php
52+ $ast->get_descendant_nodes( 'select_item' ); // A grammar rule.
53+ $ast->get_first_descendant_token( WP_MySQL_Lexer::KEYWORDS['DISTINCT'] ); // A keyword.
54+ $ast->get_first_descendant_token( WP_MySQL_Lexer::IDENT ); // A grammar terminal.
55+ $ast->get_first_descendant_token( WP_MySQL_Lexer::ASTERISK ); // A single character.
56+ ```
57+
58+ Token IDs are the grammar's own token numbers. Like in MySQL, the token stream of
59+ valid input ends with `END_OF_INPUT` and `YYEOF`. For debugging, `$token->get_name()`
60+ returns a readable token name.
61+
4062## Development
4163This package includes the full grammar compilation pipeline. The generated artifacts
4264are committed to the repository, and they only need to be regenerated on grammar
0 commit comments