Skip to content

Commit

Permalink
toml: Remove TomlConfig, add TomlFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
TheElectronWill committed Jul 31, 2017
1 parent c0013f8 commit f54fb3d
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ final class TableParser {

private static final char[] KEY_END = {'\t', ' ', '=', '.', '\n', '\r', ']'};

static TomlConfig parseInline(CharacterInput input, TomlParser parser) {
TomlConfig config = new TomlConfig();
static CommentedConfig parseInline(CharacterInput input, TomlParser parser) {
CommentedConfig config = TomlFormat.instance().createConfig();
while (true) {
char keyFirst = Toml.readNonSpaceChar(input, false);
if (keyFirst == '}') {
Expand Down Expand Up @@ -95,8 +95,8 @@ private static void checkInvalidSeparator(char sep, String key) {
}
}

static TomlConfig parseNormal(CharacterInput input, TomlParser parser) {
return parseNormal(input, parser, new TomlConfig());
static CommentedConfig parseNormal(CharacterInput input, TomlParser parser) {
return parseNormal(input, parser, TomlFormat.instance().createConfig());
}

static List<String> parseTableName(CharacterInput input, TomlParser parser, boolean array) {
Expand Down Expand Up @@ -160,7 +160,7 @@ static String parseKey(CharacterInput input, char firstChar, TomlParser parser)
if (bareKey.isEmpty()) {
throw new ParsingException("Empty bare keys aren't allowed.");
}
if(!Toml.isValidBareKey(bareKey, parser.isLenientWithBareKeys())) {
if (!Toml.isValidBareKey(bareKey, parser.isLenientWithBareKeys())) {
throw new ParsingException("Invalid bare key: " + bareKey);
}
return bareKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.electronwill.nightconfig.toml;

import com.electronwill.nightconfig.core.CommentedConfig;
import com.electronwill.nightconfig.core.Config;
import com.electronwill.nightconfig.core.UnmodifiableConfig;
import com.electronwill.nightconfig.core.ConfigFormat;

/**
* @author TheElectronWill
*/
public final class TomlFormat implements ConfigFormat<CommentedConfig, Config, UnmodifiableConfig> {
private static final TomlFormat INSTANCE = new TomlFormat();

public static TomlFormat instance() {
return INSTANCE;
}

private TomlFormat() {}

@Override
public TomlWriter createWriter() {
return new TomlWriter();
}

@Override
public TomlParser createParser() {
return new TomlParser();
}

@Override
public CommentedConfig createConfig() {
return CommentedConfig.of(this);
}

@Override
public boolean supportsComments() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import com.electronwill.nightconfig.core.CommentedConfig;
import com.electronwill.nightconfig.core.Config;
import com.electronwill.nightconfig.core.UnmodifiableConfig;
import com.electronwill.nightconfig.core.io.CharacterInput;
import com.electronwill.nightconfig.core.io.CharsWrapper;
import com.electronwill.nightconfig.core.ConfigFormat;
import com.electronwill.nightconfig.core.io.ConfigParser;
import com.electronwill.nightconfig.core.io.ParsingException;
import com.electronwill.nightconfig.core.io.ParsingMode;
import com.electronwill.nightconfig.core.io.ReaderInput;
import com.electronwill.nightconfig.core.utils.FakeCommentedConfig;
import java.io.Reader;
Expand All @@ -20,19 +23,19 @@
* @author TheElectronWill
* @see <a href="https://github.com/toml-lang/toml">TOML specification</a>
*/
public final class TomlParser implements ConfigParser<TomlConfig, Config> {
public final class TomlParser implements ConfigParser<CommentedConfig, Config> {
// --- Parser's settings ---
private int initialStringBuilderCapacity = 16, initialListCapacity = 10;
private boolean lenientBareKeys = false;

// --- Parser's methods ---
@Override
public TomlConfig parse(Reader reader) {
return parse(new ReaderInput(reader), new TomlConfig());
public CommentedConfig parse(Reader reader) {
return parse(new ReaderInput(reader), TomlFormat.instance().createConfig());
}

@Override
public void parse(Reader reader, Config destination) {
public void parse(Reader reader, Config destination, ParsingMode mode) {
parse(new ReaderInput(reader), destination);
}

Expand All @@ -59,8 +62,8 @@ private <T extends Config> T parse(CharacterInput input, T destination) {
throw new ParsingException("Cannot create entry " + path + " because of an invalid " +
"parent that isn't a table.");
}
TomlConfig table = TableParser.parseNormal(input, this);
List<TomlConfig> arrayOfTables = (List)parentMap.get(lastKey);
CommentedConfig table = TableParser.parseNormal(input, this);
List<CommentedConfig> arrayOfTables = (List)parentMap.get(lastKey);
if (arrayOfTables == null) {
arrayOfTables = createList();
parentMap.put(lastKey, arrayOfTables);
Expand All @@ -73,7 +76,7 @@ private <T extends Config> T parse(CharacterInput input, T destination) {
}
Object alreadyDeclared = parentMap.get(lastKey);
if (alreadyDeclared == null) {
TomlConfig table = TableParser.parseNormal(input, this);
CommentedConfig table = TableParser.parseNormal(input, this);
parentMap.put(lastKey, table);
} else {
if (alreadyDeclared instanceof Config) {
Expand All @@ -98,7 +101,7 @@ private Map<String, Object> getSubTableMap(Config parentTable, List<String> path
for (String key : path) {
Object value = currentMap.get(key);
if (value == null) {
Config sub = new TomlConfig();
Config sub = TomlFormat.instance().createConfig();
currentMap.put(key, sub);
currentMap = sub.valueMap();
} else if (value instanceof Config) {
Expand Down Expand Up @@ -146,6 +149,11 @@ public TomlParser setInitialListCapacity(int initialListCapacity) {
return this;
}

@Override
public ConfigFormat<CommentedConfig, Config, ?> getFormat() {
return TomlFormat.instance();
}

// --- Configured objects creation ---
<T> List<T> createList() {
return new ArrayList<>(initialListCapacity);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.electronwill.nightconfig.toml;

import com.electronwill.nightconfig.core.CommentedConfig;
import com.electronwill.nightconfig.core.file.FileNotFoundAction;
import com.electronwill.nightconfig.core.io.ParsingException;
import java.io.File;
import java.io.StringReader;
Expand All @@ -23,14 +25,14 @@ private static void assertThrows(Class<? extends Exception> exceptionClass,

private static void parseAndPrint(String tomlString) {
TomlParser parser = new TomlParser();
TomlConfig parsed = parser.parse(new StringReader(tomlString));
CommentedConfig parsed = parser.parse(new StringReader(tomlString));
System.out.println("parsed: " + parsed);
}

@Test
public void readWriteReadAgain() {
File file = new File("test.toml");
TomlConfig parsed = new TomlParser().parse(file);
CommentedConfig parsed = new TomlParser().parse(file, FileNotFoundAction.THROW_ERROR);

System.out.println("--- parsed --- \n" + parsed);
System.out.println("--------------------------------------------");
Expand All @@ -40,7 +42,7 @@ public void readWriteReadAgain() {
System.out.println("--- written --- \n" + sw);
System.out.println("--------------------------------------------");

TomlConfig reparsed = new TomlParser().parse(new StringReader(sw.toString()));
CommentedConfig reparsed = new TomlParser().parse(new StringReader(sw.toString()));
System.out.println("--- reparsed --- \n" + reparsed);
assertEquals(parsed, reparsed);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ public class TomlWriterTest {

@Test
public void writeToString() throws IOException {
Config subConfig = new TomlConfig();
Config subConfig = TomlFormat.instance().createConfig();
subConfig.set("string", "test");
subConfig.set("dateTime", ZonedDateTime.now());
subConfig.set("sub", new TomlConfig());
subConfig.set("sub", TomlFormat.instance().createConfig());

List<Config> tableArray = new ArrayList<>();
tableArray.add(subConfig);
tableArray.add(subConfig);
tableArray.add(subConfig);

Config config = new TomlConfig();
Config config = TomlFormat.instance().createConfig();
config.set("string", "\"value\"");
config.set("integer", 2);
config.set("long", 123456789L);
Expand Down

0 comments on commit f54fb3d

Please sign in to comment.