sync
[app.Builder.js] / src / Palete / ValaSource.vala
1
2 // valac TreeBuilder.vala --pkg libvala-0.24 --pkg posix -o /tmp/treebuilder
3
4 namespace Palete {
5          
6         
7         public class ValaSourceReport  : Vala.Report {
8
9                 public JsRender.JsRender file;
10                 
11                 //public Gee.ArrayList<ValaSourceNotice> notices;
12                  
13                 public Gee.HashMap<int,string> line_errors;
14                 
15                 public void  compile_notice(string type, string filename, int line, string message) {
16                         GLib.Idle.add(() => {
17                                 this.file.compile_notice(type,filename,line,message);
18                                 return false;
19                         });
20                 }
21                 
22          
23                 public ValaSourceReport(JsRender.JsRender file)
24                 {
25                         base();
26                         this.file = file;
27                         this.line_errors = new Gee.HashMap<int,string> ();
28                         //this.notices = new Gee.ArrayList<ValaSourceNotice>();
29                 }
30                 
31                 public override void warn (Vala.SourceReference? source, string message) {
32                          
33                         if (source == null) {
34                                 return;
35                                 //stderr.printf ("My error: %s\n", message);
36                         }
37                         
38                         if (source.file.filename != "~~~~~testfile.vala") {
39                                 this.compile_notice("WARN", source.file.filename , source.begin.line, message);
40                                 return;
41                         }
42                         this.compile_notice("WARN", this.file.path, source.begin.line, message);
43                         
44                 }
45                 public override void depr (Vala.SourceReference? source, string message) {
46                          
47                         if (source == null) {
48                                 return;
49                                 //stderr.printf ("My error: %s\n", message);
50                         }
51                         
52                         if (source.file.filename != "~~~~~testfile.vala") {
53                                 this.compile_notice("DEPR", source.file.filename, source.begin.line, message);
54                                 return;
55                         }
56                         this.compile_notice("DEPR",  this.file.path, source.begin.line, message);
57                         
58                 }
59                 
60                 public override void err (Vala.SourceReference? source, string message) {
61                         errors++;
62                         if (source == null) {
63                                 return;
64                                 //stderr.printf ("My error: %s\n", message);
65                         }
66                         if (source.file.filename != "~~~~~testfile.vala") {
67                                 this.compile_notice("ERR", source.file.filename, source.begin.line, message);
68                                 print ("Other file: Got error error: %d:  %s\n", source.begin.line, message);
69                                 return;
70                         }
71                         var pre = "";
72                         if (line_errors.has_key(source.begin.line)) {
73                                 pre = line_errors.get(source.begin.line) + "\n";
74                                 
75                         }
76                         line_errors.set(source.begin.line, pre +  message);
77                         this.compile_notice("ERR", this.file.path, source.begin.line, message);
78                         print ("Test file: Got error error: %d: %s\n", source.begin.line, message);
79                 }
80                 public void dump()
81                 {
82                         var iter = this.line_errors.map_iterator();
83                         while (iter.next()) {
84                                 print ("%d : %s\n\n", iter.get_key(), iter.get_value());
85                         }
86                 }
87
88         }
89
90         public class ValaSource : Vala.CodeVisitor {
91
92                 Vala.CodeContext context;
93                 ValaSourceReport report;
94                 JsRender.JsRender file; 
95                 public ValaSource(JsRender.JsRender file) {
96                         base();
97                         this.file = file;
98                         
99
100                 }
101                 public void dumpCode(string str) {
102                         var ls = str.split("\n");
103                         for (var i=0;i < ls.length; i++) {
104                                 print("%d : %s\n", i+1, ls[i]);
105                         }
106                 }
107                 
108                 public Gee.HashMap<int,string> checkFile()
109                 {
110                         return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
111                 }
112
113                 public async Gee.HashMap<int,string> checkFileWithNodePropChange(
114                                         JsRender.Node node, 
115                                         string prop,
116                                         string ptype,
117                                         string val)
118                 {
119                         Gee.HashMap<int,string> ret = new Gee.HashMap<int,string> ();
120                         var hash = ptype == "listener" ? node.listeners : node.props;
121                         
122                         // untill we get a smarter renderer..
123                         // we have some scenarios where changing the value does not work
124                         if (prop == "* xns" || prop == "xtype") {
125                                 return ret;
126                         }
127                                 
128                         
129                         var old = hash.get(prop);
130                         var newval = "/*--VALACHECK-START--*/ " + val ;
131                         
132                         hash.set(prop, newval);
133                         var tmpstring = JsRender.NodeToVala.mungeFile(this.file);
134                         hash.set(prop, old);
135                         //print("%s\n", tmpstring);
136                         var bits = tmpstring.split("/*--VALACHECK-START--*/");
137                         var offset =0;
138                         if (bits.length > 0) {
139                                 offset = bits[0].split("\n").length +1;
140                         }
141                         //this.dumpCode(tmpstring);
142                         //print("offset %d\n", offset);
143                         yield this.checkStringThread(tmpstring);
144                         
145                         // modify report
146                         
147                         var iter = this.report.line_errors.map_iterator();
148                         while (iter.next()) {
149                                 // print("%d : %s\n",iter.get_key() - offset, iter.get_value());
150                                 // we have to prefix the error with the fake line number 
151                                 // so that it's a unique mark..
152                                  ret.set(iter.get_key() - offset, 
153                                        "%d : %s".printf(iter.get_key() - offset,iter.get_value()));
154                         }
155                         return ret;
156                         
157                 }
158                 
159                 public async  Gee.HashMap<int,string> checkStringThread(string contents)
160                 {
161                         SourceFunc callback = checkStringThread.callback;
162                         var ret = new Gee.HashMap<int,string>();
163                         ThreadFunc<void*> run = () => {
164                                  
165                                 // Pass back result and schedule callback
166                                 ret = this.checkString(contents);
167                                 Idle.add((owned) callback);
168                                 return null;
169                         };
170                         Thread.create<void*>(run, false);
171
172                         // Wait for background thread to schedule our callback
173                         yield;
174                         return ret;
175                 }
176                 
177                 
178                 
179                 public Gee.HashMap<int,string> checkString(string contents)
180                 {
181                         // init context:
182                         var valac = "valac " ;
183                         
184                         context = new Vala.CodeContext ();
185                         Vala.CodeContext.push (context);
186                 
187                         context.experimental = false;
188                         context.experimental_non_null = false;
189                         
190 #if VALA_0_28
191                         var ver=28;
192 #elif VALA_0_26 
193                         var ver=26;
194 #elif VALA_0_24
195                         var ver=24;
196 #elif VALA_0_22 
197                         var ver=22;
198 #endif
199                         
200                         for (int i = 2; i <= ver; i += 2) {
201                                 context.add_define ("VALA_0_%d".printf (i));
202                         }
203                         
204                         
205                         
206                         
207                         
208                         
209                         
210                         var vapidirs = ((Project.Gtk)this.file.project).vapidirs();
211                         // what's the current version of vala???
212                         
213                         
214                         vapidirs +=  Path.get_dirname (context.get_vapi_path("glib-2.0")) ;
215                         
216                         for(var i =0 ; i < vapidirs.length; i++) {
217                                 valac += " --vapidir=" + vapidirs[i];
218                         }
219                                 
220                         
221                         // or context.get_vapi_path("glib-2.0"); // should return path..
222                         context.vapi_directories = vapidirs;
223                         context.report.enable_warnings = true;
224                         context.metadata_directories = { };
225                         context.gir_directories = {};
226                         context.thread = true;
227                         
228                         
229                         this.report = new ValaSourceReport(this.file);
230                         context.report = this.report;
231                         
232                         
233                         context.basedir = "/tmp"; //Posix.realpath (".");
234                 
235                         context.directory = context.basedir;
236                 
237
238                         // add default packages:
239                         //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
240                         context.profile = Vala.Profile.GOBJECT;
241                          
242                         var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
243                         context.root.add_using_directive (ns_ref);
244
245                         var source_file = new Vala.SourceFile (
246                                 context, 
247                                 Vala.SourceFileType.SOURCE, 
248                                         "~~~~~testfile.vala",
249                                         contents
250                         );
251                         source_file.add_using_directive (ns_ref);
252                         context.add_source_file (source_file);
253                         
254                 // add all the files (except the current one) - this.file.path
255                 var pr = ((Project.Gtk)this.file.project);
256                 if (this.file.build_module.length > 0) {
257                                 var cg =  pr.compilegroups.get(this.file.build_module);
258                                 for (var i = 0; i < cg.sources.size; i++) {
259                                         var path = pr.resolve_path(
260                                                         pr.resolve_path_combine_path(pr.firstPath(),cg.sources.get(i)));
261                                                         
262                                         if (!FileUtils.test(path, FileTest.EXISTS)) {
263                                                 continue;
264                                         }       
265                          
266                                         if (path == this.file.path.replace(".bjs", ".vala")) {
267                                                 valac += " " + path;
268                                                 continue;
269                                         }
270                                         if (FileUtils.test(path, FileTest.IS_DIR)) {
271                                                 continue;
272                                         }
273                                         //print("Add source file %s\n", path);
274                                         
275                                         valac += " " + path;
276                                         
277                                         if (Regex.match_simple("\\.c$", path)) {
278                                                 context.add_c_source_file(path);
279                                                 continue;
280                                         }
281                                         
282                                         
283                                         var xsf = new Vala.SourceFile (
284                                                 context,
285                                                 Vala.SourceFileType.SOURCE, 
286                                                 path
287                                         );
288                                         xsf.add_using_directive (ns_ref);
289                                         context.add_source_file(xsf);
290                                         
291                                 }
292                         }
293                         // default.. packages..
294                         context.add_external_package ("glib-2.0"); 
295                         context.add_external_package ("gobject-2.0");
296                         // user defined ones..
297                         
298                 var dcg = pr.compilegroups.get("_default_");
299                 for (var i = 0; i < dcg.packages.size; i++) {
300                                 valac += " --pkg " + dcg.packages.get(i);
301                                 context.add_external_package (dcg.packages.get(i));
302                         }
303                 
304                          //Vala.Config.PACKAGE_SUFFIX.substring (1)
305                         
306                         // add the modules...
307                         
308                         
309                         
310                         //context.add_external_package ("libvala-0.24");
311                         
312                         this.report.compile_notice("START", "", 0, "");
313
314                 
315                         //add_documented_files (context, settings.source_files);
316                 
317                         Vala.Parser parser = new Vala.Parser ();
318                         parser.parse (context);
319                         //gir_parser.parse (context);
320                         if (context.report.get_errors () > 0) {
321                                 print("parse got errors");
322                                 ((ValaSourceReport)context.report).dump();
323                                 
324                                 Vala.CodeContext.pop ();
325                                 this.report.compile_notice("END", "", 0, "");
326                                 return this.report.line_errors;
327                         }
328
329
330                         
331                         // check context:
332                         context.check ();
333                         if (context.report.get_errors () > 0) {
334                                 print("check got errors");
335                                 ((ValaSourceReport)context.report).dump();
336                                 Vala.CodeContext.pop ();
337                                 this.report.compile_notice("END", "", 0, "");
338                                 return this.report.line_errors;
339                                 
340                         }
341                         
342                         context.codegen = new Vala.GDBusServerModule ();
343                         
344                          
345                         context.output = "/tmp/testbuild";
346                         valac += " -o " +context.output;
347                         context.codegen.emit (context);
348                         /*
349                         var ccompiler = new Vala.CCodeCompiler ();
350                         var cc_command = Environment.get_variable ("CC");
351                         var pkg_config_command = Environment.get_variable ("PKG_CONFIG");
352 #if VALA_0_28
353                         ccompiler.compile (context, cc_command, new string[] { }, pkg_config_command);
354 #else
355                         ccompiler.compile (context, cc_command, new string[] { });
356 #endif
357                         */
358  
359                         Vala.CodeContext.pop ();
360                         this.report.compile_notice("END", "", 0, "");
361                         print("%s\n", valac);
362                         print("ALL OK?\n");
363                         return this.report.line_errors;
364                 }
365         //
366                 // startpoint:
367                 //
368          
369         }
370 }
371 /*
372 int main (string[] args) {
373
374         var a = new ValaSource(file);
375         a.create_valac_tree();
376         return 0;
377 }
378 */
379
380