mirror of
https://github.com/plexusorg/toml4j.git
synced 2024-06-25 23:40:45 +00:00
Added support for literal strings
This commit is contained in:
parent
8f877776d6
commit
7e101ce3a3
30
src/main/java/com/moandjiezana/toml/LiteralStringParser.java
Normal file
30
src/main/java/com/moandjiezana/toml/LiteralStringParser.java
Normal file
|
@ -0,0 +1,30 @@
|
|||
package com.moandjiezana.toml;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.parboiled.Parboiled;
|
||||
import org.parboiled.parserunners.BasicParseRunner;
|
||||
|
||||
class LiteralStringParser implements ValueParser {
|
||||
|
||||
static final LiteralStringParser LITERAL_STRING_PARSER = new LiteralStringParser();
|
||||
|
||||
@Override
|
||||
public boolean canParse(String s) {
|
||||
return s.startsWith("'");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object parse(String s) {
|
||||
StatementParser parser = Parboiled.createParser(StatementParser.class);
|
||||
List<Object> resultValue = new BasicParseRunner<List<Object>>(parser.LiteralString()).run(s).resultValue;
|
||||
|
||||
if (resultValue == null) {
|
||||
return ValueParserUtils.INVALID;
|
||||
}
|
||||
|
||||
return resultValue.get(0);
|
||||
}
|
||||
|
||||
private LiteralStringParser() {}
|
||||
}
|
|
@ -21,6 +21,10 @@ class StatementParser extends BaseParser<List<Object>> {
|
|||
return Sequence('[', '[', startList(), Sequence(OneOrMore(NoneOf("[]")), pushToken(match())), ']', ']', endList(), FirstOf(EOI, Sequence(TestNot(']'), ANY)));
|
||||
}
|
||||
|
||||
public Rule LiteralString() {
|
||||
return FirstOf(Sequence('\'', '\'', startList(), pushToken(""), endList()), Sequence('\'', OneOrMore(TestNot("'"), ANY), startList(), pushToken(match()) , '\'', endList(), FirstOf(EOI, OneOrMore(' ', Sequence('#', ZeroOrMore(ANY))))));
|
||||
}
|
||||
|
||||
Rule NonEmptyArray() {
|
||||
return FirstOf(Array(), OneOrMore(TestNot(']'), FirstOf(String(), Array(), ',', ' ', OtherValue())));
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import static com.moandjiezana.toml.BooleanParser.BOOLEAN_PARSER;
|
|||
import static com.moandjiezana.toml.DateParser.DATE_PARSER;
|
||||
import static com.moandjiezana.toml.FloatParser.FLOAT_PARSER;
|
||||
import static com.moandjiezana.toml.IntegerParser.INTEGER_PARSER;
|
||||
import static com.moandjiezana.toml.LiteralStringParser.LITERAL_STRING_PARSER;
|
||||
import static com.moandjiezana.toml.MultilineStringParser.MULTILINE_STRING_PARSER;
|
||||
import static com.moandjiezana.toml.StringParser.STRING_PARSER;
|
||||
import static com.moandjiezana.toml.ValueParserUtils.INVALID;
|
||||
|
@ -14,6 +15,8 @@ class ValueConverter {
|
|||
public Object convert(String value) {
|
||||
if (MULTILINE_STRING_PARSER.canParse(value)) {
|
||||
return MULTILINE_STRING_PARSER.parse(value);
|
||||
} else if (LITERAL_STRING_PARSER.canParse(value)) {
|
||||
return LITERAL_STRING_PARSER.parse(value);
|
||||
} else if (STRING_PARSER.canParse(value)) {
|
||||
return STRING_PARSER.parse(value);
|
||||
} else if (INTEGER_PARSER.canParse(value)) {
|
||||
|
|
|
@ -53,7 +53,16 @@ public class TomlTest {
|
|||
|
||||
assertEquals(toml.getString("ref"), toml.getString("multi1"));
|
||||
assertEquals(toml.getString("ref"), toml.getString("multi2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_get_literal_string() throws Exception {
|
||||
Toml toml = new Toml().parse(file("should_get_literal_string"));
|
||||
|
||||
assertEquals("C:\\Users\\nodejs\\templates", toml.getString("winpath"));
|
||||
assertEquals("\\\\ServerX\\admin$\\system32\\", toml.getString("winpath2"));
|
||||
assertEquals("Tom \"Dubs\" Preston-Werner", toml.getString("quoted"));
|
||||
assertEquals("<\\i\\c*\\s*>", toml.getString("regex"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -414,6 +423,11 @@ public class TomlTest {
|
|||
public void should_fail_on_invalid_boolean_false() {
|
||||
new Toml().parse("answer = false abc");
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void should_fail_on_invalid_literal_string() {
|
||||
new Toml().parse("a = ' ' jdkf");
|
||||
}
|
||||
|
||||
private File file(String file) {
|
||||
return new File(getClass().getResource(file + ".toml").getFile());
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
winpath = 'C:\Users\nodejs\templates'
|
||||
winpath2 = '\\ServerX\admin$\system32\' # comment
|
||||
quoted = 'Tom "Dubs" Preston-Werner'
|
||||
regex = '<\i\c*\s*>'
|
Loading…
Reference in a new issue