henrib commented on code in PR #409:
URL: https://github.com/apache/commons-jexl/pull/409#discussion_r3766406888
##########
src/main/java/org/apache/commons/jexl3/parser/Parser.jjt:
##########
@@ -107,6 +107,27 @@ public final class Parser extends JexlParser
jjtree.reset();
}
}
+
+ /**
+ * Checks whether a token image is composed only of decimal digits.
+ * <p>Used to recognize a floating point literal with an omitted leading
+ * zero (e.g. {@code .1}) that is tokenized as {@code DOT
DOT_IDENTIFIER}.</p>
+ *
+ * @param image the token image
+ * @return true if every character is a decimal digit
+ */
+ private static boolean isAllDigits(final String image) {
Review Comment:
The method might move to JexlParser since all helpers live there.
##########
src/test/java/org/apache/commons/jexl3/ArithmeticTest.java:
##########
@@ -2341,4 +2341,32 @@ void setOptions(final JexlOptions options) {
this.options = options;
}
}
+
+ @Test void testLeadingDotFloatLiteral() {
+ final JexlEngine jexl = new JexlBuilder().create();
+ final JexlContext jc = new MapContext();
+ // the exact case from JEXL-411
+ assertEquals(2.2d, jexl.createExpression("(1+.1)*2").evaluate(jc));
+ // the regression case that already worked
+ assertEquals(2.2d, jexl.createExpression("(1+0.1)*2").evaluate(jc));
+ // standalone and compound leading-dot literals
+ assertEquals(0.1d, jexl.createExpression(".1").evaluate(jc));
+ assertEquals(1.0d, jexl.createExpression(".5 + .5").evaluate(jc));
+ assertEquals(0.55d, jexl.createExpression(".55").evaluate(jc));
+ assertEquals(0.1d, jexl.createExpression("-.1 + .2").evaluate(jc));
+ assertEquals(-0.1d, jexl.createExpression(".1 + -.2").evaluate(jc));
+ assertEquals(0.1d, jexl.createExpression("-.1+.2").evaluate(jc));
+ assertEquals(-0.1d, jexl.createExpression(".1+-.2").evaluate(jc));
+ assertEquals(1.4d, jexl.createExpression(".5 - -.9").evaluate(jc));
+ assertEquals(0.4d, jexl.createExpression("-.5 - -.9").evaluate(jc));
+ assertEquals(-1.4d, jexl.createExpression("-.5+-.9").evaluate(jc));
+ // unary handling
+ assertEquals(-0.1d, jexl.createExpression("-.1").evaluate(jc));
+ // property / index access via a dot must keep working (not parsed as
a float)
+ final List<Object> array = new java.util.ArrayList<>();
+ array.add("zero");
+ array.add("one");
+ jc.set("array", array);
+ assertEquals("one", jexl.createExpression("array.1").evaluate(jc));
Review Comment:
Add the 0/zero test.
##########
src/main/java/org/apache/commons/jexl3/parser/Parser.jjt:
##########
@@ -1064,6 +1085,9 @@ void FloatLiteral() #NumberLiteral:
{
t=<FLOAT_LITERAL>
{ jjtThis.setReal(t.image); }
+ |
+ <DOT> t=<DOT_IDENTIFIER>
Review Comment:
Nice trick :-)
##########
src/test/java/org/apache/commons/jexl3/ArithmeticTest.java:
##########
@@ -2341,4 +2341,32 @@ void setOptions(final JexlOptions options) {
this.options = options;
}
}
+
+ @Test void testLeadingDotFloatLiteral() {
+ final JexlEngine jexl = new JexlBuilder().create();
+ final JexlContext jc = new MapContext();
+ // the exact case from JEXL-411
+ assertEquals(2.2d, jexl.createExpression("(1+.1)*2").evaluate(jc));
+ // the regression case that already worked
+ assertEquals(2.2d, jexl.createExpression("(1+0.1)*2").evaluate(jc));
+ // standalone and compound leading-dot literals
+ assertEquals(0.1d, jexl.createExpression(".1").evaluate(jc));
+ assertEquals(1.0d, jexl.createExpression(".5 + .5").evaluate(jc));
+ assertEquals(0.55d, jexl.createExpression(".55").evaluate(jc));
+ assertEquals(0.1d, jexl.createExpression("-.1 + .2").evaluate(jc));
+ assertEquals(-0.1d, jexl.createExpression(".1 + -.2").evaluate(jc));
+ assertEquals(0.1d, jexl.createExpression("-.1+.2").evaluate(jc));
+ assertEquals(-0.1d, jexl.createExpression(".1+-.2").evaluate(jc));
+ assertEquals(1.4d, jexl.createExpression(".5 - -.9").evaluate(jc));
+ assertEquals(0.4d, jexl.createExpression("-.5 - -.9").evaluate(jc));
+ assertEquals(-1.4d, jexl.createExpression("-.5+-.9").evaluate(jc));
+ // unary handling
+ assertEquals(-0.1d, jexl.createExpression("-.1").evaluate(jc));
Review Comment:
Add the "+.1" test for completeness.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]