Beispiel #1
0
        /** Return true if all is ok, no errors */
        protected virtual bool antlr(string fileName, string grammarFileName, string grammarStr, bool defaultListener, params string[] extraOptions)
        {
            mkdir(tmpdir);
            writeFile(tmpdir, fileName, grammarStr);
            try
            {
                string compiler = PathCombine(JavaHome, "bin", "java.exe");

                List <string> classpath = new List <string>();
                classpath.Add(GetMavenArtifact("com.tunnelvisionlabs", "antlr4-csharp", "4.0.1-SNAPSHOT"));
                classpath.Add(GetMavenArtifact("com.tunnelvisionlabs", "antlr4-runtime", "4.0.1-SNAPSHOT"));
                classpath.Add(GetMavenArtifact("com.tunnelvisionlabs", "antlr4", "4.0.1-SNAPSHOT"));
                classpath.Add(GetMavenArtifact("org.antlr", "antlr-runtime", "3.5"));
                classpath.Add(GetMavenArtifact("org.antlr", "ST4", "4.0.7"));

                List <string> options = new List <string>();
                options.Add("-cp");
                options.Add(Utils.Join(";", classpath));
                options.Add("org.antlr.v4.Tool");

                options.AddRange(extraOptions);
                options.Add("-o");
                options.Add(tmpdir);
                options.Add("-lib");
                options.Add(tmpdir);
                options.Add("-Dlanguage=CSharp");
                options.Add(grammarFileName);

                System.Diagnostics.Process process = System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(compiler, '"' + Utils.Join("\" \"", options) + '"')
                {
                    UseShellExecute        = false,
                    CreateNoWindow         = true,
                    RedirectStandardOutput = true,
                    RedirectStandardError  = true,
                    WorkingDirectory       = tmpdir
                });

                StreamVacuum stdout = new StreamVacuum(process.StandardOutput);
                StreamVacuum stderr = new StreamVacuum(process.StandardError);
                stdout.start();
                stderr.start();
                process.WaitForExit();
                stdout.join();
                stderr.join();
                if (stdout.ToString().Length > 0)
                {
                    Console.Error.WriteLine("compile stdout from: " + Utils.Join(" ", options));
                    Console.Error.WriteLine(stdout);
                }
                if (stderr.ToString().Length > 0)
                {
                    Console.Error.WriteLine("compile stderr from: " + Utils.Join(" ", options));
                    Console.Error.WriteLine(stderr);
                }
                int ret = process.ExitCode;
                return(ret == 0);
            }
            catch (Exception)
            {
                Console.Error.WriteLine("can't exec compilation");
                //e.printStackTrace(System.err);
                return(false);
            }
        }
