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/08/07 11:47:43 UTC
groovy git commit: Refine antlr4 parser Plugin
Repository: groovy
Updated Branches:
refs/heads/master b67f1e478 -> 76ea06f6e
Refine antlr4 parser Plugin
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/76ea06f6
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/76ea06f6
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/76ea06f6
Branch: refs/heads/master
Commit: 76ea06f6e44d61dd959a7d4f2ea73963e426c467
Parents: b67f1e4
Author: sunlan <su...@apache.org>
Authored: Mon Aug 7 19:47:37 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Mon Aug 7 19:47:37 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/76ea06f6/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/76ea06f6/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 8421f99..98a3b10 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
@@ -171,7 +171,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 {
@@ -193,6 +197,8 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
throw convertException(t);
}
+ this.cst = result;
+
return result;
}
@@ -4525,6 +4531,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