@Override public Integer parse(ParserState input) throws ParseException { String str = RAW_INT_PARSER.parse(input); return str != null ? Integer.valueOf(str) : null; }
@Override public T parse(ParserState input) throws ParseException { T result = parser.parse(input); if (input.next() != -1) { return null; } return result; } };
@Override public ConjureType parse(ParserState input) throws ParseException { return typeParser().parse(input); }
@Override public T parse(ParserState input) throws ParseException { T result = gingerly(firstOption).parse(input); if (result != null) { return result; } for (Parser<? extends T> nextOption : otherOptions) { result = gingerly(nextOption).parse(input); if (result != null) { return result; } } return result; } };
@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 T parse(ParserState input) throws ParseException { T lastResult = null; while (input.curr() != -1) { String directive = Parsers.gingerly(inputStringParser).parse(input); if (Parsers.nullOrEmpty(directive)) { break; } else if (parserLookup.containsKey(directive)) { lastResult = parserLookup.get(directive).parse(input); } else { throw new ParseException("Unknown directive '" + directive + "'.", input); } } return lastResult; }
@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 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 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 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 T parse(ParserState input) throws ParseException { input.mark(); T result = parser.parse(input); if (!nullOrUnexpected(result)) { input.release(); } else { input.rewind(); } return result; } };
public ConjureType parse(String input) throws ParseException { ParserState inputParserState = new StringParserState(input); ConjureType resultType = Parsers.eof(typeParser()).parse(inputParserState); if (resultType == null) { throw new ParseException(input, inputParserState); } return parse(new StringParserState(input)); }
@Override public LocalReferenceType parse(ParserState input) throws ParseException { input.mark(); String typeReference = REF_PARSER.parse(input); if (typeReference == null) { input.rewind(); return null; } input.release(); ConjureMetrics.incrementCounter(LocalReferenceType.class); return LocalReferenceType.of(TypeName.of(typeReference)); } }
@Test public void testEofParserFails() throws ParseException { assertNull(Parsers.eof(new ExpectantParser("abc")) .parse(new StringParserState("abcdef"))); }
@Test public void testEofParser() throws ParseException { assertEquals(ExpectationResult.CORRECT, Parsers.eof(new ExpectantParser("abc")) .parse(new StringParserState("abc"))); }
@Test public void testOrParser() throws ParseException { Parser<String> rawNoPunctuation = new RawStringParser(new RawStringParser.AllowableCharacters() { @Override public boolean isAllowed(char character) { return Character.isLetterOrDigit(character); } @Override public String getDescription() { return "Character.isLetterOrDigit"; } }); assertEquals("abcdef", Parsers.or(rawNoPunctuation, new QuotedStringParser()).parse(new StringParserState("abcdef"))); assertEquals("abcdef", Parsers.or(rawNoPunctuation, new QuotedStringParser()).parse(new StringParserState("\"abcdef\""))); }
@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()); } }