Search in sources :

Example 1 with Compiler

use of org.stringtemplate.v4.compiler.Compiler in project antlr4 by antlr.

the class BaseCppTest method execModule.

public String execModule(String fileName) {
    String runtimePath = locateRuntime();
    String includePath = runtimePath + "/runtime/src";
    String binPath = new File(new File(tmpdir), "a.out").getAbsolutePath();
    String inputPath = new File(new File(tmpdir), "input").getAbsolutePath();
    // Build runtime using cmake once.
    synchronized (runtimeBuiltOnce) {
        if (!runtimeBuiltOnce) {
            try {
                String[] command = { "clang++", "--version" };
                String output = runCommand(command, tmpdir, "printing compiler version", false);
                System.out.println("Compiler version is: " + output);
            } catch (Exception e) {
                System.err.println("Can't get compiler version");
            }
            runtimeBuiltOnce = true;
            if (!buildRuntime()) {
                System.out.println("C++ runtime build failed\n");
                return null;
            }
            System.out.println("C++ runtime build succeeded\n");
        }
    }
    // Create symlink to the runtime. Currently only used on OSX.
    String libExtension = (getOS().equals("mac")) ? "dylib" : "so";
    try {
        String[] command = { "ln", "-s", runtimePath + "/dist/libantlr4-runtime." + libExtension };
        if (runCommand(command, tmpdir, "sym linking C++ runtime", true) == null)
            return null;
    } catch (Exception e) {
        System.err.println("can't create link to " + runtimePath + "/dist/libantlr4-runtime." + libExtension);
        e.printStackTrace(System.err);
        return null;
    }
    try {
        List<String> command2 = new ArrayList<String>(Arrays.asList("clang++", "-std=c++11", "-I", includePath, "-L.", "-lantlr4-runtime", "-o", "a.out"));
        command2.addAll(allCppFiles(tmpdir));
        if (runCommand(command2.toArray(new String[0]), tmpdir, "building test binary", true) == null) {
            return null;
        }
    } catch (Exception e) {
        System.err.println("can't compile test module: " + e.getMessage());
        e.printStackTrace(System.err);
        return null;
    }
    // Now run the newly minted binary. Reset the error output, as we could have got compiler warnings which are not relevant here.
    this.stderrDuringParse = null;
    try {
        ProcessBuilder builder = new ProcessBuilder(binPath, inputPath);
        builder.directory(new File(tmpdir));
        Map<String, String> env = builder.environment();
        env.put("LD_PRELOAD", runtimePath + "/dist/libantlr4-runtime." + libExtension);
        String output = runProcess(builder, "running test binary", false);
        if (output.length() == 0) {
            output = null;
        }
        /* for debugging
		  System.out.println("=========================================================");
		  System.out.println(output);
		  System.out.println("=========================================================");
		  */
        return output;
    } catch (Exception e) {
        System.err.println("can't exec module: " + fileName);
        e.printStackTrace(System.err);
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) STGroupString(org.stringtemplate.v4.STGroupString) BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString) File(java.io.File) BaseRuntimeTest.writeFile(org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile) URISyntaxException(java.net.URISyntaxException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with Compiler

use of org.stringtemplate.v4.compiler.Compiler in project compiler by boalang.

the class BaseTest method codegen.

protected StartContext codegen(final String input, final String error) throws IOException {
    final File outputRoot = new File(new File(System.getProperty("java.io.tmpdir")), UUID.randomUUID().toString());
    final File outputSrcDir = new File(outputRoot, "boa");
    if (!outputSrcDir.mkdirs())
        throw new IOException("unable to mkdir " + outputSrcDir);
    final File outputFile = new File(outputSrcDir, "Test.java");
    CodeGeneratingVisitor.combineAggregatorStrings.clear();
    CodeGeneratingVisitor.reduceAggregatorStrings.clear();
    final List<String> jobnames = new ArrayList<String>();
    final List<String> jobs = new ArrayList<String>();
    final List<Integer> seeds = new ArrayList<Integer>();
    final StartContext ctx = typecheck(input);
    // use the whole input string to seed the RNG
    seeds.add(input.hashCode());
    final Start p = ctx.ast;
    try {
        new InheritedAttributeTransformer().start(p);
        new LocalAggregationTransformer().start(p);
        new VisitorOptimizingTransformer().start(p);
        final CodeGeneratingVisitor cg = new CodeGeneratingVisitor("1");
        cg.start(p);
        jobs.add(cg.getCode());
        jobnames.add("1");
        final ST st = AbstractCodeGeneratingVisitor.stg.getInstanceOf("Program");
        st.add("name", "Test");
        st.add("numreducers", 1);
        st.add("jobs", jobs);
        st.add("jobnames", jobnames);
        st.add("combineTables", CodeGeneratingVisitor.combineAggregatorStrings);
        st.add("reduceTables", CodeGeneratingVisitor.reduceAggregatorStrings);
        st.add("splitsize", 64 * 1024 * 1024);
        st.add("seeds", seeds);
        final BufferedOutputStream o = new BufferedOutputStream(new FileOutputStream(outputFile));
        try {
            o.write(st.render().getBytes());
        } finally {
            o.close();
        }
        final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        final DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
        final StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
        final Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(Arrays.asList(new File[] { outputFile }));
        if (!compiler.getTask(null, fileManager, diagnostics, Arrays.asList(new String[] { "-cp", System.getProperty("java.class.path") }), null, compilationUnits).call())
            for (final Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) throw new RuntimeException("Error on line " + diagnostic.getLineNumber() + ": " + diagnostic.getMessage(null));
        if (error != null)
            fail("expected to see exception: " + error);
    } catch (final Exception e) {
        if (error == null) {
            if (e.getMessage() == null) {
                e.printStackTrace();
                fail("unexpected exception");
            } else
                fail("found unexpected exception: " + e.getMessage());
        } else
            assertEquals(error, e.getMessage());
    }
    delete(outputSrcDir);
    return ctx;
}
Also used : Start(boa.compiler.ast.Start) ArrayList(java.util.ArrayList) Diagnostic(javax.tools.Diagnostic) JavaFileObject(javax.tools.JavaFileObject) VisitorOptimizingTransformer(boa.compiler.transforms.VisitorOptimizingTransformer) StandardJavaFileManager(javax.tools.StandardJavaFileManager) DiagnosticCollector(javax.tools.DiagnosticCollector) BufferedOutputStream(java.io.BufferedOutputStream) ST(org.stringtemplate.v4.ST) InheritedAttributeTransformer(boa.compiler.transforms.InheritedAttributeTransformer) JavaCompiler(javax.tools.JavaCompiler) LocalAggregationTransformer(boa.compiler.transforms.LocalAggregationTransformer) IOException(java.io.IOException) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) IOException(java.io.IOException) RecognitionException(org.antlr.v4.runtime.RecognitionException) StartContext(boa.parser.BoaParser.StartContext) FileOutputStream(java.io.FileOutputStream) AbstractCodeGeneratingVisitor(boa.compiler.visitors.AbstractCodeGeneratingVisitor) CodeGeneratingVisitor(boa.compiler.visitors.CodeGeneratingVisitor) File(java.io.File)

