mirror of
https://github.com/plexusorg/toml4j.git
synced 2024-12-28 19:24:15 +00:00
Integers no longer handled by Parboiled
This commit is contained in:
parent
7380d74592
commit
b6b0d733a5
2 changed files with 35 additions and 19 deletions
|
@ -1,28 +1,52 @@
|
|||
package com.moandjiezana.toml;
|
||||
|
||||
import static com.moandjiezana.toml.ValueConverterUtils.parse;
|
||||
import static com.moandjiezana.toml.ValueConverterUtils.parser;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
class IntegerConverter implements ValueConverter {
|
||||
static final IntegerConverter INTEGER_PARSER = new IntegerConverter();
|
||||
|
||||
@Override
|
||||
public boolean canConvert(String s) {
|
||||
return parse(parser().Integer(), s) != null;
|
||||
char[] chars = s.toCharArray();
|
||||
boolean whitespace = false;
|
||||
|
||||
for (int i = 0; i < chars.length; i++) {
|
||||
char c = chars[i];
|
||||
|
||||
if (Character.isDigit(c)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (i == 0 && (c == '+' || c == '-')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (Character.isWhitespace(c)) {
|
||||
whitespace = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (whitespace && c == '#') {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(String s) {
|
||||
List<String> resultValue = parse(parser().Integer(), s);
|
||||
|
||||
String longString = resultValue.get(0);
|
||||
if (longString.startsWith("+")) {
|
||||
longString = longString.substring(1);
|
||||
if (s.startsWith("+")) {
|
||||
s = s.substring(1);
|
||||
}
|
||||
|
||||
return Long.valueOf(longString);
|
||||
int startOfComment = s.indexOf('#');
|
||||
if (startOfComment > -1) {
|
||||
s = s.substring(0, startOfComment).trim();
|
||||
}
|
||||
|
||||
return Long.valueOf(s);
|
||||
}
|
||||
|
||||
private IntegerConverter() {}
|
||||
|
|
|
@ -13,10 +13,6 @@ class ValueParser extends BaseParser<List<Object>> {
|
|||
public Rule Array() {
|
||||
return FirstOf(EmptyArray(), Sequence('[', startList(), OneOrMore(FirstOf(NonEmptyArray(), ' ', ',')), ']', endList()));
|
||||
}
|
||||
|
||||
public Rule Table() {
|
||||
return Sequence('[', startList(), Sequence(OneOrMore(NoneOf("[]")), pushToken(match())), ']', endList(), Comment());
|
||||
}
|
||||
|
||||
public Rule LiteralString() {
|
||||
return FirstOf(EmptyLiteralString(), Sequence('\'', OneOrMore(TestNot("'"), ANY), startList(), pushToken(match()) , '\'', endList(), Comment()));
|
||||
|
@ -26,10 +22,6 @@ class ValueParser extends BaseParser<List<Object>> {
|
|||
return FirstOf(EmptyMultilineLiteralString(), Sequence("'''", startList(), Sequence(OneOrMore(TestNot("'''"), ANY), pushToken(match())), "'''", endList(), Comment()));
|
||||
}
|
||||
|
||||
public Rule Integer() {
|
||||
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());
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue