Table array name parsing no longer handled by Parboiled

This commit is contained in:
moandji.ezana 2015-01-23 14:17:39 +02:00
parent 537c75757e
commit f804c99534
3 changed files with 33 additions and 17 deletions

View file

@ -66,6 +66,38 @@ class Keys {
return splitKey.toArray(new Key[0]); return splitKey.toArray(new Key[0]);
} }
static String getTableArrayName(String line) {
StringBuilder sb = new StringBuilder();
char[] chars = line.toCharArray();
boolean quoted = false;
boolean terminated = false;
for (int i = 2; i < chars.length; i++) {
char c = chars[i];
if (c == '"' && chars[i - 1] != '\\') {
quoted = !quoted;
} else if (!quoted && c == ']') {
if (chars.length > i + 1 && chars[i + 1] == ']') {
terminated = true;
break;
}
} else if (!quoted && (ALLOWED_CHARS.indexOf(c) == -1)) {
break;
}
sb.append(c);
}
String tableName = sb.toString();
if (!terminated || tableName.isEmpty() || !isComment(line.substring(tableName.length() + 4))) {
return null;
}
tableName = StringConverter.STRING_PARSER.replaceUnicodeCharacters(tableName);
return tableName;
}
/** /**
* @param line trimmed TOML line to parse * @param line trimmed TOML line to parse
* @return null if line is not a valid table identifier * @return null if line is not a valid table identifier

View file

@ -1,10 +1,7 @@
package com.moandjiezana.toml; package com.moandjiezana.toml;
import static com.moandjiezana.toml.ValueConverterUtils.INVALID; 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 java.util.regex.Pattern;
class TomlParser { class TomlParser {
@ -173,12 +170,7 @@ class TomlParser {
} }
private String getTableArrayName(String line) { private String getTableArrayName(String line) {
List<Object> resultValue = parse(parser().TableArray(), line); return Keys.getTableArrayName(line);
if (resultValue == null) {
return null;
}
return (String) resultValue.get(0);
} }
private boolean isTable(String line) { private boolean isTable(String line) {

View file

@ -10,10 +10,6 @@ import org.parboiled.annotations.BuildParseTree;
@BuildParseTree @BuildParseTree
class ValueParser extends BaseParser<List<Object>> { class ValueParser extends BaseParser<List<Object>> {
public Rule T() {
return Sequence("'''", OneOrMore(TestNot("'''"), ANY), "'''", Comment());
}
public Rule Array() { public Rule Array() {
return FirstOf(EmptyArray(), Sequence('[', startList(), OneOrMore(FirstOf(NonEmptyArray(), ' ', ',')), ']', endList())); return FirstOf(EmptyArray(), Sequence('[', startList(), OneOrMore(FirstOf(NonEmptyArray(), ' ', ',')), ']', endList()));
} }
@ -22,10 +18,6 @@ class ValueParser extends BaseParser<List<Object>> {
return Sequence('[', startList(), Sequence(OneOrMore(NoneOf("[]")), pushToken(match())), ']', endList(), Comment()); return Sequence('[', startList(), Sequence(OneOrMore(NoneOf("[]")), pushToken(match())), ']', endList(), Comment());
} }
public Rule TableArray() {
return Sequence('[', '[', startList(), Sequence(OneOrMore(NoneOf("[]")), pushToken(match())), ']', ']', endList(), FirstOf(EOI, Sequence(TestNot(']'), ANY)));
}
public Rule LiteralString() { public Rule LiteralString() {
return FirstOf(EmptyLiteralString(), Sequence('\'', OneOrMore(TestNot("'"), ANY), startList(), pushToken(match()) , '\'', endList(), Comment())); return FirstOf(EmptyLiteralString(), Sequence('\'', OneOrMore(TestNot("'"), ANY), startList(), pushToken(match()) , '\'', endList(), Comment()));
} }