groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject groovy git commit: Refactor parsing GString
Date Sat, 04 Nov 2017 09:08:05 GMT
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_6_X 05074bf1e -> 9b10d057a


Refactor parsing GString


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

Branch: refs/heads/GROOVY_2_6_X
Commit: 9b10d057a716cd61a0e49db1db38b5d0bea6c04c
Parents: 05074bf
Author: sunlan <sunlan@apache.org>
Authored: Sat Nov 4 17:07:57 2017 +0800
Committer: sunlan <sunlan@apache.org>
Committed: Sat Nov 4 17:07:57 2017 +0800

----------------------------------------------------------------------
 .../apache/groovy/parser/antlr4/AstBuilder.java | 128 ++++++++++---------
 1 file changed, 65 insertions(+), 63 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/9b10d057/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 dd68126..40bbdb0 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
@@ -18,7 +18,6 @@
  */
 package org.apache.groovy.parser.antlr4;
 
-import groovy.lang.IntRange;
 import groovy.lang.Tuple2;
 import org.antlr.v4.runtime.ANTLRErrorListener;
 import org.antlr.v4.runtime.CharStream;
@@ -2437,8 +2436,15 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object>
implements Groov
 
     @Override
     public ConstantExpression visitStringLiteral(StringLiteralContext ctx) {
-        String text = ctx.StringLiteral().getText();
+        String text = parseStringLiteral(ctx.StringLiteral().getText());
 
+        ConstantExpression constantExpression = new ConstantExpression(text, true);
+        constantExpression.putNodeMetaData(IS_STRING, true);
+
+        return configureAST(constantExpression, ctx);
+    }
+
+    private String parseStringLiteral(String text) {
         int slashyType = getSlashyType(text);
         boolean startsWithSlash = false;
 
@@ -2459,12 +2465,7 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object>
implements Groov
         }
 
         //handle escapes.
