@Test public void testExpectantParser() { assertEquals(ExpectationResult.CORRECT, new ExpectantParser("abcdef").parse(new StringParserState("abcdef"))); assertEquals(ExpectationResult.INCORRECT, new ExpectantParser("abcdef").parse(new StringParserState("abcde"))); assertEquals(ExpectationResult.INCORRECT, new ExpectantParser("abcdef").parse(new StringParserState("abcdeg"))); }
@Override public T parse(ParserState input) throws ParseException { ExpectationResult result = Parsers.expect(type).parse(input); if (Parsers.nullOrUnexpected(result)) { return null; } ConjureMetrics.incrementCounter(metric); return instance; }
public static ExpectantParser expect(String expectation) { return new ExpectantParser(expectation); }
@Override public ListType parse(ParserState input) throws ParseException { ExpectationResult result = Parsers.expect("list").parse(input); if (Parsers.nullOrUnexpected(result)) { return null; } ConjureType itemType = Parsers.liberalBetween("<", TypeParser.INSTANCE, ">").parse(input); ConjureMetrics.incrementCounter(ListType.class); return ListType.of(itemType); } }
@Test public void testBetweenParser() throws ParseException { assertEquals("abcdef", new BetweenParser<String>(new ExpectantParser("{"), Parsers.prefix(Parsers.whitespace(), new RawStringParser()), Parsers.prefix(Parsers.whitespace(), new ExpectantParser("}"))) .parse(new StringParserState("{ abcdef }"))); }
@Override public SetType parse(ParserState input) throws ParseException { ExpectationResult result = Parsers.expect("set").parse(input); if (Parsers.nullOrUnexpected(result)) { return null; } ConjureType itemType = Parsers.liberalBetween("<", TypeParser.INSTANCE, ">").parse(input); ConjureMetrics.incrementCounter(SetType.class); return SetType.of(itemType); } }
@Test public void testEofParser() throws ParseException { assertEquals(ExpectationResult.CORRECT, Parsers.eof(new ExpectantParser("abc")) .parse(new StringParserState("abc"))); }
@Override public OptionalType parse(ParserState input) throws ParseException { ExpectationResult result = Parsers.expect("optional").parse(input); if (Parsers.nullOrUnexpected(result)) { return null; } ConjureType itemType = Parsers.liberalBetween("<", TypeParser.INSTANCE, ">").parse(input); ConjureMetrics.incrementCounter(OptionalType.class); return OptionalType.of(itemType); } }
@Test public void testEofParserFails() throws ParseException { assertNull(Parsers.eof(new ExpectantParser("abc")) .parse(new StringParserState("abcdef"))); }
@Override public ForeignReferenceType parse(ParserState input) throws ParseException { String namespace = NAMESPACE_PARSER.parse(input); if (Parsers.nullOrUnexpected(Parsers.expect(".").parse(input))) { return null; } String ref = TypeReferenceParser.REF_PARSER.parse(input); ConjureMetrics.incrementCounter(ForeignReferenceType.class); return ForeignReferenceType.of(Namespace.of(namespace), TypeName.of(ref)); } }
@Override public MapType parse(ParserState input) throws ParseException { ExpectationResult result = Parsers.expect("map").parse(input); if (Parsers.nullOrUnexpected(result)) { return null; } Parser<KeyValue<ConjureType, ConjureType>> kv = Parsers.liberalBetween( "<", new KeyValueParser<>( Parsers.whitespace(TypeParser.INSTANCE), Parsers.whitespace(Parsers.expect(",")), Parsers.whitespace(TypeParser.INSTANCE)), ">"); KeyValue<ConjureType, ConjureType> types = kv.parse(input); ConjureMetrics.incrementCounter(MapType.class); return MapType.of(types.getKey(), types.getValue()); } }