groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [2/2] groovy git commit: GROOVY-8680: The AST of string constant with the unary operator is different with the one generated by the old parser
Date Wed, 04 Jul 2018 02:32:17 GMT
GROOVY-8680: The AST of string constant with the unary operator is different with the one generated
by the old parser

(cherry picked from commit 2c1f7e3)


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

Branch: refs/heads/GROOVY_2_6_X
Commit: 7a1f711574d59d12fb224162ec9f9fd79f758933
Parents: 1de196c
Author: sunlan <sunlan@apache.org>
Authored: Wed Jul 4 10:30:06 2018 +0800
Committer: sunlan <sunlan@apache.org>
Committed: Wed Jul 4 10:32:05 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/7a1f7115/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 fb3895e..d7aa09d 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
@@ -2874,18 +2874,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 {
@@ -2926,6 +2924,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/7a1f7115/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/7a1f7115/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/7a1f7115/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'


Mime
View raw message