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/05/12 00:03:40 UTC

[groovy] 01/01: GROOVY-9522: Throwing NPE when I use ternary operator with something special

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

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

commit 534f99313d40131d02821ece711a97593d918ee6
Author: Daniel Sun <su...@apache.org>
AuthorDate: Tue May 12 08:03:23 2020 +0800

    GROOVY-9522: Throwing NPE when I use ternary operator with something special
---
 src/antlr/GroovyParser.g4                          | 36 ++++++++++++++++++++--
 .../groovy/parser/antlr4/GroovyParserTest.groovy   |  4 +++
 .../src/test/resources/bugs/BUG-GROOVY-9522.groovy | 32 +++++++++++++++++++
 3 files changed, 70 insertions(+), 2 deletions(-)

diff --git a/src/antlr/GroovyParser.g4 b/src/antlr/GroovyParser.g4
index 29f347c..f564a8a 100644
--- a/src/antlr/GroovyParser.g4
+++ b/src/antlr/GroovyParser.g4
@@ -1008,6 +1008,13 @@ options { baseContext = primary; }
     |   parExpression                                                                       #parenPrmrAlt
     ;
 
+namedArgPrimary
+options { baseContext = primary; }
+    :   identifier                                                                          #identifierPrmrAlt
+    |   literal                                                                             #literalPrmrAlt
+    |   gstring                                                                             #gstringPrmrAlt
+    ;
+
 commandPrimary
 options { baseContext = primary; }
     :   identifier                                                                          #identifierPrmrAlt
@@ -1047,6 +1054,12 @@ options { baseContext = mapEntry; }
     |   MUL COLON nls expression
     ;
 
+namedArg
+options { baseContext = mapEntry; }
+    :   namedArgLabel COLON nls expression
+    |   MUL COLON nls expression
+    ;
+
 mapEntryLabel
     :   keywords
     |   primary
@@ -1058,6 +1071,12 @@ options { baseContext = mapEntryLabel; }
     |   namedPropertyArgPrimary
     ;
 
+namedArgLabel
+options { baseContext = mapEntryLabel; }
+    :   keywords
+    |   namedArgPrimary
+    ;
+
 /**
  *  t 0: general creation; 1: non-static inner class creation
  */
@@ -1105,25 +1124,38 @@ arguments
 
 argumentList
 options { baseContext = enhancedArgumentList; }
-    :   argumentListElement
+    :   firstArgumentListElement
         (   COMMA nls
             argumentListElement
         )*
     ;
 
 enhancedArgumentList
-    :   enhancedArgumentListElement
+    :   firstEnhancedArgumentListElement
         (   COMMA nls
             enhancedArgumentListElement
         )*
     ;
 
+firstArgumentListElement
+options { baseContext = enhancedArgumentListElement; }
+    :   expressionListElement[true]
+    |   namedArg
+    ;
+
 argumentListElement
 options { baseContext = enhancedArgumentListElement; }
     :   expressionListElement[true]
     |   namedPropertyArg
     ;
 
+firstEnhancedArgumentListElement
+options { baseContext = enhancedArgumentListElement; }
+    :   expressionListElement[true]
+    |   standardLambdaExpression
+    |   namedArg
+    ;
+
 enhancedArgumentListElement
     :   expressionListElement[true]
     |   standardLambdaExpression
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 cf841e5..83e63f9 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
@@ -462,4 +462,8 @@ final class GroovyParserTest extends GroovyTestCase {
     void "test groovy core - GROOVY-9507"() {
         doTest('bugs/BUG-GROOVY-9507.groovy');
     }
+
+    void "test groovy core - GROOVY-9522"() {
+        doTest('bugs/BUG-GROOVY-9522.groovy');
+    }
 }
diff --git a/subprojects/parser-antlr4/src/test/resources/bugs/BUG-GROOVY-9522.groovy b/subprojects/parser-antlr4/src/test/resources/bugs/BUG-GROOVY-9522.groovy
new file mode 100644
index 0000000..021d62b
--- /dev/null
+++ b/subprojects/parser-antlr4/src/test/resources/bugs/BUG-GROOVY-9522.groovy
@@ -0,0 +1,32 @@
+/*
+ *  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.
+ */
+
+static String a() {
+    null
+}
+
+static String b() {
+    ''
+}
+
+def x = a() ? [b(), a()].join(',') : b()
+def x = a() ? [b(), a()] : b()
+def x = a() ? ([b(), a()]).join(',') : b()
+
+x == ''