-
Notifications
You must be signed in to change notification settings - Fork 5
Using the parser
The implementation split the grammar and the Abstract Syntax Tree (AST) building. As a general rule, a class implementing the grammar of a SQL command is suffixed by Grammar
and a class implementing an AST builder is suffixed by ASTBuilder
.
You can find these classes in PostgreSQL-Parser
package. Each SQL command should be implemented as a separated grammar class. However some grammar classes do not implement a SQL command. For example, PSQLExpressionGrammar
implements the parser for PostgreSQL expression. It is used as a dependency by many other parser classes because expressions are basically everywhere in SQL commands.
Parsing an input string using a grammar class will return the input tokenized. You can use the grammar classes as follow:
PSQLExpressionGrammar parse: '42 + ''42''::int4'
You can find the classes representing the nodes of the AST in PostgreSQL-AST
package. The classes implementing the AST builders are in PostgreSQL-AST-Builder
package. AST builder classes are always inheriting from a grammar class.
Parsing an input string using a AST builder class will return an AST composed of instances of subclasses of PSQLASTNode
. You can use the AST builder classes as follow:
PSQLExpressionASTBuilder parse: '42 + ''42''::int4'
To process the AST generated, one should implement a visitor. To do that, you need to subclass PSQLASTVisitor
from PostgreSQL-AST-Visitors
package and override methods corresponding to the AST nodes you need to process.
Some grammars/AST builders are using others grammars/AST builders to avoid code duplication. The following diagrams show these dependencies. To declare these dependencies, grammar classes implement #dependencies
method in class-side.