HHH-18754 improve HQLParser's error listener usage in StandardHqlTranslator

This commit is contained in:
nathan.xu 2024-10-23 00:18:55 -04:00 committed by Steve Ebersole
parent 64c26fa5ab
commit 2eeb6153ff
2 changed files with 12 additions and 29 deletions

View File

@ -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.hibernate.query.sqm.tree.SqmStatement;
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 @@ public class StandardHqlTranslator implements HqlTranslator {
// 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 class StandardHqlTranslator implements HqlTranslator {
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 ) {

View File

@ -120,10 +120,10 @@ public class Validation {
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 @@ public class Validation {
// fall back to LL(k)-based parsing
hqlParser.getInterpreter().setPredictionMode( PredictionMode.LL );
hqlParser.setErrorHandler( new DefaultErrorStrategy() );
hqlParser.addErrorListener( handler );
return hqlParser.statement();
}