@Test public void testConsumeValue() throws IOException { validateConsume("undefined __after__", null); validateConsume("not.a.ref\n__after__", null); ProvidenceConfigContext context = mock(ProvidenceConfigContext.class); when(context.getReference(eq("ref"), any(Token.class), any(Tokenizer.class))) .thenReturn(true); validateConsume("ref __after__", context); reset(context); validateConsume("{} __after__", null); validateConsume("unknown {} __after__", null); validateConsume("{ FIRST: SECOND } __after__", null); validateConsume("{ foo = \"bar\" } __after__", null); validateConsume("{ foo { bar = 12 } } __after__", null); validateConsume("{ foo = 12 bar = 13 } __after__", null); validateConsume("{ foo & bar = 12 ; other = bar } __after__", null); validateConsume("[] __after__", null); validateConsume("[ \"foo\", ] __after__", null); validateConsume("[ \"foo\" ] __after__", null); validateConsume("hex(abcdef) __after__", null); validateConsume("b64(AbDfm_) __after__", null); }
static void consumeValue(@Nonnull ProvidenceConfigContext context, @Nonnull Tokenizer tokenizer, @Nonnull Token token) throws IOException { boolean isMessage = false; if (UNDEFINED.equals(token.asString())) { return; } else if (token.asString().equals(Token.B64)) { tokenizer.expectSymbol("b64 body start", Token.kParamsStart); tokenizer.readBinary(Token.kParamsEnd); } else if (token.asString().equals(Token.HEX)) { tokenizer.expectSymbol("hex body start", Token.kParamsStart); tokenizer.readBinary(Token.kParamsEnd); } else if (token.isReferenceIdentifier()) { if (!tokenizer.peek("message start").isSymbol(Token.kMessageStart)) { return; } isMessage = true; token = tokenizer.expect("start of message"); } if (token.isSymbol(Token.kMessageStart)) { token = tokenizer.expect("map or message first entry"); if (token.isSymbol(Token.kMessageEnd)) { return; } Token firstSep = tokenizer.peek("First separator"); if (!isMessage && !firstSep.isSymbol(Token.kFieldValueSep) && !firstSep.isSymbol(Token.kMessageStart) && !firstSep.isSymbol(DEFINE_REFERENCE)) { while (!token.isSymbol(Token.kMessageEnd)) { if (!token.isIdentifier() && token.isReferenceIdentifier()) { throw new TokenizerException(token, "Invalid map key: " + token.asString()) .setLine(tokenizer.getLine()); } consumeValue(context, tokenizer, token); tokenizer.expectSymbol("key value sep.", Token.kKeyValueSep); consumeValue(context, tokenizer, tokenizer.expect("map value")); token = nextNotLineSep(tokenizer, "map key, sep or end"); } } else { while (!token.isSymbol(Token.kMessageEnd)) { if (!token.isIdentifier()) { throw new TokenizerException(token, "Invalid field name: " + token.asString()) .setLine(tokenizer.getLine()); } token = tokenizer.expect("field value sep"); if (token.isSymbol(DEFINE_REFERENCE)) { token = tokenizer.expectIdentifier("reference name"); context.setReference( context.initReference(token, tokenizer), null); token = tokenizer.expect("field value sep"); } if (token.isSymbol(Token.kMessageStart)) { consumeValue(context, tokenizer, token); } else if (token.isSymbol(Token.kFieldValueSep)) { consumeValue(context, tokenizer, tokenizer.expect("field value")); } else { throw new TokenizerException(token, "Unknown field value sep: " + token.asString()) .setLine(tokenizer.getLine()); } token = nextNotLineSep(tokenizer, "message field or end"); } } } else if (token.isSymbol(Token.kListStart)) { token = tokenizer.expect("list value or end"); while (!token.isSymbol(Token.kListEnd)) { consumeValue(context, tokenizer, token); if (tokenizer.expectSymbol("list separator or end", Token.kLineSep1, Token.kListEnd) == Token.kListEnd) { break; } token = tokenizer.expect("list value or end"); } } }
ProvidenceConfigUtil { static void consumeValue(@Nonnull ProvidenceConfigContext context, @Nonnull Tokenizer tokenizer, @Nonnull Token token) throws IOException { boolean isMessage = false; if (UNDEFINED.equals(token.asString())) { return; } else if (token.asString().equals(Token.B64)) { tokenizer.expectSymbol("b64 body start", Token.kParamsStart); tokenizer.readBinary(Token.kParamsEnd); } else if (token.asString().equals(Token.HEX)) { tokenizer.expectSymbol("hex body start", Token.kParamsStart); tokenizer.readBinary(Token.kParamsEnd); } else if (token.isReferenceIdentifier()) { if (!tokenizer.peek("message start").isSymbol(Token.kMessageStart)) { return; } isMessage = true; token = tokenizer.expect("start of message"); } if (token.isSymbol(Token.kMessageStart)) { token = tokenizer.expect("map or message first entry"); if (token.isSymbol(Token.kMessageEnd)) { return; } Token firstSep = tokenizer.peek("First separator"); if (!isMessage && !firstSep.isSymbol(Token.kFieldValueSep) && !firstSep.isSymbol(Token.kMessageStart) && !firstSep.isSymbol(DEFINE_REFERENCE)) { while (!token.isSymbol(Token.kMessageEnd)) { if (!token.isIdentifier() && token.isReferenceIdentifier()) { throw new TokenizerException(token, "Invalid map key: " + token.asString()) .setLine(tokenizer.getLine()); } consumeValue(context, tokenizer, token); tokenizer.expectSymbol("key value sep.", Token.kKeyValueSep); consumeValue(context, tokenizer, tokenizer.expect("map value")); token = nextNotLineSep(tokenizer, "map key, sep or end"); } } else { while (!token.isSymbol(Token.kMessageEnd)) { if (!token.isIdentifier()) { throw new TokenizerException(token, "Invalid field name: " + token.asString()) .setLine(tokenizer.getLine()); } token = tokenizer.expect("field value sep"); if (token.isSymbol(DEFINE_REFERENCE)) { token = tokenizer.expectIdentifier("reference name"); context.setReference( context.initReference(token, tokenizer), null); token = tokenizer.expect("field value sep"); } if (token.isSymbol(Token.kMessageStart)) { consumeValue(context, tokenizer, token); } else if (token.isSymbol(Token.kFieldValueSep)) { consumeValue(context, tokenizer, tokenizer.expect("field value")); } else { throw new TokenizerException(token, "Unknown field value sep: " + token.asString()) .setLine(tokenizer.getLine()); } token = nextNotLineSep(tokenizer, "message field or end"); } } } else if (token.isSymbol(Token.kListStart)) { token = tokenizer.expect("list value or end"); while (!token.isSymbol(Token.kListEnd)) { consumeValue(context, tokenizer, token); if (tokenizer.expectSymbol("list separator or end", Token.kLineSep1, Token.kListEnd) == Token.kListEnd) { break; } token = tokenizer.expect("list value or end"); } } } }
ProvidenceConfigUtil { static void consumeValue(@Nonnull ProvidenceConfigContext context, @Nonnull Tokenizer tokenizer, @Nonnull Token token) throws IOException { boolean isMessage = false; if (UNDEFINED.equals(token.asString())) { return; } else if (token.asString().equals(Token.B64)) { tokenizer.expectSymbol("b64 body start", Token.kParamsStart); tokenizer.readBinary(Token.kParamsEnd); } else if (token.asString().equals(Token.HEX)) { tokenizer.expectSymbol("hex body start", Token.kParamsStart); tokenizer.readBinary(Token.kParamsEnd); } else if (token.isReferenceIdentifier()) { if (!tokenizer.peek("message start").isSymbol(Token.kMessageStart)) { return; } isMessage = true; token = tokenizer.expect("start of message"); } if (token.isSymbol(Token.kMessageStart)) { token = tokenizer.expect("map or message first entry"); if (token.isSymbol(Token.kMessageEnd)) { return; } Token firstSep = tokenizer.peek("First separator"); if (!isMessage && !firstSep.isSymbol(Token.kFieldValueSep) && !firstSep.isSymbol(Token.kMessageStart) && !firstSep.isSymbol(DEFINE_REFERENCE)) { while (!token.isSymbol(Token.kMessageEnd)) { if (!token.isIdentifier() && token.isReferenceIdentifier()) { throw new TokenizerException(token, "Invalid map key: " + token.asString()) .setLine(tokenizer.getLine()); } consumeValue(context, tokenizer, token); tokenizer.expectSymbol("key value sep.", Token.kKeyValueSep); consumeValue(context, tokenizer, tokenizer.expect("map value")); token = nextNotLineSep(tokenizer, "map key, sep or end"); } } else { while (!token.isSymbol(Token.kMessageEnd)) { if (!token.isIdentifier()) { throw new TokenizerException(token, "Invalid field name: " + token.asString()) .setLine(tokenizer.getLine()); } token = tokenizer.expect("field value sep"); if (token.isSymbol(DEFINE_REFERENCE)) { token = tokenizer.expectIdentifier("reference name"); context.setReference( context.initReference(token, tokenizer), null); token = tokenizer.expect("field value sep"); } if (token.isSymbol(Token.kMessageStart)) { consumeValue(context, tokenizer, token); } else if (token.isSymbol(Token.kFieldValueSep)) { consumeValue(context, tokenizer, tokenizer.expect("field value")); } else { throw new TokenizerException(token, "Unknown field value sep: " + token.asString()) .setLine(tokenizer.getLine()); } token = nextNotLineSep(tokenizer, "message field or end"); } } } else if (token.isSymbol(Token.kListStart)) { token = tokenizer.expect("list value or end"); while (!token.isSymbol(Token.kListEnd)) { consumeValue(context, tokenizer, token); if (tokenizer.expectSymbol("list separator or end", Token.kLineSep1, Token.kListEnd) == Token.kListEnd) { break; } token = tokenizer.expect("list value or end"); } } } private  ProvidenceConfigUtil(); }
ProvidenceConfigUtil { static void consumeValue(@Nonnull ProvidenceConfigContext context, @Nonnull Tokenizer tokenizer, @Nonnull Token token) throws IOException { boolean isMessage = false; if (UNDEFINED.equals(token.asString())) { return; } else if (token.asString().equals(Token.B64)) { tokenizer.expectSymbol("b64 body start", Token.kParamsStart); tokenizer.readBinary(Token.kParamsEnd); } else if (token.asString().equals(Token.HEX)) { tokenizer.expectSymbol("hex body start", Token.kParamsStart); tokenizer.readBinary(Token.kParamsEnd); } else if (token.isReferenceIdentifier()) { if (!tokenizer.peek("message start").isSymbol(Token.kMessageStart)) { return; } isMessage = true; token = tokenizer.expect("start of message"); } if (token.isSymbol(Token.kMessageStart)) { token = tokenizer.expect("map or message first entry"); if (token.isSymbol(Token.kMessageEnd)) { return; } Token firstSep = tokenizer.peek("First separator"); if (!isMessage && !firstSep.isSymbol(Token.kFieldValueSep) && !firstSep.isSymbol(Token.kMessageStart) && !firstSep.isSymbol(DEFINE_REFERENCE)) { while (!token.isSymbol(Token.kMessageEnd)) { if (!token.isIdentifier() && token.isReferenceIdentifier()) { throw new TokenizerException(token, "Invalid map key: " + token.asString()) .setLine(tokenizer.getLine()); } consumeValue(context, tokenizer, token); tokenizer.expectSymbol("key value sep.", Token.kKeyValueSep); consumeValue(context, tokenizer, tokenizer.expect("map value")); token = nextNotLineSep(tokenizer, "map key, sep or end"); } } else { while (!token.isSymbol(Token.kMessageEnd)) { if (!token.isIdentifier()) { throw new TokenizerException(token, "Invalid field name: " + token.asString()) .setLine(tokenizer.getLine()); } token = tokenizer.expect("field value sep"); if (token.isSymbol(DEFINE_REFERENCE)) { token = tokenizer.expectIdentifier("reference name"); context.setReference( context.initReference(token, tokenizer), null); token = tokenizer.expect("field value sep"); } if (token.isSymbol(Token.kMessageStart)) { consumeValue(context, tokenizer, token); } else if (token.isSymbol(Token.kFieldValueSep)) { consumeValue(context, tokenizer, tokenizer.expect("field value")); } else { throw new TokenizerException(token, "Unknown field value sep: " + token.asString()) .setLine(tokenizer.getLine()); } token = nextNotLineSep(tokenizer, "message field or end"); } } } else if (token.isSymbol(Token.kListStart)) { token = tokenizer.expect("list value or end"); while (!token.isSymbol(Token.kListEnd)) { consumeValue(context, tokenizer, token); if (tokenizer.expectSymbol("list separator or end", Token.kLineSep1, Token.kListEnd) == Token.kListEnd) { break; } token = tokenizer.expect("list value or end"); } } } private  ProvidenceConfigUtil(); static Path canonicalFileLocation(@Nonnull Path file); }
ProvidenceConfigUtil { static void consumeValue(@Nonnull ProvidenceConfigContext context, @Nonnull Tokenizer tokenizer, @Nonnull Token token) throws IOException { boolean isMessage = false; if (UNDEFINED.equals(token.asString())) { return; } else if (token.asString().equals(Token.B64)) { tokenizer.expectSymbol("b64 body start", Token.kParamsStart); tokenizer.readBinary(Token.kParamsEnd); } else if (token.asString().equals(Token.HEX)) { tokenizer.expectSymbol("hex body start", Token.kParamsStart); tokenizer.readBinary(Token.kParamsEnd); } else if (token.isReferenceIdentifier()) { if (!tokenizer.peek("message start").isSymbol(Token.kMessageStart)) { return; } isMessage = true; token = tokenizer.expect("start of message"); } if (token.isSymbol(Token.kMessageStart)) { token = tokenizer.expect("map or message first entry"); if (token.isSymbol(Token.kMessageEnd)) { return; } Token firstSep = tokenizer.peek("First separator"); if (!isMessage && !firstSep.isSymbol(Token.kFieldValueSep) && !firstSep.isSymbol(Token.kMessageStart) && !firstSep.isSymbol(DEFINE_REFERENCE)) { while (!token.isSymbol(Token.kMessageEnd)) { if (!token.isIdentifier() && token.isReferenceIdentifier()) { throw new TokenizerException(token, "Invalid map key: " + token.asString()) .setLine(tokenizer.getLine()); } consumeValue(context, tokenizer, token); tokenizer.expectSymbol("key value sep.", Token.kKeyValueSep); consumeValue(context, tokenizer, tokenizer.expect("map value")); token = nextNotLineSep(tokenizer, "map key, sep or end"); } } else { while (!token.isSymbol(Token.kMessageEnd)) { if (!token.isIdentifier()) { throw new TokenizerException(token, "Invalid field name: " + token.asString()) .setLine(tokenizer.getLine()); } token = tokenizer.expect("field value sep"); if (token.isSymbol(DEFINE_REFERENCE)) { token = tokenizer.expectIdentifier("reference name"); context.setReference( context.initReference(token, tokenizer), null); token = tokenizer.expect("field value sep"); } if (token.isSymbol(Token.kMessageStart)) { consumeValue(context, tokenizer, token); } else if (token.isSymbol(Token.kFieldValueSep)) { consumeValue(context, tokenizer, tokenizer.expect("field value")); } else { throw new TokenizerException(token, "Unknown field value sep: " + token.asString()) .setLine(tokenizer.getLine()); } token = nextNotLineSep(tokenizer, "message field or end"); } } } else if (token.isSymbol(Token.kListStart)) { token = tokenizer.expect("list value or end"); while (!token.isSymbol(Token.kListEnd)) { consumeValue(context, tokenizer, token); if (tokenizer.expectSymbol("list separator or end", Token.kLineSep1, Token.kListEnd) == Token.kListEnd) { break; } token = tokenizer.expect("list value or end"); } } } private  ProvidenceConfigUtil(); static Path canonicalFileLocation(@Nonnull Path file); static final String UNDEFINED; }