Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,19 @@
import io.trino.sql.jsonpath.tree.PathNode;
import io.trino.sql.parser.ParsingException;
import io.trino.sql.tree.NodeLocation;
import org.antlr.v4.runtime.BailErrorStrategy;
import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonToken;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.DefaultErrorStrategy;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.atn.PredictionMode;
import org.antlr.v4.runtime.misc.Pair;
import org.antlr.v4.runtime.misc.ParseCancellationException;
import org.antlr.v4.runtime.tree.TerminalNode;

import java.util.Arrays;
Expand Down Expand Up @@ -95,20 +98,20 @@ public PathNode parseJsonPath(String path)
lexer.addErrorListener(errorListener);

parser.removeErrorListeners();
parser.addErrorListener(errorListener);

ParserRuleContext tree;
try {
// first, try parsing with potentially faster SLL mode
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
parser.setErrorHandler(new BailErrorStrategy());
tree = parser.path();
}
catch (ParsingException ex) {
catch (ParseCancellationException e) {
// if we fail, parse with LL mode
tokenStream.seek(0); // rewind input stream
parser.reset();

parser.getInterpreter().setPredictionMode(PredictionMode.LL);
parser.setErrorHandler(new DefaultErrorStrategy());
parser.addErrorListener(errorListener);
tree = parser.path();
}

Expand Down
40 changes: 21 additions & 19 deletions core/trino-parser/src/main/java/io/trino/sql/parser/SqlParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.trino.sql.tree.RowPattern;
import io.trino.sql.tree.Statement;
import org.antlr.v4.runtime.ANTLRErrorListener;
import org.antlr.v4.runtime.BailErrorStrategy;
import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonToken;
Expand All @@ -39,6 +40,7 @@
import org.antlr.v4.runtime.atn.PredictionMode;
import org.antlr.v4.runtime.misc.Interval;
import org.antlr.v4.runtime.misc.Pair;
import org.antlr.v4.runtime.misc.ParseCancellationException;
import org.antlr.v4.runtime.tree.TerminalNode;

import java.util.Arrays;
Expand Down Expand Up @@ -144,42 +146,27 @@ private Node invokeParser(String name, String sql, Optional<NodeLocation> locati
SqlBaseParser parser = new SqlBaseParser(tokenStream);
initializer.accept(lexer, parser);

// Override the default error strategy to not attempt inserting or deleting a token.
// Otherwise, it messes up error reporting
parser.setErrorHandler(new DefaultErrorStrategy()
{
@Override
public Token recoverInline(Parser recognizer)
throws RecognitionException
{
if (nextTokensContext == null) {
throw new InputMismatchException(recognizer);
}
throw new InputMismatchException(recognizer, nextTokensState, nextTokensContext);
}
});

parser.addParseListener(new PostProcessor(Arrays.asList(parser.getRuleNames()), parser));

lexer.removeErrorListeners();
lexer.addErrorListener(LEXER_ERROR_LISTENER);

parser.removeErrorListeners();
parser.addErrorListener(PARSER_ERROR_HANDLER);

ParserRuleContext tree;
try {
try {
// first, try parsing with potentially faster SLL mode
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
parser.setErrorHandler(new BailErrorStrategy());
tree = parseFunction.apply(parser);
}
catch (ParsingException ex) {
catch (ParseCancellationException e) {
// if we fail, parse with LL mode
tokenStream.seek(0); // rewind input stream
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did this disappear?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Parser.reset() method already does this

parser.reset();

parser.getInterpreter().setPredictionMode(PredictionMode.LL);
parser.setErrorHandler(new NonRecoveringErrorStrategy());
parser.addErrorListener(PARSER_ERROR_HANDLER);
tree = parseFunction.apply(parser);
}
}
Expand All @@ -203,6 +190,21 @@ public Token recoverInline(Parser recognizer)
}
}

// Override the default error strategy to not attempt inserting or deleting a token.
// Otherwise, it messes up error reporting.
private static final class NonRecoveringErrorStrategy
extends DefaultErrorStrategy
{
@Override
public Token recoverInline(Parser recognizer)
{
if (nextTokensContext == null) {
throw new InputMismatchException(recognizer);
}
throw new InputMismatchException(recognizer, nextTokensState, nextTokensContext);
}
}

private static class PostProcessor
extends SqlBaseBaseListener
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@
import io.trino.sql.parser.ParsingException;
import io.trino.sql.tree.NodeLocation;
import org.antlr.v4.runtime.ANTLRErrorListener;
import org.antlr.v4.runtime.BailErrorStrategy;
import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.DefaultErrorStrategy;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;
import org.antlr.v4.runtime.atn.PredictionMode;
import org.antlr.v4.runtime.misc.ParseCancellationException;

import java.math.BigInteger;
import java.util.Map;
Expand Down Expand Up @@ -85,20 +88,20 @@ private static ParserRuleContext parseTypeCalculation(String calculation)
lexer.addErrorListener(ERROR_LISTENER);

parser.removeErrorListeners();
parser.addErrorListener(ERROR_LISTENER);

ParserRuleContext tree;
try {
// first, try parsing with potentially faster SLL mode
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
parser.setErrorHandler(new BailErrorStrategy());
tree = parser.typeCalculation();
}
catch (ParsingException ex) {
catch (ParseCancellationException e) {
// if we fail, parse with LL mode
tokenStream.seek(0); // rewind input stream
parser.reset();

parser.getInterpreter().setPredictionMode(PredictionMode.LL);
parser.setErrorHandler(new DefaultErrorStrategy());
parser.addErrorListener(ERROR_LISTENER);
tree = parser.typeCalculation();
}
return tree;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@

import com.google.common.collect.ImmutableMap;
import org.antlr.v4.runtime.ANTLRErrorListener;
import org.antlr.v4.runtime.BailErrorStrategy;
import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.DefaultErrorStrategy;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;
import org.antlr.v4.runtime.atn.PredictionMode;
import org.antlr.v4.runtime.misc.ParseCancellationException;

import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -69,20 +72,20 @@ public Object invokeParser(String input, Function<ConnectorExpressionPatternPars
lexer.addErrorListener(ERROR_LISTENER);

parser.removeErrorListeners();
parser.addErrorListener(ERROR_LISTENER);

ParserRuleContext tree;
try {
// first, try parsing with potentially faster SLL mode
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
parser.setErrorHandler(new BailErrorStrategy());
tree = parseFunction.apply(parser);
}
catch (IllegalArgumentException ex) {
catch (ParseCancellationException e) {
// if we fail, parse with LL mode
tokenStream.seek(0); // rewind input stream
parser.reset();

parser.getInterpreter().setPredictionMode(PredictionMode.LL);
parser.setErrorHandler(new DefaultErrorStrategy());
parser.addErrorListener(ERROR_LISTENER);
tree = parseFunction.apply(parser);
}
return new ExpressionPatternBuilder(typeClasses).visit(tree);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@

import com.google.common.annotations.VisibleForTesting;
import org.antlr.v4.runtime.ANTLRErrorListener;
import org.antlr.v4.runtime.BailErrorStrategy;
import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.DefaultErrorStrategy;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;
import org.antlr.v4.runtime.atn.PredictionMode;
import org.antlr.v4.runtime.misc.ParseCancellationException;

import java.util.function.Function;

Expand Down Expand Up @@ -68,20 +71,20 @@ private static Object invokeParser(String input, Function<SparkExpressionBasePar
lexer.addErrorListener(ERROR_LISTENER);

parser.removeErrorListeners();
parser.addErrorListener(ERROR_LISTENER);

ParserRuleContext tree;
try {
// first, try parsing with potentially faster SLL mode
parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
parser.setErrorHandler(new BailErrorStrategy());
tree = parseFunction.apply(parser);
}
catch (ParsingException ex) {
catch (ParseCancellationException e) {
// if we fail, parse with LL mode
tokenStream.seek(0); // rewind input stream
parser.reset();

parser.getInterpreter().setPredictionMode(PredictionMode.LL);
parser.setErrorHandler(new DefaultErrorStrategy());
parser.addErrorListener(ERROR_LISTENER);
tree = parseFunction.apply(parser);
}
return new SparkExpressionBuilder().visit(tree);
Expand Down
Loading