Skip to content

Commit 2eeb615

Browse files
nathan.xusebersole
authored andcommitted
HHH-18754 improve HQLParser's error listener usage in StandardHqlTranslator
1 parent 64c26fa commit 2eeb615

File tree

2 files changed

+12
-29
lines changed

2 files changed

+12
-29
lines changed

hibernate-core/src/main/java/org/hibernate/query/hql/internal/StandardHqlTranslator.java

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
*/
55
package org.hibernate.query.hql.internal;
66

7-
import java.util.BitSet;
8-
97
import org.antlr.v4.runtime.CommonToken;
108
import org.antlr.v4.runtime.InputMismatchException;
119
import org.antlr.v4.runtime.NoViableAltException;
@@ -29,13 +27,11 @@
2927

3028
import org.antlr.v4.runtime.ANTLRErrorListener;
3129
import org.antlr.v4.runtime.BailErrorStrategy;
30+
import org.antlr.v4.runtime.BaseErrorListener;
3231
import org.antlr.v4.runtime.DefaultErrorStrategy;
33-
import org.antlr.v4.runtime.Parser;
3432
import org.antlr.v4.runtime.RecognitionException;
3533
import org.antlr.v4.runtime.Recognizer;
36-
import org.antlr.v4.runtime.atn.ATNConfigSet;
3734
import org.antlr.v4.runtime.atn.PredictionMode;
38-
import org.antlr.v4.runtime.dfa.DFA;
3935
import org.antlr.v4.runtime.misc.ParseCancellationException;
4036

4137
import static java.util.stream.Collectors.toList;
@@ -50,7 +46,6 @@ public class StandardHqlTranslator implements HqlTranslator {
5046
private final SqmCreationContext sqmCreationContext;
5147
private final SqmCreationOptions sqmCreationOptions;
5248

53-
5449
public StandardHqlTranslator(
5550
SqmCreationContext sqmCreationContext,
5651
SqmCreationOptions sqmCreationOptions) {
@@ -101,30 +96,9 @@ private HqlParser.StatementContext parseHql(String hql) {
10196
// Build the parse tree
10297
final HqlParser hqlParser = HqlParseTreeBuilder.INSTANCE.buildHqlParser( hql, hqlLexer );
10398

104-
ANTLRErrorListener errorListener = new ANTLRErrorListener() {
105-
@Override
106-
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
107-
throw new SyntaxException( prettifyAntlrError( offendingSymbol, line, charPositionInLine, msg, e, hql, true ), hql );
108-
}
109-
110-
@Override
111-
public void reportAmbiguity(Parser recognizer, DFA dfa, int startIndex, int stopIndex, boolean exact, BitSet ambigAlts, ATNConfigSet configs) {
112-
}
113-
114-
@Override
115-
public void reportAttemptingFullContext(Parser recognizer, DFA dfa, int startIndex, int stopIndex, BitSet conflictingAlts, ATNConfigSet configs) {
116-
}
117-
118-
@Override
119-
public void reportContextSensitivity(Parser recognizer, DFA dfa, int startIndex, int stopIndex, int prediction, ATNConfigSet configs) {
120-
}
121-
};
122-
12399
// try to use SLL(k)-based parsing first - its faster
124-
hqlLexer.addErrorListener( errorListener );
125100
hqlParser.getInterpreter().setPredictionMode( PredictionMode.SLL );
126101
hqlParser.removeErrorListeners();
127-
hqlParser.addErrorListener( errorListener );
128102
hqlParser.setErrorHandler( new BailErrorStrategy() );
129103

130104
try {
@@ -139,6 +113,14 @@ public void reportContextSensitivity(Parser recognizer, DFA dfa, int startIndex,
139113
hqlParser.getInterpreter().setPredictionMode( PredictionMode.LL );
140114
hqlParser.setErrorHandler( new DefaultErrorStrategy() );
141115

116+
final ANTLRErrorListener errorListener = new BaseErrorListener() {
117+
@Override
118+
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
119+
throw new SyntaxException( prettifyAntlrError( offendingSymbol, line, charPositionInLine, msg, e, hql, true ), hql );
120+
}
121+
};
122+
hqlParser.addErrorListener( errorListener );
123+
142124
return hqlParser.statement();
143125
}
144126
catch ( ParsingException ex ) {

tooling/metamodel-generator/src/main/java/org/hibernate/processor/validation/Validation.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,10 @@ private static SemanticQueryBuilder<?> createSemanticQueryBuilder(
120120
private static HqlParser.StatementContext parseAndCheckSyntax(String hql, Handler handler) {
121121
final HqlLexer hqlLexer = HqlParseTreeBuilder.INSTANCE.buildHqlLexer( hql );
122122
final HqlParser hqlParser = HqlParseTreeBuilder.INSTANCE.buildHqlParser( hql, hqlLexer );
123-
hqlLexer.addErrorListener( handler );
123+
124124
hqlParser.getInterpreter().setPredictionMode( PredictionMode.SLL );
125125
hqlParser.removeErrorListeners();
126-
hqlParser.addErrorListener( handler );
126+
127127
hqlParser.setErrorHandler( new BailErrorStrategy() );
128128

129129
try {
@@ -137,6 +137,7 @@ private static HqlParser.StatementContext parseAndCheckSyntax(String hql, Handle
137137
// fall back to LL(k)-based parsing
138138
hqlParser.getInterpreter().setPredictionMode( PredictionMode.LL );
139139
hqlParser.setErrorHandler( new DefaultErrorStrategy() );
140+
hqlParser.addErrorListener( handler );
140141

141142
return hqlParser.statement();
142143
}

0 commit comments

Comments
 (0)