Support decimal constants with trailing [dD] in painless (#21412)

This adds support to painless for decimal constants with trailing `d` or
`D` to make it compatible with Java. It already supported integer
constants with a trailing `d` or `D` but this adds tests for it.

Closes #21116
This commit is contained in:
Nik Everett 2016-11-12 11:08:39 -05:00 committed by GitHub
parent a26b5a113c
commit 2a328034ef
5 changed files with 40 additions and 28 deletions

View File

@ -103,7 +103,7 @@ AUSH: '>>>=';
OCTAL: '0' [0-7]+ [lL]?;
HEX: '0' [xX] [0-9a-fA-F]+ [lL]?;
INTEGER: ( '0' | [1-9] [0-9]* ) [lLfFdD]?;
DECIMAL: ( '0' | [1-9] [0-9]* ) (DOT [0-9]+)? ( [eE] [+\-]? [0-9]+ )? [fF]?;
DECIMAL: ( '0' | [1-9] [0-9]* ) (DOT [0-9]+)? ( [eE] [+\-]? [0-9]+ )? [fFdD]?;
STRING: ( '"' ( '\\"' | '\\\\' | ~[\\"] )*? '"' ) | ( '\'' ( '\\\'' | '\\\\' | ~[\\"] )*? '\'' );
REGEX: '/' ( ~('/' | '\n') | '\\' ~'\n' )+ '/' [cilmsUux]* { SlashStrategy.slashIsRegex(this) }?;

View File

@ -204,21 +204,21 @@ class PainlessLexer extends Lexer {
"~@\u0080A\u0082B\u0084C\u0086D\u0088E\u008aF\u008cG\u008eH\u0090I\u0092"+
"J\u0094K\u0096L\u0098M\u009aN\u009cO\u009eP\u00a0Q\u00a2R\u00a4S\u00a6"+
"T\4\2\3\24\5\2\13\f\17\17\"\"\4\2\f\f\17\17\3\2\629\4\2NNnn\4\2ZZzz\5"+
"\2\62;CHch\3\2\63;\3\2\62;\b\2FFHHNNffhhnn\4\2GGgg\4\2--//\4\2HHhh\4\2"+
"$$^^\4\2\f\f\61\61\3\2\f\f\t\2WWeekknouuwwzz\5\2C\\aac|\6\2\62;C\\aac"+
"|\u026b\2\4\3\2\2\2\2\6\3\2\2\2\2\b\3\2\2\2\2\n\3\2\2\2\2\f\3\2\2\2\2"+
"\16\3\2\2\2\2\20\3\2\2\2\2\22\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\2\2\n\u00ca\3\2\2\2\f\u00cc\3\2\2"+

View File

@ -27,7 +27,7 @@ import org.antlr.v4.runtime.TokenSource;
import org.antlr.v4.runtime.misc.Pair;
/**
* Token factory that preseres that last non-whitespace token so you can do token level lookbehind in the lexer.
* Token factory that preserves that last non-whitespace token so you can do token level lookbehind in the lexer.
*/
public class StashingTokenFactory<T extends Token> implements TokenFactory<T> {
private final TokenFactory<T> delegate;

View File

@ -58,8 +58,12 @@ public final class EDecimal extends AExpression {
throw createError(new IllegalArgumentException("Invalid float constant [" + value + "]."));
}
} else {
String toParse = value;
if (toParse.endsWith("d") || value.endsWith("D")) {
toParse = toParse.substring(0, value.length() - 1);
}
try {
constant = Double.parseDouble(value);
constant = Double.parseDouble(toParse);
actual = Definition.DOUBLE_TYPE;
} catch (NumberFormatException exception) {
throw createError(new IllegalArgumentException("Invalid double constant [" + value + "]."));

View File

@ -27,16 +27,24 @@ public class BasicExpressionTests extends ScriptTestCase {
/** simple tests returning a constant value */
public void testReturnConstant() {
assertEquals(5, exec("return 5;"));
assertEquals(7L, exec("return 7L;"));
assertEquals(7.0, exec("return 7.0;"));
assertEquals(32.0F, exec("return 32.0F;"));
assertEquals((byte)255, exec("return (byte)255;"));
assertEquals((short)5, exec("return (short)5;"));
assertEquals("string", exec("return \"string\";"));
assertEquals(true, exec("return true;"));
assertEquals(false, exec("return false;"));
assertNull(exec("return null;"));
assertEquals(5, exec("return 5"));
assertEquals(6L, exec("return 6l"));
assertEquals(7L, exec("return 7L"));
assertEquals(7.0d, exec("return 7.0"));
assertEquals(18.0d, exec("return 18d"));
assertEquals(19.0d, exec("return 19.0d"));
assertEquals(20.0d, exec("return 20D"));
assertEquals(21.0d, exec("return 21.0D"));
assertEquals(32.0F, exec("return 32.0f"));
assertEquals(33.0F, exec("return 33f"));
assertEquals(34.0F, exec("return 34.0F"));
assertEquals(35.0F, exec("return 35F"));
assertEquals((byte)255, exec("return (byte)255"));
assertEquals((short)5, exec("return (short)5"));
assertEquals("string", exec("return \"string\""));
assertEquals(true, exec("return true"));
assertEquals(false, exec("return false"));
assertNull(exec("return null"));
}
public void testReturnConstantChar() {