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 2019/09/28 07:24:40 UTC

[groovy] branch master updated (10c40d4 -> fb23266)

This is an automated email from the ASF dual-hosted git repository.

paulk pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git.


    from 10c40d4  Minor refactoring: use new API of `CurlyCountingGroovyLexer`
     new 9874a32  Numbers class: prepare for removal of antlr2
     new fb23266  Numbers class: remove antlr2 code

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../codehaus/groovy/antlr/AntlrParserPlugin.java   |  5 +-
 .../java/org/codehaus/groovy/syntax/Numbers.java   | 69 ++++++----------------
 .../apache/groovy/parser/antlr4/AstBuilder.java    |  4 +-
 3 files changed, 23 insertions(+), 55 deletions(-)


[groovy] 01/02: Numbers class: prepare for removal of antlr2

Posted by pa...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 9874a326e2c5af94d31a4244c4af8c1fb3b5734f
Author: Paul King <pa...@asert.com.au>
AuthorDate: Sat Sep 28 17:12:07 2019 +1000

    Numbers class: prepare for removal of antlr2
---
 .../codehaus/groovy/antlr/AntlrParserPlugin.java   |   1 +
 .../java/org/codehaus/groovy/syntax/Numbers.java   | 113 +++++++++++++++++----
 .../apache/groovy/parser/antlr4/AstBuilder.java    |   4 +-
 3 files changed, 94 insertions(+), 24 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/antlr/AntlrParserPlugin.java b/src/main/java/org/codehaus/groovy/antlr/AntlrParserPlugin.java
index e3f6abb..f6330c0 100644
--- a/src/main/java/org/codehaus/groovy/antlr/AntlrParserPlugin.java
+++ b/src/main/java/org/codehaus/groovy/antlr/AntlrParserPlugin.java
@@ -136,6 +136,7 @@ import static org.codehaus.groovy.ast.tools.GeneralUtils.nullX;
 /**
  * A parser plugin which adapts the JSR Antlr Parser to the Groovy runtime
  */
