From bde639951a073e9da147d3f0595a1e54f4d2414b Mon Sep 17 00:00:00 2001 From: "moandji.ezana" Date: Fri, 23 Jan 2015 16:06:06 +0200 Subject: [PATCH] Exponents no longer handled by Parboiled --- .../moandjiezana/toml/ExponentConverter.java | 57 +++++++++++++++++-- .../com/moandjiezana/toml/ValueParser.java | 4 -- 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/moandjiezana/toml/ExponentConverter.java b/src/main/java/com/moandjiezana/toml/ExponentConverter.java index 5f0cf69..327067a 100644 --- a/src/main/java/com/moandjiezana/toml/ExponentConverter.java +++ b/src/main/java/com/moandjiezana/toml/ExponentConverter.java @@ -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])); } diff --git a/src/main/java/com/moandjiezana/toml/ValueParser.java b/src/main/java/com/moandjiezana/toml/ValueParser.java index 2357101..4030fe6 100644 --- a/src/main/java/com/moandjiezana/toml/ValueParser.java +++ b/src/main/java/com/moandjiezana/toml/ValueParser.java @@ -22,10 +22,6 @@ class ValueParser extends BaseParser> { 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()))); }