/** 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); } }
/** 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); } }