use of net.morimekta.providence.serializer.pretty.TokenizerException in project providence by morimekta.
the class ProvidenceConfigParser method checkAndParseInternal.
// --- private
private <M extends PMessage<M, F>, F extends PField> Pair<M, Set<String>> checkAndParseInternal(@Nonnull Path configFile, @Nullable M parent, String... includeStack) throws ProvidenceConfigException {
try {
// So we map actual loaded files by the absolute canonical location.
String canonicalFile = readCanonicalPath(configFile).toString();
List<String> stackList = new ArrayList<>();
Collections.addAll(stackList, includeStack);
if (Arrays.binarySearch(includeStack, canonicalFile) >= 0) {
stackList.add(canonicalFile);
throw new ProvidenceConfigException("Circular includes detected: " + String.join(" -> ", stackList.stream().map(p -> new File(p).getName()).collect(Collectors.toList())));
}
stackList.add(canonicalFile);
return parseConfigRecursively(configFile, parent, stackList.toArray(new String[stackList.size()]));
} catch (IOException e) {
if (e instanceof ProvidenceConfigException) {
ProvidenceConfigException pce = (ProvidenceConfigException) e;
if (pce.getFile() == null) {
pce.setFile(configFile.getFileName().toString());
}
throw pce;
}
if (e instanceof TokenizerException) {
TokenizerException te = (TokenizerException) e;
if (te.getFile() == null) {
te.setFile(configFile.getFileName().toString());
}
throw new ProvidenceConfigException(te);
}
throw new ProvidenceConfigException(e, e.getMessage()).setFile(configFile.getFileName().toString());
}
}
use of net.morimekta.providence.serializer.pretty.TokenizerException in project providence by morimekta.
the class ProvidenceConfigParser method parseDefinitionValue.
@SuppressWarnings("unchecked")
Object parseDefinitionValue(ProvidenceConfigContext context, Tokenizer tokenizer) throws IOException {
Token token = tokenizer.expect("Start of def value");
if (token.isReal()) {
return Double.parseDouble(token.asString());
} else if (token.isInteger()) {
return Long.parseLong(token.asString());
} else if (token.isStringLiteral()) {
return token.decodeLiteral(strict);
} else if (TRUE.equalsIgnoreCase(token.asString())) {
return Boolean.TRUE;
} else if (FALSE.equalsIgnoreCase(token.asString())) {
return Boolean.FALSE;
} else if (Token.B64.equals(token.asString())) {
tokenizer.expectSymbol("binary data enclosing start", Token.kParamsStart);
return Binary.fromBase64(tokenizer.readBinary(Token.kParamsEnd));
} else if (Token.HEX.equals(token.asString())) {
tokenizer.expectSymbol("binary data enclosing start", Token.kParamsStart);
return Binary.fromHexString(tokenizer.readBinary(Token.kParamsEnd));
} else if (token.isDoubleQualifiedIdentifier()) {
// this may be an enum reference, must be
// - package.EnumType.IDENTIFIER
String id = token.asString();
int l = id.lastIndexOf(Token.kIdentifierSep);
try {
PEnumDescriptor ed = registry.getEnumType(id.substring(0, l));
PEnumValue val = ed.findByName(id.substring(l + 1));
if (val == null && strict) {
throw new TokenizerException(token, "Unknown %s value: %s", id.substring(0, l), id.substring(l + 1)).setLine(tokenizer.getLine());
}
// Note that unknown enum value results in null. Therefore we don't catch null values here.
return val;
} catch (IllegalArgumentException e) {
// No such declared type.
if (strict) {
throw new TokenizerException(token, "Unknown enum identifier: %s", id.substring(0, l)).setLine(tokenizer.getLine());
}
consumeValue(context, tokenizer, token);
} catch (ClassCastException e) {
// Not an enum.
throw new TokenizerException(token, "Identifier " + id + " does not reference an enum, from " + token.asString()).setLine(tokenizer.getLine());
}
} else if (token.isQualifiedIdentifier()) {
// Message type.
PMessageDescriptor descriptor;
try {
descriptor = registry.getMessageType(token.asString());
} catch (IllegalArgumentException e) {
// - strict mode: all types must be known.
if (strict) {
throw new TokenizerException(token, "Unknown declared type: %s", token.asString()).setLine(tokenizer.getLine());
}
consumeValue(context, tokenizer, token);
return null;
}
PMessageBuilder builder = descriptor.builder();
if (tokenizer.expectSymbol("message start or inherits", '{', ':') == ':') {
token = tokenizer.expect("inherits reference");
PMessage inheritsFrom = resolve(context, token, tokenizer, descriptor);
if (inheritsFrom == null) {
throw new TokenizerException(token, "Inheriting from null reference: %s", token.asString()).setLine(tokenizer.getLine());
}
builder.merge(inheritsFrom);
tokenizer.expectSymbol("message start", '{');
}
return parseMessage(tokenizer, context, builder);
} else {
throw new TokenizerException(token, "Invalid define value " + token.asString()).setLine(tokenizer.getLine());
}
return null;
}
Aggregations