-        text = StringUtils.replaceEscapes(text, slashyType);
-
-        ConstantExpression constantExpression = new ConstantExpression(text, true);
-        constantExpression.putNodeMetaData(IS_STRING, true);
-
-        return configureAST(constantExpression, ctx);
+        return StringUtils.replaceEscapes(text, slashyType);
     }
 
     private int getSlashyType(String text) {
@@ -3285,62 +3286,18 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object>
implements Groov
     // gstring {       --------------------------------------------------------------------
     @Override
     public GStringExpression visitGstring(GstringContext ctx) {
-        List<ConstantExpression> strings = new LinkedList<>();
-
-        String begin = ctx.GStringBegin().getText();
-        final int slashyType = getSlashyType(begin);
-
-        {
-            String it = begin;
-            if (it.startsWith(TDQ_STR)) {
-                it = StringUtils.removeCR(it);
-                it = it.substring(2); // translate leading """ to "
-            } else if (it.startsWith(DOLLAR_SLASH_STR)) {
-                it = StringUtils.removeCR(it);
-                it = DQ_STR + it.substring(2); // translate leading $/ to "
-            } else if (it.startsWith(SLASH_STR)) {
-                it = StringUtils.removeCR(it);
-            }
-
-            it = StringUtils.replaceEscapes(it, slashyType);
-            it = (it.length() == 2)
-                    ? ""
-                    : StringGroovyMethods.getAt(it, new IntRange(true, 1, -2));
-
-            strings.add(configureAST(new ConstantExpression(it), ctx.GStringBegin()));
-        }
+        final List<ConstantExpression> stringLiteralList = new LinkedList<>();
+        final String begin = ctx.GStringBegin().getText();
+        final String beginQuotation = beginQuotation(begin);
+        stringLiteralList.add(configureAST(new ConstantExpression(parseGStringBegin(ctx,
beginQuotation)), ctx.GStringBegin()));
 
         List<ConstantExpression> partStrings = new LinkedList<>();
         for (TerminalNode e : ctx.GStringPart()) {
-            String it = e.getText();
-
-            it = StringUtils.removeCR(it);
-            it = StringUtils.replaceEscapes(it, slashyType);
-            it = it.length() == 1 ? "" : StringGroovyMethods.getAt(it, new IntRange(true,
0, -2));
-
-            partStrings.add(configureAST(new ConstantExpression(it), e));
-        }
-        strings.addAll(partStrings);
-
-        {
-            String it = ctx.GStringEnd().getText();
-            if (it.endsWith(TDQ_STR)) {
-                it = StringUtils.removeCR(it);
-                it = StringGroovyMethods.getAt(it, new IntRange(true, 0, -3)); // translate
tailing """ to "
-            } else if (it.endsWith(SLASH_DOLLAR_STR)) {
-                it = StringUtils.removeCR(it);
-                it = StringGroovyMethods.getAt(it, new IntRange(false, 0, -2)) + DQ_STR;
// translate tailing /$ to "
-            } else if (it.endsWith(SLASH_STR)) {
-                it = StringUtils.removeCR(it);
-            }
-
-            it = StringUtils.replaceEscapes(it, slashyType);
-            it = (it.length() == 1)
-                    ? ""
-                    : StringGroovyMethods.getAt(it, new IntRange(true, 0, -2));
-
-            strings.add(configureAST(new ConstantExpression(it), ctx.GStringEnd()));
+            partStrings.add(configureAST(new ConstantExpression(parseGStringPart(e, beginQuotation)),
e));
         }
+        stringLiteralList.addAll(partStrings);
+
+        stringLiteralList.add(configureAST(new ConstantExpression(parseGStringEnd(ctx, beginQuotation)),
ctx.GStringEnd()));
 
         List<Expression> values = new LinkedList<>();
         for (GstringValueContext e : ctx.gstringValue()) {
@@ -3368,8 +3325,8 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object>
implements Groov
         }
 
         StringBuilder verbatimText = new StringBuilder(ctx.getText().length());
-        for (int i = 0, n = strings.size(), s = values.size(); i < n; i++) {
-            verbatimText.append(strings.get(i).getValue());
+        for (int i = 0, n = stringLiteralList.size(), s = values.size(); i < n; i++) {
+            verbatimText.append(stringLiteralList.get(i).getValue());
 
             if (i == s) {
                 continue;
@@ -3384,9 +3341,45 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object>
implements Groov
             verbatimText.append(value.getText());
         }
 
-        return configureAST(new GStringExpression(verbatimText.toString(), strings, values),
ctx);
+        return configureAST(new GStringExpression(verbatimText.toString(), stringLiteralList,
values), ctx);
+    }
+
+    private String parseGStringEnd(GstringContext ctx, String beginQuotation) {
+        String text = ctx.GStringEnd().getText();
+        text = beginQuotation + text;
+
+        return this.parseStringLiteral(text);
+    }
+
+    private String parseGStringPart(TerminalNode e, String beginQuotation) {
+        String text = e.getText();
+        text = text.substring(0, text.length() - 1);  // remove the tailing $
+        text = beginQuotation + text + QUOTATION_MAP.get(beginQuotation);
+
+        return this.parseStringLiteral(text);
+    }
+
+    private String parseGStringBegin(GstringContext ctx, String beginQuotation) {
+        String text = ctx.GStringBegin().getText();
+        text = text.substring(0, text.length() - 1);  // remove the tailing $
+        text = text + QUOTATION_MAP.get(beginQuotation);
+
+        return this.parseStringLiteral(text);
     }
 
+    private String beginQuotation(String text) {
+        if (text.startsWith(TDQ_STR)) {
+            return TDQ_STR;
+        } else if (text.startsWith(DOLLAR_SLASH_STR)) {
+            return DOLLAR_SLASH_STR;
+        } else if (text.startsWith(SLASH_STR)) {
+            return SLASH_STR;
+        } else {
+            return String.valueOf(text.charAt(0));
+        }
+    }
+
+
     @Override
     public Expression visitGstringValue(GstringValueContext ctx) {
         if (asBoolean(ctx.gstringPath())) {
@@ -4567,6 +4560,15 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object>
implements Groov
     private static final String DQ_STR = "\"";
     private static final String DOLLAR_SLASH_STR = "$/";
 
+    private static final Map<String, String> QUOTATION_MAP = Maps.of(
+            DQ_STR, DQ_STR,
+            SQ_STR, SQ_STR,
+            TDQ_STR, TDQ_STR,
+            TSQ_STR, TSQ_STR,
+            SLASH_STR, SLASH_STR,
+            DOLLAR_SLASH_STR, SLASH_DOLLAR_STR
+    );
+
     private static final String PACKAGE_INFO = "package-info";
     private static final String PACKAGE_INFO_FILE_NAME = PACKAGE_INFO + ".groovy";
     private static final String GROOVY_TRANSFORM_TRAIT = "groovy.transform.Trait";


Mime
View raw message