Multiline literal strings no longer handled by Parboiled

This commit is contained in:
moandji.ezana 2015-01-23 22:03:55 +02:00
parent 40cab5c499
commit 77788d3c87
3 changed files with 31 additions and 34 deletions

View file

@ -1,10 +1,6 @@
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;
class MultilineLiteralStringConverter implements ValueConverter {
@ -17,13 +13,33 @@ class MultilineLiteralStringConverter implements ValueConverter {
@Override
public Object convert(String s) {
List<String> result = parse(parser().MultilineLiteralString(), s);
char[] chars = s.toCharArray();
boolean terminated = false;
StringBuilder sb = new StringBuilder(s.length());
if (result == null) {
return INVALID;
for (int i = 3; i < chars.length; i++) {
char c = chars[i];
if (c == '\'' && chars.length > i + 2 && chars[i + 1] == '\'' && chars[i + 2] == '\'') {
i += 2;
terminated = true;
continue;
}
if (!terminated) {
sb.append(c);
}
if (terminated && c == '#') {
break;
}
if (terminated && !Character.isWhitespace(c)) {
return INVALID;
}
}
return result.get(0);
return sb.toString();
}
private MultilineLiteralStringConverter() {}

View file

@ -14,10 +14,6 @@ class ValueParser extends BaseParser<List<Object>> {
return FirstOf(EmptyArray(), Sequence('[', startList(), OneOrMore(FirstOf(NonEmptyArray(), ' ', ',')), ']', endList()));
}
public Rule MultilineLiteralString() {
return FirstOf(EmptyMultilineLiteralString(), Sequence("'''", startList(), Sequence(OneOrMore(TestNot("'''"), ANY), pushToken(match())), "'''", endList(), Comment()));
}
Rule NonEmptyArray() {
return FirstOf(Array(), OneOrMore(TestNot(']'), FirstOf(StringToken(), Array(), ',', ' ', OtherValue())));
}
@ -26,34 +22,14 @@ class ValueParser extends BaseParser<List<Object>> {
return Sequence(Sequence('"', ZeroOrMore(Sequence(TestNot('"'), ANY)), '"'), pushToken(match()));
}
Rule EmptyLiteralString() {
return Sequence('\'', '\'', startList(), pushToken(""), endList());
}
Rule EmptyMultilineLiteralString() {
return Sequence("'''", "'''", startList(), pushToken(""), endList(), Comment());
}
Rule EmptyArray() {
return Sequence('[', ']', startList(), endList());
}
Rule SignedNumber() {
return Sequence(Optional(AnyOf("+-")), Number());
}
Rule Number() {
return OneOrMore(CharRange('0', '9'));
}
Rule OtherValue() {
return Sequence(ZeroOrMore(NoneOf("],")), pushToken(match()));
}
Rule Comment() {
return FirstOf(EOI, OneOrMore(' ', Sequence('#', ZeroOrMore(ANY))));
}
boolean startList() {
ArrayList<Object> newTokens = new ArrayList<Object>();

View file

@ -101,10 +101,15 @@ public class StringTest {
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_unterminated_multiline_literal_string() throws Exception {
public void should_fail_on_multiline_literal_string_with_malformed_comment() throws Exception {
new Toml().parse("a = '''some\n text\n''\nb = '''1'''");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_unterminated_multiline_literal_string() throws Exception {
new Toml().parse("a = '''some\n text\n''");
}
@Test(expected = IllegalStateException.class)
public void should_fail_on_unterminated_multiline_literal_string_on_single_line() throws Exception {
new Toml().parse("a = '''some text''");