Exponents no longer handled by Parboiled

This commit is contained in:
moandji.ezana 2015-01-23 16:06:06 +02:00
parent b6b0d733a5
commit bde639951a
2 changed files with 53 additions and 8 deletions

View file

@ -1,7 +1,5 @@
package com.moandjiezana.toml;
import static com.moandjiezana.toml.ValueConverterUtils.parse;
import static com.moandjiezana.toml.ValueConverterUtils.parser;
class ExponentConverter implements ValueConverter {
@ -9,12 +7,63 @@ class ExponentConverter implements ValueConverter {
@Override
public boolean canConvert(String s) {
return parse(parser().Exponent(), s) != null;
char[] chars = s.toCharArray();
boolean whitespace = false;
boolean exponent = false;
boolean signable = true;
boolean decimal = false;
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
if (Character.isDigit(c)) {
signable = false;
continue;
}
if (signable && (c == '+' || c == '-') && chars.length > i + 1 && Character.isDigit(chars[i + 1])) {
signable = false;
continue;
}
if (i > 0 && (c == 'E' || c == 'e')) {
signable = true;
exponent = true;
continue;
}
if (i > 0 && c == '.' && !decimal && !exponent) {
decimal = true;
continue;
}
if (Character.isWhitespace(c)) {
whitespace = true;
continue;
}
if (whitespace && c == '#') {
break;
}
return false;
}
return exponent && !signable;
}
@Override
public Object convert(String s) {
String[] exponentString = ((String) parse(parser().Exponent(), s).get(0)).split("[eE]");
if (s.startsWith("+")) {
s = s.substring(1);
}
int startOfComment = s.indexOf('#');
if (startOfComment > -1) {
s = s.substring(0, startOfComment).trim();
}
String[] exponentString = s.split("[eE]");
return Double.parseDouble(exponentString[0]) * Math.pow(10, Double.parseDouble(exponentString[1]));
}

View file

@ -22,10 +22,6 @@ class ValueParser extends BaseParser<List<Object>> {
return FirstOf(EmptyMultilineLiteralString(), Sequence("'''", startList(), Sequence(OneOrMore(TestNot("'''"), ANY), 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() {
return FirstOf(Array(), OneOrMore(TestNot(']'), FirstOf(StringToken(), Array(), ',', ' ', OtherValue())));
}