@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 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 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; }
String directive = Parsers.gingerly(inputStringParser).parse(input);
@Test public void testGingerlyBooleanFalse() throws ParseException { StringParserState state = new StringParserState("false 123"); assertEquals(Boolean.FALSE, Parsers.gingerly(bp).parse(state)); assertEquals("123", rsp.parse(state)); }
@Test public void testGingerlyBooleanTrue() throws ParseException { StringParserState state = new StringParserState("true 123"); assertEquals(Boolean.TRUE, Parsers.gingerly(bp).parse(state)); assertEquals("123", rsp.parse(state)); }