@Override public String parse(ParserState input) throws ParseException { StringBuilder sb = new StringBuilder(); if (input.curr() != '"') { throw new ParseException("Expected a quoted string, but didn't observe a quote.", input); } int curr; boolean escaped = false; while ((curr = input.next()) != -1) { if (escaped) { escaped = false; } else { if (curr == '\\') { escaped = true; continue; } else if (curr == '"') { input.next(); break; } } if (curr == -1) { throw new ParseException("Reached end of file while processing quoted string.", input); } if (respectLineBreaks || curr != '\n') { sb.append((char) curr); } } return sb.toString(); }
@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 { 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 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; } }
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)); }
throw new ParseException("Unknown directive '" + directive + "' and no default parser specified.", input);
@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; }