Support for exponents and doubles with a plus sign

This commit is contained in:
moandji.ezana 2014-11-10 23:02:10 +02:00
parent 16e9fa16f0
commit 1390487d3f
5 changed files with 56 additions and 3 deletions

View file

@ -0,0 +1,21 @@
package com.moandjiezana.toml;
import static com.moandjiezana.toml.ValueConverterUtils.parse;
import static com.moandjiezana.toml.ValueConverterUtils.parser;
class ExponentConverter implements ValueConverter {
public static final ExponentConverter EXPONENT_PARSER = new ExponentConverter();
@Override
public boolean canConvert(String s) {
return parse(parser().Exponent(), s) != null;
}
@Override
public Object convert(String s) {
String[] exponentString = ((String) parse(parser().Exponent(), s).get(0)).split("[eE]");
return Math.pow(Double.parseDouble(exponentString[0]), Double.parseDouble(exponentString[1]));
}
}

View file

@ -6,7 +6,7 @@ import java.util.regex.Pattern;
class FloatConverter implements ValueConverter {
public static final FloatConverter FLOAT_PARSER = new FloatConverter();
private static final Pattern FLOAT_REGEX = Pattern.compile("(-?\\d+\\.\\d+)(.*)");
private static final Pattern FLOAT_REGEX = Pattern.compile("([+-]?\\d+\\.\\d+)(.*)");
@Override
public boolean canConvert(String s) {

View file

@ -3,6 +3,7 @@ package com.moandjiezana.toml;
import static com.moandjiezana.toml.ArrayConverter.ARRAY_PARSER;
import static com.moandjiezana.toml.BooleanConverter.BOOLEAN_PARSER;
import static com.moandjiezana.toml.DateConverter.DATE_PARSER;
import static com.moandjiezana.toml.ExponentConverter.EXPONENT_PARSER;
import static com.moandjiezana.toml.FloatConverter.FLOAT_PARSER;
import static com.moandjiezana.toml.IntegerConverter.INTEGER_PARSER;
import static com.moandjiezana.toml.LiteralStringConverter.LITERAL_STRING_PARSER;
@ -14,7 +15,7 @@ import static com.moandjiezana.toml.ValueConverterUtils.INVALID;
class ValueConverters {
private static final ValueConverter[] PARSERS = {
MULTILINE_STRING_PARSER, MULTILINE_LITERAL_STRING_CONVERTER, LITERAL_STRING_PARSER, STRING_PARSER, DATE_PARSER, INTEGER_PARSER, FLOAT_PARSER, BOOLEAN_PARSER, ARRAY_PARSER
MULTILINE_STRING_PARSER, MULTILINE_LITERAL_STRING_CONVERTER, LITERAL_STRING_PARSER, STRING_PARSER, DATE_PARSER, EXPONENT_PARSER, INTEGER_PARSER, FLOAT_PARSER, BOOLEAN_PARSER, ARRAY_PARSER
};
public Object convert(String value) {

View file

@ -48,7 +48,11 @@ class ValueParser extends BaseParser<List<Object>> {
}
public Rule Integer() {
return Sequence(startList(), Sequence(Sequence(Optional(AnyOf("+-")), OneOrMore(CharRange('0', '9'))), pushToken(match())), endList(), Comment());
return Sequence(startList(), Sequence(SignedNumber(), pushToken(match())), endList(), Comment());
}
public Rule Exponent() {
return Sequence(startList(), Sequence(Sequence(SignedNumber(), Optional(Sequence('.', Number())), FirstOf('e', 'E'), SignedNumber()), pushToken(match())), endList(), Comment());
}
Rule NonEmptyArray() {
@ -70,6 +74,14 @@ class ValueParser extends BaseParser<List<Object>> {
Rule EmptyArray() {
return Sequence('[', ']', startList(), endList());
}
Rule SignedNumber() {
return Sequence(Optional(AnyOf("+-")), Number());
}
Rule Number() {
return OneOrMore(CharRange('0', '9'));
}
Rule OtherValue() {
return Sequence(ZeroOrMore(NoneOf("],")), pushToken(match()));

View file

@ -158,6 +158,25 @@ public class TomlTest {
assertEquals(-5.25D, toml.getDouble("double").doubleValue(), 0.0);
}
@Test
public void should_get_double_with_a_plus_sign() throws Exception {
Toml toml = new Toml().parse("double = +5.25");
assertEquals(5.25D, toml.getDouble("double").doubleValue(), 0.0);
}
@Test
public void should_get_exponent() throws Exception {
Toml toml = new Toml().parse("lower_case = 1e6\nupper_case = 2E6\nwith_plus = 5e+22\nboth_plus = +5E+22\nnegative = -2E-2\nfractional = 6.626e-34");
assertEquals(Math.pow(1, 6), toml.getDouble("lower_case"), 0.0);
assertEquals(Math.pow(2, 6), toml.getDouble("upper_case"), 0.0);
assertEquals(Math.pow(5, 22), toml.getDouble("with_plus"), 0.0);
assertEquals(Math.pow(5, 22), toml.getDouble("both_plus"), 0.0);
assertEquals(Math.pow(-2, -2), toml.getDouble("negative"), 0.0);
assertEquals(Math.pow(6.626D, -34), toml.getDouble("fractional"), 0.0);
}
@Test
public void should_get_table() throws Exception {