use of net.morimekta.providence.reflect.parser.ParseException in project providence by morimekta.
the class Config method run.
public void run(String... args) {
ConfigOptions op = new ConfigOptions(tty);
try {
ArgumentParser cli = op.getArgumentParser("pvdcfg", "Providence Config Tool");
try {
cli.parse(args);
if (op.showHelp()) {
System.out.println(cli.getDescription() + " - " + getVersionString());
System.out.println("Usage: " + cli.getSingleLineUsage());
System.out.println();
cli.printUsage(System.out);
System.out.println();
System.out.println("Available Commands:");
System.out.println();
op.getCommandSet().printUsage(System.out);
return;
} else if (op.showVersion()) {
System.out.println(cli.getDescription() + " - " + getVersionString());
return;
}
cli.validate();
op.execute();
return;
} catch (ArgumentException e) {
System.err.println("Invalid argument: " + e.getMessage());
System.err.println("Usage: " + cli.getSingleLineUsage());
if (op.verbose()) {
e.printStackTrace();
}
} catch (ParseException e) {
System.out.flush();
System.err.println(e.asString());
if (op.verbose()) {
System.err.println();
e.printStackTrace();
}
} catch (TokenizerException e) {
System.out.flush();
System.err.println(e.asString());
if (op.verbose()) {
System.err.println();
e.printStackTrace();
}
} catch (SerializerException e) {
System.out.flush();
System.err.println("Serialization error: " + e.toString());
if (op.verbose()) {
System.err.println();
e.printStackTrace();
}
} catch (IOException | RuntimeException e) {
System.out.flush();
System.err.println("IO Error: " + e.toString());
if (op.verbose()) {
System.err.println();
e.printStackTrace();
}
}
} catch (Exception e) {
System.out.flush();
System.err.println("Unhandled exception: " + e.toString());
if (op.verbose()) {
System.err.println();
e.printStackTrace();
}
}
exit(1);
}
use of net.morimekta.providence.reflect.parser.ParseException in project providence by morimekta.
the class TypeLoader method loadInternal.
private ProgramTypeRegistry loadInternal(File file, List<String> loadStack) throws IOException {
loadStack = new ArrayList<>(loadStack);
file = file.getCanonicalFile();
if (!file.exists()) {
throw new IllegalArgumentException("No such file " + file);
}
if (!file.isFile()) {
throw new IllegalArgumentException("Unable to load thrift program: " + file + " is not a file.");
}
file = file.getAbsoluteFile();
String path = file.getPath();
if (loadStack.contains(path)) {
// Only show the circular includes, not the path to get there.
while (!loadStack.get(0).equals(path)) {
loadStack.remove(0);
}
loadStack.add(path);
String prefix = longestCommonPrefixPath(loadStack);
if (prefix.length() > 0) {
loadStack = stripCommonPrefix(loadStack);
throw new IllegalArgumentException("Circular includes detected: " + prefix + "... " + String.join(" -> ", loadStack));
}
throw new IllegalArgumentException("Circular includes detected: " + String.join(" -> ", loadStack));
}
loadStack.add(path);
ProgramTypeRegistry registry = this.programRegistry.registryForPath(path);
if (programRegistry.containsProgramPath(path)) {
return registry;
}
InputStream in = new BufferedInputStream(new FileInputStream(file));
ProgramType doc = parser.parse(in, file, includes);
ArrayList<File> queue = new ArrayList<>();
if (doc.hasIncludes()) {
for (String include : doc.getIncludes()) {
File location = new File(file.getParent(), include).getCanonicalFile();
if (!location.exists()) {
if (include.startsWith(".") || include.startsWith(File.separator)) {
throw new ParseException("No such file \"" + include + "\" to include from " + file.getName());
}
for (File inc : includes) {
File i = new File(inc, include);
if (i.exists()) {
location = i.getCanonicalFile();
break;
}
}
}
if (location.exists() && !queue.contains(location)) {
queue.add(location.getAbsoluteFile());
}
}
}
// Load includes in reverse order, in case of serial dependencies.
Collections.reverse(queue);
loadedDocuments.put(path, doc);
for (File include : queue) {
registry.registerInclude(programNameFromPath(include.getPath()), loadInternal(include, ImmutableList.copyOf(loadStack)));
}
// Now everything it depends on is loaded.
CProgram program = converter.convert(path, doc);
programRegistry.putProgram(path, program);
programRegistry.putProgramType(path, doc);
return registry;
}
use of net.morimekta.providence.reflect.parser.ParseException in project providence by morimekta.
the class GeneratorMain method run.
public void run(String... args) {
try {
ArgumentParser cli = options.getArgumentParser("pvdgen", "Providence generator");
cli.parse(args);
if (options.isHelp()) {
System.out.println("Providence generator - " + Utils.getVersionString());
System.out.println("Usage: pvdgen [-I dir] [-o dir] -g generator[:opt[,opt]*] file...");
System.out.println();
if (options.help.factory != null) {
System.out.format("%s : %s%n", options.help.factory.generatorName(), options.help.factory.generatorDescription());
System.out.println();
System.out.println("Available options");
System.out.println();
options.help.factory.printGeneratorOptionsHelp(System.out);
} else {
System.out.println("Example code to run:");
System.out.println("$ pvdgen -I thrift/ --out target/ --gen java:android thrift/the-one.thrift");
System.out.println();
cli.printUsage(System.out);
System.out.println();
System.out.println("Available generators:");
Map<String, GeneratorFactory> factories = options.getFactories();
for (GeneratorFactory lang : factories.values()) {
System.out.format(" - %-10s : %s%n", lang.generatorName(), lang.generatorDescription());
}
}
return;
} else if (options.version) {
System.out.println("Providence generator - " + Utils.getVersionString());
return;
}
cli.validate();
ProgramParser parser = options.getParser();
List<File> includes = options.getIncludes();
List<File> input = options.getInputFiles();
TypeLoader loader = new TypeLoader(includes, parser);
Generator generator = options.getGenerator(loader);
for (File f : input) {
ProgramTypeRegistry registry = loader.load(f);
if (options.skipIfMissingNamespace && registry.getProgram().getNamespaceForLanguage(options.gen.factory.generatorName()) == null) {
System.out.println("Skipping (no " + options.gen.factory.generatorName() + " namespace) " + f.getCanonicalFile().getAbsolutePath());
}
generator.generate(loader.load(f));
}
generator.generateGlobal(loader.getProgramRegistry(), input);
return;
} catch (ArgumentException e) {
System.err.println("Usage: pvdgen [-I dir] [-o dir] -g generator[:opt[,opt]*] file...");
System.err.println(e.getLocalizedMessage());
System.err.println();
System.err.println("Run $ pvdgen --help # for available options.");
if (options.verbose) {
e.printStackTrace();
}
} catch (ParseException e) {
System.err.println(e.asString());
if (options.verbose) {
e.printStackTrace();
}
} catch (IllegalArgumentException e) {
System.err.println(e.getMessage());
if (options.verbose) {
e.printStackTrace();
}
} catch (GeneratorException e) {
System.err.println("Generator error: " + e.getMessage());
if (options.verbose) {
e.printStackTrace();
}
} catch (IOException e) {
System.err.println("I/O error: " + e.getMessage());
if (options.verbose) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
exit(1);
}
use of net.morimekta.providence.reflect.parser.ParseException in project providence by morimekta.
the class ThriftTokenizerTest method testJavaComments.
@Test
public void testJavaComments() throws IOException {
try {
tokenizer("/").next();
fail("no exception");
} catch (ParseException e) {
assertThat(e.asString(), is("Error on line 1, pos 2: Expected java-style comment, got end of file\n" + "/\n" + "-^"));
}
try {
tokenizer("/b").next();
fail("no exception");
} catch (ParseException e) {
assertThat(e.asString(), is("Error on line 1, pos 1: Expected java-style comment, got 'b' after '/'\n" + "/b\n" + "^^"));
}
Token token = tokenizer("\n\n// b\n").next();
assertThat(token, is(notNullValue()));
assertThat(token.asString(), is("//"));
}
use of net.morimekta.providence.reflect.parser.ParseException in project providence by morimekta.
the class TypeLoaderTest method testFailures.
@Test
public void testFailures() throws IOException {
File fail = copyResourceTo("/failure/duplicate_field_id.thrift", temp.getRoot());
TypeLoader loader = new TypeLoader(ImmutableList.of());
File folder = temp.newFolder("boo");
try {
loader.load(folder);
fail("no exception");
} catch (IllegalArgumentException e) {
assertThat(e.getMessage(), is("Unable to load thrift program: " + folder.getCanonicalFile() + " is not a file."));
}
File noFile = new File(temp.getRoot(), "boo");
try {
loader.load(noFile);
fail("no exception");
} catch (IllegalArgumentException e) {
assertThat(e.getMessage(), is("Unable to load thrift program: " + noFile.getCanonicalFile() + " is not a file."));
}
try {
loader.load(fail);
fail("no exception");
} catch (ParseException e) {
assertThat(e.getMessage(), is("Field id 1 already exists in T"));
assertThat(e.getFile(), is(fail.getName()));
}
}
Aggregations