You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2022/07/12 21:51:09 UTC
[groovy] 03/03: retain primitive semantics for literal numbers
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 1942684f645a30969e4f94bdfb7112e4e326dd92
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Mon Jul 11 15:29:21 2022 -0500
retain primitive semantics for literal numbers
---
.../apache/groovy/parser/antlr4/AstBuilder.java | 91 +++++++++-------------
.../classgen/asm/sc/BugsStaticCompileTest.groovy | 2 +-
2 files changed, 38 insertions(+), 55 deletions(-)
diff --git a/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java b/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
index 801d0dd1cb..a268e5bcc7 100644
--- a/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
+++ b/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
@@ -2302,7 +2302,7 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> {
if (classNode.isInterface()) {
if (!asBoolean(initialValue)) {
- initialValue = !asBoolean(defaultValue) ? null : new ConstantExpression(defaultValue);
+ initialValue = !asBoolean(defaultValue) ? null : new ConstantExpression(defaultValue, true);
}
modifiers |= Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL;
@@ -3109,9 +3109,8 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> {
public ConstantExpression visitStringLiteral(final StringLiteralContext ctx) {
String text = parseStringLiteral(ctx.StringLiteral().getText());
- ConstantExpression constantExpression = new ConstantExpression(text, true);
- constantExpression.putNodeMetaData(IS_STRING, true);
-
+ ConstantExpression constantExpression = new ConstantExpression(text);
+ constantExpression.putNodeMetaData(IS_STRING, Boolean.TRUE);
return configureAST(constantExpression, ctx);
}
@@ -3257,56 +3256,44 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> {
@Override
public Expression visitUnaryAddExprAlt(final UnaryAddExprAltContext ctx) {
- ExpressionContext expressionCtx = ctx.expression();
- Expression expression = (Expression) this.visit(expressionCtx);
-
+ Expression expression = (Expression) this.visit(ctx.expression());
switch (ctx.op.getType()) {
- case ADD: {
- if (isNonStringConstantOutsideParentheses(expression)) {
- return configureAST(expression, ctx);
- }
-
- return configureAST(new UnaryPlusExpression(expression), ctx);
+ case ADD:
+ if (this.isNonStringConstantOutsideParentheses(expression)) {
+ return configureAST(expression, ctx);
}
- case SUB: {
- if (isNonStringConstantOutsideParentheses(expression)) {
- ConstantExpression constantExpression = (ConstantExpression) expression;
-
- try {
- String integerLiteralText = constantExpression.getNodeMetaData(INTEGER_LITERAL_TEXT);
- if (null != integerLiteralText) {
-
- ConstantExpression result = new ConstantExpression(Numbers.parseInteger(SUB_STR + integerLiteralText));
-
- this.numberFormatError = null; // reset the numberFormatError
-
- return configureAST(result, ctx);
- }
-
- String floatingPointLiteralText = constantExpression.getNodeMetaData(FLOATING_POINT_LITERAL_TEXT);
- if (null != floatingPointLiteralText) {
- ConstantExpression result = new ConstantExpression(Numbers.parseDecimal(SUB_STR + floatingPointLiteralText));
-
- this.numberFormatError = null; // reset the numberFormatError
-
- return configureAST(result, ctx);
- }
- } catch (Exception e) {
- throw createParsingFailedException(e.getMessage(), ctx);
+ return configureAST(new UnaryPlusExpression(expression), ctx);
+
+ case SUB:
+ if (this.isNonStringConstantOutsideParentheses(expression)) {
+ ConstantExpression constantExpression = (ConstantExpression) expression;
+ try {
+ String integerLiteralText = constantExpression.getNodeMetaData(INTEGER_LITERAL_TEXT);
+ if (integerLiteralText != null) {
+ ConstantExpression result = new ConstantExpression(Numbers.parseInteger(SUB_STR + integerLiteralText), true);
+ this.numberFormatError = null; // reset
+ return configureAST(result, ctx);
}
- throw new GroovyBugError("Failed to find the original number literal text: " + constantExpression.getText());
+ String floatingPointLiteralText = constantExpression.getNodeMetaData(FLOATING_POINT_LITERAL_TEXT);
+ if (floatingPointLiteralText != null) {
+ ConstantExpression result = new ConstantExpression(Numbers.parseDecimal(SUB_STR + floatingPointLiteralText), true);
+ this.numberFormatError = null; // reset
+ return configureAST(result, ctx);
+ }
+ } catch (Exception e) {
+ throw this.createParsingFailedException(e.getMessage(), ctx);
}
-
- return configureAST(new UnaryMinusExpression(expression), ctx);
+ throw new GroovyBugError("Failed to find the original number literal text: " + constantExpression.getText());
}
+ return configureAST(new UnaryMinusExpression(expression), ctx);
- case INC:
- case DEC:
- return configureAST(new PrefixExpression(this.createGroovyToken(ctx.op), expression), ctx);
+ case INC:
+ case DEC:
+ return configureAST(new PrefixExpression(this.createGroovyToken(ctx.op), expression), ctx);
- default:
- throw createParsingFailedException("Unsupported unary operation: " + ctx.getText(), ctx);
+ default:
+ throw this.createParsingFailedException("Unsupported unary operation: " + ctx.getText(), ctx);
}
}
@@ -3902,7 +3889,6 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> {
@Override
public ConstantExpression visitIntegerLiteralAlt(final IntegerLiteralAltContext ctx) {
String text = ctx.IntegerLiteral().getText();
-
Number num = null;
try {
num = Numbers.parseInteger(text);
@@ -3910,17 +3896,15 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> {
this.numberFormatError = tuple(ctx, e);
}
- ConstantExpression constantExpression = new ConstantExpression(num, !text.startsWith(SUB_STR));
- constantExpression.putNodeMetaData(IS_NUMERIC, Boolean.TRUE);
+ ConstantExpression constantExpression = new ConstantExpression(num, true);
constantExpression.putNodeMetaData(INTEGER_LITERAL_TEXT, text);
-
+ constantExpression.putNodeMetaData(IS_NUMERIC, Boolean.TRUE);
return configureAST(constantExpression, ctx);
}
@Override
public ConstantExpression visitFloatingPointLiteralAlt(final FloatingPointLiteralAltContext ctx) {
String text = ctx.FloatingPointLiteral().getText();
-
Number num = null;
try {
num = Numbers.parseDecimal(text);
@@ -3928,10 +3912,9 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> {
this.numberFormatError = tuple(ctx, e);
}
- ConstantExpression constantExpression = new ConstantExpression(num, !text.startsWith(SUB_STR));
- constantExpression.putNodeMetaData(IS_NUMERIC, Boolean.TRUE);
+ ConstantExpression constantExpression = new ConstantExpression(num, true);
constantExpression.putNodeMetaData(FLOATING_POINT_LITERAL_TEXT, text);
-
+ constantExpression.putNodeMetaData(IS_NUMERIC, Boolean.TRUE);
return configureAST(constantExpression, ctx);
}
diff --git a/src/test/org/codehaus/groovy/classgen/asm/sc/BugsStaticCompileTest.groovy b/src/test/org/codehaus/groovy/classgen/asm/sc/BugsStaticCompileTest.groovy
index c78f02a04a..b608b6879c 100644
--- a/src/test/org/codehaus/groovy/classgen/asm/sc/BugsStaticCompileTest.groovy
+++ b/src/test/org/codehaus/groovy/classgen/asm/sc/BugsStaticCompileTest.groovy
@@ -1355,7 +1355,7 @@ println someInt
class Foo {
long rankOrderingOrId
void setRankOrderingOrId(long rankOrderingOrId) {
- this.rankOrderingOrId = rankOrderingOrId < 0 ? -1L : rankOrderingOrId
+ this.rankOrderingOrId = rankOrderingOrId < 0 ? -1 : rankOrderingOrId
}
}
def f = new Foo()