Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_6_X e5d950c10 -> baa108488


Validate number format

(cherry picked from commit 1edfb97)


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

Branch: refs/heads/GROOVY_2_6_X
Commit: baa108488e2687356692eaddd4282afa2fe6de13
Parents: e5d950c
Author: sunlan <sun...@apache.org>
Authored: Sun Aug 6 23:04:36 2017 +0800
Committer: sunlan <sun...@apache.org>
Committed: Sun Aug 6 23:11:13 2017 +0800

----------------------------------------------------------------------
 .../apache/groovy/parser/antlr4/AstBuilder.java | 45 ++++++++++++++++----
 .../parser/antlr4/GroovyParserTest.groovy       |  4 ++
 .../groovy/parser/antlr4/SyntaxErrorTest.groovy |  5 +++
 .../src/test/resources/core/Number_01x.groovy   | 24 +++++++++++
 .../src/test/resources/fail/Number_01x.groovy   | 19 +++++++++
 .../src/test/resources/fail/Number_02x.groovy   | 19 +++++++++
 6 files changed, 107 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/baa10848/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 5c5f966..bbf7665 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
@@ -253,6 +253,10 @@ public class AstBuilder extends 
GroovyParserBaseVisitor<Object> implements Groov
 
         this.configureScriptClassNode();
 
+        if (null != this.numberFormatError) {
+            throw 
createParsingFailedException(this.numberFormatError.value.getMessage(), 
this.numberFormatError.key);
+        }
+
         return moduleNode;
     }
 
@@ -2523,16 +2527,22 @@ public class AstBuilder extends 
GroovyParserBaseVisitor<Object> implements Groov
             }
             case SUB: {
                 if (expression instanceof ConstantExpression && !insidePar) {
+                    this.numberFormatError = null; // reset the 
numberFormatError, try to parse the negative number
+
                     ConstantExpression constantExpression = 
(ConstantExpression) expression;
 
-                    String integerLiteralText = 
constantExpression.getNodeMetaData(INTEGER_LITERAL_TEXT);
-                    if (null != integerLiteralText) {
-                        return this.configureAST(new 
ConstantExpression(Numbers.parseInteger(null, SUB_STR + integerLiteralText)), 
ctx);
-                    }
+                    try {
+                        String integerLiteralText = 
constantExpression.getNodeMetaData(INTEGER_LITERAL_TEXT);
+                        if (null != integerLiteralText) {
+                            return this.configureAST(new 
ConstantExpression(Numbers.parseInteger(null, SUB_STR + integerLiteralText)), 
ctx);
+                        }
 
-                    String floatingPointLiteralText = 
constantExpression.getNodeMetaData(FLOATING_POINT_LITERAL_TEXT);
-                    if (null != floatingPointLiteralText) {
-                        return this.configureAST(new 
ConstantExpression(Numbers.parseDecimal(SUB_STR + floatingPointLiteralText)), 
ctx);
+                        String floatingPointLiteralText = 
constantExpression.getNodeMetaData(FLOATING_POINT_LITERAL_TEXT);
+                        if (null != floatingPointLiteralText) {
+                            return this.configureAST(new 
ConstantExpression(Numbers.parseDecimal(SUB_STR + floatingPointLiteralText)), 
ctx);
+                        }
+                    } catch (Exception e) {
+                        throw createParsingFailedException(e.getMessage(), 
ctx);
                     }
 
                     throw new GroovyBugError("Failed to find the original 
number literal text: " + constantExpression.getText());
@@ -3131,7 +3141,14 @@ public class AstBuilder extends 
GroovyParserBaseVisitor<Object> implements Groov
     public ConstantExpression visitIntegerLiteralAlt(IntegerLiteralAltContext 
ctx) {
         String text = ctx.IntegerLiteral().getText();
 
-        ConstantExpression constantExpression = new 
ConstantExpression(Numbers.parseInteger(null, text), !text.startsWith(SUB_STR));
+        Number num = null;
+        try {
+            num = Numbers.parseInteger(null, text);
+        } catch (Exception e) {
+            this.numberFormatError = new Pair<>(ctx, e);
+        }
+
+        ConstantExpression constantExpression = new ConstantExpression(num, 
!text.startsWith(SUB_STR));
         constantExpression.putNodeMetaData(IS_NUMERIC, true);
         constantExpression.putNodeMetaData(INTEGER_LITERAL_TEXT, text);
 
@@ -3142,7 +3159,14 @@ public class AstBuilder extends 
GroovyParserBaseVisitor<Object> implements Groov
     public ConstantExpression 
visitFloatingPointLiteralAlt(FloatingPointLiteralAltContext ctx) {
         String text = ctx.FloatingPointLiteral().getText();
 
-        ConstantExpression constantExpression = new 
ConstantExpression(Numbers.parseDecimal(text), !text.startsWith(SUB_STR));
+        Number num = null;
+        try {
+            num = Numbers.parseDecimal(text);
+        } catch (Exception e) {
+            this.numberFormatError = new Pair<>(ctx, e);
+        }
+
+        ConstantExpression constantExpression = new ConstantExpression(num, 
!text.startsWith(SUB_STR));
         constantExpression.putNodeMetaData(IS_NUMERIC, true);
         constantExpression.putNodeMetaData(FLOATING_POINT_LITERAL_TEXT, text);
 
@@ -4594,6 +4618,9 @@ public class AstBuilder extends 
GroovyParserBaseVisitor<Object> implements Groov
     private final Deque<ClassNode> classNodeStack = new ArrayDeque<>();
     private final Deque<List<InnerClassNode>> 
anonymousInnerClassesDefinedInMethodStack = new ArrayDeque<>();
     private int anonymousInnerClassCounter = 1;
+
+    private Pair<GroovyParserRuleContext, Exception> numberFormatError;
+
     private static final String QUESTION_STR = "?";
     private static final String DOT_STR = ".";
     private static final String SUB_STR = "-";

http://git-wip-us.apache.org/repos/asf/groovy/blob/baa10848/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
----------------------------------------------------------------------
diff --git 
a/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
 
b/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
index 696dab9..c0936dc 100644
--- 
a/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
+++ 
b/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
@@ -369,6 +369,10 @@ class GroovyParserTest extends GroovyTestCase {
         doRunAndTest('core/FieldDeclaration_01x.groovy');
     }
 
+    void "test groovy core - Number"() {
+        doRunAndTest('core/Number_01x.groovy');
+    }
+
     void "test groovy core - BUG"() {
         doRunAndTest('bugs/BUG-GROOVY-4757.groovy');
         doRunAndTest('bugs/BUG-GROOVY-5652.groovy');

http://git-wip-us.apache.org/repos/asf/groovy/blob/baa10848/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 b031654..f29c1e1 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
@@ -197,6 +197,11 @@ class SyntaxErrorTest extends GroovyTestCase {
         TestUtils.doRunAndShouldFail('fail/VarArgParameter_01x.groovy');
     }
 
+    void "test groovy core - Number"() {
+        TestUtils.doRunAndShouldFail('fail/Number_01x.groovy');
+        TestUtils.doRunAndShouldFail('fail/Number_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/baa10848/subprojects/parser-antlr4/src/test/resources/core/Number_01x.groovy
----------------------------------------------------------------------
diff --git 
a/subprojects/parser-antlr4/src/test/resources/core/Number_01x.groovy 
b/subprojects/parser-antlr4/src/test/resources/core/Number_01x.groovy
new file mode 100644
index 0000000..a63eb87
--- /dev/null
+++ b/subprojects/parser-antlr4/src/test/resources/core/Number_01x.groovy
@@ -0,0 +1,24 @@
+/*
+ *  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.
+ */
+def a = 2147483647I
+def b = -2147483648I
+def c = -2147483647I
+def d = 9223372036854775807L
+def e = -9223372036854775808L
+def f = -9223372036854775807L

http://git-wip-us.apache.org/repos/asf/groovy/blob/baa10848/subprojects/parser-antlr4/src/test/resources/fail/Number_01x.groovy
----------------------------------------------------------------------
diff --git 
a/subprojects/parser-antlr4/src/test/resources/fail/Number_01x.groovy 
b/subprojects/parser-antlr4/src/test/resources/fail/Number_01x.groovy
new file mode 100644
index 0000000..8559ec4
--- /dev/null
+++ b/subprojects/parser-antlr4/src/test/resources/fail/Number_01x.groovy
@@ -0,0 +1,19 @@
+/*
+ *  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.
+ */
+2147483648I

http://git-wip-us.apache.org/repos/asf/groovy/blob/baa10848/subprojects/parser-antlr4/src/test/resources/fail/Number_02x.groovy
----------------------------------------------------------------------
diff --git 
a/subprojects/parser-antlr4/src/test/resources/fail/Number_02x.groovy 
b/subprojects/parser-antlr4/src/test/resources/fail/Number_02x.groovy
new file mode 100644
index 0000000..4d7aa9d
--- /dev/null
+++ b/subprojects/parser-antlr4/src/test/resources/fail/Number_02x.groovy
@@ -0,0 +1,19 @@
+/*
+ *  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.
+ */
+9223372036854775808L

Reply via email to