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 2018/02/25 11:31:48 UTC

groovy git commit: Refine "Support command expression in parentheses"

Repository: groovy
Updated Branches:
  refs/heads/master c690063f0 -> cf2483df7


Refine "Support command expression in parentheses"


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/cf2483df
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/cf2483df
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/cf2483df

Branch: refs/heads/master
Commit: cf2483df757f7b47aab2e08e27d0e0af4041bfab
Parents: c690063
Author: danielsun1106 <re...@hotmail.com>
Authored: Sun Feb 25 19:31:38 2018 +0800
Committer: danielsun1106 <re...@hotmail.com>
Committed: Sun Feb 25 19:31:38 2018 +0800

----------------------------------------------------------------------
 .../gls/syntax/MethodCallValidationTest.groovy   |  2 --
 .../apache/groovy/parser/antlr4/AstBuilder.java  | 18 ++++++++++++++++++
 .../groovy/parser/antlr4/SyntaxErrorTest.groovy  |  3 +++
 .../resources/fail/MethodDeclaration_04x.groovy  | 19 +++++++++++++++++++
 .../resources/fail/MethodDeclaration_05x.groovy  | 19 +++++++++++++++++++
 5 files changed, 59 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/cf2483df/src/test/gls/syntax/MethodCallValidationTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/gls/syntax/MethodCallValidationTest.groovy b/src/test/gls/syntax/MethodCallValidationTest.groovy
index 670b097..9fe6d52 100644
--- a/src/test/gls/syntax/MethodCallValidationTest.groovy
+++ b/src/test/gls/syntax/MethodCallValidationTest.groovy
@@ -21,8 +21,6 @@ package gls.syntax
 public class MethodCallValidationTest extends gls.CompilableTestSupport {
 
     void testDeclarationInMethodCall() {
-        if (true) return; // skip for the time being
-
         shouldNotCompile """
             foo(String a)
         """

http://git-wip-us.apache.org/repos/asf/groovy/blob/cf2483df/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
index 8f4f382..0e36164 100644
--- a/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
+++ b/subprojects/parser-antlr4/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
@@ -2119,6 +2119,8 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
             methodCallExpression = configureAST(this.createCallMethodCallExpression(baseExpr, arguments), arguments);
         }
 
+        methodCallExpression.putNodeMetaData(IS_COMMAND_EXPRESSION, true);
+
         if (!asBoolean(ctx.commandArgument())) {
             return configureAST(methodCallExpression, ctx);
         }
@@ -3415,6 +3417,8 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
     public Expression visitExpressionListElement(ExpressionListElementContext ctx) {
         Expression expression = (Expression) this.visit(ctx.expression());
 
+        validateExpressionListElement(ctx, expression);
+
         if (asBoolean(ctx.MUL())) {
             return configureAST(new SpreadExpression(expression), ctx);
         }
@@ -3422,6 +3426,19 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
         return configureAST(expression, ctx);
     }
 
+    private void validateExpressionListElement(ExpressionListElementContext ctx, Expression expression) {
+        if (!(expression instanceof MethodCallExpression && isTrue(expression, IS_COMMAND_EXPRESSION))) {
+            return;
+        }
+
+        // statements like `foo(String a)` is invalid
+        MethodCallExpression methodCallExpression = (MethodCallExpression) expression;
+        String methodName = methodCallExpression.getMethodAsString();
+        if (Character.isUpperCase(methodName.codePointAt(0)) || PRIMITIVE_TYPE_SET.contains(methodName)) {
+            throw createParsingFailedException("Invalid method declaration", ctx);
+        }
+    }
+
 
     // literal {       --------------------------------------------------------------------
     @Override
@@ -4743,6 +4760,7 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
     private static final String IS_STRING = "_IS_STRING";
     private static final String IS_INTERFACE_WITH_DEFAULT_METHODS = "_IS_INTERFACE_WITH_DEFAULT_METHODS";
     private static final String IS_INSIDE_CONDITIONAL_EXPRESSION = "_IS_INSIDE_CONDITIONAL_EXPRESSION";
+    private static final String IS_COMMAND_EXPRESSION = "_IS_COMMAND_EXPRESSION";
 
     private static final String PATH_EXPRESSION_BASE_EXPR = "_PATH_EXPRESSION_BASE_EXPR";
     private static final String PATH_EXPRESSION_BASE_EXPR_GENERICS_TYPES = "_PATH_EXPRESSION_BASE_EXPR_GENERICS_TYPES";

http://git-wip-us.apache.org/repos/asf/groovy/blob/cf2483df/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy b/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
index f0a372a..80a6d62 100644
--- a/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
+++ b/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
@@ -151,6 +151,9 @@ class SyntaxErrorTest extends GroovyTestCase {
         TestUtils.shouldFail('fail/MethodDeclaration_01.groovy');
         TestUtils.doRunAndShouldFail('fail/MethodDeclaration_02x.groovy');
         TestUtils.doRunAndShouldFail('fail/MethodDeclaration_03x.groovy');
+        TestUtils.doRunAndShouldFail('fail/MethodDeclaration_04x.groovy');
+        TestUtils.doRunAndShouldFail('fail/MethodDeclaration_05x.groovy');
+
     }
 
     void "test groovy core - ConstructorDeclaration"() {

http://git-wip-us.apache.org/repos/asf/groovy/blob/cf2483df/subprojects/parser-antlr4/src/test/resources/fail/MethodDeclaration_04x.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/resources/fail/MethodDeclaration_04x.groovy b/subprojects/parser-antlr4/src/test/resources/fail/MethodDeclaration_04x.groovy
new file mode 100644
index 0000000..8457f00
--- /dev/null
+++ b/subprojects/parser-antlr4/src/test/resources/fail/MethodDeclaration_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.
+ */
+foo(String a)

http://git-wip-us.apache.org/repos/asf/groovy/blob/cf2483df/subprojects/parser-antlr4/src/test/resources/fail/MethodDeclaration_05x.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/resources/fail/MethodDeclaration_05x.groovy b/subprojects/parser-antlr4/src/test/resources/fail/MethodDeclaration_05x.groovy
new file mode 100644
index 0000000..15ab3f1
--- /dev/null
+++ b/subprojects/parser-antlr4/src/test/resources/fail/MethodDeclaration_05x.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.
+ */
+foo(int a)