mirror of
https://github.com/plexusorg/toml4j.git
synced 2025-02-11 11:40:27 +00:00
Removed regexes from BooleanParser and IntegerParser and using Parboiled
instead
This commit is contained in:
parent
cd7b7b68d9
commit
c841c4e1fe
4 changed files with 35 additions and 24 deletions
|
@ -2,6 +2,11 @@ package com.moandjiezana.toml;
|
|||
|
||||
import static com.moandjiezana.toml.ValueParserUtils.INVALID;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.parboiled.Parboiled;
|
||||
import org.parboiled.parserunners.BasicParseRunner;
|
||||
|
||||
class BooleanParser implements ValueParser {
|
||||
|
||||
static final BooleanParser BOOLEAN_PARSER = new BooleanParser();
|
||||
|
@ -13,12 +18,15 @@ class BooleanParser implements ValueParser {
|
|||
|
||||
@Override
|
||||
public Object parse(String s) {
|
||||
if (s.startsWith("true") && !ValueParserUtils.isComment(s.substring(4)) ||
|
||||
s.startsWith("false") && !ValueParserUtils.isComment(s.substring(5))) {
|
||||
StatementParser parser = Parboiled.createParser(StatementParser.class);
|
||||
|
||||
List<String> resultValue = new BasicParseRunner<List<String>>(parser.Boolean()).run(s).resultValue;
|
||||
|
||||
if (resultValue == null) {
|
||||
return INVALID;
|
||||
}
|
||||
|
||||
return s.startsWith("true") ? Boolean.TRUE : Boolean.FALSE;
|
||||
return Boolean.valueOf(resultValue.get(0));
|
||||
}
|
||||
|
||||
private BooleanParser() {}
|
||||
|
|
|
@ -1,28 +1,29 @@
|
|||
package com.moandjiezana.toml;
|
||||
|
||||
import static com.moandjiezana.toml.ValueParserUtils.isComment;
|
||||
import java.util.List;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import org.parboiled.Parboiled;
|
||||
import org.parboiled.parserunners.BasicParseRunner;
|
||||
|
||||
class IntegerParser implements ValueParser {
|
||||
|
||||
private static final Pattern INTEGER_REGEX = Pattern.compile("(-?[0-9]*)(.*)");
|
||||
static final IntegerParser INTEGER_PARSER = new IntegerParser();
|
||||
|
||||
@Override
|
||||
public boolean canParse(String s) {
|
||||
Matcher matcher = INTEGER_REGEX.matcher(s);
|
||||
|
||||
return matcher.matches() && isComment(matcher.group(2));
|
||||
StatementParser parser = Parboiled.createParser(StatementParser.class);
|
||||
return new BasicParseRunner<Object>(parser.Integer()).run(s).resultValue != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object parse(String s) {
|
||||
Matcher matcher = INTEGER_REGEX.matcher(s);
|
||||
matcher.matches();
|
||||
StatementParser parser = Parboiled.createParser(StatementParser.class);
|
||||
List<String> resultValue = new BasicParseRunner<List<String>>(parser.Integer()).run(s).resultValue;
|
||||
|
||||
return Long.valueOf(matcher.group(1));
|
||||
if (resultValue == null) {
|
||||
return ValueParserUtils.INVALID;
|
||||
}
|
||||
|
||||
return Long.valueOf(resultValue.get(0));
|
||||
}
|
||||
|
||||
private IntegerParser() {}
|
||||
|
|
|
@ -5,7 +5,6 @@ import java.util.List;
|
|||
|
||||
import org.parboiled.BaseParser;
|
||||
import org.parboiled.Rule;
|
||||
import org.parboiled.annotations.SuppressNode;
|
||||
|
||||
class StatementParser extends BaseParser<List<Object>> {
|
||||
|
||||
|
@ -14,7 +13,7 @@ class StatementParser extends BaseParser<List<Object>> {
|
|||
}
|
||||
|
||||
public Rule Table() {
|
||||
return Sequence('[', startList(), Sequence(OneOrMore(NoneOf("[]")), pushToken(match())), ']', endList(), FirstOf(EOI, Sequence(TestNot(']'), ANY)));
|
||||
return Sequence('[', startList(), Sequence(OneOrMore(NoneOf("[]")), pushToken(match())), ']', endList(), Comment());
|
||||
}
|
||||
|
||||
public Rule TableArray() {
|
||||
|
@ -22,7 +21,15 @@ class StatementParser extends BaseParser<List<Object>> {
|
|||
}
|
||||
|
||||
public Rule LiteralString() {
|
||||
return FirstOf(Sequence('\'', '\'', startList(), pushToken(""), endList()), Sequence('\'', OneOrMore(TestNot("'"), ANY), startList(), pushToken(match()) , '\'', endList(), FirstOf(EOI, OneOrMore(' ', Sequence('#', ZeroOrMore(ANY))))));
|
||||
return FirstOf(Sequence('\'', '\'', startList(), pushToken(""), endList()), Sequence('\'', OneOrMore(TestNot("'"), ANY), startList(), pushToken(match()) , '\'', endList(), Comment()));
|
||||
}
|
||||
|
||||
public Rule Boolean() {
|
||||
return Sequence(startList(), FirstOf("true", "false"), pushToken(match()), endList(), Comment());
|
||||
}
|
||||
|
||||
public Rule Integer() {
|
||||
return Sequence(startList(), Sequence(Sequence(Optional('-'), OneOrMore(CharRange('0', '9'))), pushToken(match())), endList(), Comment());
|
||||
}
|
||||
|
||||
Rule NonEmptyArray() {
|
||||
|
@ -40,10 +47,9 @@ class StatementParser extends BaseParser<List<Object>> {
|
|||
Rule OtherValue() {
|
||||
return Sequence(ZeroOrMore(NoneOf("],")), pushToken(match()));
|
||||
}
|
||||
|
||||
@SuppressNode
|
||||
|
||||
Rule Comment() {
|
||||
return OneOrMore(FirstOf(AnyOf("\t"), EOI));
|
||||
return FirstOf(EOI, OneOrMore(' ', Sequence('#', ZeroOrMore(ANY))));
|
||||
}
|
||||
|
||||
boolean startList() {
|
||||
|
|
|
@ -59,10 +59,6 @@ class TomlParser {
|
|||
String tableName = getTableName(line);
|
||||
if (tableName != null) {
|
||||
results.startTables(tableName);
|
||||
String afterTableName = line.substring(tableName.length() + 2);
|
||||
if (!isComment(afterTableName)) {
|
||||
results.errors.append("Invalid table definition: " + line + "\n\n");
|
||||
}
|
||||
} else {
|
||||
results.errors.append("Invalid table definition: " + line + "\n\n");
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue