* Makefile.am:
[monodevelop.git] / extras / NemerleBinding / NemerleBindingCompilerServices.cs
blobc16893e7f62a89c24b75d9edf08c1aaf7b97e469
1 using System;
2 using System.Collections;
3 using System.Diagnostics;
4 using System.IO;
5 using System.CodeDom.Compiler;
6 using System.Threading;
8 using MonoDevelop.Core;
9 using MonoDevelop.Projects;
10 using MonoDevelop.Core.Gui.Components;
12 namespace NemerleBinding
14 public class NemerleBindingCompilerServices
16 class CompilerResultsParser : CompilerResults
18 public CompilerResultsParser() : base (new TempFileCollection ())
22 bool SetErrorType(CompilerError error, string t)
24 switch(t.Trim ())
26 case "error":
27 error.IsWarning = false;
28 return true;
29 case "warning":
30 error.IsWarning = true;
31 return true;
32 case "hint":
33 error.IsWarning = true;
34 return true;
35 default:
36 return false;
40 public void Parse(string l)
42 CompilerError error = new CompilerError();
43 error.ErrorNumber = String.Empty;
45 char [] delim = {':'};
46 string [] s = l.Split(delim, 7);
48 try
50 SetErrorType (error, s[5]);
51 if (s[6].StartsWith ("N") && s[6].Contains (": "))
53 string[] e = s[6].Split (delim, 2);
54 error.ErrorNumber = s[0];
55 error.ErrorText = s[1].Trim ();
57 else
58 error.ErrorText = s[6].Trim ();
59 error.FileName = s[0];
60 error.Line = int.Parse(s[1]);
61 error.Column = int.Parse(s[2]);
63 catch
65 SetErrorType (error, s[0]);
66 error.ErrorText = s[1].Trim ();
67 error.FileName = "";
68 error.Line = 0;
69 error.Column = 0;
72 /*if (SetErrorType(error, s[5]))
74 error.ErrorText = s[6]; // l.Substring(l.IndexOf(s[0]+": ") + s[0].Length+2);
75 error.FileName = "";
76 error.Line = 0;
77 error.Column = 0;
78 } else
79 if ((s.Length >= 4) && SetErrorType(error, s[3].Substring(1)))
81 error.ErrorText = l.Substring(l.IndexOf(s[3]+": ") + s[3].Length+2);
82 error.FileName = s[0];
83 error.Line = int.Parse(s[1]);
84 error.Column = int.Parse(s[2]);
85 } else
87 error.ErrorText = l;
88 error.FileName = "";
89 error.Line = 0;
90 error.Column = 0;
91 error.IsWarning = false;
92 }*/
93 Errors.Add(error);
96 public BuildResult GetResult()
98 return new BuildResult(this, "");
102 static string ncc = "ncc";
104 private string GetOptionsString (DotNetProjectConfiguration configuration, NemerleParameters cp)
106 string options = " ";
107 if (cp.Nostdmacros)
108 options += " -no-stdmacros";
109 if (cp.Nostdlib)
110 options += " -no-stdlib";
111 if (cp.Ot)
112 options += " -Ot";
113 if (cp.Greedy)
114 options += " -greedy";
115 if (cp.Pedantic)
116 options += " -pedantic-lexer";
117 if (configuration.CompileTarget == CompileTarget.Library)
118 options += " -tdll";
120 return options;
123 public bool CanCompile(string fileName)
125 return (System.IO.Path.GetExtension(fileName).ToLower() == ".n");
128 public BuildResult Compile (ProjectFileCollection projectFiles, ProjectReferenceCollection projectReferences, DotNetProjectConfiguration configuration, IProgressMonitor monitor)
130 NemerleParameters cp = (NemerleParameters) configuration.CompilationParameters;
131 if (cp == null) cp = new NemerleParameters ();
133 string references = "";
134 string files = "";
136 foreach (ProjectReference lib in projectReferences)
137 foreach (string a in lib.GetReferencedFileNames())
138 references += " -r \"" + a + "\"";
140 foreach (ProjectFile f in projectFiles)
141 if (f.Subtype != Subtype.Directory)
142 switch (f.BuildAction)
144 case BuildAction.Compile:
145 files += " \"" + f.Name + "\"";
146 break;
149 if (!Directory.Exists (configuration.OutputDirectory))
150 Directory.CreateDirectory (configuration.OutputDirectory);
152 string args = "-q -no-color " + GetOptionsString (configuration, cp) + references + files + " -o " + configuration.CompiledOutputName;
153 return DoCompilation (args);
156 // This enables check if we have output without blocking
157 class VProcess : Process
159 Thread t = null;
160 public void thr()
162 while (StandardOutput.Peek() == -1){};
164 public void OutWatch()
166 t = new Thread(new ThreadStart(thr));
167 t.Start();
169 public bool HasNoOut()
171 return t.IsAlive;
175 private BuildResult DoCompilation(string arguments)
177 string l;
178 ProcessStartInfo si = new ProcessStartInfo(ncc, arguments);
179 si.RedirectStandardOutput = true;
180 si.RedirectStandardError = true;
181 si.UseShellExecute = false;
182 VProcess p = new VProcess();
183 p.StartInfo = si;
184 p.Start();
186 p.OutWatch();
187 while ((!p.HasExited) && p.HasNoOut())
188 // while ((!p.HasExited) && (p.StandardOutput.Peek() == -1)) // this could eliminate VProcess outgrowth
190 System.Threading.Thread.Sleep (100);
193 CompilerResultsParser cr = new CompilerResultsParser();
194 while ((l = p.StandardOutput.ReadLine()) != null)
196 cr.Parse(l);
199 if ((l = p.StandardError.ReadLine()) != null)
201 cr.Parse("error: " + ncc + " execution problem");
204 return cr.GetResult();