Example 3 with Compiler

use of org.stringtemplate.v4.compiler.Compiler in project bndtools by bndtools.

the class StringTemplateEngine method loadRawTemplate.

private CompiledST loadRawTemplate(STGroup stg, String name, Resource resource) throws IOException {
    if (resource.getType() != ResourceType.File)
        throw new IllegalArgumentException(String.format("Cannot build resource from resource of type %s (name='%s').", resource.getType(), name));
    try (InputStream is = resource.getContent()) {
        ANTLRInputStream templateStream = new ANTLRInputStream(is, resource.getTextEncoding());
        String template = templateStream.substring(0, templateStream.size() - 1);
        CompiledST impl = new Compiler(stg).compile(name, template);
        CommonToken nameT = new CommonToken(STLexer.SEMI);
        nameT.setInputStream(templateStream);
        stg.rawDefineTemplate("/" + name, impl, nameT);
        impl.defineImplicitlyDefinedTemplates(stg);
        return impl;
    }
}
Also used : Compiler(org.stringtemplate.v4.compiler.Compiler) ANTLRInputStream(st4hidden.org.antlr.runtime.ANTLRInputStream) InputStream(java.io.InputStream) CommonToken(st4hidden.org.antlr.runtime.CommonToken) ANTLRInputStream(st4hidden.org.antlr.runtime.ANTLRInputStream) CompiledST(org.stringtemplate.v4.compiler.CompiledST)

Example 4 with Compiler

use of org.stringtemplate.v4.compiler.Compiler in project antlr4 by antlr.

the class BaseJavaTest method compile.

/**
	 * Wow! much faster than compiling outside of VM. Finicky though.
	 * Had rules called r and modulo. Wouldn't compile til I changed to 'a'.
	 */
protected boolean compile(String... fileNames) {
    List<File> files = new ArrayList<File>();
    for (String fileName : fileNames) {
        File f = new File(tmpdir, fileName);
        files.add(f);
    }
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    //		DiagnosticCollector<JavaFileObject> diagnostics =
    //			new DiagnosticCollector<JavaFileObject>();
    StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
    Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(files);
    Iterable<String> compileOptions = Arrays.asList("-g", "-source", "1.6", "-target", "1.6", "-implicit:class", "-Xlint:-options", "-d", tmpdir, "-cp", tmpdir + pathSep + CLASSPATH);
    JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, null, compileOptions, null, compilationUnits);
    boolean ok = task.call();
    try {
        fileManager.close();
    } catch (IOException ioe) {
        ioe.printStackTrace(System.err);
    }
    return ok;
}
Also used : ArrayList(java.util.ArrayList) JavaCompiler(javax.tools.JavaCompiler) StandardJavaFileManager(javax.tools.StandardJavaFileManager) STGroupString(org.stringtemplate.v4.STGroupString) IOException(java.io.IOException) File(java.io.File) BaseRuntimeTest.writeFile(org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile)

Aggregations

File (java.io.File)3 ArrayList (java.util.ArrayList)3 IOException (java.io.IOException)2 JavaCompiler (javax.tools.JavaCompiler)2 StandardJavaFileManager (javax.tools.StandardJavaFileManager)2 BaseRuntimeTest.writeFile (org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile)2 STGroupString (org.stringtemplate.v4.STGroupString)2 Start (boa.compiler.ast.Start)1 InheritedAttributeTransformer (boa.compiler.transforms.InheritedAttributeTransformer)1 LocalAggregationTransformer (boa.compiler.transforms.LocalAggregationTransformer)1 VisitorOptimizingTransformer (boa.compiler.transforms.VisitorOptimizingTransformer)1 AbstractCodeGeneratingVisitor (boa.compiler.visitors.AbstractCodeGeneratingVisitor)1 CodeGeneratingVisitor (boa.compiler.visitors.CodeGeneratingVisitor)1 StartContext (boa.parser.BoaParser.StartContext)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 URISyntaxException (java.net.URISyntaxException)1 Diagnostic (javax.tools.Diagnostic)1