Beispiel #2
0
        public string execClass(string className)
        {
            if (TEST_IN_SAME_PROCESS)
            {
                try
                {
                    ClassLoader       loader       = new URLClassLoader(new URL[] { new File(tmpdir).toURI().toURL() }, ClassLoader.getSystemClassLoader());
                    Class             mainClass    = (Class)loader.loadClass(className);
                    Method            mainMethod   = mainClass.getDeclaredMethod("main", typeof(string[]));
                    PipedInputStream  stdoutIn     = new PipedInputStream();
                    PipedInputStream  stderrIn     = new PipedInputStream();
                    PipedOutputStream stdoutOut    = new PipedOutputStream(stdoutIn);
                    PipedOutputStream stderrOut    = new PipedOutputStream(stderrIn);
                    StreamVacuum      stdoutVacuum = new StreamVacuum(stdoutIn);
                    StreamVacuum      stderrVacuum = new StreamVacuum(stderrIn);

                    PrintStream originalOut = Console.Out;
                    System.setOut(new PrintStream(stdoutOut));
                    try
                    {
                        PrintStream originalErr = System.err;
                        try
                        {
                            System.setErr(new PrintStream(stderrOut));
                            stdoutVacuum.start();
                            stderrVacuum.start();
                            mainMethod.invoke(null, (Object) new string[] { new File(tmpdir, "input").getAbsolutePath() });
                        }
                        finally
                        {
                            System.setErr(originalErr);
                        }
                    }
                    finally
                    {
                        System.setOut(originalOut);
                    }

                    stdoutOut.close();
                    stderrOut.close();
                    stdoutVacuum.join();
                    stderrVacuum.join();
                    string output = stdoutVacuum.tostring();
                    if (stderrVacuum.tostring().length() > 0)
                    {
                        this.stderrDuringParse = stderrVacuum.tostring();
                        Console.Error.WriteLine("exec stderrVacuum: " + stderrVacuum);
                    }
                    return(output);
                }
                catch (MalformedURLException ex)
                {
                    LOGGER.log(Level.SEVERE, null, ex);
                }
                catch (IOException ex)
                {
                    LOGGER.log(Level.SEVERE, null, ex);
                }
                catch (InterruptedException ex)
                {
                    LOGGER.log(Level.SEVERE, null, ex);
                }
                catch (IllegalAccessException ex)
                {
                    LOGGER.log(Level.SEVERE, null, ex);
                }
                catch (IllegalArgumentException ex)
                {
                    LOGGER.log(Level.SEVERE, null, ex);
                }
                catch (InvocationTargetException ex)
                {
                    LOGGER.log(Level.SEVERE, null, ex);
                }
                catch (NoSuchMethodException ex)
                {
                    LOGGER.log(Level.SEVERE, null, ex);
                }
                catch (SecurityException ex)
                {
                    LOGGER.log(Level.SEVERE, null, ex);
                }
                catch (ClassNotFoundException ex)
                {
                    LOGGER.log(Level.SEVERE, null, ex);
                }
            }

            try
            {
                string[] args = new string[] {
                    "java", "-classpath", tmpdir + pathSep + CLASSPATH,
                    className, new File(tmpdir, "input").getAbsolutePath()
                };
                //string cmdLine = "java -classpath "+CLASSPATH+pathSep+tmpdir+" Test " + new File(tmpdir, "input").getAbsolutePath();
                //Console.WriteLine("execParser: "+cmdLine);
                Process process =
                    Runtime.getRuntime().exec(args, null, new File(tmpdir));
                StreamVacuum stdoutVacuum = new StreamVacuum(process.getInputStream());
                StreamVacuum stderrVacuum = new StreamVacuum(process.getErrorStream());
                stdoutVacuum.start();
                stderrVacuum.start();
                process.waitFor();
                stdoutVacuum.join();
                stderrVacuum.join();
                string output = stdoutVacuum.tostring();
                if (stderrVacuum.tostring().length() > 0)
                {
                    this.stderrDuringParse = stderrVacuum.tostring();
                    Console.Error.WriteLine("exec stderrVacuum: " + stderrVacuum);
                }
                return(output);
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("can't exec recognizer");
                e.printStackTrace(System.err);
            }
            return(null);
        }
Beispiel #3
0
        /** 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 virtual bool compile(params string[] fileNames)
        {
            DirectoryInfo outputDir = new DirectoryInfo(tmpdir);

            try
            {
                string compiler = PathCombine(WindowsFolder, "Microsoft.NET", "Framework64", "v4.0.30319", "csc.exe");

                List <string> args = new List <string>();
                args.AddRange(getCompileOptions());

                bool hasTestClass = false;
                foreach (String fileName in fileNames)
                {
                    if (fileName.Equals("Test.cs"))
                    {
                        hasTestClass = true;
                    }

                    if (fileName.EndsWith(".dll"))
                    {
                        args.Add("/reference:" + fileName);
                    }
                    else
                    {
                        args.Add(fileName);
                    }
                }

                if (hasTestClass)
                {
                    args.Insert(1, "/target:exe");
                    args.Insert(1, "/reference:Parser.dll");
                    args.Insert(1, "/out:Test.exe");
                }
                else
                {
                    args.Insert(1, "/target:library");
                    args.Insert(1, "/out:Parser.dll");
                }

                System.Diagnostics.Process process = System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(compiler, '"' + Utils.Join("\" \"", args) + '"')
                {
                    UseShellExecute        = false,
                    CreateNoWindow         = true,
                    RedirectStandardOutput = true,
                    RedirectStandardError  = true,
                    WorkingDirectory       = tmpdir
                });

                StreamVacuum stdout = new StreamVacuum(process.StandardOutput);
                StreamVacuum stderr = new StreamVacuum(process.StandardError);
                stdout.start();
                stderr.start();
                process.WaitForExit();
                stdout.join();
                stderr.join();
                if (stdout.ToString().Length > 0)
                {
                    Console.Error.WriteLine("compile stdout from: " + Utils.Join(" ", args));
                    Console.Error.WriteLine(stdout);
                }
                if (stderr.ToString().Length > 0)
                {
                    Console.Error.WriteLine("compile stderr from: " + Utils.Join(" ", args));
                    Console.Error.WriteLine(stderr);
                }
                int ret = process.ExitCode;
                return(ret == 0);
            }
            catch (Exception)
            {
                Console.Error.WriteLine("can't exec compilation");
                //e.printStackTrace(System.err);
                return(false);
            }
        }