You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2015/06/21 13:50:52 UTC

incubator-groovy git commit: Revert "GROOVY-7428: Change precedence priority of ** compared to pre/post inc/dec and unary plus/minus"

Repository: incubator-groovy
Updated Branches:
  refs/heads/master 798a95d99 -> 0dc6cffce


Revert "GROOVY-7428: Change precedence priority of ** compared to pre/post inc/dec and unary plus/minus"

This reverts commit e874e68a51a44af6277b5e24146bd90c644a0e8a.


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

Branch: refs/heads/master
Commit: 0dc6cffce46abfa72b53c1cb36a2ea65ffb4c5da
Parents: 798a95d
Author: Paul King <pa...@asert.com.au>
Authored: Sun Jun 21 21:50:08 2015 +1000
Committer: Paul King <pa...@asert.com.au>
Committed: Sun Jun 21 21:50:08 2015 +1000

----------------------------------------------------------------------
 src/main/org/codehaus/groovy/antlr/groovy.g     | 32 ++++++++++++++------
 src/spec/doc/core-operators.adoc                |  4 +--
 .../groovy/operator/PowerOperatorsTest.groovy   | 12 +++++---
 3 files changed, 33 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/0dc6cffc/src/main/org/codehaus/groovy/antlr/groovy.g
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/antlr/groovy.g b/src/main/org/codehaus/groovy/antlr/groovy.g
index b1ad444..2cc12f2 100644
--- a/src/main/org/codehaus/groovy/antlr/groovy.g
+++ b/src/main/org/codehaus/groovy/antlr/groovy.g
@@ -2342,7 +2342,7 @@ commandArgument
 //         nextHigherPrecedenceExpression
 //                 (OPERATOR nextHigherPrecedenceExpression)*
 // which is a standard recursive definition for a parsing an expression.
-// The operators have the following precedences:
+// The operators in java have the following precedences:
 //      lowest  ( 15)  = **= *= /= %= += -= <<= >>= >>>= &= ^= |=
 //              ( 14)  ?: (conditional expression and elvis)
 //              ( 13)  ||
@@ -2358,11 +2358,11 @@ commandArgument
 //              (  4)  * / %
 //              (  3)  **(power)
 //              (  2)  ++(pre) --(pre) +(unary) -(unary)
-//              (  1)  ~  ! (type) ++(post) --(post)
-//                     . (member access) .& (method closure) .@ (field/attribute access)
-//                     ?. (safe dereference) * *. *: (spread ops)
-//                     [] (index)  () (method call)  {} (closableBlock)  [] (list/map)
-//                     new (object creation) () (explicit parenthesis)
+//              (  1)  ~  ! $ (type) ++(post) --(post)
+//                     . ?. *. (dot -- identifier qualification)
+//                     []   () (method call)  {} (closableBlock)  [] (list/map)
+//                     new  () (explicit parenthesis)
+//                     $x (scope escape)
 //
 // the last two are not usually on a precedence chart; I put them in
 // to point out that new has a higher precedence than '.', so you
@@ -2833,7 +2833,11 @@ additiveExpression[int lc_stmt]
 
 // multiplication/division/modulo (level 4)
 multiplicativeExpression[int lc_stmt]
-    :    powerExpression[lc_stmt] ((STAR^ | DIV^ | MOD^ )  nls!  powerExpression[0])*
+    :    ( INC^ nls!  powerExpressionNotPlusMinus[0] ((STAR^ | DIV^ | MOD^ )  nls!  powerExpression[0])* )
+    |    ( DEC^ nls!  powerExpressionNotPlusMinus[0] ((STAR^ | DIV^ | MOD^ )  nls!  powerExpression[0])* )
+    |    ( MINUS^ {#MINUS.setType(UNARY_MINUS);} nls!   powerExpressionNotPlusMinus[0] ((STAR^ | DIV^ | MOD^ )  nls!  powerExpression[0])* )
+    |    ( PLUS^ {#PLUS.setType(UNARY_PLUS);} nls!   powerExpressionNotPlusMinus[0] ((STAR^ | DIV^ | MOD^ )  nls!  powerExpression[0])* )
+    |    (  powerExpressionNotPlusMinus[lc_stmt] ((STAR^ | DIV^ | MOD^ )  nls!  powerExpression[0])* )
     ;
 
 // math power operator (**) (level 3)
@@ -2841,6 +2845,14 @@ powerExpression[int lc_stmt]
     :   unaryExpression[lc_stmt] (STAR_STAR^ nls! unaryExpression[0])*
     ;
 
+// math power operator (**) (level 3)
+// (without ++(prefix)/--(prefix)/+(unary)/-(unary))
+// The different rules are needed to avoid ambiguous selection
+// of alternatives.
+powerExpressionNotPlusMinus[int lc_stmt]
+    :   unaryExpressionNotPlusMinus[lc_stmt] (STAR_STAR^ nls! unaryExpression[0])*
+    ;
+
 // ++(prefix)/--(prefix)/+(unary)/-(unary) (level 2)
 unaryExpression[int lc_stmt]
     :   INC^ nls! unaryExpression[0]
@@ -2852,7 +2864,9 @@ unaryExpression[int lc_stmt]
 
 // ~(BNOT)/!(LNOT)/(type casting) (level 1)
 unaryExpressionNotPlusMinus[int lc_stmt]
-    :   BNOT^ nls! unaryExpression[0]
+    :   //BAND^    {#BAND.setType(MEMBER_POINTER_DEFAULT);}   nls!  namePart
+    //|
+        BNOT^ nls! unaryExpression[0]
     |   LNOT^ nls! unaryExpression[0]
     |   (   // subrule allows option to shut off warnings
             options {
@@ -2880,7 +2894,7 @@ unaryExpressionNotPlusMinus[int lc_stmt]
         )
     ;
 
-// qualified names, array expressions, method invocation, post inc/dec (level 1)
+// qualified names, array expressions, method invocation, post inc/dec
 postfixExpression[int lc_stmt]
     :
         pathExpression[lc_stmt]

http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/0dc6cffc/src/spec/doc/core-operators.adoc
----------------------------------------------------------------------
diff --git a/src/spec/doc/core-operators.adoc b/src/spec/doc/core-operators.adoc
index 79a760a..99adfd4 100644
--- a/src/spec/doc/core-operators.adoc
+++ b/src/spec/doc/core-operators.adoc
@@ -663,8 +663,8 @@ The table below lists all groovy operators in order of precedence.
 |   | `?.` &#160; `\*` &#160; `*.` &#160; `*:` | safe dereferencing, spread, spread-dot, spread-map
 |   | `~` &#160; `!` &#160; `(type)` | bitwise negate/pattern, not, typecast
 |   | `[]` &#160; `++` &#160; `--` | list/map/array index, post inc/decrement
-| 2 | `++` &#160; `--` &#160; `+` &#160; `-` | pre inc/decrement, unary plus, unary minus
-| 3 | `**` | power
+| 2 | `**` | power
+| 3 | `++` &#160; `--` &#160; `+` &#160; `-` | pre inc/decrement, unary plus, unary minus
 | 4 | `*` &#160; `/` &#160; `%` | multiply, div, modulo
 | 5 | `+` &#160; `-` | addition, subtraction
 | 6 | `<<` &#160; `>>` &#160; `>>>` &#160; `..` &#160; `..<` | left/right (unsigned) shift, inclusive/exclusive range

http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/0dc6cffc/src/test/groovy/operator/PowerOperatorsTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/operator/PowerOperatorsTest.groovy b/src/test/groovy/operator/PowerOperatorsTest.groovy
index b5138cc..85e81e7 100644
--- a/src/test/groovy/operator/PowerOperatorsTest.groovy
+++ b/src/test/groovy/operator/PowerOperatorsTest.groovy
@@ -20,6 +20,9 @@ package groovy.operator
 
 /** 
  * Test Math Power Operation in Classic/New Groovy
+ * 
+ * @author Pilho Kim
+ * @version $Revision: 4996 $
  */
 class PowerOperatorsTest extends GroovyTestCase {
 
@@ -27,11 +30,11 @@ class PowerOperatorsTest extends GroovyTestCase {
         assert 2**5 == 32
         assert -2**5 == -32
         assert 3**4 == 81
-        assert -3**4 == 81
+        assert -3**4 == -81
         assert 3**-4 == 3.power(-4)
-        assert -3**-4 != -3.power(-4)
+        assert -3**-4 == -3.power(-4)
         assert 7**2 - 7*3 + 2 == 30         //  49 - 21 + 2 = 30
-        assert -7**2 - 7*3 + 2 == 30       // 49 - 21 + 2 = -68
+        assert -7**2 - 7*3 + 2 == -68       // -49 - 21 + 2 = -68
         assert -(7**2) - 7*3 + 2 == -68     // -49 - 21 + 2 = -68
         assert (-7)**2 - 7*3 + 2 == 30     //  49 - 21 + 2 = 30
     }
@@ -72,7 +75,8 @@ class PowerOperatorsTest extends GroovyTestCase {
         x **= 2
         assert x == 25
         assert x**2 == 625
-        assert -x**2 == 625
+        assert -x**2 != 625
+        assert -x**2 == -625
     }
 
     void testPowerAssignmentOperation() {