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