Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_6_X 19b4f4383 -> 284b6dfa6


Minor refactoring

(cherry picked from commit 53d5af8)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/284b6dfa
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/284b6dfa
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/284b6dfa

Branch: refs/heads/GROOVY_2_6_X
Commit: 284b6dfa605274c3b15ddb72444c787e5003a9e7
Parents: 19b4f43
Author: sunlan <[email protected]>
Authored: Mon Aug 7 22:24:13 2017 +0800
Committer: sunlan <[email protected]>
Committed: Mon Aug 7 22:28:57 2017 +0800

----------------------------------------------------------------------
 .../parser/antlr4/Antlr4ParserPlugin.java       | 42 ++++++++++++--------
 1 file changed, 26 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/284b6dfa/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/Antlr4ParserPlugin.java
----------------------------------------------------------------------
diff --git 
a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/Antlr4ParserPlugin.java
 
b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/Antlr4ParserPlugin.java
index 790c0d3..457c64f 100644
--- 
a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/Antlr4ParserPlugin.java
+++ 
b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/Antlr4ParserPlugin.java
@@ -18,7 +18,6 @@
  */
 package org.apache.groovy.parser.antlr4;
 
-import org.codehaus.groovy.GroovyBugError;
 import org.codehaus.groovy.ast.ModuleNode;
 import org.codehaus.groovy.control.CompilationFailedException;
 import org.codehaus.groovy.control.ParserPlugin;
@@ -30,6 +29,7 @@ import org.codehaus.groovy.syntax.ParserException;
 import org.codehaus.groovy.syntax.Reduction;
 
 import java.io.IOException;
+import java.io.Reader;
 
 /**
  * A parser plugin for the new parser
@@ -42,26 +42,17 @@ public class Antlr4ParserPlugin implements ParserPlugin {
     private AstBuilder builder;
 
     @Override
-    public Reduction parseCST(SourceUnit sourceUnit, java.io.Reader reader) 
throws CompilationFailedException {
-        try {
-
-            this.readerSource = new 
StringReaderSource(IOGroovyMethods.getText(reader), 
sourceUnit.getConfiguration());
-
-            handleSourceUnit(sourceUnit);
-
-            this.builder = new AstBuilder(sourceUnit, null);
-
-            builder.buildCST();
-        } catch (IOException e) {
-            throw new GroovyBugError("Failed to create StringReaderSource 
instance", e);
-        }
+    public Reduction parseCST(SourceUnit sourceUnit, Reader reader) throws 
CompilationFailedException {
+        handleSourceUnit(sourceUnit, reader);
+        this.builder = new AstBuilder(sourceUnit, null);
+        builder.buildCST();
 
         return null;
     }
 
     @Override
     public ModuleNode buildAST(SourceUnit sourceUnit, ClassLoader classLoader, 
Reduction cst) throws ParserException {
-        handleSourceUnit(sourceUnit);
+        handleSourceUnit(sourceUnit, null);
 
         if (null == this.builder) {
             this.builder = new AstBuilder(sourceUnit, classLoader);
@@ -70,9 +61,11 @@ public class Antlr4ParserPlugin implements ParserPlugin {
         return builder.buildAST();
     }
 
-    private void handleSourceUnit(SourceUnit sourceUnit) {
+    private void handleSourceUnit(SourceUnit sourceUnit, Reader reader) {
         try {
             ReaderSource readerSource = sourceUnit.getSource();
+            initReaderSource(sourceUnit, reader, readerSource);
+
             if (null == readerSource || null == readerSource.getReader()) {
                 sourceUnit.setSource(this.readerSource);
             }
@@ -80,4 +73,21 @@ public class Antlr4ParserPlugin implements ParserPlugin {
             sourceUnit.setSource(this.readerSource);
         }
     }
+
+    private void initReaderSource(SourceUnit sourceUnit, Reader reader, 
ReaderSource readerSource) throws IOException {
+        if (null != this.readerSource) {
+            return;
+        }
+
+        if (null != readerSource && null != readerSource.getReader()) {
+            this.readerSource = readerSource;
+            return;
+        }
+
+        if (null == reader) {
+            return;
+        }
+
+        this.readerSource = new 
StringReaderSource(IOGroovyMethods.getText(reader), 
sourceUnit.getConfiguration());
+    }
 }

Reply via email to