This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new 3084279cd9 GROOVY-8551: simplify parser rules for array
3084279cd9 is described below

commit 3084279cd93cc541a625ae5f4f29d9fc48844498
Author: Daniel Sun <[email protected]>
AuthorDate: Wed Jan 22 02:30:07 2025 +0900

    GROOVY-8551: simplify parser rules for array
---
 src/antlr/GroovyParser.g4                          | 16 ++---------
 .../apache/groovy/parser/antlr4/AstBuilder.java    | 32 +++++++++++++++++++---
 src/test-resources/fail/Array_03x.groovy           | 19 +++++++++++++
 src/test-resources/fail/Array_04x.groovy           | 19 +++++++++++++
 .../groovy/parser/antlr4/SyntaxErrorTest.groovy    |  2 ++
 5 files changed, 71 insertions(+), 17 deletions(-)

diff --git a/src/antlr/GroovyParser.g4 b/src/antlr/GroovyParser.g4
index a23d19e010..f9d3b7b2d3 100644
--- a/src/antlr/GroovyParser.g4
+++ b/src/antlr/GroovyParser.g4
@@ -1141,22 +1141,12 @@ options { baseContext = mapEntryLabel; }
 creator[int t]
     :   createdName
         (   nls arguments anonymousInnerClassDeclaration[0]?
-        |   dim[0]+ nls arrayInitializer
-        |   dim[1]+ dim[0]*
+        |   dim+ (nls arrayInitializer)?
         )
     ;
 
-/**
- *  n 0: dim without size expression; 1: dim with size expression
- */
-dim[int n]
-    :   annotationsOpt LBRACK
-        (
-            { $n == 1 }?
-            expression
-        |
-        )
-        RBRACK
+dim
+    :   annotationsOpt LBRACK expression? RBRACK
     ;
 
 arrayInitializer
diff --git a/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java 
b/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
index 02e40817cf..f7799fdadc 100644
--- a/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
+++ b/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
@@ -3482,7 +3482,15 @@ public class AstBuilder extends 
GroovyParserBaseVisitor<Object> {
             final int nDim = dims.size();
             if (asBoolean(ctx.arrayInitializer())) { // create array: new 
Type[][]{ ... }
                 List<List<AnnotationNode>> typeAnnotations = new 
ArrayList<>(nDim);
-                for (var dim : dims) 
typeAnnotations.add(this.visitAnnotationsOpt(dim.annotationsOpt()));
+                for (var dim : dims) {
+                    final ExpressionContext dimExpressionContext = 
dim.expression();
+                    if (asBoolean(dimExpressionContext)) {
+                        throw createParsingFailedException(
+                                "Unsupported array dimension expression: " + 
dimExpressionContext.getText(),
+                                dimExpressionContext);
+                    }
+                    
typeAnnotations.add(this.visitAnnotationsOpt(dim.annotationsOpt()));
+                }
 
                 ClassNode elementType = classNode;
                 for (int i = nDim - 1; i > 0; i -= 1) {
@@ -3500,10 +3508,26 @@ public class AstBuilder extends 
GroovyParserBaseVisitor<Object> {
             } else { // create array: new Type[n][]
                 final List<Expression> sizeExpressions = new ArrayList<>(nDim);
                 final List<List<AnnotationNode>> typeAnnotations = new 
ArrayList<>(nDim);
-                for (var dim : dims) {
+                ExpressionContext lastDimExpressionContext = null;
+                for (int i = 0, n = dims.size(); i < n; i += 1) {
+                    var dim = dims.get(i);
+                    final ExpressionContext dimExpressionContext = 
dim.expression();
+                    if (i == 0) {
+                        if (!asBoolean(dimExpressionContext)) {
+                            throw createParsingFailedException("array 
dimension expression is expected", dim);
+                        }
+                    } else {
+                        if (asBoolean(dimExpressionContext) && 
!asBoolean(lastDimExpressionContext)) {
+                            throw createParsingFailedException(
+                                    "Unsupported array dimension expression: " 
+ dimExpressionContext.getText(),
+                                    dimExpressionContext);
+                        }
+                    }
+                    lastDimExpressionContext = dimExpressionContext;
+
                     final Expression sizeExpression;
-                    if (asBoolean(dim.expression())) {
-                        sizeExpression = (Expression) 
this.visit(dim.expression());
+                    if (asBoolean(dimExpressionContext)) {
+                        sizeExpression = (Expression) 
this.visit(dimExpressionContext);
                     } else {
                         sizeExpression = ConstantExpression.EMPTY_EXPRESSION;
                     }
diff --git a/src/test-resources/fail/Array_03x.groovy 
b/src/test-resources/fail/Array_03x.groovy
new file mode 100644
index 0000000000..562e0d7254
--- /dev/null
+++ b/src/test-resources/fail/Array_03x.groovy
@@ -0,0 +1,19 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+def foo = new double[2][][5]
diff --git a/src/test-resources/fail/Array_04x.groovy 
b/src/test-resources/fail/Array_04x.groovy
new file mode 100644
index 0000000000..84a3a9b72f
--- /dev/null
+++ b/src/test-resources/fail/Array_04x.groovy
@@ -0,0 +1,19 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+def foo = new double[2] { 1.0, 2.0 }
diff --git a/src/test/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy 
b/src/test/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
index a322a85374..c4c91afa62 100644
--- a/src/test/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
+++ b/src/test/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
@@ -496,6 +496,8 @@ final class SyntaxErrorTest {
     void 'groovy core - Array'() {
         TestUtils.doRunAndShouldFail('fail/Array_01x.groovy')
         TestUtils.doRunAndShouldFail('fail/Array_02x.groovy')
+        TestUtils.doRunAndShouldFail('fail/Array_03x.groovy')
+        TestUtils.doRunAndShouldFail('fail/Array_04x.groovy')
     }
 
     @Test

Reply via email to