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); } }
String name = reader.readName(); MessageElement.Builder builder = MessageElement.builder(location) .name(name)
/** Reads an enumerated type declaration and returns it. */ private EnumElement readEnumElement(Location location, String documentation) { String name = reader.readName(); EnumElement.Builder builder = EnumElement.builder(location) .name(name) .documentation(documentation); ImmutableList.Builder<EnumConstantElement> constants = ImmutableList.builder(); ImmutableList.Builder<OptionElement> options = ImmutableList.builder(); reader.require('{'); while (true) { String valueDocumentation = reader.readDocumentation(); if (reader.peekChar('}')) break; Object declared = readDeclaration(valueDocumentation, Context.ENUM); if (declared instanceof EnumConstantElement) { constants.add((EnumConstantElement) declared); } else if (declared instanceof OptionElement) { options.add((OptionElement) declared); } } return builder.options(options.build()) .constants(constants.build()) .build(); }
/** Reads a service declaration and returns it. */ private ServiceElement readService(Location location, String documentation) { String name = reader.readName(); ServiceElement.Builder builder = ServiceElement.builder(location) .name(name) .documentation(documentation); reader.require('{'); ImmutableList.Builder<RpcElement> rpcs = ImmutableList.builder(); ImmutableList.Builder<OptionElement> options = ImmutableList.builder(); while (true) { String rpcDocumentation = reader.readDocumentation(); if (reader.peekChar('}')) break; Object declared = readDeclaration(rpcDocumentation, Context.SERVICE); if (declared instanceof RpcElement) { rpcs.add((RpcElement) declared); } else if (declared instanceof OptionElement) { options.add((OptionElement) declared); } } return builder.options(options.build()) .rpcs(rpcs.build()) .build(); }
private OneOfElement readOneOf(String documentation) { OneOfElement.Builder builder = OneOfElement.builder() .name(reader.readName()) .documentation(documentation); ImmutableList.Builder<FieldElement> fields = ImmutableList.builder(); ImmutableList.Builder<GroupElement> groups = ImmutableList.builder(); reader.require('{'); while (true) { String nestedDocumentation = reader.readDocumentation(); if (reader.peekChar('}')) break; Location location = reader.location(); String type = reader.readDataType(); if (type.equals("group")) { groups.add(readGroup(location, nestedDocumentation, null)); } else { fields.add(readField(location, nestedDocumentation, null, type)); } } return builder.fields(fields.build()) .groups(groups.build()) .build(); }
/** Reads an extend declaration. */ private ExtendElement readExtend(Location location, String documentation) { String name = reader.readName(); ExtendElement.Builder builder = ExtendElement.builder(location) .name(name) .documentation(documentation); reader.require('{'); ImmutableList.Builder<FieldElement> fields = ImmutableList.builder(); while (true) { String nestedDocumentation = reader.readDocumentation(); if (reader.peekChar('}')) break; Object declared = readDeclaration(nestedDocumentation, Context.EXTEND); if (declared instanceof FieldElement) { fields.add((FieldElement) declared); } } return builder.fields(fields.build()) .build(); }
/** 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); }
if (!context.permitsPackage()) throw reader.unexpected(location, "'package' in " + context); if (packageName != null) throw reader.unexpected(location, "too many package names"); packageName = reader.readName(); fileBuilder.packageName(packageName); prefix = packageName + ".";
/** Reads an field declaration and returns it. */ private FieldElement readField( Location location, String documentation, @Nullable Field.Label label, String type) { String name = reader.readName(); reader.require('='); int tag = reader.readInt(); FieldElement.Builder builder = FieldElement.builder(location) .label(label) .type(type) .name(name) .tag(tag); List<OptionElement> options = new OptionReader(reader).readOptions(); reader.require(';'); options = new ArrayList<>(options); // Mutable copy for extractDefault. String defaultValue = stripDefault(options); documentation = reader.tryAppendTrailingDocumentation(documentation); return builder.documentation(documentation) .defaultValue(defaultValue) .options(ImmutableList.copyOf(options)) .build(); }
.name(reader.readName()) .documentation(documentation);
private void readDeclaration(String documentation) { Location location = reader.location(); String label = reader.readWord(); if ("package".equals(label)) { if (packageName != null) { throw reader.unexpected(location, "too many package names"); } packageName = reader.readName(); reader.require(';'); } else if ("import".equals(label)) { String importString = reader.readString(); imports.add(importString); reader.require(';'); } else if ("type".equals(label)) { typeConfigs.add(readTypeConfig(location, documentation)); } else { throw reader.unexpected(location, "unexpected label: " + label); } }
String name = reader.readName(); MessageElement.Builder builder = MessageElement.builder(location) .name(name)
/** Reads an enumerated type declaration and returns it. */ private EnumElement readEnumElement(Location location, String documentation) { String name = reader.readName(); EnumElement.Builder builder = EnumElement.builder(location) .name(name) .documentation(documentation); ImmutableList.Builder<EnumConstantElement> constants = ImmutableList.builder(); ImmutableList.Builder<OptionElement> options = ImmutableList.builder(); reader.require('{'); while (true) { String valueDocumentation = reader.readDocumentation(); if (reader.peekChar('}')) break; Object declared = readDeclaration(valueDocumentation, Context.ENUM); if (declared instanceof EnumConstantElement) { constants.add((EnumConstantElement) declared); } else if (declared instanceof OptionElement) { options.add((OptionElement) declared); } } return builder.options(options.build()) .constants(constants.build()) .build(); }
/** Reads a service declaration and returns it. */ private ServiceElement readService(Location location, String documentation) { String name = reader.readName(); ServiceElement.Builder builder = ServiceElement.builder(location) .name(name) .documentation(documentation); reader.require('{'); ImmutableList.Builder<RpcElement> rpcs = ImmutableList.builder(); ImmutableList.Builder<OptionElement> options = ImmutableList.builder(); while (true) { String rpcDocumentation = reader.readDocumentation(); if (reader.peekChar('}')) break; Object declared = readDeclaration(rpcDocumentation, Context.SERVICE); if (declared instanceof RpcElement) { rpcs.add((RpcElement) declared); } else if (declared instanceof OptionElement) { options.add((OptionElement) declared); } } return builder.options(options.build()) .rpcs(rpcs.build()) .build(); }
private OneOfElement readOneOf(String documentation) { OneOfElement.Builder builder = OneOfElement.builder() .name(reader.readName()) .documentation(documentation); ImmutableList.Builder<FieldElement> fields = ImmutableList.builder(); ImmutableList.Builder<GroupElement> groups = ImmutableList.builder(); reader.require('{'); while (true) { String nestedDocumentation = reader.readDocumentation(); if (reader.peekChar('}')) break; Location location = reader.location(); String type = reader.readDataType(); if (type.equals("group")) { groups.add(readGroup(nestedDocumentation, null)); } else { fields.add(readField(location, nestedDocumentation, null, type)); } } return builder.fields(fields.build()) .groups(groups.build()) .build(); }
/** Reads an extend declaration. */ private ExtendElement readExtend(Location location, String documentation) { String name = reader.readName(); ExtendElement.Builder builder = ExtendElement.builder(location) .name(name) .documentation(documentation); reader.require('{'); ImmutableList.Builder<FieldElement> fields = ImmutableList.builder(); while (true) { String nestedDocumentation = reader.readDocumentation(); if (reader.peekChar('}')) break; Object declared = readDeclaration(nestedDocumentation, Context.EXTEND); if (declared instanceof FieldElement) { fields.add((FieldElement) declared); } } return builder.fields(fields.build()) .build(); }
/** Reads a option containing a name, an '=' or ':', and a value. */ private 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"); } OptionKindAndValue kindAndValue = readKindAndValue(); OptionElement.Kind kind = kindAndValue.kind(); Object value = kindAndValue.value(); if (subName != null) { value = OptionElement.create(subName, kind, value); kind = OptionElement.Kind.OPTION; } return OptionElement.create(name, kind, value, isParenthesized); }
String name = reader.readName(); reader.require('='); int tag = reader.readInt();
if (!context.permitsPackage()) throw reader.unexpected(location, "'package' in " + context); if (packageName != null) throw reader.unexpected(location, "too many package names"); packageName = reader.readName(); fileBuilder.packageName(packageName); prefix = packageName + ".";
.name(reader.readName()) .documentation(documentation);