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/07/04 02:30:11 UTC

groovy git commit: GROOVY-8680: The AST of string constant with the unary operator is different with the one generated by the old parser

Repository: groovy
Updated Branches:
  refs/heads/master 30b160d0a -> 2c1f7e3c4


GROOVY-8680: The AST of string constant with the unary operator is different with the one generated by the old parser


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

Branch: refs/heads/master
Commit: 2c1f7e3c47b3c1d12c05a1540307c43f1267b10f
Parents: 30b160d
Author: sunlan <su...@apache.org>
Authored: Wed Jul 4 10:30:06 2018 +0800
Committer: sunlan <su...@apache.org>
Committed: Wed Jul 4 10:30:06 2018 +0800

----------------------------------------------------------------------
 .../apache/groovy/parser/antlr4/AstBuilder.java | 12 ++++++++----
 .../groovy/parser/antlr4/SyntaxErrorTest.groovy |  5 +++++
 .../resources/fail/UnaryOperator_01x.groovy     | 20 ++++++++++++++++++++
 .../resources/fail/UnaryOperator_02x.groovy     | 20 ++++++++++++++++++++
 4 files changed, 53 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/2c1f7e3c/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 031680f..c1bee5c 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
@@ -2845,18 +2845,16 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
         ExpressionContext expressionCtx = ctx.expression();
         Expression expression = (Expression) this.visit(expressionCtx);
 
-        Boolean insidePar = isInsideParentheses(expression);
-
         switch (ctx.op.getType()) {
             case ADD: {
-                if (expression instanceof ConstantExpression && !insidePar) {
+                if (isNonStringConstantOutsideParentheses(expression)) {
                     return configureAST(expression, ctx);
                 }
 
                 return configureAST(new UnaryPlusExpression(expression), ctx);
             }
             case SUB: {
-                if (expression instanceof ConstantExpression && !insidePar) {
+                if (isNonStringConstantOutsideParentheses(expression)) {
                     ConstantExpression constantExpression = (ConstantExpression) expression;
 
                     try {
@@ -2897,6 +2895,12 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
         }
     }
 
+    private boolean isNonStringConstantOutsideParentheses(Expression expression) {
+        return expression instanceof ConstantExpression
+                && !(((ConstantExpression) expression).getValue() instanceof String)
+                && !isInsideParentheses(expression);
+    }
+
     @Override
     public BinaryExpression visitMultiplicativeExprAlt(MultiplicativeExprAltContext ctx) {
         return this.createBinaryExpression(ctx.left, ctx.op, ctx.right, ctx);

http://git-wip-us.apache.org/repos/asf/groovy/blob/2c1f7e3c/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 868269f..1712849 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
@@ -231,6 +231,11 @@ class SyntaxErrorTest extends GroovyTestCase {
         TestUtils.doRunAndShouldFail('fail/Import_02x.groovy');
     }
 
+    void "test groovy core - UnaryOperator"() {
+        TestUtils.doRunAndShouldFail('fail/UnaryOperator_01x.groovy');
+        TestUtils.doRunAndShouldFail('fail/UnaryOperator_02x.groovy');
+    }
+
     /**************************************/
     static unzipScriptAndShouldFail(String entryName, List ignoreClazzList, Map<String, String> replacementsMap=[:], boolean toCheckNewParserOnly = false) {
         ignoreClazzList.addAll(TestUtils.COMMON_IGNORE_CLASS_LIST)

http://git-wip-us.apache.org/repos/asf/groovy/blob/2c1f7e3c/subprojects/parser-antlr4/src/test/resources/fail/UnaryOperator_01x.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/resources/fail/UnaryOperator_01x.groovy b/subprojects/parser-antlr4/src/test/resources/fail/UnaryOperator_01x.groovy
new file mode 100644
index 0000000..3483233
--- /dev/null
+++ b/subprojects/parser-antlr4/src/test/resources/fail/UnaryOperator_01x.groovy
@@ -0,0 +1,20 @@
+/*
+ *  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.
+ */
+
++'bar2'

http://git-wip-us.apache.org/repos/asf/groovy/blob/2c1f7e3c/subprojects/parser-antlr4/src/test/resources/fail/UnaryOperator_02x.groovy
----------------------------------------------------------------------
diff --git a/subprojects/parser-antlr4/src/test/resources/fail/UnaryOperator_02x.groovy b/subprojects/parser-antlr4/src/test/resources/fail/UnaryOperator_02x.groovy
new file mode 100644
index 0000000..83395bc
--- /dev/null
+++ b/subprojects/parser-antlr4/src/test/resources/fail/UnaryOperator_02x.groovy
@@ -0,0 +1,20 @@
+/*
+ *  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.
+ */
+
+-'bar2'