public class HQLParser extends antlr.LLkParser implements HQLParserTokenTypes
HQLLexer
). More specifically, the parser does the following:
nextToken
method.
This method tokenizes the lexer's input stream of characters and
returns the next found Token
object.
expression()
method as an entry point.
HQLPreprocessor
.
This grammar is highly specific to ANTLR 2.7.4 and it may not be valid in future versions of ANTLR. This is due to the fact that the generated code and the grammar syntax of ANTLR can change between versions. These factors are the most important constraints defining the structure of the resulting grammar. As a result, it is possible that the grammar will require changes to run in future versions of ANTLR.
Please note that this is a generated file using ANTLR 2.7.4 and a
grammar specified in hql.g
.
Modifier and Type | Field and Description |
---|---|
static java.lang.String[] |
_tokenNames |
static antlr.collections.impl.BitSet |
_tokenSet_0 |
static antlr.collections.impl.BitSet |
_tokenSet_1 |
static antlr.collections.impl.BitSet |
_tokenSet_10 |
static antlr.collections.impl.BitSet |
_tokenSet_11 |
static antlr.collections.impl.BitSet |
_tokenSet_2 |
static antlr.collections.impl.BitSet |
_tokenSet_3 |
static antlr.collections.impl.BitSet |
_tokenSet_4 |
static antlr.collections.impl.BitSet |
_tokenSet_5 |
static antlr.collections.impl.BitSet |
_tokenSet_6 |
static antlr.collections.impl.BitSet |
_tokenSet_7 |
static antlr.collections.impl.BitSet |
_tokenSet_8 |
static antlr.collections.impl.BitSet |
_tokenSet_9 |
private static java.lang.String |
AST_CLASS
Name of the specialized AST class to use when parsing.
|
private int |
nextSubstitutionIndex
Next index value to be assigned to a query substitution node
|
private static java.util.Map |
tokenLookup
Provides an efficient reverse lookup of names to token types.
|
astFactory, inputState, returnAST, tokenNames, tokenTypeToASTClassMap, traceDepth
ALIAS, AND, AS, BOOL_FALSE, BOOL_TRUE, CASE, CAST, COMMA, CONCAT, DEC_LITERAL, DIGIT, DIVIDE, DMO, DOT, ELSE, END, EOF, EQUALS, ESCAPE, FROM, FUNCTION, GT, GTE, IN, INDEX, IS, IS_NULL, LBRACKET, LETTER, LIKE, LONG_LITERAL, LPARENS, LT, LTE, MINUS, MULTIPLY, NOT, NOT_EQ, NOT_NULL, NULL, NULL_TREE_LOOKAHEAD, NUM_LITERAL, OR, PLUS, PROPERTY, RBRACKET, RPARENS, SELECT, STRING, SUBSCRIPT, SUBSELECT, SUBST, SYM_CHAR, SYMBOL, TERNARY, THEN, UN_MINUS, VALID_1ST_IDENT, VALID_SYM_CHAR, WHEN, WHERE, WS
Modifier | Constructor and Description |
---|---|
|
HQLParser(antlr.ParserSharedInputState state) |
|
HQLParser(antlr.TokenBuffer tokenBuf) |
protected |
HQLParser(antlr.TokenBuffer tokenBuf,
int k) |
|
HQLParser(antlr.TokenStream lexer) |
protected |
HQLParser(antlr.TokenStream lexer,
int k) |
Modifier and Type | Method and Description |
---|---|
void |
alias()
Matches an alias for a data model object, which is simply a SYMBOL,
optionally followed by a property.
|
void |
as_expr()
Matches an as phrase, which is an optional assignment of an alias to a DMO
at the end of a from phrase.
|
protected void |
buildTokenTypeASTClassMap() |
void |
compare_expr()
Implements the 4th lowest precedence level of expressions - the comparison
operators.
|
void |
concat_expr()
Implements the 5th lowest precedence level of expressions - concatenation.
|
void |
convertNullTest(antlr.CommonAST ast)
Implements special processing for
NULL equality and
inequality tests. |
void |
equality_expr()
Implements the 3rd lowest precedence level of expressions - the equality
operators.
|
void |
expr()
Implements the lowest (1st) precedence level of expressions:
OR (the logical OR operator). |
void |
expression()
Parses an HQL expression and creates an AST that is suitable for further
processing.
|
void |
from_expr()
Matches a from phrase embedded within a where clause as part of a
subselect statement.
|
void |
function()
Matches HQL function signatures: a SYMBOL followed by a parameter list in
parentheses.
|
void |
literal()
Matches all variations of expression literals (constants).
|
void |
log_and_expr()
Implements the 2nd lowest precedence level of expressions:
AND
(the logical AND operator). |
static java.lang.String |
lookupTokenName(int type)
Translates an integer value of a parser token type into the associated
human readable text representation.
|
static int |
lookupTokenType(java.lang.String tokenName)
Translates a text representation of a parser token type into the
actual integer value.
|
static void |
main(java.lang.String[] args)
Provides a command line interface for an end user to drive and/or test
the HQLParser class.
|
private static long[] |
mk_tokenSet_0() |
private static long[] |
mk_tokenSet_1() |
private static long[] |
mk_tokenSet_10() |
private static long[] |
mk_tokenSet_11() |
private static long[] |
mk_tokenSet_2() |
private static long[] |
mk_tokenSet_3() |
private static long[] |
mk_tokenSet_4() |
private static long[] |
mk_tokenSet_5() |
private static long[] |
mk_tokenSet_6() |
private static long[] |
mk_tokenSet_7() |
private static long[] |
mk_tokenSet_8() |
private static long[] |
mk_tokenSet_9() |
void |
parentheses()
Matches a subexpression enclosed in parenetheses.
|
void |
primary_expr()
Implements the 8th and highest precedence level of expressions, the
parenthesis precedence operators,
literal() , function() ,
ternary() , property() , subselect() , and parentheses() rules. |
void |
prod_expr()
Implements the 6th lowest precedence level of expressions - the
multiplicative operators.
|
void |
property()
Matches a qualified or unqualified property name, with an optional
subscript() . |
void |
reportError(antlr.RecognitionException exc)
Parser error-reporting function.
|
void |
select_expr()
Matches a select phrase embedded within a where clause as part of a
subselect statement.
|
void |
subscript()
Matches a
property() subscript, for array-type properties. |
void |
subselect()
Matches a subselect phrase embedded within a where clause.
|
void |
substitution()
Matches a query substitution parameter placeholder.
|
void |
sum_expr()
Implements the 5th lowest precedence level of expressions - the additive
operators.
|
void |
ternary()
Matches a ternary expression which uses the CASE WHEN construct in its
ternary form.
|
void |
un_expr()
Implements the 7th lowest precedence level of expressions - the unary
operators.
|
void |
unqprop()
Matches an unqualified property name, with an optional
subscript() . |
static void |
visit(antlr.CommonAST ast,
int level)
Simple walker to print out the contents of an AST for debugging.
|
void |
where_expr()
Matches a where phrase embedded within a where clause as part of a
subselect statement.
|
addMessageListener, addParserListener, addParserMatchListener, addParserTokenListener, addSemanticPredicateListener, addSyntacticPredicateListener, addTraceListener, consumeUntil, consumeUntil, defaultDebuggingSetup, getAST, getASTFactory, getFilename, getInputState, getTokenName, getTokenNames, getTokenTypeToASTClassMap, isDebugMode, mark, match, match, matchNot, panic, recover, removeMessageListener, removeParserListener, removeParserMatchListener, removeParserTokenListener, removeSemanticPredicateListener, removeSyntacticPredicateListener, removeTraceListener, reportError, reportWarning, rewind, setASTFactory, setASTNodeClass, setASTNodeType, setDebugMode, setFilename, setIgnoreInvalidDebugCalls, setInputState, setTokenBuffer, traceIndent
private static final java.lang.String AST_CLASS
private static java.util.Map tokenLookup
private int nextSubstitutionIndex
public static final java.lang.String[] _tokenNames
public static final antlr.collections.impl.BitSet _tokenSet_0
public static final antlr.collections.impl.BitSet _tokenSet_1
public static final antlr.collections.impl.BitSet _tokenSet_2
public static final antlr.collections.impl.BitSet _tokenSet_3
public static final antlr.collections.impl.BitSet _tokenSet_4
public static final antlr.collections.impl.BitSet _tokenSet_5
public static final antlr.collections.impl.BitSet _tokenSet_6
public static final antlr.collections.impl.BitSet _tokenSet_7
public static final antlr.collections.impl.BitSet _tokenSet_8
public static final antlr.collections.impl.BitSet _tokenSet_9
public static final antlr.collections.impl.BitSet _tokenSet_10
public static final antlr.collections.impl.BitSet _tokenSet_11
protected HQLParser(antlr.TokenBuffer tokenBuf, int k)
public HQLParser(antlr.TokenBuffer tokenBuf)
protected HQLParser(antlr.TokenStream lexer, int k)
public HQLParser(antlr.TokenStream lexer)
public HQLParser(antlr.ParserSharedInputState state)
public static void visit(antlr.CommonAST ast, int level)
ast
- The current node to print.level
- The number of levels down from the root (0 == root node),
which determines the indention level to print.public static int lookupTokenType(java.lang.String tokenName)
tokenName
- The text representation of the parser token type.public static java.lang.String lookupTokenName(int type)
type
- The integer token type.null
if
no match was found.public void convertNullTest(antlr.CommonAST ast)
NULL
equality and
inequality tests. If a NULL
token is detected on either
side of either of the operators, the result will be rooted at an
IS_NULL
(equality) or NOT_NULL
(inequality)
node. The operand that is NULL
will be dropped and the
other operand will be the only child.
This is separated from the main processing and really is a kind of post-processing of the entire tree.
public void reportError(antlr.RecognitionException exc)
reportError
in class antlr.Parser
public static void main(java.lang.String[] args)
Syntax:
java HQLParser <expression>
args
- List of command line arguments.public final void expression() throws antlr.RecognitionException, antlr.TokenStreamException
HQLAst
).
The AST is structured with operator nodes as the root nodes and each operator (unary or binary) has the matching number of child nodes (1 or 2 respectively). Each child node is an operand and the semantics of left and right operands (for binary operators) is maintained.
Non-operator nodes in the tree can be literals or HQL functions. The list
of literals can be seen in the literal()
rule.
All of the following operators are supported and the precedence order follows that of SQL, which we assume is the same precedence order used by HQL (TODO: confirm this with Hibernate sources).
lowest logical OR : or | conditional AND : and | equality : = | relational : <, >, ≤, ≥ | additive : +, - | multiplicative : *, / | unary operators : -, not | function call : function_name(...) highest parentheses : (...)
To handle the precedence issue, each precedence level (see the table
above) is handled in a separate rule. Starting at in this top level
entry point, the lowest precedence rule is referenced. This reference is
made with an optional OR
operator. If this operator is
present, it is the root of the resulting tree. This means it is the
last operator to be evaluated since its operands will be processed
first. This is the proper definition of lowest precedence. If this
operator is present, then a second operand is expected.
Each precedence level is similarly constructed, the left operand being required, all operators being optional and each operator choice an equal alternative. If any of the operators exists, then it is the root of the subtree and a right operand is expected. This holds true for all rules that process binary operators. Please note that this left-mandatory and optional right side (operator + right operand) is critical for allowing an expression to be as simple as a single variable, function call or literal. Alternatively, it can expand into a highly nested set of recursively parsed expressions that all evaluate to a single scalar result.
At the bottom of the hierarchy of rules are unary operators, these are implemented as an optional prefix operator and a mandatory right operand.
The final level is the primary expression. This is where the precedence operator parenthesis is handled. It also is where we allow a single literal, alias, or function reference.
The expr()
rule is called as the only rule reference.
antlr.RecognitionException
antlr.TokenStreamException
public final void expr() throws antlr.RecognitionException, antlr.TokenStreamException
OR
(the logical OR operator). Valid expressions can contain
sub-expressions and this entry point is properly recursive without causing
any ambiguity.
This rule is designed to allow recursion (the highest precedence levels
function()
and primary_expr()
reference this rule again,
allowing infinite nesting of sub-expressions, with proper parsing,
evaluation and tree building.
This method is called by the top level expression()
rule. This
method calls the log_and_expr()
rule.
Implements special processing for NULL
equality and inequality
tests. If a NULL
token is detected on either side of either
of the operators, the result will be rooted at an IS_NULL
(equality) or NOT_NULL
(inequality) node. The operand that
is NULL
will be dropped and the other operand will be the
only child. This must be implemented after the entire tree is created
and the method convertNullTest(antlr.CommonAST)
is used to implement this
processing.
(TODO: is this necessary?)
antlr.RecognitionException
antlr.TokenStreamException
public final void log_and_expr() throws antlr.RecognitionException, antlr.TokenStreamException
AND
(the logical AND operator).
This is only called by the expr()
rule. This method calls the
equality_expr()
rule.
antlr.RecognitionException
antlr.TokenStreamException
public final void equality_expr() throws antlr.RecognitionException, antlr.TokenStreamException
This is only called by the log_and_expr()
rule. This method
calls the compare_expr()
rule.
Provides the following operators:
= (equality) != (inequality)
antlr.RecognitionException
antlr.TokenStreamException
public final void compare_expr() throws antlr.RecognitionException, antlr.TokenStreamException
This is only called by the equality_expr()
rule. This method calls
the concat_expr()
rule.
Provides the following operators:
> (greater than) < (less than) ≥ (greater than or equal to) ≤ (less than or equal to) like
antlr.RecognitionException
antlr.TokenStreamException
public final void concat_expr() throws antlr.RecognitionException, antlr.TokenStreamException
This is only called by the compare_expr()
rule. This method calls
the sum_expr()
rule.
Provides the following operator:
|| (concatenation of two string operands)
antlr.RecognitionException
antlr.TokenStreamException
public final void sum_expr() throws antlr.RecognitionException, antlr.TokenStreamException
This is only called by the compare_expr()
rule. This method calls
the prod_expr()
rule.
Provides the following operators:
+ (binary plus) - (binary minus)
antlr.RecognitionException
antlr.TokenStreamException
public final void prod_expr() throws antlr.RecognitionException, antlr.TokenStreamException
This is only called by the sum_expr()
rule. This method calls
the un_expr()
rule.
Provides the following operators:
* (multiply) / (divide)
antlr.RecognitionException
antlr.TokenStreamException
public final void un_expr() throws antlr.RecognitionException, antlr.TokenStreamException
This is only called by the prod_expr()
rule. This method calls
the primary_expr()
rule.
Provides the following operators:
- unary minus not logical NOT
antlr.RecognitionException
antlr.TokenStreamException
public final void primary_expr() throws antlr.RecognitionException, antlr.TokenStreamException
literal()
, function()
,
ternary()
, property()
, subselect()
, and parentheses()
rules.
This rule is different in structure from the previous levels, as the
there is just a set of alternatives and one of them allows direct recursion
back to the expr
expression entry point.
This is only called by the un_expr()
rule.
See the rule expr()
for more details.
antlr.RecognitionException
antlr.TokenStreamException
public final void literal() throws antlr.RecognitionException, antlr.TokenStreamException
NUM_LITERAL LONG_LITERAL DEC_LITERAL STRING BOOL_TRUE BOOL_FALSE NULL SUBST
antlr.RecognitionException
antlr.TokenStreamException
public final void function() throws antlr.RecognitionException, antlr.TokenStreamException
primary_expr()
rule.antlr.RecognitionException
antlr.TokenStreamException
public final void ternary() throws antlr.RecognitionException, antlr.TokenStreamException
antlr.RecognitionException
antlr.TokenStreamException
public final void property() throws antlr.RecognitionException, antlr.TokenStreamException
subscript()
. This rule is called by the primary_expr()
rule, but
only after the function()
rule is invoked. This order is necessary
to avoid ambiguity with the latter rule, which looks for a leading SYMBOL
token.antlr.RecognitionException
antlr.TokenStreamException
public final void subselect() throws antlr.RecognitionException, antlr.TokenStreamException
primary_expr()
rule.antlr.RecognitionException
antlr.TokenStreamException
public final void parentheses() throws antlr.RecognitionException, antlr.TokenStreamException
primary_expr()
rule; recursively calls expr()
rule.antlr.RecognitionException
antlr.TokenStreamException
public final void alias() throws antlr.RecognitionException, antlr.TokenStreamException
primary_expr()
rule, but only after the function()
rule is invoked.
This order is necessary to avoid ambiguity with the latter rule, which
looks for a leading SYMBOL token.antlr.RecognitionException
antlr.TokenStreamException
public final void unqprop() throws antlr.RecognitionException, antlr.TokenStreamException
subscript()
.antlr.RecognitionException
antlr.TokenStreamException
public final void subscript() throws antlr.RecognitionException, antlr.TokenStreamException
property()
subscript, for array-type properties.antlr.RecognitionException
antlr.TokenStreamException
public final void substitution() throws antlr.RecognitionException, antlr.TokenStreamException
antlr.RecognitionException
antlr.TokenStreamException
public final void select_expr() throws antlr.RecognitionException, antlr.TokenStreamException
subselect()
rule.antlr.RecognitionException
antlr.TokenStreamException
public final void from_expr() throws antlr.RecognitionException, antlr.TokenStreamException
subselect()
rule.antlr.RecognitionException
antlr.TokenStreamException
public final void where_expr() throws antlr.RecognitionException, antlr.TokenStreamException
subselect()
rule. Recursively
calls expr()
rule.antlr.RecognitionException
antlr.TokenStreamException
public final void as_expr() throws antlr.RecognitionException, antlr.TokenStreamException
from_expr()
rule. This
portion of the phrase is optional if no other part of the subselect phrase
references an alias for the DMO.antlr.RecognitionException
antlr.TokenStreamException
protected void buildTokenTypeASTClassMap()
private static final long[] mk_tokenSet_0()
private static final long[] mk_tokenSet_1()
private static final long[] mk_tokenSet_2()
private static final long[] mk_tokenSet_3()
private static final long[] mk_tokenSet_4()
private static final long[] mk_tokenSet_5()
private static final long[] mk_tokenSet_6()
private static final long[] mk_tokenSet_7()
private static final long[] mk_tokenSet_8()
private static final long[] mk_tokenSet_9()
private static final long[] mk_tokenSet_10()
private static final long[] mk_tokenSet_11()