Search in sources :

Example 1 with HqlParser

use of org.hibernate.grammars.hql.HqlParser in project hibernate-orm by hibernate.

the class StandardHqlTranslator method parseHql.

private HqlParser.StatementContext parseHql(String hql) {
    // Build the lexer
    final HqlLexer hqlLexer = HqlParseTreeBuilder.INSTANCE.buildHqlLexer(hql);
    // Build the parse tree
    final HqlParser hqlParser = HqlParseTreeBuilder.INSTANCE.buildHqlParser(hql, hqlLexer);
    // try to use SLL(k)-based parsing first - its faster
    hqlLexer.addErrorListener(ERR_LISTENER);
    hqlParser.getInterpreter().setPredictionMode(PredictionMode.SLL);
    hqlParser.removeErrorListeners();
    hqlParser.addErrorListener(ERR_LISTENER);
    hqlParser.setErrorHandler(new BailErrorStrategy());
    try {
        return hqlParser.statement();
    } catch (ParseCancellationException e) {
        // reset the input token stream and parser state
        hqlLexer.reset();
        hqlParser.reset();
        // fall back to LL(k)-based parsing
        hqlParser.getInterpreter().setPredictionMode(PredictionMode.LL);
        hqlParser.setErrorHandler(new DefaultErrorStrategy());
        return hqlParser.statement();
    } catch (ParsingException ex) {
        throw new SemanticException("A query exception occurred", hql, ex);
    }
}
Also used : ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) BailErrorStrategy(org.antlr.v4.runtime.BailErrorStrategy) DefaultErrorStrategy(org.antlr.v4.runtime.DefaultErrorStrategy) HqlLexer(org.hibernate.grammars.hql.HqlLexer) HqlParser(org.hibernate.grammars.hql.HqlParser) ParsingException(org.hibernate.query.sqm.ParsingException) SemanticException(org.hibernate.query.SemanticException)

Aggregations

BailErrorStrategy (org.antlr.v4.runtime.BailErrorStrategy)1 DefaultErrorStrategy (org.antlr.v4.runtime.DefaultErrorStrategy)1 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)1 HqlLexer (org.hibernate.grammars.hql.HqlLexer)1 HqlParser (org.hibernate.grammars.hql.HqlParser)1 SemanticException (org.hibernate.query.SemanticException)1 ParsingException (org.hibernate.query.sqm.ParsingException)1