Skip to content

Commit

Permalink
toml: Fix Toml parsing for 2.0 branch
Browse files Browse the repository at this point in the history
Fix issues #22, #23 and #24
  • Loading branch information
TheElectronWill committed Aug 1, 2017
1 parent bde5f03 commit a37dd62
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ static <T extends CommentedConfig> T parseNormal(CharacterInput input, TomlParse
List<CharsWrapper> commentsList = new ArrayList<>(2);
int keyFirst = Toml.readUseful(input, commentsList);
if (keyFirst == -1 || keyFirst == '[') {
parser.setComment(commentsList);// Saves the comments that are above the next table
return config;// No more data, or beginning of an other table
}
String key = parseKey(input, (char)keyFirst, parser);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* @author TheElectronWill
*/
final class Toml {

private static final char[] WHITESPACE_OR_NEWLINE = {'\t', ' ', '\n', '\r'};
private static final char[] WHITESPACE = {'\t', ' '};
private static final char[] NEWLINE = {'\n'};
Expand Down Expand Up @@ -60,7 +59,12 @@ static int readNonSpace(CharacterInput input, boolean skipNewlines) {
* Reads all the characters before the next newline or the end of the data.
*/
static CharsWrapper readLine(CharacterInput input) {
return input.readUntil(NEWLINE);
CharsWrapper chars = input.readUntil(NEWLINE);
int lastIndex = chars.length() - 1;
if (chars.get(lastIndex) == '\r') {
return chars.subView(0, lastIndex);
}
return chars;
}

static boolean isValidInBareKey(char c, boolean lenient) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.electronwill.nightconfig.core.utils.FakeCommentedConfig;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -49,10 +50,18 @@ private <T extends Config> T parse(CharacterInput input, T destination) {
final int lastIndex = path.size() - 1;
final String lastKey = path.get(lastIndex);
final List<String> parentPath = path.subList(0, lastIndex);
final Map<String, Object> parentMap = getSubTableMap(rootTable, parentPath);
final Config parentConfig = getSubTable(rootTable, parentPath);
final Map<String, Object> parentMap = (parentConfig != null) ? parentConfig.valueMap()
: null;

if (hasPendingComment()) {// Handles comments that are before the table declaration
rootTable.setComment(path, consumeComment());
String comment = consumeComment();
System.out.println("path: " + path);
System.out.println("comment: \"" + comment + "\"");
if (parentConfig instanceof CommentedConfig) {
List<String> lastPath = Collections.singletonList(lastKey);
((CommentedConfig)parentConfig).setComment(lastPath, comment);
}
}
if (isArray) {// It's an element of an array of tables
if (parentMap == null) {
Expand Down Expand Up @@ -90,32 +99,32 @@ private <T extends Config> T parse(CharacterInput input, T destination) {
return destination;
}

private Map<String, Object> getSubTableMap(Config parentTable, List<String> path) {
private Config getSubTable(Config parentTable, List<String> path) {
if (path.isEmpty()) {
return parentTable.valueMap();
return parentTable;
}
Map<String, Object> currentMap = parentTable.valueMap();
Config currentConfig = parentTable;
for (String key : path) {
Object value = currentMap.get(key);
Object value = currentConfig.valueMap().get(key);
if (value == null) {
Config sub = new TomlConfig();
currentMap.put(key, sub);
currentMap = sub.valueMap();
currentConfig.valueMap().put(key, sub);
currentConfig = sub;
} else if (value instanceof Config) {
currentMap = ((Config)value).valueMap();
currentConfig = (Config)value;
} else if (value instanceof List) {
List<?> list = (List<?>)value;
if (!list.isEmpty() && list.get(0) instanceof Config) {// Arrays of tables
int lastIndex = list.size() - 1;
currentMap = ((Config)list.get(lastIndex)).valueMap();
currentConfig = ((Config)list.get(lastIndex));
} else {
return null;
}
} else {
return null;
}
}
return currentMap;
return currentConfig;
}

private void checkContainsOnlySubtables(Config table, List<String> path) {
Expand Down Expand Up @@ -187,7 +196,7 @@ void setComment(List<CharsWrapper> commentsList) {
builder.append('\n');
builder.append(it.next());
}
setComment(builder.build());// Appends the builder to the current comment if any
}
currentComment = builder.toString();
}
}

0 comments on commit a37dd62

Please sign in to comment.