You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2017/07/16 13:06:37 UTC
groovy git commit: Provide readable error messages for unexpected
input
Repository: groovy
Updated Branches:
refs/heads/master df389dd6c -> 24ce80ada
Provide readable error messages for unexpected input
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/24ce80ad
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/24ce80ad
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/24ce80ad
Branch: refs/heads/master
Commit: 24ce80ada1fa5e6815a81f5c8bcbf9cc509183c5
Parents: df389dd
Author: sunlan <su...@apache.org>
Authored: Sun Jul 16 21:06:22 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Sun Jul 16 21:06:22 2017 +0800
----------------------------------------------------------------------
.../org/apache/groovy/parser/antlr4/AstBuilder.java | 16 ++++++++++------
.../antlr4/internal/DescriptiveErrorStrategy.java | 15 +++++++++++++--
2 files changed, 23 insertions(+), 8 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/24ce80ad/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
index 72dfa36..6dd2efa 100644
--- a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
+++ b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
@@ -21,6 +21,7 @@ package org.apache.groovy.parser.antlr4;
import groovy.lang.IntRange;
import org.antlr.v4.runtime.ANTLRErrorListener;
import org.antlr.v4.runtime.ANTLRInputStream;
+import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;
@@ -156,13 +157,16 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
this.moduleNode = new ModuleNode(sourceUnit);
this.classLoader = classLoader; // unused for the time being
- this.lexer = new GroovyLangLexer(
- new ANTLRInputStream(
- this.readSourceCode(sourceUnit)));
- this.parser = new GroovyLangParser(
- new CommonTokenStream(this.lexer));
+ CharStream charStream =
+ new ANTLRInputStream(
+ this.readSourceCode(sourceUnit));
- this.parser.setErrorHandler(new DescriptiveErrorStrategy());
+ this.lexer = new GroovyLangLexer(charStream);
+ this.parser =
+ new GroovyLangParser(
+ new CommonTokenStream(this.lexer));
+
+ this.parser.setErrorHandler(new DescriptiveErrorStrategy(charStream));
this.tryWithResourcesASTTransformation = new TryWithResourcesASTTransformation(this);
this.groovydocManager = new GroovydocManager(this);
http://git-wip-us.apache.org/repos/asf/groovy/blob/24ce80ad/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/internal/DescriptiveErrorStrategy.java
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/internal/DescriptiveErrorStrategy.java b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/internal/DescriptiveErrorStrategy.java
index 12081ed..1a9c8fa 100644
--- a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/internal/DescriptiveErrorStrategy.java
+++ b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/internal/DescriptiveErrorStrategy.java
@@ -19,6 +19,7 @@
package org.apache.groovy.parser.antlr4.internal;
import org.antlr.v4.runtime.BailErrorStrategy;
+import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.FailedPredicateException;
import org.antlr.v4.runtime.InputMismatchException;
import org.antlr.v4.runtime.NoViableAltException;
@@ -28,6 +29,7 @@ import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.TokenStream;
import org.antlr.v4.runtime.atn.PredictionMode;
+import org.antlr.v4.runtime.misc.Interval;
import org.antlr.v4.runtime.misc.NotNull;
import org.antlr.v4.runtime.misc.ParseCancellationException;
@@ -38,6 +40,12 @@ import org.antlr.v4.runtime.misc.ParseCancellationException;
* Created on 2016/10/19
*/
public class DescriptiveErrorStrategy extends BailErrorStrategy {
+ private CharStream charStream;
+
+ public DescriptiveErrorStrategy(CharStream charStream) {
+ this.charStream = charStream;
+ }
+
@Override
public void recover(Parser recognizer, RecognitionException e) {
for (ParserRuleContext context = recognizer.getContext(); context != null; context = context.getParent()) {
@@ -69,8 +77,11 @@ public class DescriptiveErrorStrategy extends BailErrorStrategy {
TokenStream tokens = recognizer.getInputStream();
String input;
if (tokens != null) {
- if (e.getStartToken().getType() == Token.EOF) input = "<EOF>";
- else input = tokens.getText(e.getStartToken(), e.getOffendingToken());
+ if (e.getStartToken().getType() == Token.EOF) {
+ input = "<EOF>";
+ } else {
+ input = charStream.getText(Interval.of(e.getStartToken().getStartIndex(), e.getOffendingToken().getStopIndex()));
+ }
} else {
input = "<unknown input>";
}