Skip to content

Instantly share code, notes, and snippets.

@dmitryvk
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmitryvk/4a460fbe46e5812fba51 to your computer and use it in GitHub Desktop.
Save dmitryvk/4a460fbe46e5812fba51 to your computer and use it in GitHub Desktop.
NHibernate HQL AST Grammar
tree grammar HqlSqlWalker options {language=CSharp3; output=AST; tokenVocab=Hql; ASTLabelType=IASTNode; } ;
public statement : selectStatement | updateStatement
| deleteStatement | insertStatement;
selectStatement : query ;
updateStatement : ^(UPDATE VERSIONED? fromClause setClause whereClause?);
deleteStatement : ^(DELETE fromClause whereClause?) ;
insertStatement : ^(INSERT intoClause query) ;
intoClause : ^(INTO path insertablePropertySpec) ;
insertablePropertySpec : ^(RANGE IDENT+);
setClause : ^(SET assignment*);
assignment : ^(EQ propertyRef newValue);
newValue : expr | query;
query : unionedQuery | ^(UNION unionedQuery query);
unionedQuery : ^(QUERY ^(SELECT_FROM fromClause selectClause?) whereClause?
groupClause? havingClause? orderClause? skipClause? takeClause?);
orderClause : ^(ORDER (orderExprs | query (ASCENDING|DESCENDING)?));
orderExprs : orderExpr ( ASCENDING | DESCENDING )? orderExprs?;
orderExpr : resultVariableRef | expr;
resultVariableRef : identifier;
skipClause : ^(SKIP (NUM_INT | parameter));
takeClause : ^(TAKE (NUM_INT | parameter));
groupClause : ^(GROUP expr+) ;
havingClause : ^(HAVING logicalExpr) ;
selectClause : ^(SELECT DISTINCT? selectExprList);
selectExprList : (selectExpr | aliasedSelectExpr)+ ;
aliasedSelectExpr : ^(AS selectExpr identifier) ;
selectExpr : propertyRef | ^( ALL aliasRef ) | ^( OBJECT aliasRef )
| constructor | functionCall | parameter | count
| collectionFunction| literal | arithmeticExpr | query;
count : ^( COUNT ( DISTINCT | ALL )? ( aggregateExpr | ROW_STAR ) ) ;
constructor : ^(CONSTRUCTOR path (selectExpr | aliasedSelectExpr)*) ;
aggregateExpr : expr | collectionFunction;
fromClause : ^(FROM fromElementList ) ;
fromElementList : fromElement+ ;
fromElement : ^( RANGE path ALIAS? FETCH?)
| joinElement
| FILTER_ENTITY ALIAS;
joinElement : ^( JOIN joinType? FETCH? propertyRef
ALIAS? FETCH? ( ^(WITH .*) )?
);
joinType: (LEFT|RIGHT) OUTER? | FULL | INNER;
path: identifier | ^( DOT path identifier );
pathAsIdent : path;
withClause : ^(WITH logicalExpr ) ;
whereClause : ^(WHERE logicalExpr );
logicalExpr : ^(AND logicalExpr logicalExpr) | ^(OR logicalExpr logicalExpr)
| ^(NOT logicalExpr) | comparisonExpr | functionCall | logicalPath;
logicalPath : addrExpr;
comparisonExpr: ^( EQ exprOrSubquery exprOrSubquery )
| ^( NE exprOrSubquery exprOrSubquery )
| ^( LT exprOrSubquery exprOrSubquery )
| ^( GT exprOrSubquery exprOrSubquery )
| ^( LE exprOrSubquery exprOrSubquery )
| ^( GE exprOrSubquery exprOrSubquery )
| ^( LIKE exprOrSubquery expr ( ^( ESCAPE expr ) )? )
| ^( NOT_LIKE exprOrSubquery expr ( ^( ESCAPE expr ) )? )
| ^( BETWEEN exprOrSubquery exprOrSubquery exprOrSubquery )
| ^( NOT_BETWEEN exprOrSubquery exprOrSubquery exprOrSubquery )
| ^( IN exprOrSubquery inRhs ) | ^( NOT_IN exprOrSubquery inRhs )
| ^( IS_NULL exprOrSubquery ) | ^( IS_NOT_NULL exprOrSubquery )
| ^( EXISTS ( expr | collectionFunctionOrSubselect ) ) ;
inRhs : ^( IN_LIST ( collectionFunctionOrSubselect | expr* ) ) ;
exprOrSubquery : expr | query | ^( ANY collectionFunctionOrSubselect )
| ^( ALL collectionFunctionOrSubselect )
| ^( SOME collectionFunctionOrSubselect );
collectionFunctionOrSubselect : collectionFunction | query;
expr : addrExpr | ^( VECTOR_EXPR ( expr )* ) | constant | arithmeticExpr
| functionCall | parameter | count;
arithmeticExpr : ^( PLUS exprOrSubquery exprOrSubquery )
| ^( MINUS exprOrSubquery exprOrSubquery )
| ^( DIV exprOrSubquery exprOrSubquery )
| ^( STAR exprOrSubquery exprOrSubquery )
| ^( BNOT exprOrSubquery )
| ^( BAND exprOrSubquery exprOrSubquery )
| ^( BOR exprOrSubquery exprOrSubquery )
| ^( BXOR exprOrSubquery exprOrSubquery )
| ^( UNARY_MINUS exprOrSubquery )
| caseExpr;
caseExpr : ^( CASE ( ^( WHEN logicalExpr expr ) )+ ( ^( ELSE expr ) )? )
| ^( CASE2 expr ( ^( WHEN expr expr ) )+ ( ^( ELSE expr ) )? );
collectionFunction : ^(ELEMENTS propertyRef ) | ^(INDICES propertyRef );
functionCall : ^(METHOD_CALL pathAsIdent (^(EXPR_LIST (expr|query|comparisonExpr)*))?)
| ^(AGGREGATE aggregateExpr);
constant : literal | NULL | TRUE | FALSE | JAVA_CONSTANT;
literal : numericLiteral | stringLiteral;
numericLiteral : NUM_INT | NUM_LONG | NUM_FLOAT | NUM_DOUBLE | NUM_DECIMAL;
stringLiteral : QUOTED_String ;
identifier : IDENT | WEIRD_IDENT;
addrExpr : addrExprDot | addrExprIndex | addrExprIdent;
addrExprDot : ^(DOT addrExpr propertyName);
addrExprIndex : ^(INDEX_OP addrExpr expr);
addrExprIdent : identifier;
propertyName : identifier | CLASS | ELEMENTS | INDICES;
propertyRef : propertyRefPath | propertyRefIdent;
propertyRefPath : ^(DOT propertyRef propertyName );
propertyRefIdent : identifier ;
aliasRef : identifier ;
parameter : ^(COLON identifier) | ^(PARAM NUM_INT?);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment