2 // valac TreeBuilder.vala --pkg libvala-0.24 --pkg posix -o /tmp/treebuilder
6 public class ValaSourceNotice : Object {
11 public ValaSourceNotice( string t, string f, int l, string m) {
19 public class ValaSourceReport : Vala.Report {
23 public Gee.HashMap<int,string> line_warnings;
24 public Gee.HashMap<int,string> line_depr;
25 public Gee.HashMap<int,string> line_errors;
27 public ValaSourceReport()
30 this.line_errors = new Gee.HashMap<int,string> ();
31 this.line_depr = new Gee.HashMap<int,string> ();
32 this.line_warnings = new Gee.HashMap<int,string> ();
35 public override void warn (Vala.SourceReference? source, string message) {
39 //stderr.printf ("My error: %s\n", message);
41 if (source.file.filename != "~~~~~testfile.vala") {
42 print ("Warning: %d: %s\n", source.begin.line, message);
46 if (line_warnings.has_key(source.begin.line)) {
47 pre = line_warnings.get(source.begin.line) + "\n";
50 line_warnings.set(source.begin.line, pre + "Warn: " + message);
51 print ("Test file: Got error WARN: %d: %s\n", source.begin.line, message);
53 public override void depr (Vala.SourceReference? source, string message) {
57 //stderr.printf ("My error: %s\n", message);
59 if (source.file.filename != "~~~~~testfile.vala") {
60 print ("Depr: %d: %s\n", source.begin.line, message);
64 if (line_depr.has_key(source.begin.line)) {
65 pre = line_depr.get(source.begin.line) + "\n";
68 line_depr.set(source.begin.line, pre + "Warn: " + message);
69 print ("Test file: Got error DEPR: %d: %s\n", source.begin.line, message);
72 public override void err (Vala.SourceReference? source, string message) {
76 //stderr.printf ("My error: %s\n", message);
78 if (source.file.filename != "~~~~~testfile.vala") {
79 print ("Other file: Got error error: %d: %s\n", source.begin.line, message);
83 if (line_errors.has_key(source.begin.line)) {
84 pre = line_errors.get(source.begin.line) + "\n";
87 line_errors.set(source.begin.line, pre + message);
88 print ("Test file: Got error error: %d: %s\n", source.begin.line, message);
92 var iter = this.line_errors.map_iterator();
94 print ("%d : %s\n\n", iter.get_key(), iter.get_value());
100 public class ValaSource : Vala.CodeVisitor {
102 Vala.CodeContext context;
103 ValaSourceReport report;
104 JsRender.JsRender file;
105 public ValaSource(JsRender.JsRender file) {
111 public void dumpCode(string str) {
112 var ls = str.split("\n");
113 for (var i=0;i < ls.length; i++) {
114 print("%d : %s\n", i+1, ls[i]);
118 public Gee.HashMap<int,string> checkFile()
120 return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
123 public async Gee.HashMap<int,string> checkFileWithNodePropChange(
129 Gee.HashMap<int,string> ret = new Gee.HashMap<int,string> ();
130 var hash = ptype == "listener" ? node.listeners : node.props;
132 // untill we get a smarter renderer..
133 // we have some scenarios where changing the value does not work
134 if (prop == "* xns" || prop == "xtype") {
139 var old = hash.get(prop);
140 var newval = "/*--VALACHECK-START--*/ " + val ;
142 hash.set(prop, newval);
143 var tmpstring = JsRender.NodeToVala.mungeFile(this.file);
144 var bits = tmpstring.split("/*--VALACHECK-START--*/");
146 if (bits.length > 0) {
147 offset = bits[0].split("\n").length +1;
149 //this.dumpCode(tmpstring);
150 //print("offset %d\n", offset);
151 yield this.checkStringThread(tmpstring);
155 var iter = this.report.line_errors.map_iterator();
156 while (iter.next()) {
157 // print("%d : %s\n",iter.get_key() - offset, iter.get_value());
158 // we have to prefix the error with the fake line number
159 // so that it's a unique mark..
160 ret.set(iter.get_key() - offset,
161 "%d : %s".printf(iter.get_key() - offset,iter.get_value()));
167 public async Gee.HashMap<int,string> checkStringThread(string contents)
169 SourceFunc callback = checkStringThread.callback;
170 var ret = new Gee.HashMap<int,string>();
171 ThreadFunc<void*> run = () => {
173 // Pass back result and schedule callback
174 ret = this.checkString(contents);
175 Idle.add((owned) callback);
178 Thread.create<void*>(run, false);
180 // Wait for background thread to schedule our callback
187 public Gee.HashMap<int,string> checkString(string contents)
190 var valac = "valac " ;
192 context = new Vala.CodeContext ();
193 Vala.CodeContext.push (context);
195 context.experimental = false;
196 context.experimental_non_null = false;
208 for (int i = 2; i <= ver; i += 2) {
209 context.add_define ("VALA_0_%d".printf (i));
218 var vapidirs = ((Project.Gtk)this.file.project).vapidirs();
219 // what's the current version of vala???
222 vapidirs += Path.get_dirname (context.get_vapi_path("glib-2.0")) ;
224 for(var i =0 ; i < vapidirs.length; i++) {
225 valac += " --vapidir=" + vapidirs[i];
229 // or context.get_vapi_path("glib-2.0"); // should return path..
230 context.vapi_directories = vapidirs;
231 context.report.enable_warnings = true;
232 context.metadata_directories = { };
233 context.gir_directories = {};
234 context.thread = true;
237 this.report = new ValaSourceReport();;
238 context.report = this.report;
241 context.basedir = "/tmp"; //Posix.realpath (".");
243 context.directory = context.basedir;
246 // add default packages:
247 //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
248 context.profile = Vala.Profile.GOBJECT;
250 var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
251 context.root.add_using_directive (ns_ref);
253 var source_file = new Vala.SourceFile (
255 Vala.SourceFileType.SOURCE,
256 "~~~~~testfile.vala",
259 source_file.add_using_directive (ns_ref);
260 context.add_source_file (source_file);
262 // add all the files (except the current one) - this.file.path
263 var pr = ((Project.Gtk)this.file.project);
264 if (this.file.build_module.length > 0) {
265 var cg = pr.compilegroups.get(this.file.build_module);
266 for (var i = 0; i < cg.sources.size; i++) {
267 var path = pr.resolve_path(
268 pr.resolve_path_combine_path(pr.firstPath(),cg.sources.get(i)));
270 if (!FileUtils.test(path, FileTest.EXISTS)) {
274 if (path == this.file.path.replace(".bjs", ".vala")) {
278 if (FileUtils.test(path, FileTest.IS_DIR)) {
281 //print("Add source file %s\n", path);
285 if (Regex.match_simple("\\.c$", path)) {
286 context.add_c_source_file(path);
291 var xsf = new Vala.SourceFile (
293 Vala.SourceFileType.SOURCE,
296 xsf.add_using_directive (ns_ref);
297 context.add_source_file(xsf);
301 // default.. packages..
302 context.add_external_package ("glib-2.0");
303 context.add_external_package ("gobject-2.0");
304 // user defined ones..
306 var dcg = pr.compilegroups.get("_default_");
307 for (var i = 0; i < dcg.packages.size; i++) {
308 valac += " --pkg " + dcg.packages.get(i);
309 context.add_external_package (dcg.packages.get(i));
312 //Vala.Config.PACKAGE_SUFFIX.substring (1)
314 // add the modules...
318 //context.add_external_package ("libvala-0.24");
323 //add_documented_files (context, settings.source_files);
325 Vala.Parser parser = new Vala.Parser ();
326 parser.parse (context);
327 //gir_parser.parse (context);
328 if (context.report.get_errors () > 0) {
329 print("parse got errors");
330 ((ValaSourceReport)context.report).dump();
331 Vala.CodeContext.pop ();
332 return this.report.line_errors;
339 if (context.report.get_errors () > 0) {
340 print("check got errors");
341 ((ValaSourceReport)context.report).dump();
342 Vala.CodeContext.pop ();
343 return this.report.line_errors;
347 context.codegen = new Vala.GDBusServerModule ();
350 context.output = "/tmp/testbuild";
351 valac += " -o " +context.output;
352 context.codegen.emit (context);
354 var ccompiler = new Vala.CCodeCompiler ();
355 var cc_command = Environment.get_variable ("CC");
356 var pkg_config_command = Environment.get_variable ("PKG_CONFIG");
358 ccompiler.compile (context, cc_command, new string[] { }, pkg_config_command);
360 ccompiler.compile (context, cc_command, new string[] { });
364 Vala.CodeContext.pop ();
365 print("%s\n", valac);
367 return this.report.line_errors;
376 int main (string[] args) {
378 var a = new ValaSource(file);
379 a.create_valac_tree();