+@Deprecated
 public class AntlrParserPlugin extends ASTHelper implements ParserPlugin, GroovyTokenTypes {
 
     private static class AnonymousInnerClassCarrier extends Expression {
diff --git a/src/main/java/org/codehaus/groovy/syntax/Numbers.java b/src/main/java/org/codehaus/groovy/syntax/Numbers.java
index be43e69..cc9231a 100644
--- a/src/main/java/org/codehaus/groovy/syntax/Numbers.java
+++ b/src/main/java/org/codehaus/groovy/syntax/Numbers.java
@@ -32,7 +32,6 @@ public class Numbers {
     //---------------------------------------------------------------------------
     // LEXING SUPPORT
 
-
     /**
      * Returns true if the specified character is a base-10 digit.
      */
@@ -40,7 +39,6 @@ public class Numbers {
         return c >= '0' && c <= '9';
     }
 
-
     /**
      * Returns true if the specific character is a base-8 digit.
      */
@@ -48,7 +46,6 @@ public class Numbers {
         return c >= '0' && c <= '7';
     }
 
-
     /**
      * Returns true if the specified character is a base-16 digit.
      */
@@ -56,7 +53,6 @@ public class Numbers {
         return isDigit(c) || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f');
     }
 
-
     /**
      * Returns true if the specified character is a valid type specifier
      * for a numeric value.
@@ -87,11 +83,9 @@ public class Numbers {
         return false;
     }
 
-
     //---------------------------------------------------------------------------
     // PARSING SUPPORT
 
-
     private static final BigInteger MAX_LONG = BigInteger.valueOf(Long.MAX_VALUE);
     private static final BigInteger MIN_LONG = BigInteger.valueOf(Long.MIN_VALUE);
 
@@ -104,20 +98,102 @@ public class Numbers {
     private static final BigDecimal MAX_FLOAT = new BigDecimal(String.valueOf(Float.MAX_VALUE));
     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)
+     * @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);
+        String text1 = text;
+        // remove place holder underscore before starting
+        text1 = text1.replace("_", "");
+
+        char c = ' ';
+        int length = text1.length();
+
+        //
+        // Strip off the sign, if present
+
+        boolean negative = false;
+        if ((c = text1.charAt(0)) == '-' || c == '+') {
+            negative = (c == '-');
+            text1 = text1.substring(1, length);
+            length -= 1;
+        }
+
+        //
+        // Determine radix (default is 10).
+
+        int radix = 10;
+        if (text1.charAt(0) == '0' && length > 1) {
+            c = text1.charAt(1);
+            if (c == 'X' || c == 'x') {
+                radix = 16;
+                text1 = text1.substring(2, length);
+                length -= 2;
+            } else if (c == 'B' || c == 'b') {
+                radix = 2;
+                text1 = text1.substring(2, length);
+                length -= 2;
+            } else {
+                radix = 8;
+            }
+        }
+
+        //
+        // Strip off any type specifier and convert it to lower
+        // case, if present.
+
+        char type = 'x';  // pick best fit
+        if (isNumericTypeSpecifier(text1.charAt(length - 1), false)) {
+            type = Character.toLowerCase(text1.charAt(length - 1));
+            text1 = text1.substring(0, length - 1);
+
+            length -= 1;
+        }
+
+        //
+        // Add the sign back, if necessary
+
+        if (negative) {
+            text1 = "-" + text1;
+        }
+
+        //
+        // Build the specified type or, if no type was specified, the
+        // smallest type in which the number will fit.
+
+        BigInteger value = new BigInteger(text1, radix);
+
+        switch (type) {
+            case 'i':
+                if (radix == 10 && (value.compareTo(MAX_INTEGER) > 0 || value.compareTo(MIN_INTEGER) < 0)) {
+                    throw new NumberFormatException("Number of value " + value + " does not fit in the range of int, but int was enforced.");
+                } else {
+                    return value.intValue();
+                }
+            case 'l':
+                if (radix == 10 && (value.compareTo(MAX_LONG) > 0 || value.compareTo(MIN_LONG) < 0)) {
+                    throw new NumberFormatException("Number of value " + value + " does not fit in the range of long, but long was enforced.");
+                } else {
+                    return value.longValue();
+                }
+            case 'g':
+                return value;
+            default:
+                // If not specified, we will return the narrowest possible
+                // of Integer, Long, and BigInteger.
+                if (value.compareTo(MAX_INTEGER) <= 0 && value.compareTo(MIN_INTEGER) >= 0) {
+                    return value.intValue();
+                } else if (value.compareTo(MAX_LONG) <= 0 && value.compareTo(MIN_LONG) >= 0) {
+                    return value.longValue();
+                }
+                return value;
+        }
     }
 
     /**
@@ -127,10 +203,10 @@ public class Numbers {
      * @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)
+     * @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(AST reportNode, String text) {
         // remove place holder underscore before starting
         text = text.replace("_", "");
@@ -138,7 +214,6 @@ public class Numbers {
         char c = ' ';
         int length = text.length();
 
-
         //
         // Strip off the sign, if present
 
@@ -149,7 +224,6 @@ public class Numbers {
             length -= 1;
         }
 
-
         //
         // Determine radix (default is 10).
 
@@ -169,7 +243,6 @@ public class Numbers {
             }
         }
 
-
         //
         // Strip off any type specifier and convert it to lower
         // case, if present.
@@ -182,7 +255,6 @@ public class Numbers {
             length -= 1;
         }
 
-
         //
         // Add the sign back, if necessary
 
@@ -190,7 +262,6 @@ public class Numbers {
             text = "-" + text;
         }
 
-
         //
         // Build the specified type or, if no type was specified, the
         // smallest type in which the number will fit.
@@ -224,7 +295,6 @@ public class Numbers {
         }
     }
 
-
     /**
      * Builds a Number from the given decimal descriptor.  Uses BigDecimal,
      * unless, Double or Float is requested.
@@ -239,7 +309,6 @@ public class Numbers {
         text = text.replace("_", "");
         int length = text.length();
 
-
         //
         // Strip off any type specifier and convert it to lower
         // case, if present.
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 ee109cb..f102184 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
@@ -2837,7 +2837,7 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
                         String integerLiteralText = constantExpression.getNodeMetaData(INTEGER_LITERAL_TEXT);
                         if (null != integerLiteralText) {
 
-                            ConstantExpression result = new ConstantExpression(Numbers.parseInteger(null, SUB_STR + integerLiteralText));
+                            ConstantExpression result = new ConstantExpression(Numbers.parseInteger(SUB_STR + integerLiteralText));
 
                             this.numberFormatError = null; // reset the numberFormatError
 
@@ -3471,7 +3471,7 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> implements Groov
 
         Number num = null;
         try {
-            num = Numbers.parseInteger(null, text);
+            num = Numbers.parseInteger(text);
         } catch (Exception e) {
             this.numberFormatError = tuple(ctx, e);
         }


[groovy] 02/02: Numbers class: remove antlr2 code

Posted by pa...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit fb232669a173a33a0b52a40e037a6caeb4781151
Author: Paul King <pa...@asert.com.au>
AuthorDate: Sat Sep 28 17:24:08 2019 +1000

    Numbers class: remove antlr2 code
---
 .../codehaus/groovy/antlr/AntlrParserPlugin.java   |   4 +-
 .../java/org/codehaus/groovy/syntax/Numbers.java   | 102 ---------------------
 2 files changed, 2 insertions(+), 104 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/antlr/AntlrParserPlugin.java b/src/main/java/org/codehaus/groovy/antlr/AntlrParserPlugin.java
index f6330c0..8e8fe06 100644
--- a/src/main/java/org/codehaus/groovy/antlr/AntlrParserPlugin.java
+++ b/src/main/java/org/codehaus/groovy/antlr/AntlrParserPlugin.java
@@ -2847,7 +2847,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(unaryMinusExpr,"-" + text));
+                ConstantExpression constantLongExpression = new ConstantExpression(Numbers.parseInteger("-" + text));
                 configureAST(constantLongExpression, unaryMinusExpr);
                 return constantLongExpression;
 
@@ -2887,7 +2887,7 @@ public class AntlrParserPlugin extends ASTHelper implements ParserPlugin, Groovy
 
     protected ConstantExpression integerExpression(AST node) {
         String text = node.getText();
-        Object number = Numbers.parseInteger(node, text);
+        Object number = Numbers.parseInteger(text);
         boolean keepPrimitive = number instanceof Integer || number instanceof Long;
         ConstantExpression constantExpression = new ConstantExpression(number, keepPrimitive);
         configureAST(constantExpression, node);
diff --git a/src/main/java/org/codehaus/groovy/syntax/Numbers.java b/src/main/java/org/codehaus/groovy/syntax/Numbers.java
index cc9231a..d033def 100644
--- a/src/main/java/org/codehaus/groovy/syntax/Numbers.java
+++ b/src/main/java/org/codehaus/groovy/syntax/Numbers.java
@@ -18,9 +18,6 @@
  */
 package org.codehaus.groovy.syntax;
 
-import antlr.collections.AST;
-import org.codehaus.groovy.antlr.ASTRuntimeException;
-
 import java.math.BigDecimal;
 import java.math.BigInteger;
 
@@ -197,105 +194,6 @@ public class Numbers {
     }
 
     /**
-     * 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)
-     */
-    @Deprecated
-    public static Number parseInteger(AST reportNode, String text) {
-        // remove place holder underscore before starting
-        text = text.replace("_", "");
-
-        char c = ' ';
-        int length = text.length();
-
-        //
-        // Strip off the sign, if present
-
-        boolean negative = false;
-        if ((c = text.charAt(0)) == '-' || c == '+') {
-            negative = (c == '-');
-            text = text.substring(1, length);
-            length -= 1;
-        }
-
-        //
-        // Determine radix (default is 10).
-
-        int radix = 10;
-        if (text.charAt(0) == '0' && length > 1) {
-            c = text.charAt(1);
-            if (c == 'X' || c == 'x') {
-                radix = 16;
-                text = text.substring(2, length);
-                length -= 2;
-            } else if (c == 'B' || c == 'b') {
-                radix = 2;
-                text = text.substring(2, length);
-                length -= 2;
-            } else {
-                radix = 8;
-            }
-        }
-
-        //
-        // Strip off any type specifier and convert it to lower
-        // case, if present.
-
-        char type = 'x';  // pick best fit
-        if (isNumericTypeSpecifier(text.charAt(length - 1), false)) {
-            type = Character.toLowerCase(text.charAt(length - 1));
-            text = text.substring(0, length - 1);
-
-            length -= 1;
-        }
-
-        //
-        // Add the sign back, if necessary
-
-        if (negative) {
-            text = "-" + text;
-        }
-
-        //
-        // Build the specified type or, if no type was specified, the
-        // smallest type in which the number will fit.
-
-        BigInteger value = new BigInteger(text, radix);
-
-        switch (type) {
-            case 'i':
-                if (radix == 10 && (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 value.intValue();
-                }
-            case 'l':
-                if (radix == 10 && (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 value.longValue();
-                }
-            case 'g':
-                return value;
-            default:
-                // If not specified, we will return the narrowest possible
-                // of Integer, Long, and BigInteger.
-                if (value.compareTo(MAX_INTEGER) <= 0 && value.compareTo(MIN_INTEGER) >= 0) {
-                    return value.intValue();
-                } else if (value.compareTo(MAX_LONG) <= 0 && value.compareTo(MIN_LONG) >= 0) {
-                    return value.longValue();
-                }
-                return value;
-        }
-    }
-
-    /**
      * Builds a Number from the given decimal descriptor.  Uses BigDecimal,
      * unless, Double or Float is requested.
      *