groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject groovy git commit: Minor refactoring
Date Mon, 07 Aug 2017 14:24:29 GMT
Repository: groovy
Updated Branches:
  refs/heads/master 76ea06f6e -> 53d5af898


Minor refactoring


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

Branch: refs/heads/master
Commit: 53d5af8982e6a3e7cb0b495b94e16a62d6909881
Parents: 76ea06f
Author: sunlan <sunlan@apache.org>
Authored: Mon Aug 7 22:24:13 2017 +0800
Committer: sunlan <sunlan@apache.org>
Committed: Mon Aug 7 22:24:13 2017 +0800

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


http://git-wip-us.apache.org/repos/asf/groovy/blob/53d5af89/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());
+    }
 }


Mime
View raw message