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 2020/03/04 14:17:28 UTC

[groovy] branch GROOVY_3_0_X updated: GROOVY-9433: grammar ambiguity with ternary expression being confused with safe indexing

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

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


The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
     new 512a235  GROOVY-9433: grammar ambiguity with ternary expression being confused with safe indexing
512a235 is described below

commit 512a235df9a7c06b2e01779e9cb55cf42378d168
Author: Daniel Sun <su...@apache.org>
AuthorDate: Wed Mar 4 21:53:05 2020 +0800

    GROOVY-9433: grammar ambiguity with ternary expression being confused with safe indexing
    
    (cherry picked from commit c7e59ab2c4ebee91d7a9dbb685af60f55e209b7c)
---
 src/antlr/GroovyParser.g4                          | 31 +++++++++++++++++++---
 .../groovy/parser/antlr4/GroovyParserTest.groovy   |  4 +++
 .../src/test/resources/bugs/BUG-GROOVY-9433.groovy | 19 +++++++++++++
 3 files changed, 51 insertions(+), 3 deletions(-)

diff --git a/src/antlr/GroovyParser.g4 b/src/antlr/GroovyParser.g4
index a4fbf14..de512f2 100644
--- a/src/antlr/GroovyParser.g4
+++ b/src/antlr/GroovyParser.g4
@@ -981,7 +981,7 @@ indexPropertyArgs
     ;
 
 namedPropertyArgs
-    :   QUESTION? LBRACK (mapEntryList | COLON) RBRACK
+    :   QUESTION? LBRACK (namedPropertyArgList | COLON) RBRACK
     ;
 
 primary
@@ -1001,6 +1001,14 @@ primary
     |   builtInType                                                                         #builtInTypePrmrAlt
     ;
 
+namedPropertyArgPrimary
+options { baseContext = primary; }
+    :   identifier                                                                          #identifierPrmrAlt
+    |   literal                                                                             #literalPrmrAlt
+    |   gstring                                                                             #gstringPrmrAlt
+    |   parExpression                                                                       #parenPrmrAlt
+    ;
+
 list
     :   LBRACK expressionList[true]? COMMA? RBRACK
     ;
@@ -1017,16 +1025,33 @@ mapEntryList
     :   mapEntry (COMMA mapEntry)*
     ;
 
+namedPropertyArgList
+options { baseContext = mapEntryList; }
+    :   namedPropertyArg (COMMA namedPropertyArg)*
+    ;
+
 mapEntry
     :   mapEntryLabel COLON nls expression
     |   MUL COLON nls expression
     ;
 
+namedPropertyArg
+options { baseContext = mapEntry; }
+    :   namedPropertyArgLabel COLON nls expression
+    |   MUL COLON nls expression
+    ;
+
 mapEntryLabel
     :   keywords
     |   primary
     ;
 
+namedPropertyArgLabel
+options { baseContext = mapEntryLabel; }
+    :   keywords
+    |   namedPropertyArgPrimary
+    ;
+
 /**
  *  t 0: general creation; 1: non-static inner class creation
  */
@@ -1090,13 +1115,13 @@ enhancedArgumentList
 argumentListElement
 options { baseContext = enhancedArgumentListElement; }
     :   expressionListElement[true]
-    |   mapEntry
+    |   namedPropertyArg
     ;
 
 enhancedArgumentListElement
     :   expressionListElement[true]
     |   standardLambdaExpression
-    |   mapEntry
+    |   namedPropertyArg
     ;
 
 stringLiteral
diff --git a/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy b/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
index 33a4813..b64e18a 100644
--- a/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
+++ b/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
@@ -442,4 +442,8 @@ final class GroovyParserTest extends GroovyTestCase {
     void "test groovy core - GROOVY-9427"() {
         doTest('bugs/BUG-GROOVY-9427.groovy');
     }
+
+    void "test groovy core - GROOVY-9433"() {
+        doTest('bugs/BUG-GROOVY-9433.groovy');
+    }
 }
diff --git a/subprojects/parser-antlr4/src/test/resources/bugs/BUG-GROOVY-9433.groovy b/subprojects/parser-antlr4/src/test/resources/bugs/BUG-GROOVY-9433.groovy
new file mode 100644
index 0000000..138a6b6
--- /dev/null
+++ b/subprojects/parser-antlr4/src/test/resources/bugs/BUG-GROOVY-9433.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.
+ */
+true ? ["abc"] as String[] : new String[0]
\ No newline at end of file