From c1dff2bdf01d4e19b3e134744236f3e6388bc375 Mon Sep 17 00:00:00 2001 From: "nathan.xu" Date: Wed, 23 Oct 2024 00:18:55 -0400 Subject: [PATCH] HHH-18754 improve HQLParser's error listener usage in StandardHqlTranslator --- .../hql/internal/StandardHqlTranslator.java | 36 +++++-------------- .../processor/validation/Validation.java | 5 +-- 2 files changed, 12 insertions(+), 29 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/query/hql/internal/StandardHqlTranslator.java b/hibernate-core/src/main/java/org/hibernate/query/hql/internal/StandardHqlTranslator.java index ab0d411b77fc..9a97960e869d 100644 --- a/hibernate-core/src/main/java/org/hibernate/query/hql/internal/StandardHqlTranslator.java +++ b/hibernate-core/src/main/java/org/hibernate/query/hql/internal/StandardHqlTranslator.java @@ -4,8 +4,6 @@ */ package org.hibernate.query.hql.internal; -import java.util.BitSet; - import org.antlr.v4.runtime.CommonToken; import org.antlr.v4.runtime.InputMismatchException; import org.antlr.v4.runtime.NoViableAltException; @@ -29,13 +27,11 @@ import org.antlr.v4.runtime.ANTLRErrorListener; import org.antlr.v4.runtime.BailErrorStrategy; +import org.antlr.v4.runtime.BaseErrorListener; import org.antlr.v4.runtime.DefaultErrorStrategy; -import org.antlr.v4.runtime.Parser; import org.antlr.v4.runtime.RecognitionException; import org.antlr.v4.runtime.Recognizer; -import org.antlr.v4.runtime.atn.ATNConfigSet; import org.antlr.v4.runtime.atn.PredictionMode; -import org.antlr.v4.runtime.dfa.DFA; import org.antlr.v4.runtime.misc.ParseCancellationException; import static java.util.stream.Collectors.toList; @@ -50,7 +46,6 @@ public class StandardHqlTranslator implements HqlTranslator { private final SqmCreationContext sqmCreationContext; private final SqmCreationOptions sqmCreationOptions; - public StandardHqlTranslator( SqmCreationContext sqmCreationContext, SqmCreationOptions sqmCreationOptions) { @@ -101,30 +96,9 @@ private HqlParser.StatementContext parseHql(String hql) { // Build the parse tree final HqlParser hqlParser = HqlParseTreeBuilder.INSTANCE.buildHqlParser( hql, hqlLexer ); - ANTLRErrorListener errorListener = new ANTLRErrorListener() { - @Override - public void syntaxError(Recognizer recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { - throw new SyntaxException( prettifyAntlrError( offendingSymbol, line, charPositionInLine, msg, e, hql, true ), hql ); - } - - @Override - public void reportAmbiguity(Parser recognizer, DFA dfa, int startIndex, int stopIndex, boolean exact, BitSet ambigAlts, ATNConfigSet configs) { - } - - @Override - public void reportAttemptingFullContext(Parser recognizer, DFA dfa, int startIndex, int stopIndex, BitSet conflictingAlts, ATNConfigSet configs) { - } - - @Override - public void reportContextSensitivity(Parser recognizer, DFA dfa, int startIndex, int stopIndex, int prediction, ATNConfigSet configs) { - } - }; - // try to use SLL(k)-based parsing first - its faster - hqlLexer.addErrorListener( errorListener ); hqlParser.getInterpreter().setPredictionMode( PredictionMode.SLL ); hqlParser.removeErrorListeners(); - hqlParser.addErrorListener( errorListener ); hqlParser.setErrorHandler( new BailErrorStrategy() ); try { @@ -139,6 +113,14 @@ public void reportContextSensitivity(Parser recognizer, DFA dfa, int startIndex, hqlParser.getInterpreter().setPredictionMode( PredictionMode.LL ); hqlParser.setErrorHandler( new DefaultErrorStrategy() ); + final ANTLRErrorListener errorListener = new BaseErrorListener() { + @Override + public void syntaxError(Recognizer recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) { + throw new SyntaxException( prettifyAntlrError( offendingSymbol, line, charPositionInLine, msg, e, hql, true ), hql ); + } + }; + hqlParser.addErrorListener( errorListener ); + return hqlParser.statement(); } catch ( ParsingException ex ) { diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/Validation.java b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/Validation.java index ee67b2ee8949..bbfc7b9f1f5f 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/Validation.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/Validation.java @@ -120,10 +120,10 @@ private static SemanticQueryBuilder createSemanticQueryBuilder( private static HqlParser.StatementContext parseAndCheckSyntax(String hql, Handler handler) { final HqlLexer hqlLexer = HqlParseTreeBuilder.INSTANCE.buildHqlLexer( hql ); final HqlParser hqlParser = HqlParseTreeBuilder.INSTANCE.buildHqlParser( hql, hqlLexer ); - hqlLexer.addErrorListener( handler ); + hqlParser.getInterpreter().setPredictionMode( PredictionMode.SLL ); hqlParser.removeErrorListeners(); - hqlParser.addErrorListener( handler ); + hqlParser.setErrorHandler( new BailErrorStrategy() ); try { @@ -137,6 +137,7 @@ private static HqlParser.StatementContext parseAndCheckSyntax(String hql, Handle // fall back to LL(k)-based parsing hqlParser.getInterpreter().setPredictionMode( PredictionMode.LL ); hqlParser.setErrorHandler( new DefaultErrorStrategy() ); + hqlParser.addErrorListener( handler ); return hqlParser.statement(); }