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/09/01 21:24:21 UTC
groovy git commit: Improve the performance of parsing
Repository: groovy
Updated Branches:
refs/heads/master 8301ba03d -> 365a3e83f
Improve the performance of parsing
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/365a3e83
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/365a3e83
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/365a3e83
Branch: refs/heads/master
Commit: 365a3e83f8bf1fb53257b53865c30573192fbea5
Parents: 8301ba0
Author: sunlan <su...@apache.org>
Authored: Sat Sep 2 05:24:14 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Sat Sep 2 05:24:14 2017 +0800
----------------------------------------------------------------------
src/main/antlr/GroovyParser.g4 | 31 +++----
.../parser/antlr4/SemanticPredicates.java | 9 +-
.../groovy/parser/antlr4/util/StringUtils.java | 96 ++++++++------------
3 files changed, 53 insertions(+), 83 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/365a3e83/src/main/antlr/GroovyParser.g4
----------------------------------------------------------------------
diff --git a/src/main/antlr/GroovyParser.g4 b/src/main/antlr/GroovyParser.g4
index b5e4c84..4fe61e3 100644
--- a/src/main/antlr/GroovyParser.g4
+++ b/src/main/antlr/GroovyParser.g4
@@ -280,7 +280,7 @@ memberDeclaration[int t]
*/
methodDeclaration[int t, int ct]
: { 3 == $ct }?
- returnType[$ct] methodName LPAREN rparen (DEFAULT nls elementValue)?
+ returnType[$ct] methodName LPAREN RPAREN (DEFAULT nls elementValue)?
|
( { 0 == $t }?
modifiersOpt typeParameters?
@@ -335,7 +335,7 @@ variableInitializers
;
dims
- : (annotationsOpt LBRACK rbrack)+
+ : (annotationsOpt LBRACK RBRACK)+
;
dimsOpt
@@ -406,7 +406,7 @@ qualifiedClassNameList
;
formalParameters
- : LPAREN formalParameterList? rparen
+ : LPAREN formalParameterList? RPAREN
;
formalParameterList
@@ -556,7 +556,7 @@ elementValue
;
elementValueArrayInitializer
- : LBRACK (elementValue (COMMA elementValue)*)? (COMMA)? rbrack
+ : LBRACK (elementValue (COMMA elementValue)*)? (COMMA)? RBRACK
;
// STATEMENTS / BLOCKS
@@ -596,7 +596,7 @@ variableDeclaration[int t]
;
typeNamePairs
- : LPAREN typeNamePair (COMMA typeNamePair)* rparen
+ : LPAREN typeNamePair (COMMA typeNamePair)* RPAREN
;
typeNamePair
@@ -1040,11 +1040,11 @@ dynamicMemberName
* The brackets may also be empty, as in T[]. This is how Groovy names array types.
*/
indexPropertyArgs
- : QUESTION? LBRACK expressionList[true]? rbrack
+ : QUESTION? LBRACK expressionList[true]? RBRACK
;
namedPropertyArgs
- : LBRACK mapEntryList rbrack
+ : LBRACK mapEntryList RBRACK
;
primary
@@ -1073,7 +1073,7 @@ locals[boolean empty = true]
COMMA
{ require(!$empty, "Empty list constructor should not contain any comma(,)", -1); }
)?
- rbrack
+ RBRACK
;
map
@@ -1081,7 +1081,7 @@ map
( mapEntryList COMMA?
| COLON
)
- rbrack
+ RBRACK
;
mapEntryList
@@ -1101,7 +1101,7 @@ mapEntryLabel
creator
: createdName
( nls arguments anonymousInnerClassDeclaration[0]?
- | (annotationsOpt LBRACK expression rbrack)+ dimsOpt
+ | (annotationsOpt LBRACK expression RBRACK)+ dimsOpt
| dims nls arrayInitializer
)
;
@@ -1251,16 +1251,7 @@ rparen
: RPAREN
|
// !!!Error Alternative, impact the performance of parsing
- ~LPAREN
- { require(false, "Missing ')'", -1); }
- ;
-
-rbrack
- : RBRACK
- |
- // !!!Error Alternative, impact the performance of parsing
- ~LBRACK
- { require(false, "Missing ']'", -1); }
+ { require(false, "Missing ')'"); }
;
nls
http://git-wip-us.apache.org/repos/asf/groovy/blob/365a3e83/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/SemanticPredicates.java
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/SemanticPredicates.java b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/SemanticPredicates.java
index 32939b4..695f573 100644
--- a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/SemanticPredicates.java
+++ b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/SemanticPredicates.java
@@ -142,10 +142,11 @@ public class SemanticPredicates {
tokenType = token.getType();
tokenType3 = ts.LT(index + 2).getType();
- return //VOID == tokenType ||
- !(ASSIGN == tokenType3 || (LT == tokenType2 || LBRACK == tokenType2))
- && !(BuiltInPrimitiveType == tokenType || MODIFIER_SET.contains(tokenType))
- && Character.isLowerCase(token.getText().codePointAt(0));
+ return // VOID == tokenType ||
+ !(BuiltInPrimitiveType == tokenType || MODIFIER_SET.contains(tokenType))
+ && Character.isLowerCase(token.getText().codePointAt(0))
+ && !(ASSIGN == tokenType3 || (LT == tokenType2 || LBRACK == tokenType2));
+
}
}
http://git-wip-us.apache.org/repos/asf/groovy/blob/365a3e83/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/util/StringUtils.java
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/util/StringUtils.java b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/util/StringUtils.java
index 40313cd..b6e75aa 100644
--- a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/util/StringUtils.java
+++ b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/util/StringUtils.java
@@ -32,33 +32,33 @@ import java.util.regex.Pattern;
* Created on 2016/08/20
*/
public class StringUtils {
- public static String replaceHexEscapes(String text) {
- Pattern p = Pattern.compile("(\\\\*)\\\\u+([0-9abcdefABCDEF]{4})");
- return StringGroovyMethods.replaceAll((CharSequence) text, p, new Closure<Void>(null, null) {
- Object doCall(String _0, String _1, String _2) {
+ public static String replaceHexEscapes(String text) {
+ Pattern p = Pattern.compile("(\\\\*)\\\\u+([0-9abcdefABCDEF]{4})");
+ return StringGroovyMethods.replaceAll((CharSequence) text, p, new Closure<Void>(null, null) {
+ Object doCall(String _0, String _1, String _2) {
if (isLengthOdd(_1)) {
return _0;
}
- return _1 + new String(Character.toChars(Integer.parseInt(_2, 16)));
- }
- });
- }
+ return _1 + new String(Character.toChars(Integer.parseInt(_2, 16)));
+ }
+ });
+ }
public static String replaceOctalEscapes(String text) {
- Pattern p = Pattern.compile("(\\\\*)\\\\([0-3]?[0-7]?[0-7])");
- return StringGroovyMethods.replaceAll((CharSequence) text, p, new Closure<Void>(null, null) {
- Object doCall(String _0, String _1, String _2) {
+ Pattern p = Pattern.compile("(\\\\*)\\\\([0-3]?[0-7]?[0-7])");
+ return StringGroovyMethods.replaceAll((CharSequence) text, p, new Closure<Void>(null, null) {
+ Object doCall(String _0, String _1, String _2) {
if (isLengthOdd(_1)) {
return _0;
}
- return _1 + new String(Character.toChars(Integer.parseInt(_2, 8)));
- }
- });
- }
+ return _1 + new String(Character.toChars(Integer.parseInt(_2, 8)));
+ }
+ });
+ }
- private static final Map<Character, Character> STANDARD_ESCAPES = Maps.of(
+ private static final Map<Character, Character> STANDARD_ESCAPES = Maps.of(
'b', '\b',
't', '\t',
'n', '\n',
@@ -67,21 +67,21 @@ public class StringUtils {
);
public static String replaceStandardEscapes(String text) {
- Pattern p = Pattern.compile("(\\\\*)\\\\([btnfr\"'])");
+ Pattern p = Pattern.compile("(\\\\*)\\\\([btnfr\"'])");
- String result = StringGroovyMethods.replaceAll((CharSequence) text, p, new Closure<Void>(null, null) {
- Object doCall(String _0, String _1, String _2) {
- if (isLengthOdd(_1)) {
- return _0;
- }
+ String result = StringGroovyMethods.replaceAll((CharSequence) text, p, new Closure<Void>(null, null) {
+ Object doCall(String _0, String _1, String _2) {
+ if (isLengthOdd(_1)) {
+ return _0;
+ }
- Character character = STANDARD_ESCAPES.get(_2.charAt(0));
- return _1 + (character != null ? character : _2);
- }
- });
+ Character character = STANDARD_ESCAPES.get(_2.charAt(0));
+ return _1 + (character != null ? character : _2);
+ }
+ });
- return replace(result, Maps.of("\\\\", "\\"));
- }
+ return result.replace("\\\\", "\\");
+ }
public static final int NONE_SLASHY = 0;
public static final int SLASHY = 1;
@@ -92,16 +92,15 @@ public class StringUtils {
text = StringUtils.replaceHexEscapes(text);
text = StringUtils.replaceLineEscape(text);
- StringBuilder sb = new StringBuilder(text);
if (slashyType == SLASHY) {
- replace(sb, Maps.of("\\/", "/"));
+ text = text.replace("\\/", "/");
}
if (slashyType == DOLLAR_SLASHY) {
- replace(sb, Maps.of("$$", "$", "$/", "/"));
+ text = text.replace("$$", "$");
+ text = text.replace("$/", "/");
}
- text = sb.toString();
} else if (slashyType == NONE_SLASHY) {
text = StringUtils.replaceEscapes(text);
} else {
@@ -112,11 +111,12 @@ public class StringUtils {
}
private static String replaceEscapes(String text) {
- text = replace(text, Maps.of("\\$", "$"));
+ text = text.replace("\\$", "$");
+
text = StringUtils.replaceLineEscape(text);
- return StringUtils.replaceStandardEscapes(replaceHexEscapes(replaceOctalEscapes(text)));
- }
+ return StringUtils.replaceStandardEscapes(replaceHexEscapes(replaceOctalEscapes(text)));
+ }
private static String replaceLineEscape(String text) {
Pattern p = Pattern.compile("(\\\\*)\\\\\r?\n");
@@ -138,35 +138,13 @@ public class StringUtils {
}
public static String removeCR(String text) {
- return replace(text, Maps.of("\r\n", "\n"));
- }
+ return text.replace("\r\n", "\n");
+ }
public static long countChar(String text, char c) {
return text.chars().filter(e -> c == e).count();
}
- public static String replace(String str, Map<String, String> replacements) {
- return replace(new StringBuilder(str), replacements).toString();
- }
-
- public static StringBuilder replace(StringBuilder sb, Map<String, String> replacements) {
- for (Map.Entry<String, String> replacementEntry : replacements.entrySet()) {
- String key = replacementEntry.getKey();
- int keyLength = key.length();
-
- String value = replacementEntry.getValue();
- int valueLength = value.length();
-
- int start = sb.indexOf(key, 0);
- while (start > -1) {
- sb.replace(start, start + keyLength, value);
- start = sb.indexOf(key, start + valueLength);
- }
- }
-
- return sb;
- }
-
public static String trimQuotations(String text, int quotationLength) {
int length = text.length();