Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_6_X 65a21bac4 -> 19b4f4383


Refine antlr4 parser Plugin

(cherry picked from commit 76ea06f)


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

Branch: refs/heads/GROOVY_2_6_X
Commit: 19b4f4383cc75029df7594658ce8c3f8f0da0a41
Parents: 65a21ba
Author: sunlan <sun...@apache.org>
Authored: Mon Aug 7 19:47:37 2017 +0800
Committer: sunlan <sun...@apache.org>
Committed: Mon Aug 7 19:48:30 2017 +0800

----------------------------------------------------------------------
 .../parser/antlr4/Antlr4ParserPlugin.java       | 22 ++++++++++++++++----
 .../apache/groovy/parser/antlr4/AstBuilder.java |  9 +++++++-
 2 files changed, 26 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/19b4f438/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 0b4728f..790c0d3 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
@@ -39,11 +39,19 @@ import java.io.IOException;
  */
 public class Antlr4ParserPlugin implements ParserPlugin {
     private ReaderSource readerSource;
+    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);
         }
@@ -53,6 +61,16 @@ public class Antlr4ParserPlugin implements ParserPlugin {
 
     @Override
     public ModuleNode buildAST(SourceUnit sourceUnit, ClassLoader classLoader, 
Reduction cst) throws ParserException {
+        handleSourceUnit(sourceUnit);
+
+        if (null == this.builder) {
+            this.builder = new AstBuilder(sourceUnit, classLoader);
+        }
+
+        return builder.buildAST();
+    }
+
+    private void handleSourceUnit(SourceUnit sourceUnit) {
         try {
             ReaderSource readerSource = sourceUnit.getSource();
             if (null == readerSource || null == readerSource.getReader()) {
@@ -61,9 +79,5 @@ public class Antlr4ParserPlugin implements ParserPlugin {
         } catch (IOException e) {
             sourceUnit.setSource(this.readerSource);
         }
-
-        AstBuilder builder = new AstBuilder(sourceUnit, classLoader);
-
-        return builder.buildAST();
     }
 }

http://git-wip-us.apache.org/repos/asf/groovy/blob/19b4f438/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 c7b8dd1..ad268c7 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
@@ -169,7 +169,11 @@ public class AstBuilder extends 
GroovyParserBaseVisitor<Object> implements Groov
         this.groovydocManager = new GroovydocManager(this);
     }
 
-    private GroovyParserRuleContext buildCST() throws 
CompilationFailedException {
+    public GroovyParserRuleContext buildCST() throws 
CompilationFailedException {
+        if (null != this.cst) {
+            return this.cst;
+        }
+
         GroovyParserRuleContext result;
 
         try {
@@ -191,6 +195,8 @@ public class AstBuilder extends 
GroovyParserBaseVisitor<Object> implements Groov
             throw convertException(t);
         }
 
+        this.cst = result;
+
         return result;
     }
 
@@ -4613,6 +4619,7 @@ public class AstBuilder extends 
GroovyParserBaseVisitor<Object> implements Groov
         }
     }
 
+    private GroovyParserRuleContext cst;
     private final ModuleNode moduleNode;
     private final SourceUnit sourceUnit;
     private final ClassLoader classLoader; // Our ClassLoader, which provides 
information on external types

Reply via email to