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 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); }
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); } }
/** * 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 ']'"); } }
ImmutableList.Builder<GroupElement> groups = ImmutableList.builder(); reader.require('{'); while (true) { String nestedDocumentation = reader.readDocumentation();
/** 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(); }
/** 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(); }
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 an enum constant like "ROCK = 0;". The label is the constant name. */ private EnumConstantElement readEnumConstant( String documentation, Location location, String label) { reader.require('='); int tag = reader.readInt(); ImmutableList<OptionElement> options = new OptionReader(reader).readOptions(); reader.require(';'); documentation = reader.tryAppendTrailingDocumentation(documentation); return EnumConstantElement.builder(location) .name(label) .tag(tag) .documentation(documentation) .options(options) .build(); }
String adapter = null; reader.require('{'); while (!reader.peekChar('}')) { Location wordLocation = reader.location(); if (!reader.readWord().equals("using")) throw reader.unexpected("expected 'using'"); String adapterType = reader.readWord(); reader.require('#'); String adapterConstant = reader.readWord(); reader.require(';'); adapter = adapterType + '#' + adapterConstant; break; reader.require(';'); break;
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(); }
/** 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(); }
fileBuilder.packageName(packageName); prefix = packageName + "."; reader.require(';'); return null; } else if (label.equals("import")) { imports.add(importString); reader.require(';'); return null; } else if (label.equals("syntax")) { if (!context.permitsSyntax()) throw reader.unexpected(location, "'syntax' in " + context); reader.require('='); if (index != 0) { throw reader.unexpected( throw reader.unexpected(location, e.getMessage()); reader.require(';'); return null; } else if (label.equals("option")) { OptionElement result = new OptionReader(reader).readOption('='); reader.require(';'); return result; } else if (label.equals("reserved")) {
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); } }
/** 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 AbstractProfileFileElement read() { String label = reader.readWord(); if (!"syntax".equals(label)) { throw reader.unexpected("expected 'syntax'"); } reader.require('='); String syntaxString = reader.readQuotedString(); if (!"wire2".equals(syntaxString)) { 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); } }
/** 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(); }