@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; } };
/** * Runs the whitespace parser followed by `parser`, returning result of the latter. * * @param <T> the type the target parser returns * @param parser the target parser to run * @return the specified parser */ public static <T> Parser<T> whitespace(Parser<T> parser) { return prefix(whitespace(), parser); }
@Test public void testEofParserFails() throws ParseException { assertNull(Parsers.eof(new ExpectantParser("abc")) .parse(new StringParserState("abcdef"))); }
@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; }
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)); }
@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)); }
@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 int curr() { return delegate.curr(); }
@Override public int getLine() { return delegate.getLine(); }
@Override public void rewind() { delegate.rewind(); }
@Override public int next() { return delegate.next(); }
@Override public void mark() { delegate.mark(); }
@Override public void release() { delegate.release(); }
@Test public void testEofParser() throws ParseException { assertEquals(ExpectationResult.CORRECT, Parsers.eof(new ExpectantParser("abc")) .parse(new StringParserState("abc"))); }
@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)); }
@Override public int next() { // not sure this matters, but we'll enforce that current // never exceeds actual length by more than 1 current = (++current <= seq.length()) ? current : seq.length(); return curr(); }
@Override public int getCharPosition() { return delegate.getLine(); }