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;
}
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;
}
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;
}
}
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;
}
Aggregations