2 // valac TreeBuilder.vala --pkg libvala-0.24 --pkg posix -o /tmp/treebuilder
6 public class ValaSourceReport : Vala.Report {
10 public Gee.HashMap<int,string> line_warnings
11 public Gee.HashMap<int,string> line_depr;
12 public Gee.HashMap<int,string> line_errors;
14 public ValaSourceReport()
17 this.line_errors = new Gee.HashMap<int,string> ();
18 this.line_depr = new Gee.HashMap<int,string> ();
19 this.line_warnings = new Gee.HashMap<int,string> ();
22 public override void warn (Vala.SourceReference? source, string message) {
26 //stderr.printf ("My error: %s\n", message);
28 if (source.file.filename != "~~~~~testfile.vala") {
29 print ("Warning: %d: %s\n", source.begin.line, message);
33 if (line_warnings.has_key(source.begin.line)) {
34 pre = line_warnings.get(source.begin.line) + "\n";
37 line_warnings.set(source.begin.line, pre + "Warn: " + message);
38 print ("Test file: Got error error: %d: %s\n", source.begin.line, message);
40 public override void depr (Vala.SourceReference? source, string message) {
44 //stderr.printf ("My error: %s\n", message);
46 if (source.file.filename != "~~~~~testfile.vala") {
47 print ("Depr: %d: %s\n", source.begin.line, message);
51 if (line_depr.has_key(source.begin.line)) {
52 pre = line_depr.get(source.begin.line) + "\n";
55 line_depr.set(source.begin.line, pre + "Warn: " + message);
56 print ("Test file: Got error error: %d: %s\n", source.begin.line, message);
59 public override void err (Vala.SourceReference? source, string message) {
63 //stderr.printf ("My error: %s\n", message);
65 if (source.file.filename != "~~~~~testfile.vala") {
66 print ("Other file: Got error error: %d: %s\n", source.begin.line, message);
70 if (line_errors.has_key(source.begin.line)) {
71 pre = line_errors.get(source.begin.line) + "\n";
74 line_errors.set(source.begin.line, pre + message);
75 print ("Test file: Got error error: %d: %s\n", source.begin.line, message);
79 var iter = this.line_errors.map_iterator();
81 print ("%d : %s\n\n", iter.get_key(), iter.get_value());
87 public class ValaSource : Vala.CodeVisitor {
89 Vala.CodeContext context;
90 ValaSourceReport report;
91 JsRender.JsRender file;
92 public ValaSource(JsRender.JsRender file) {
98 public void dumpCode(string str) {
99 var ls = str.split("\n");
100 for (var i=0;i < ls.length; i++) {
101 print("%d : %s\n", i+1, ls[i]);
105 public Gee.HashMap<int,string> checkFile()
107 return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
110 public async Gee.HashMap<int,string> checkFileWithNodePropChange(
116 Gee.HashMap<int,string> ret = new Gee.HashMap<int,string> ();
117 var hash = ptype == "listener" ? node.listeners : node.props;
119 // untill we get a smarter renderer..
120 // we have some scenarios where changing the value does not work
121 if (prop == "* xns" || prop == "xtype") {
126 var old = hash.get(prop);
127 var newval = "/*--VALACHECK-START--*/ " + val ;
129 hash.set(prop, newval);
130 var tmpstring = JsRender.NodeToVala.mungeFile(this.file);
131 var bits = tmpstring.split("/*--VALACHECK-START--*/");
133 if (bits.length > 0) {
134 offset = bits[0].split("\n").length +1;
136 //this.dumpCode(tmpstring);
137 //print("offset %d\n", offset);
138 yield this.checkStringThread(tmpstring);
142 var iter = this.report.line_errors.map_iterator();
143 while (iter.next()) {
144 // print("%d : %s\n",iter.get_key() - offset, iter.get_value());
145 // we have to prefix the error with the fake line number
146 // so that it's a unique mark..
147 ret.set(iter.get_key() - offset,
148 "%d : %s".printf(iter.get_key() - offset,iter.get_value()));
154 public async Gee.HashMap<int,string> checkStringThread(string contents)
156 SourceFunc callback = checkStringThread.callback;
157 var ret = new Gee.HashMap<int,string>();
158 ThreadFunc<void*> run = () => {
160 // Pass back result and schedule callback
161 ret = this.checkString(contents);
162 Idle.add((owned) callback);
165 Thread.create<void*>(run, false);
167 // Wait for background thread to schedule our callback
174 public Gee.HashMap<int,string> checkString(string contents)
177 var valac = "valac " ;
179 context = new Vala.CodeContext ();
180 Vala.CodeContext.push (context);
182 context.experimental = false;
183 context.experimental_non_null = false;
195 for (int i = 2; i <= ver; i += 2) {
196 context.add_define ("VALA_0_%d".printf (i));
205 var vapidirs = ((Project.Gtk)this.file.project).vapidirs();
206 // what's the current version of vala???
209 vapidirs += Path.get_dirname (context.get_vapi_path("glib-2.0")) ;
211 for(var i =0 ; i < vapidirs.length; i++) {
212 valac += " --vapidir=" + vapidirs[i];
216 // or context.get_vapi_path("glib-2.0"); // should return path..
217 context.vapi_directories = vapidirs;
218 context.report.enable_warnings = true;
219 context.metadata_directories = { };
220 context.gir_directories = {};
221 context.thread = true;
224 this.report = new ValaSourceReport();;
225 context.report = this.report;
228 context.basedir = "/tmp"; //Posix.realpath (".");
230 context.directory = context.basedir;
233 // add default packages:
234 //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
235 context.profile = Vala.Profile.GOBJECT;
237 var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
238 context.root.add_using_directive (ns_ref);
240 var source_file = new Vala.SourceFile (
242 Vala.SourceFileType.SOURCE,
243 "~~~~~testfile.vala",
246 source_file.add_using_directive (ns_ref);
247 context.add_source_file (source_file);
249 // add all the files (except the current one) - this.file.path
250 var pr = ((Project.Gtk)this.file.project);
251 if (this.file.build_module.length > 0) {
252 var cg = pr.compilegroups.get(this.file.build_module);
253 for (var i = 0; i < cg.sources.size; i++) {
254 var path = pr.resolve_path(
255 pr.resolve_path_combine_path(pr.firstPath(),cg.sources.get(i)));
257 if (!FileUtils.test(path, FileTest.EXISTS)) {
261 if (path == this.file.path.replace(".bjs", ".vala")) {
265 if (FileUtils.test(path, FileTest.IS_DIR)) {
268 //print("Add source file %s\n", path);
272 if (Regex.match_simple("\\.c$", path)) {
273 context.add_c_source_file(path);
278 var xsf = new Vala.SourceFile (
280 Vala.SourceFileType.SOURCE,
283 xsf.add_using_directive (ns_ref);
284 context.add_source_file(xsf);
288 // default.. packages..
289 context.add_external_package ("glib-2.0");
290 context.add_external_package ("gobject-2.0");
291 // user defined ones..
293 var dcg = pr.compilegroups.get("_default_");
294 for (var i = 0; i < dcg.packages.size; i++) {
295 valac += " --pkg " + dcg.packages.get(i);
296 context.add_external_package (dcg.packages.get(i));
299 //Vala.Config.PACKAGE_SUFFIX.substring (1)
301 // add the modules...
305 //context.add_external_package ("libvala-0.24");
310 //add_documented_files (context, settings.source_files);
312 Vala.Parser parser = new Vala.Parser ();
313 parser.parse (context);
314 //gir_parser.parse (context);
315 if (context.report.get_errors () > 0) {
316 print("parse got errors");
317 ((ValaSourceReport)context.report).dump();
318 Vala.CodeContext.pop ();
319 return this.report.line_errors;
326 if (context.report.get_errors () > 0) {
327 print("check got errors");
328 ((ValaSourceReport)context.report).dump();
329 Vala.CodeContext.pop ();
330 return this.report.line_errors;
334 context.codegen = new Vala.GDBusServerModule ();
337 context.output = "/tmp/testbuild";
338 valac += " -o " +context.output;
339 context.codegen.emit (context);
341 var ccompiler = new Vala.CCodeCompiler ();
342 var cc_command = Environment.get_variable ("CC");
343 var pkg_config_command = Environment.get_variable ("PKG_CONFIG");
345 ccompiler.compile (context, cc_command, new string[] { }, pkg_config_command);
347 ccompiler.compile (context, cc_command, new string[] { });
351 Vala.CodeContext.pop ();
352 print("%s\n", valac);
354 return this.report.line_errors;
363 int main (string[] args) {
365 var a = new ValaSource(file);
366 a.create_valac_tree();