groovy git commit: Provide readable error messages for unexpected input

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

groovy git commit: Provide readable error messages for unexpected input

sunlan
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 <[hidden email]>
Authored: Sun Jul 16 21:06:22 2017 +0800
Committer: sunlan <[hidden email]>
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>";
         }

Loading...