ValueConverterUtils.parse() and ValueConverterUtils.parser() make it

easier to use Parboiled.
This commit is contained in:
moandji.ezana 2014-08-13 16:01:31 +02:00
parent 2b8bf136a9
commit b6af2c6d48
6 changed files with 34 additions and 36 deletions

View file

@ -1,14 +1,12 @@
package com.moandjiezana.toml;
import static com.moandjiezana.toml.ValueConverterUtils.INVALID;
import static com.moandjiezana.toml.ValueConverterUtils.parse;
import static com.moandjiezana.toml.ValueConverterUtils.parser;
import java.util.ArrayList;
import java.util.List;
import org.parboiled.Parboiled;
import org.parboiled.parserunners.BasicParseRunner;
import org.parboiled.support.ParsingResult;
class ArrayConverter implements ValueConverter {
static final ArrayConverter ARRAY_PARSER = new ArrayConverter();
@ -23,9 +21,7 @@ class ArrayConverter implements ValueConverter {
@Override
public Object convert(String s) {
ValueParser parser = Parboiled.createParser(ValueParser.class);
ParsingResult<List<Object>> parsingResult = new BasicParseRunner<List<Object>>(parser.Array()).run(s);
List<Object> tokens = parsingResult.resultValue;
List<Object> tokens = parse(parser().Array(), s);
List<Object> values = convertList(tokens);
if (values == INVALID_ARRAY) {

View file

@ -1,12 +1,11 @@
package com.moandjiezana.toml;
import static com.moandjiezana.toml.ValueConverterUtils.INVALID;
import static com.moandjiezana.toml.ValueConverterUtils.parse;
import static com.moandjiezana.toml.ValueConverterUtils.parser;
import java.util.List;
import org.parboiled.Parboiled;
import org.parboiled.parserunners.BasicParseRunner;
class BooleanConverter implements ValueConverter {
static final BooleanConverter BOOLEAN_PARSER = new BooleanConverter();
@ -18,9 +17,7 @@ class BooleanConverter implements ValueConverter {
@Override
public Object convert(String s) {
ValueParser parser = Parboiled.createParser(ValueParser.class);
List<String> resultValue = new BasicParseRunner<List<String>>(parser.Boolean()).run(s).resultValue;
List<String> resultValue = parse(parser().Boolean(), s);
if (resultValue == null) {
return INVALID;

View file

@ -1,23 +1,21 @@
package com.moandjiezana.toml;
import java.util.List;
import static com.moandjiezana.toml.ValueConverterUtils.parse;
import static com.moandjiezana.toml.ValueConverterUtils.parser;
import org.parboiled.Parboiled;
import org.parboiled.parserunners.BasicParseRunner;
import java.util.List;
class IntegerConverter implements ValueConverter {
static final IntegerConverter INTEGER_PARSER = new IntegerConverter();
@Override
public boolean canConvert(String s) {
ValueParser parser = Parboiled.createParser(ValueParser.class);
return new BasicParseRunner<Object>(parser.Integer()).run(s).resultValue != null;
return parse(parser().Integer(), s) != null;
}
@Override
public Object convert(String s) {
ValueParser parser = Parboiled.createParser(ValueParser.class);
List<String> resultValue = new BasicParseRunner<List<String>>(parser.Integer()).run(s).resultValue;
List<String> resultValue = parse(parser().Integer(), s);
if (resultValue == null) {
return ValueConverterUtils.INVALID;

View file

@ -3,7 +3,6 @@ package com.moandjiezana.toml;
import java.util.List;
import org.parboiled.Parboiled;
import org.parboiled.parserunners.BasicParseRunner;
class LiteralStringConverter implements ValueConverter {
@ -17,7 +16,7 @@ class LiteralStringConverter implements ValueConverter {
@Override
public Object convert(String s) {
ValueParser parser = Parboiled.createParser(ValueParser.class);
List<Object> resultValue = new BasicParseRunner<List<Object>>(parser.LiteralString()).run(s).resultValue;
List<String> resultValue = ValueConverterUtils.parse(ValueConverterUtils.parser().LiteralString(), s);
if (resultValue == null) {
return ValueConverterUtils.INVALID;

View file

@ -1,14 +1,12 @@
package com.moandjiezana.toml;
import static com.moandjiezana.toml.ValueConverterUtils.INVALID;
import static com.moandjiezana.toml.ValueConverterUtils.parse;
import static com.moandjiezana.toml.ValueConverterUtils.parser;
import java.util.List;
import java.util.regex.Pattern;
import org.parboiled.Parboiled;
import org.parboiled.parserunners.BasicParseRunner;
import org.parboiled.support.ParsingResult;
class TomlParser {
private static final Pattern MULTILINE_ARRAY_REGEX = Pattern.compile("\\s*\\[([^\\]]*)");
private static final Pattern MULTILINE_ARRAY_REGEX_END = Pattern.compile("\\s*\\]");
@ -141,14 +139,12 @@ class TomlParser {
}
private String getTableArrayName(String line) {
ValueParser parser = Parboiled.createParser(ValueParser.class);
ParsingResult<List<Object>> parsingResult = new BasicParseRunner<List<Object>>(parser.TableArray()).run(line);
if (parsingResult.resultValue == null) {
List<Object> resultValue = parse(parser().TableArray(), line);
if (resultValue == null) {
return null;
}
return (String) parsingResult.resultValue.get(0);
return (String) resultValue.get(0);
}
private boolean isTable(String line) {
@ -156,14 +152,12 @@ class TomlParser {
}
private String getTableName(String line) {
ValueParser parser = Parboiled.createParser(ValueParser.class);
ParsingResult<List<Object>> parsingResult = new BasicParseRunner<List<Object>>(parser.Table()).run(line);
if (parsingResult.resultValue == null) {
List<Object> resultValue = parse(parser().Table(), line);
if (resultValue == null) {
return null;
}
return (String) parsingResult.resultValue.get(0);
return (String) resultValue.get(0);
}
private boolean isKeyValid(String key) {

View file

@ -1,7 +1,21 @@
package com.moandjiezana.toml;
import java.util.List;
import org.parboiled.Parboiled;
import org.parboiled.Rule;
import org.parboiled.parserunners.BasicParseRunner;
class ValueConverterUtils {
static final Object INVALID = new Object();
static ValueParser parser() {
return Parboiled.createParser(ValueParser.class);
}
static <T> List<T> parse(Rule rule, String s) {
return new BasicParseRunner<List<T>>(rule).run(s).resultValue;
}
static boolean isComment(String line) {
if (line == null || line.isEmpty()) {