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