Repository: incubator-groovy
Updated Branches:
  refs/heads/master 19487e954 -> a258a3d02


GROOVY-7385: ensure an error is thrown for integer literals not fitting in a 
given integer type


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

Branch: refs/heads/master
Commit: a258a3d026ebefc411797b2f69915fe1860262db
Parents: 19487e9
Author: Jochen Theodorou <[email protected]>
Authored: Wed Apr 22 17:35:23 2015 +0200
Committer: Jochen Theodorou <[email protected]>
Committed: Wed Apr 22 17:36:55 2015 +0200

----------------------------------------------------------------------
 .../groovy/antlr/AntlrParserPlugin.java         |  4 +--
 .../org/codehaus/groovy/syntax/Numbers.java     | 34 +++++++++++++++++---
 2 files changed, 31 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/a258a3d0/src/main/org/codehaus/groovy/antlr/AntlrParserPlugin.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/antlr/AntlrParserPlugin.java 
b/src/main/org/codehaus/groovy/antlr/AntlrParserPlugin.java
index a92366d..968ce47 100644
--- a/src/main/org/codehaus/groovy/antlr/AntlrParserPlugin.java
+++ b/src/main/org/codehaus/groovy/antlr/AntlrParserPlugin.java
@@ -2755,7 +2755,7 @@ public class AntlrParserPlugin extends ASTHelper 
implements ParserPlugin, Groovy
             case NUM_BIG_INT:
             case NUM_INT:
             case NUM_LONG:
-                ConstantExpression constantLongExpression = new 
ConstantExpression(Numbers.parseInteger("-" + text));
+                ConstantExpression constantLongExpression = new 
ConstantExpression(Numbers.parseInteger(unaryMinusExpr,"-" + text));
                 configureAST(constantLongExpression, unaryMinusExpr);
                 return constantLongExpression;
 
@@ -2795,7 +2795,7 @@ public class AntlrParserPlugin extends ASTHelper 
implements ParserPlugin, Groovy
 
     protected ConstantExpression integerExpression(AST node) {
         String text = node.getText();
-        Object number = Numbers.parseInteger(text);
+        Object number = Numbers.parseInteger(node, text);
         boolean keepPrimitive = number instanceof Integer || number instanceof 
Long;
         ConstantExpression constantExpression = new ConstantExpression(number, 
keepPrimitive);
         configureAST(constantExpression, node);

http://git-wip-us.apache.org/repos/asf/incubator-groovy/blob/a258a3d0/src/main/org/codehaus/groovy/syntax/Numbers.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/syntax/Numbers.java 
b/src/main/org/codehaus/groovy/syntax/Numbers.java
index 448012f..65a060f 100644
--- a/src/main/org/codehaus/groovy/syntax/Numbers.java
+++ b/src/main/org/codehaus/groovy/syntax/Numbers.java
@@ -18,6 +18,9 @@
  */
 package org.codehaus.groovy.syntax;
 
+import antlr.collections.AST;
+import org.codehaus.groovy.antlr.ASTRuntimeException;
+
 import java.math.BigInteger;
 import java.math.BigDecimal;
 
@@ -128,19 +131,33 @@ public class Numbers
     private static final BigDecimal MIN_FLOAT   = MAX_FLOAT.negate();
 
 
+    /**
+     *  Builds a Number from the given integer descriptor.  Creates the 
narrowest
+     *  type possible, or a specific type, if specified.
+     *
+     *  @param  text literal text to parse
+     *  @return instantiated Number object
+     *  @throws NumberFormatException if the number does not fit within the 
type
+     *          requested by the type specifier suffix (invalid numbers don't 
make
+     *          it here)
+     */
+    @Deprecated
+    public static Number parseInteger(String text ) {
+        return parseInteger(null, text);
+    }
 
    /**
     *  Builds a Number from the given integer descriptor.  Creates the 
narrowest
     *  type possible, or a specific type, if specified.
     *
+    *  @param  reportNode at node for error reporting in the parser
     *  @param  text literal text to parse
     *  @return instantiated Number object
     *  @throws NumberFormatException if the number does not fit within the type
     *          requested by the type specifier suffix (invalid numbers don't 
make
     *          it here)
     */
-
-    public static Number parseInteger( String text )
+    public static Number parseInteger(AST reportNode, String text )
     {
         // remove place holder underscore before starting
         text = text.replace("_", "");
@@ -213,9 +230,17 @@ public class Numbers
         switch (type)
         {
             case 'i':
-                return Integer.valueOf( value.intValue() );
+                if (radix==10 && reportNode != null && 
(value.compareTo(MAX_INTEGER) > 0 || value.compareTo(MIN_INTEGER) < 0) ) {
+                    throw new ASTRuntimeException(reportNode, "Number of value 
"+value+" does not fit in the range of int, but int was enforced.");
+                } else {
+                    return Integer.valueOf(value.intValue());
+                }
             case 'l':
-                return new Long( value.longValue() );
+                if (radix==10 && reportNode != null && 
(value.compareTo(MAX_LONG) > 0 || value.compareTo(MIN_LONG) < 0) ) {
+                    throw new ASTRuntimeException(reportNode, "Number of value 
"+value+" does not fit in the range of long, but long was enforced.");
+                } else {
+                    return new Long( value.longValue() );
+                }
             case 'g':
                 return value ;
             default:
@@ -245,7 +270,6 @@ public class Numbers
     *          requested by the type specifier suffix (invalid numbers don't 
make
     *          it here)
     */
-
     public static Number parseDecimal( String text )
     {
         text = text.replace("_", "");

Reply via email to