You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2017/04/11 01:37:52 UTC
[16/50] groovy git commit: Support array initializer of Java style
Support array initializer of Java style
Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/e553c91f
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/e553c91f
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/e553c91f
Branch: refs/heads/master
Commit: e553c91f4fef77e1d4f8522ea6103a29a5c0cbe7
Parents: c683c74
Author: sunlan <su...@apache.org>
Authored: Tue Jan 24 20:04:33 2017 +0800
Committer: sunlan <su...@apache.org>
Committed: Tue Jan 24 20:04:33 2017 +0800
----------------------------------------------------------------------
.../apache/groovy/parser/antlr4/GroovyParser.g4 | 12 ++--
.../apache/groovy/parser/antlr4/AstBuilder.java | 72 ++++++++++++--------
.../parser/antlr4/GroovyParserTest.groovy | 5 ++
.../src/test/resources/core/Array_01x.groovy | 72 ++++++++++++++++++++
4 files changed, 130 insertions(+), 31 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/groovy/blob/e553c91f/subprojects/groovy-parser-antlr4/src/main/antlr4/org/apache/groovy/parser/antlr4/GroovyParser.g4
----------------------------------------------------------------------
diff --git a/subprojects/groovy-parser-antlr4/src/main/antlr4/org/apache/groovy/parser/antlr4/GroovyParser.g4 b/subprojects/groovy-parser-antlr4/src/main/antlr4/org/apache/groovy/parser/antlr4/GroovyParser.g4
index 5bf6f87..db105c9 100644
--- a/subprojects/groovy-parser-antlr4/src/main/antlr4/org/apache/groovy/parser/antlr4/GroovyParser.g4
+++ b/subprojects/groovy-parser-antlr4/src/main/antlr4/org/apache/groovy/parser/antlr4/GroovyParser.g4
@@ -355,13 +355,12 @@ variableDeclaratorId
;
variableInitializer
- : arrayInitializer
- | statementExpression
+ : statementExpression
| standardLambda
;
-arrayInitializer
- : LBRACK (variableInitializer (COMMA variableInitializer)* (COMMA)? )? RBRACK
+variableInitializers
+ : variableInitializer nls (COMMA nls variableInitializer nls)* nls COMMA?
;
standardType
@@ -1086,6 +1085,11 @@ mapEntryLabel
creator
: createdName nls arguments anonymousInnerClassDeclaration[0]?
| createdName (LBRACK expression RBRACK)+ (b+=LBRACK RBRACK)*
+ | createdName (b+=LBRACK RBRACK)+ arrayInitializer
+ ;
+
+arrayInitializer
+ : LBRACE nls variableInitializers? nls RBRACE
;
/**
http://git-wip-us.apache.org/repos/asf/groovy/blob/e553c91f/subprojects/groovy-parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
----------------------------------------------------------------------
diff --git a/subprojects/groovy-parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java b/subprojects/groovy-parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
index 049719b..7f81e19 100644
--- a/subprojects/groovy-parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
+++ b/subprojects/groovy-parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
@@ -1542,10 +1542,6 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
return EmptyExpression.INSTANCE;
}
- if (asBoolean(ctx.arrayInitializer())) {
- return this.configureAST(this.visitArrayInitializer(ctx.arrayInitializer()), ctx);
- }
-
if (asBoolean(ctx.statementExpression())) {
return this.configureAST(
((ExpressionStatement) this.visit(ctx.statementExpression())).getExpression(),
@@ -1560,15 +1556,24 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
}
@Override
- public ListExpression visitArrayInitializer(ArrayInitializerContext ctx) {
- return this.configureAST(
- new ListExpression(
- ctx.variableInitializer().stream()
- .map(this::visitVariableInitializer)
- .collect(Collectors.toList())),
- ctx);
+ public List<Expression> visitVariableInitializers(VariableInitializersContext ctx) {
+ if (!asBoolean(ctx)) {
+ return Collections.emptyList();
+ }
+
+ return ctx.variableInitializer().stream()
+ .map(this::visitVariableInitializer)
+ .collect(Collectors.toList());
}
+ @Override
+ public List<Expression> visitArrayInitializer(ArrayInitializerContext ctx) {
+ if (!asBoolean(ctx)) {
+ return Collections.emptyList();
+ }
+
+ return this.visitVariableInitializers(ctx.variableInitializers());
+ }
@Override
public Statement visitBlock(BlockContext ctx) {
@@ -2611,24 +2616,37 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
}
if (asBoolean(ctx.LBRACK())) { // create array
- Expression[] empties;
- if (asBoolean(ctx.b)) {
- empties = new Expression[ctx.b.size()];
- Arrays.setAll(empties, i -> ConstantExpression.EMPTY_EXPRESSION);
+ if (asBoolean(ctx.arrayInitializer())) {
+ ClassNode arrayType = classNode;
+ for (int i = 0, n = ctx.b.size() - 1; i < n; i++) {
+ arrayType = arrayType.makeArray();
+ }
+
+ return this.configureAST(
+ new ArrayExpression(
+ arrayType,
+ this.visitArrayInitializer(ctx.arrayInitializer())),
+ ctx);
} else {
- empties = new Expression[0];
- }
+ Expression[] empties;
+ if (asBoolean(ctx.b)) {
+ empties = new Expression[ctx.b.size()];
+ Arrays.setAll(empties, i -> ConstantExpression.EMPTY_EXPRESSION);
+ } else {
+ empties = new Expression[0];
+ }
- return this.configureAST(
- new ArrayExpression(
- classNode,
- null,
- Stream.concat(
- ctx.expression().stream()
- .map(e -> (Expression) this.visit(e)),
- Arrays.stream(empties)
- ).collect(Collectors.toList())),
- ctx);
+ return this.configureAST(
+ new ArrayExpression(
+ classNode,
+ null,
+ Stream.concat(
+ ctx.expression().stream()
+ .map(e -> (Expression) this.visit(e)),
+ Arrays.stream(empties)
+ ).collect(Collectors.toList())),
+ ctx);
+ }
}
throw createParsingFailedException("Unsupported creator: " + ctx.getText(), ctx);
http://git-wip-us.apache.org/repos/asf/groovy/blob/e553c91f/subprojects/groovy-parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy b/subprojects/groovy-parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
index 98d434c..86309c3 100644
--- a/subprojects/groovy-parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
+++ b/subprojects/groovy-parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
@@ -339,4 +339,9 @@ class GroovyParserTest extends GroovyTestCase {
doRunAndTest('core/BreakingChange_03x.groovy');
doRunAndTest('core/BreakingChange_04x.groovy');
}
+
+ void "test groovy core - Array"() {
+ doRunAndTest('core/Array_01x.groovy');
+ }
+
}
http://git-wip-us.apache.org/repos/asf/groovy/blob/e553c91f/subprojects/groovy-parser-antlr4/src/test/resources/core/Array_01x.groovy
----------------------------------------------------------------------
diff --git a/subprojects/groovy-parser-antlr4/src/test/resources/core/Array_01x.groovy b/subprojects/groovy-parser-antlr4/src/test/resources/core/Array_01x.groovy
new file mode 100644
index 0000000..033cb34
--- /dev/null
+++ b/subprojects/groovy-parser-antlr4/src/test/resources/core/Array_01x.groovy
@@ -0,0 +1,72 @@
+import groovy.transform.CompileStatic
+
+def testArrayInitializer() {
+ def x = new double[] {}
+ assert x.length == 0
+
+ def y = new double[][] {}
+ assert y.length == 0
+
+ def a = new int[] {1, 2}
+ assert a[0] == 1
+ assert a[1] == 2
+ assert a as List == [1, 2]
+
+ def b = new int[][] {
+ new int[] {1, 1.plus(1)},
+ new int[] {2.plus(1), 4}
+ }
+ assert b[0][0] == 1
+ assert b[0][1] == 2
+ assert b[1][0] == 3
+ assert b[1][1] == 4
+
+ def c = new String[] {
+ 'a'
+ ,
+ 'b'
+ ,
+ 'c'
+ ,
+ }
+ assert c[0] == 'a'
+ assert c[1] == 'b'
+ assert c[2] == 'c'
+}
+testArrayInitializer();
+
+@CompileStatic
+def testArrayInitializerCS() {
+ def x = new double[] {}
+ assert x.length == 0
+
+ def y = new double[][] {}
+ assert y.length == 0
+
+ def a = new int[] {1, 2}
+ assert a[0] == 1
+ assert a[1] == 2
+ assert a as List == [1, 2]
+
+ def b = new int[][] {
+ new int[] {1, 1.plus(1)},
+ new int[] {2.plus(1), 4}
+ }
+ assert b[0][0] == 1
+ assert b[0][1] == 2
+ assert b[1][0] == 3
+ assert b[1][1] == 4
+
+ def c = new String[] {
+ 'a'
+ ,
+ 'b'
+ ,
+ 'c'
+ ,
+ }
+ assert c[0] == 'a'
+ assert c[1] == 'b'
+ assert c[2] == 'c'
+}
+testArrayInitializerCS();
\ No newline at end of file