use of com.squareup.wire.schema.Location in project wire by square.
the class ProtoParser method readDeclaration.
private Object readDeclaration(String documentation, Context context) {
int index = declarationCount++;
// Skip unnecessary semicolons, occasionally used after a nested message declaration.
if (reader.peekChar(';'))
return null;
Location location = reader.location();
String label = reader.readWord();
if (label.equals("package")) {
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 + ".";
reader.require(';');
return null;
} else if (label.equals("import")) {
if (!context.permitsImport())
throw reader.unexpected(location, "'import' in " + context);
String importString = reader.readString();
if ("public".equals(importString)) {
publicImports.add(reader.readString());
} else {
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(location, "'syntax' element must be the first declaration in a file");
}
String syntaxString = reader.readQuotedString();
try {
syntax = ProtoFile.Syntax.get(syntaxString);
} catch (IllegalArgumentException e) {
throw reader.unexpected(location, e.getMessage());
}
reader.require(';');
return null;
} else if (label.equals("option")) {
OptionElement result = readOption('=');
reader.require(';');
return result;
} else if (label.equals("reserved")) {
return readReserved(location, documentation);
} else if (label.equals("message")) {
return readMessage(location, documentation);
} else if (label.equals("enum")) {
return readEnumElement(location, documentation);
} else if (label.equals("service")) {
return readService(location, documentation);
} else if (label.equals("extend")) {
return readExtend(location, documentation);
} else if (label.equals("rpc")) {
if (!context.permitsRpc())
throw reader.unexpected(location, "'rpc' in " + context);
return readRpc(location, documentation);
} else if (label.equals("oneof")) {
if (!context.permitsOneOf()) {
throw reader.unexpected(location, "'oneof' must be nested in message");
}
return readOneOf(documentation);
} else if (label.equals("extensions")) {
if (!context.permitsExtensions()) {
throw reader.unexpected(location, "'extensions' must be nested");
}
return readExtensions(location, documentation);
} else if (context == Context.MESSAGE || context == Context.EXTEND) {
return readField(documentation, location, label);
} else if (context == Context.ENUM) {
return readEnumConstant(documentation, location, label);
} else {
throw reader.unexpected(location, "unexpected label: " + label);
}
}
use of com.squareup.wire.schema.Location in project wire by square.
the class ProfileLoader method load.
public Profile load() throws IOException {
Set<Location> protoLocations = new LinkedHashSet<>();
for (ProtoFile protoFile : schema.protoFiles()) {
protoLocations.add(protoFile.location());
}
Multimap<Path, String> pathsToAttempt = pathsToAttempt(protoLocations);
ImmutableList<ProfileFileElement> profileFiles = loadProfileFiles(pathsToAttempt);
Profile profile = new Profile(profileFiles);
validate(schema, profileFiles);
return profile;
}
use of com.squareup.wire.schema.Location in project wire by square.
the class ProfileParser method readTypeConfig.
/** Reads a type config and returns it. */
private TypeConfigElement readTypeConfig(Location location, String documentation) {
String name = reader.readDataType();
String target = null;
String adapter = null;
reader.require('{');
while (!reader.peekChar('}')) {
Location wordLocation = reader.location();
String word = reader.readWord();
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('#');
String adapterConstant = reader.readWord();
reader.require(';');
adapter = adapterType + '#' + adapterConstant;
break;
default:
throw reader.unexpected(wordLocation, "unexpected label: " + word);
}
}
return TypeConfigElement.builder(location).type(name).documentation(documentation).target(target).adapter(adapter).build();
}
use of com.squareup.wire.schema.Location in project wire by square.
the class ProfileLoader method loadProfileFile.
/**
* Parses the {@code .wire} file at {@code base/path} and returns it. Returns null if no such
* file exists.
*/
private ProfileFileElement loadProfileFile(Path base, String path) throws IOException {
Source source = source(base, path);
if (source == null)
return null;
try {
Location location = Location.get(base.toString(), path);
String data = Okio.buffer(source).readUtf8();
return new ProfileParser(location, data).read();
} catch (IOException e) {
throw new IOException("Failed to load " + source + " from " + base, e);
} finally {
source.close();
}
}
use of com.squareup.wire.schema.Location in project wire by square.
the class ProfileParser method readDeclaration.
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);
}
}
Aggregations