@Override public List<T> parse(ParserState input) throws ParseException { List<T> results = new ArrayList<>(); do { T val = Parsers.gingerly(valueParser).parse(input); if (!Parsers.nullOrUnexpected(val)) { results.add(val); } else { break; } } while (Parsers.gingerly(separator).parse(input) != null); return results; }
@Override public KeyValue<A, B> parse(ParserState input) throws ParseException { A key = Parsers.gingerly(keyParser).parse(input); if (!Parsers.nullOrUnexpected(key)) { if (!Parsers.nullOrUnexpected(Parsers.gingerly(separator).parse(input))) { B val = Parsers.gingerly(valueParser).parse(input); if (!Parsers.nullOrUnexpected(val)) { return new KeyValue<>(key, val); } else { throw new ParseException("Found key '" + key + "' without associated value.", input); } } } return null; }
@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; }
@Override public Map<A, B> parse(ParserState input) throws ParseException { Map<A, B> results = Maps.newHashMap(); do { A key = Parsers.gingerly(keyParser).parse(input); if (!Parsers.nullOrUnexpected(key)) { B val = Parsers.gingerly(valueParser).parse(input); if (!Parsers.nullOrUnexpected(val)) { results.put(key, val); } else { throw new ParseException("Found key '" + key + "' without associated value.", input); } } else { break; } } while (Parsers.gingerly(separator).parse(input) != null); return results; } }
@Override public T parse(ParserState input) throws ParseException { // First, consume the thing you expect to find at the beginning. // This is likely to be a string constant like "{". if (Parsers.nullOrUnexpected(start.parse(input))) { // TODO(melliot): improve this exception throw new ParseException("Expected startToken", input); } // Then, consume the thing you expect to find in the middle. // This is likely to be done with a more complicated parser // such as DispatchingParser. T item = parser.parse(input); // Finally, consume the thing you expect to find at the end. // This is likely to be a string constant like "}". if (Parsers.nullOrUnexpected(end.parse(input))) { // TODO(melliot): improve this exception throw new ParseException("Expected endToken", input); } // The thing we care about was in the middle. // Parser<?>, and return the T.) return item; }
@Override public T parse(ParserState input) throws ParseException { input.mark(); T result = parser.parse(input); if (!nullOrUnexpected(result)) { input.release(); } else { input.rewind(); } return result; } };
@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); } }
@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); } }
@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); } }
@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()); } }