/** Reads a non-whitespace character 'c', or throws an exception. */ public void require(char c) { if (readChar() != c) throw unexpected("expected '" + c + "'"); }
/** * Peeks a non-whitespace character and returns it. The only difference * between this and {@code readChar} is that this doesn't consume the char. */ public char peekChar() { skipWhitespace(true); if (pos == data.length) throw unexpected("unexpected end of file"); return data[pos]; }
/** Reads an integer and returns it. */ public int readInt() { String tag = readWord(); try { int radix = 10; if (tag.startsWith("0x") || tag.startsWith("0X")) { tag = tag.substring("0x".length()); radix = 16; } return Integer.valueOf(tag, radix); } catch (Exception e) { throw unexpected("expected an integer but was " + tag); } }
public RuntimeException unexpected(String message) { return unexpected(location(), message); }
private char readNumericEscape(int radix, int len) { int value = -1; for (int endPos = Math.min(pos + len, data.length); pos < endPos; pos++) { int digit = hexDigit(data[pos]); if (digit == -1 || digit >= radix) break; if (value < 0) { value = digit; } else { value = value * radix + digit; } } if (value < 0) throw unexpected("expected a digit after \\x or \\X"); return (char) value; }
/** Reads a scalar, map, or type name with {@code name} as a prefix word. */ public String readDataType(String name) { if (name.equals("map")) { if (readChar() != '<') throw unexpected("expected '<'"); String keyType = readDataType(); if (readChar() != ',') throw unexpected("expected ','"); String valueType = readDataType(); if (readChar() != '>') throw unexpected("expected '>'"); return String.format("map<%s, %s>", keyType, valueType); } else { return name; } }
/** Reads a non-empty word and returns it. */ public String readWord() { skipWhitespace(true); int start = pos; while (pos < data.length) { char c = data[pos]; if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || (c == '_') || (c == '-') || (c == '.')) { pos++; } else { break; } } if (start == pos) { throw unexpected("expected a word"); } return new String(data, start, pos - start); }
/** * Reads options enclosed in '[' and ']' if they are present and returns them. Returns an empty * list if no options are present. */ public ImmutableList<OptionElement> readOptions() { if (!reader.peekChar('[')) return ImmutableList.of(); ImmutableList.Builder<OptionElement> result = ImmutableList.builder(); while (true) { result.add(readOption('=')); // Check for closing ']' if (reader.peekChar(']')) break; // Discard optional ','. if (!reader.peekChar(',')) throw reader.unexpected("Expected ',' or ']"); } return result.build(); }
private void readDeclaration(String documentation) { Location location = reader.location(); String label = reader.readWord(); if (label.equals("package")) { if (packageName != null) throw reader.unexpected(location, "too many package names"); packageName = reader.readName(); reader.require(';'); } else if (label.equals("import")) { String importString = reader.readString(); imports.add(importString); reader.require(';'); } else if (label.equals("type")) { typeConfigs.add(readTypeConfig(location, documentation)); } else { throw reader.unexpected(location, "unexpected label: " + label); } }
/** Reads a reserved tags and names list like "reserved 10, 12 to 14, 'foo';". */ private ReservedElement readReserved(Location location, String documentation) { ImmutableList.Builder<Object> valuesBuilder = ImmutableList.builder(); while (true) { char c = reader.peekChar(); if (c == '"' || c == '\'') { valuesBuilder.add(reader.readQuotedString()); } else { int tagStart = reader.readInt(); c = reader.peekChar(); if (c != ',' && c != ';') { if (!reader.readWord().equals("to")) { throw reader.unexpected("expected ',', ';', or 'to'"); } int tagEnd = reader.readInt(); valuesBuilder.add(Range.closed(tagStart, tagEnd)); } else { valuesBuilder.add(tagStart); } } c = reader.readChar(); if (c == ';') break; if (c != ',') throw reader.unexpected("expected ',' or ';'"); } ImmutableList<Object> values = valuesBuilder.build(); if (values.isEmpty()) { throw reader.unexpected("'reserved' must have at least one field name or tag"); } return ReservedElement.create(location, documentation, values); }
case "required": if (syntax == ProtoFile.Syntax.PROTO_3) { throw reader.unexpected( location, "'required' label forbidden in proto3 field declarations"); throw reader.unexpected( location, "'optional' label forbidden in proto3 field declarations"); if (syntax != ProtoFile.Syntax.PROTO_3 && (!word.equals("map") || reader.peekChar() != '<')) { throw reader.unexpected(location, "unexpected label: " + word); throw reader.unexpected(location, "'map' type cannot have label");
/** Reads a (paren-wrapped), [square-wrapped] or naked symbol name. */ public String readName() { String optionName; char c = peekChar(); if (c == '(') { pos++; optionName = readWord(); if (readChar() != ')') throw unexpected("expected ')'"); } else if (c == '[') { pos++; optionName = readWord(); if (readChar() != ']') throw unexpected("expected ']'"); } else { optionName = readWord(); } return optionName; }
/** Reads extensions like "extensions 101;" or "extensions 101 to max;". */ private ExtensionsElement readExtensions(Location location, String documentation) { int start = reader.readInt(); // Range start. int end = start; if (reader.peekChar() != ';') { if (!"to".equals(reader.readWord())) throw reader.unexpected("expected ';' or 'to'"); String s = reader.readWord(); // Range end. if (s.equals("max")) { end = Util.MAX_TAG_VALUE; } else { end = Integer.parseInt(s); } } reader.require(';'); return ExtensionsElement.create(location, start, end, documentation); }
/** * Returns a list of values. This is similar to JSON with '[' and ']' surrounding the list and ',' * separating values. */ private List<Object> readList() { reader.require('['); List<Object> result = new ArrayList<>(); while (true) { // If we see the close brace, finish immediately. This handles [] and ,] cases. if (reader.peekChar(']')) return result; result.add(readKindAndValue().value()); if (reader.peekChar(',')) continue; if (reader.peekChar() != ']') throw reader.unexpected("expected ',' or ']'"); } }
public ProfileFileElement read() { String label = reader.readWord(); if (!label.equals("syntax")) throw reader.unexpected("expected 'syntax'"); reader.require('='); String syntaxString = reader.readQuotedString(); if (!syntaxString.equals("wire2")) throw reader.unexpected("expected 'wire2'"); reader.require(';'); while (true) { String documentation = reader.readDocumentation(); if (reader.exhausted()) { return fileBuilder.packageName(packageName) .imports(imports.build()) .typeConfigs(typeConfigs.build()) .build(); } readDeclaration(documentation); } }
if (pos == data.length) throw unexpected("unexpected end of file"); c = data[pos++]; switch (c) { if (c == '\n') newline(); throw unexpected("unterminated string");
switch (word) { case "target": if (target != null) throw reader.unexpected(wordLocation, "too many targets"); target = reader.readWord(); if (!reader.readWord().equals("using")) throw reader.unexpected("expected 'using'"); String adapterType = reader.readWord(); reader.require('#'); throw reader.unexpected(wordLocation, "unexpected label: " + word);
private GroupElement readGroup(Location location, String documentation, Field.Label label) { String name = reader.readWord(); reader.require('='); int tag = reader.readInt(); GroupElement.Builder builder = GroupElement.builder(location) .label(label) .name(name) .tag(tag) .documentation(documentation); ImmutableList.Builder<FieldElement> fields = ImmutableList.builder(); reader.require('{'); while (true) { String nestedDocumentation = reader.readDocumentation(); if (reader.peekChar('}')) break; Location fieldLocation = reader.location(); String fieldLabel = reader.readWord(); Object field = readField(nestedDocumentation, fieldLocation, fieldLabel); if (!(field instanceof FieldElement)) { throw reader.unexpected("expected field declaration, was " + field); } fields.add((FieldElement) field); } return builder.fields(fields.build()) .build(); }
reader.require(')'); if (!reader.readWord().equals("returns")) throw reader.unexpected("expected 'returns'");
/** Reads a option containing a name, an '=' or ':', and a value. */ public OptionElement readOption(char keyValueSeparator) { boolean isExtension = (reader.peekChar() == '['); boolean isParenthesized = (reader.peekChar() == '('); String name = reader.readName(); // Option name. if (isExtension) { name = "[" + name + "]"; } String subName = null; char c = reader.readChar(); if (c == '.') { // Read nested field name. For example "baz" in "(foo.bar).baz = 12". subName = reader.readName(); c = reader.readChar(); } if (keyValueSeparator == ':' && c == '{') { // In text format, values which are maps can omit a separator. Backtrack so it can be re-read. reader.pushBack('{'); } else if (c != keyValueSeparator) { throw reader.unexpected("expected '" + keyValueSeparator + "' in option"); } KindAndValue kindAndValue = readKindAndValue(); Kind kind = kindAndValue.kind(); Object value = kindAndValue.value(); if (subName != null) { value = OptionElement.create(subName, kind, value); kind = Kind.OPTION; } return OptionElement.create(name, kind, value, isParenthesized); }