Search in sources :

Example 1 with org.antlr.v4.runtime

use of org.antlr.v4.runtime in project antlr4 by antlr.

the class BaseGoTest method groupSetUp.

/**
	 * Copies all files from go runtime to a temporary folder that is inside a valid GOPATH project structure.
	 */
public static void groupSetUp() throws Exception {
    tmpGopath = new File(System.getProperty("java.io.tmpdir"), "antlr-goruntime-tmpgopath-" + Long.toHexString(System.currentTimeMillis()));
    ArrayList<String> pathsegments = new ArrayList<String>();
    pathsegments.add("src");
    pathsegments.addAll(Arrays.asList(GO_RUNTIME_IMPORT_PATH.split("/")));
    File tmpPackageDir = tmpGopath;
    for (String pathsegment : pathsegments) {
        tmpPackageDir = new File(tmpPackageDir, pathsegment);
    }
    if (!tmpPackageDir.mkdirs()) {
        throw new Exception("Could not create temp go runtime package dirs!");
    }
    File[] runtimeFiles = locateRuntime().listFiles();
    if (runtimeFiles == null) {
        throw new Exception("Go runtime file list is empty.");
    }
    for (File runtimeFile : runtimeFiles) {
        File dest = new File(tmpPackageDir, runtimeFile.getName());
        copyFile(runtimeFile, dest);
    }
    cacheGoRuntime(tmpPackageDir);
}
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) IOException(java.io.IOException)

Example 2 with org.antlr.v4.runtime

use of org.antlr.v4.runtime in project antlr4 by antlr.

the class BaseGoTest method locateRuntime.

private static File locateRuntime() {
    final ClassLoader loader = Thread.currentThread().getContextClassLoader();
    final URL runtimeSrc = loader.getResource("Go");
    if (runtimeSrc == null) {
        throw new RuntimeException("Cannot find Go ANTLR runtime");
    }
    File runtimeDir = new File(runtimeSrc.getPath(), "antlr");
    if (!runtimeDir.exists()) {
        throw new RuntimeException("Cannot find Go ANTLR runtime");
    }
    return runtimeDir;
}
Also used : File(java.io.File) BaseRuntimeTest.writeFile(org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile) URL(java.net.URL)

Example 3 with org.antlr.v4.runtime

use of org.antlr.v4.runtime in project antlr4 by antlr.

the class BaseCppTest method locateRuntime.

protected String locateRuntime() {
    final ClassLoader loader = Thread.currentThread().getContextClassLoader();
    final URL runtimeURL = loader.getResource("Cpp");
    if (runtimeURL == null) {
        throw new RuntimeException("Cannot find runtime");
    }
    // Windows not getting runtime right. See:
    // http://stackoverflow.com/questions/6164448/convert-url-to-normal-windows-filename-java
    // it was coming back "/C:/projects/antlr4-l7imv/runtime-testsuite/target/classes/Cpp"
    String p;
    try {
        p = Paths.get(runtimeURL.toURI()).toFile().toString();
    } catch (URISyntaxException use) {
        p = "Can't find runtime";
    }
    return p;
}
Also used : STGroupString(org.stringtemplate.v4.STGroupString) BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString) URISyntaxException(java.net.URISyntaxException) URL(java.net.URL)

Example 4 with org.antlr.v4.runtime

use of org.antlr.v4.runtime in project antlr4 by antlr.

the class BaseCppTest method buildRuntime.

// TODO: add a buildRuntimeOnWindows variant.
private boolean buildRuntime() {
    String runtimePath = locateRuntime();
    System.out.println("Building ANTLR4 C++ runtime (if necessary) at " + runtimePath);
    try {
        String[] command = { "cmake", ".", /*"-DCMAKE_CXX_COMPILER=clang++",*/
        "-DCMAKE_BUILD_TYPE=release" };
        if (runCommand(command, runtimePath, "antlr runtime cmake", false) == null) {
            return false;
        }
    } catch (Exception e) {
        System.err.println("can't configure antlr cpp runtime cmake file");
    }
    try {
        // Assuming a reasonable amount of available CPU cores.
        String[] command = { "make", "-j", "8" };
        if (runCommand(command, runtimePath, "building antlr runtime", true) == null)
            return false;
    } catch (Exception e) {
        System.err.println("can't compile antlr cpp runtime");
        e.printStackTrace(System.err);
        try {
            String[] command = { "ls", "-la" };
            String output = runCommand(command, runtimePath + "/dist/", "printing library folder content", true);
            System.out.println(output);
        } catch (Exception e2) {
            System.err.println("can't even list folder content");
            e2.printStackTrace(System.err);
        }
    }
    return true;
}
Also used : STGroupString(org.stringtemplate.v4.STGroupString) BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString) URISyntaxException(java.net.URISyntaxException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 5 with org.antlr.v4.runtime

use of org.antlr.v4.runtime 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)

Aggregations

BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)8 File (java.io.File)6 URL (java.net.URL)6 BaseRuntimeTest.writeFile (org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile)6 STGroupString (org.stringtemplate.v4.STGroupString)6 IOException (java.io.IOException)5 URISyntaxException (java.net.URISyntaxException)3 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)3 InputStream (java.io.InputStream)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ArrayList (java.util.ArrayList)2 NoViableAltException (org.antlr.v4.runtime.NoViableAltException)2 RecognitionException (org.antlr.v4.runtime.RecognitionException)2 Pair (org.antlr.v4.runtime.misc.Pair)2 ParseTree (org.antlr.v4.runtime.tree.ParseTree)2 ST (org.stringtemplate.v4.ST)2 STGroup (org.stringtemplate.v4.STGroup)2 STGroupFile (org.stringtemplate.v4.STGroupFile)2 StringRenderer (org.stringtemplate.v4.StringRenderer)2 Position (ast.Ast.Position)1