2022-05-05 07:49:03 +00:00
|
|
|
package dev.plex.toml;
|
2022-05-04 12:18:07 +00:00
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
2022-05-05 07:49:03 +00:00
|
|
|
class InlineTableValueReader implements dev.plex.toml.ValueReader
|
|
|
|
{
|
2022-05-04 12:18:07 +00:00
|
|
|
|
2022-05-10 05:08:45 +00:00
|
|
|
static final InlineTableValueReader INLINE_TABLE_VALUE_READER = new InlineTableValueReader();
|
2022-05-04 12:18:07 +00:00
|
|
|
|
2022-05-10 05:08:45 +00:00
|
|
|
@Override
|
|
|
|
public boolean canRead(String s)
|
|
|
|
{
|
|
|
|
return s.startsWith("{");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Object read(String s, AtomicInteger sharedIndex, dev.plex.toml.Context context)
|
|
|
|
{
|
|
|
|
AtomicInteger line = context.line;
|
|
|
|
int startLine = line.get();
|
|
|
|
int startIndex = sharedIndex.get();
|
|
|
|
boolean inKey = true;
|
|
|
|
boolean inValue = false;
|
|
|
|
boolean terminated = false;
|
|
|
|
StringBuilder currentKey = new StringBuilder();
|
|
|
|
HashMap<String, Object> results = new HashMap<String, Object>();
|
|
|
|
dev.plex.toml.Results.Errors errors = new dev.plex.toml.Results.Errors();
|
|
|
|
|
|
|
|
for (int i = sharedIndex.incrementAndGet(); sharedIndex.get() < s.length(); i = sharedIndex.incrementAndGet())
|
|
|
|
{
|
|
|
|
char c = s.charAt(i);
|
|
|
|
|
|
|
|
if (inValue && !Character.isWhitespace(c))
|
|
|
|
{
|
|
|
|
Object converted = dev.plex.toml.ValueReaders.VALUE_READERS.convert(s, sharedIndex, context.with(dev.plex.toml.Identifier.from(currentKey.toString(), context)));
|
|
|
|
|
|
|
|
if (converted instanceof dev.plex.toml.Results.Errors)
|
|
|
|
{
|
2022-05-19 18:59:16 +00:00
|
|
|
errors.add((dev.plex.toml.Results.Errors) converted);
|
2022-05-10 05:08:45 +00:00
|
|
|
return errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
String currentKeyTrimmed = currentKey.toString().trim();
|
|
|
|
Object previous = results.put(currentKeyTrimmed, converted);
|
|
|
|
|
|
|
|
if (previous != null)
|
|
|
|
{
|
|
|
|
errors.duplicateKey(currentKeyTrimmed, context.line.get());
|
|
|
|
return errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentKey = new StringBuilder();
|
|
|
|
inValue = false;
|
|
|
|
}
|
|
|
|
else if (c == ',')
|
|
|
|
{
|
|
|
|
inKey = true;
|
|
|
|
inValue = false;
|
|
|
|
currentKey = new StringBuilder();
|
|
|
|
}
|
|
|
|
else if (c == '=')
|
|
|
|
{
|
|
|
|
inKey = false;
|
|
|
|
inValue = true;
|
|
|
|
}
|
|
|
|
else if (c == '}')
|
|
|
|
{
|
|
|
|
terminated = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (inKey)
|
|
|
|
{
|
|
|
|
currentKey.append(c);
|
|
|
|
}
|
2022-05-04 12:18:07 +00:00
|
|
|
}
|
2022-05-10 05:08:45 +00:00
|
|
|
|
|
|
|
if (!terminated)
|
|
|
|
{
|
|
|
|
errors.unterminated(context.identifier.getName(), s.substring(startIndex), startLine);
|
2022-05-04 12:18:07 +00:00
|
|
|
}
|
2022-05-10 05:08:45 +00:00
|
|
|
|
|
|
|
if (errors.hasErrors())
|
|
|
|
{
|
|
|
|
return errors;
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
2022-05-04 12:18:07 +00:00
|
|
|
}
|
|
|
|
|
2022-05-10 05:08:45 +00:00
|
|
|
private InlineTableValueReader()
|
|
|
|
{
|
|
|
|
}
|
2022-05-04 12:18:07 +00:00
|
|
|
}
|