-
Notifications
You must be signed in to change notification settings - Fork 6
internals
The SQL string is first processed by Lexer
and converted to TokenStream
object aggregating Token
instances. Parser
then goes over that stream and builds the Abstract Syntax Tree of Node
s.
This class is based on flex lexer defined in src/backend/parser/scan.l
file of Postgres sources.
This class represents a token and has knowledge of its type, value and position in input string. It implements a matches()
method that checks whether token's type and/or value matches the given values.
This is a stream of Token
s. The class allows forward movement with next()
and skip()
, lookahead with look()
, matching of the current token(s) with matches()
and expect()
.
There are also slightly optimized wrappers for most common matches()
cases: matchesKeyword()
, matchesSpecialChar()
, matchesAnyType()
, matchesKeywordSequence()
.
Token
and TokenStream
implement magic __toString()
method allowing easy debug output:
use sad_spirit\pg_builder\Lexer;
$lexer = new Lexer();
echo $lexer->tokenize('select * from some_table');
yields
keyword 'select' at position 0
special character '*' at position 7
keyword 'from' at position 9
identifier 'some_table' at position 14
end of input
This is a LL(*) recursive descent parser. It tries to closely follow bison grammar defined in src/backend/parser/gram.y
file of Postgres sources, but the implementation is completely independent.
Differences from Postgres parser: the following constructs are not supported
-
TABLE name
alias forSELECT * FROM name
SELECT INTO
-
WHERE CURRENT OF cursor
forUPDATE
andDELETE
queries - Undocumented
TREAT()
function