diff --git a/src/main/java/com/moandjiezana/toml/ArrayConverter.java b/src/main/java/com/moandjiezana/toml/ArrayConverter.java index e0c37eb..725278b 100644 --- a/src/main/java/com/moandjiezana/toml/ArrayConverter.java +++ b/src/main/java/com/moandjiezana/toml/ArrayConverter.java @@ -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> parsingResult = new BasicParseRunner>(parser.Array()).run(s); - List tokens = parsingResult.resultValue; + List tokens = parse(parser().Array(), s); List values = convertList(tokens); if (values == INVALID_ARRAY) { diff --git a/src/main/java/com/moandjiezana/toml/BooleanConverter.java b/src/main/java/com/moandjiezana/toml/BooleanConverter.java index bb8fee8..c7a7c98 100644 --- a/src/main/java/com/moandjiezana/toml/BooleanConverter.java +++ b/src/main/java/com/moandjiezana/toml/BooleanConverter.java @@ -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 resultValue = new BasicParseRunner>(parser.Boolean()).run(s).resultValue; + List resultValue = parse(parser().Boolean(), s); if (resultValue == null) { return INVALID; diff --git a/src/main/java/com/moandjiezana/toml/IntegerConverter.java b/src/main/java/com/moandjiezana/toml/IntegerConverter.java index 2601b4b..55fb3d4 100644 --- a/src/main/java/com/moandjiezana/toml/IntegerConverter.java +++ b/src/main/java/com/moandjiezana/toml/IntegerConverter.java @@ -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(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 resultValue = new BasicParseRunner>(parser.Integer()).run(s).resultValue; + List resultValue = parse(parser().Integer(), s); if (resultValue == null) { return ValueConverterUtils.INVALID; diff --git a/src/main/java/com/moandjiezana/toml/LiteralStringConverter.java b/src/main/java/com/moandjiezana/toml/LiteralStringConverter.java index 562a8c7..977153f 100644 --- a/src/main/java/com/moandjiezana/toml/LiteralStringConverter.java +++ b/src/main/java/com/moandjiezana/toml/LiteralStringConverter.java @@ -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 resultValue = new BasicParseRunner>(parser.LiteralString()).run(s).resultValue; + List resultValue = ValueConverterUtils.parse(ValueConverterUtils.parser().LiteralString(), s); if (resultValue == null) { return ValueConverterUtils.INVALID; diff --git a/src/main/java/com/moandjiezana/toml/TomlParser.java b/src/main/java/com/moandjiezana/toml/TomlParser.java index 5658358..e558cff 100644 --- a/src/main/java/com/moandjiezana/toml/TomlParser.java +++ b/src/main/java/com/moandjiezana/toml/TomlParser.java @@ -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> parsingResult = new BasicParseRunner>(parser.TableArray()).run(line); - - if (parsingResult.resultValue == null) { + List 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> parsingResult = new BasicParseRunner>(parser.Table()).run(line); - - if (parsingResult.resultValue == null) { + List 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) { diff --git a/src/main/java/com/moandjiezana/toml/ValueConverterUtils.java b/src/main/java/com/moandjiezana/toml/ValueConverterUtils.java index ca1641c..e2f7b31 100644 --- a/src/main/java/com/moandjiezana/toml/ValueConverterUtils.java +++ b/src/main/java/com/moandjiezana/toml/ValueConverterUtils.java @@ -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 List parse(Rule rule, String s) { + return new BasicParseRunner>(rule).run(s).resultValue; + } static boolean isComment(String line) { if (line == null || line.isEmpty()) {