Search in sources :

Example 1 with ParseException

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);
}
Also used : TokenizerException(net.morimekta.providence.serializer.pretty.TokenizerException) ArgumentException(net.morimekta.console.args.ArgumentException) ParseException(net.morimekta.providence.reflect.parser.ParseException) IOException(java.io.IOException) ArgumentParser(net.morimekta.console.args.ArgumentParser) SerializerException(net.morimekta.providence.serializer.SerializerException) TokenizerException(net.morimekta.providence.serializer.pretty.TokenizerException) IOException(java.io.IOException) ParseException(net.morimekta.providence.reflect.parser.ParseException) SerializerException(net.morimekta.providence.serializer.SerializerException) ArgumentException(net.morimekta.console.args.ArgumentException)

Example 2 with ParseException

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;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) FileInputStream(java.io.FileInputStream) ProgramTypeRegistry(net.morimekta.providence.reflect.util.ProgramTypeRegistry) BufferedInputStream(java.io.BufferedInputStream) CProgram(net.morimekta.providence.reflect.contained.CProgram) ProgramType(net.morimekta.providence.model.ProgramType) ParseException(net.morimekta.providence.reflect.parser.ParseException) File(java.io.File)

Example 3 with ParseException

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);
}
Also used : ProgramParser(net.morimekta.providence.reflect.parser.ProgramParser) TypeLoader(net.morimekta.providence.reflect.TypeLoader) IOException(java.io.IOException) ArgumentParser(net.morimekta.console.args.ArgumentParser) GeneratorException(net.morimekta.providence.generator.GeneratorException) IOException(java.io.IOException) ParseException(net.morimekta.providence.reflect.parser.ParseException) ArgumentException(net.morimekta.console.args.ArgumentException) GeneratorFactory(net.morimekta.providence.generator.GeneratorFactory) ProgramTypeRegistry(net.morimekta.providence.reflect.util.ProgramTypeRegistry) ArgumentException(net.morimekta.console.args.ArgumentException) ParseException(net.morimekta.providence.reflect.parser.ParseException) GeneratorException(net.morimekta.providence.generator.GeneratorException) File(java.io.File) Generator(net.morimekta.providence.generator.Generator)

Example 4 with ParseException

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("//"));
}
Also used : Token(net.morimekta.providence.serializer.pretty.Token) ParseException(net.morimekta.providence.reflect.parser.ParseException) Test(org.junit.Test)

Example 5 with ParseException

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()));
    }
}
Also used : ParseException(net.morimekta.providence.reflect.parser.ParseException) File(java.io.File) Test(org.junit.Test)

Aggregations

ParseException (net.morimekta.providence.reflect.parser.ParseException)5 File (java.io.File)3 IOException (java.io.IOException)2 ArgumentException (net.morimekta.console.args.ArgumentException)2 ArgumentParser (net.morimekta.console.args.ArgumentParser)2 ProgramTypeRegistry (net.morimekta.providence.reflect.util.ProgramTypeRegistry)2 Test (org.junit.Test)2 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Generator (net.morimekta.providence.generator.Generator)1 GeneratorException (net.morimekta.providence.generator.GeneratorException)1 GeneratorFactory (net.morimekta.providence.generator.GeneratorFactory)1 ProgramType (net.morimekta.providence.model.ProgramType)1 TypeLoader (net.morimekta.providence.reflect.TypeLoader)1 CProgram (net.morimekta.providence.reflect.contained.CProgram)1 ProgramParser (net.morimekta.providence.reflect.parser.ProgramParser)1 SerializerException (net.morimekta.providence.serializer.SerializerException)1 Token (net.morimekta.providence.serializer.pretty.Token)1