2 // valac TreeBuilder.vala --pkg libvala-0.24 --pkg posix -o /tmp/treebuilder
6 /*public class ValaSourceNotice : Object {
11 public ValaSourceNotice( string type, string file, int line, string message) {
20 public class ValaSourceReport : Vala.Report {
22 public JsRender.JsRender file;
24 //public Gee.ArrayList<ValaSourceNotice> notices;
26 public Gee.HashMap<int,string> line_errors;
28 public signal void notice(string type, string file, int line, string message);
30 public ValaSourceReport(JsRender.JsRender file)
34 this.line_errors = new Gee.HashMap<int,string> ();
35 //this.notices = new Gee.ArrayList<ValaSourceNotice>();
38 public override void warn (Vala.SourceReference? source, string message) {
42 //stderr.printf ("My error: %s\n", message);
45 if (source.file.filename != "~~~~~testfile.vala") {
46 this.notice("Warn", this.file.path, source.begin.line, message);
49 this.notice("Warn", source.file.filename, source.begin.line, message);
52 public override void depr (Vala.SourceReference? source, string message) {
56 //stderr.printf ("My error: %s\n", message);
59 if (source.file.filename != "~~~~~testfile.vala") {
60 this.notice("Depr", this.file.path, source.begin.line, message);
63 this.notice("Depr", source.file.filename, source.begin.line, message);
67 public override void err (Vala.SourceReference? source, string message) {
71 //stderr.printf ("My error: %s\n", message);
73 if (source.file.filename != "~~~~~testfile.vala") {
74 this.notice("Err", this.file.path, source.begin.line, message);
75 print ("Other file: Got error error: %d: %s\n", source.begin.line, message);
79 if (line_errors.has_key(source.begin.line)) {
80 pre = line_errors.get(source.begin.line) + "\n";
83 line_errors.set(source.begin.line, pre + message);
84 this.notice("Err", source.file.filename, source.begin.line, message);
85 print ("Test file: Got error error: %d: %s\n", source.begin.line, message);
89 var iter = this.line_errors.map_iterator();
91 print ("%d : %s\n\n", iter.get_key(), iter.get_value());
97 public class ValaSource : Vala.CodeVisitor {
99 Vala.CodeContext context;
100 ValaSourceReport report;
101 JsRender.JsRender file;
102 public ValaSource(JsRender.JsRender file) {
108 public void dumpCode(string str) {
109 var ls = str.split("\n");
110 for (var i=0;i < ls.length; i++) {
111 print("%d : %s\n", i+1, ls[i]);
115 public Gee.HashMap<int,string> checkFile()
117 return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
120 public async Gee.HashMap<int,string> checkFileWithNodePropChange(
126 Gee.HashMap<int,string> ret = new Gee.HashMap<int,string> ();
127 var hash = ptype == "listener" ? node.listeners : node.props;
129 // untill we get a smarter renderer..
130 // we have some scenarios where changing the value does not work
131 if (prop == "* xns" || prop == "xtype") {
136 var old = hash.get(prop);
137 var newval = "/*--VALACHECK-START--*/ " + val ;
139 hash.set(prop, newval);
140 var tmpstring = JsRender.NodeToVala.mungeFile(this.file);
141 var bits = tmpstring.split("/*--VALACHECK-START--*/");
143 if (bits.length > 0) {
144 offset = bits[0].split("\n").length +1;
146 //this.dumpCode(tmpstring);
147 //print("offset %d\n", offset);
148 yield this.checkStringThread(tmpstring);
152 var iter = this.report.line_errors.map_iterator();
153 while (iter.next()) {
154 // print("%d : %s\n",iter.get_key() - offset, iter.get_value());
155 // we have to prefix the error with the fake line number
156 // so that it's a unique mark..
157 ret.set(iter.get_key() - offset,
158 "%d : %s".printf(iter.get_key() - offset,iter.get_value()));
164 public async Gee.HashMap<int,string> checkStringThread(string contents)
166 SourceFunc callback = checkStringThread.callback;
167 var ret = new Gee.HashMap<int,string>();
168 ThreadFunc<void*> run = () => {
170 // Pass back result and schedule callback
171 ret = this.checkString(contents);
172 Idle.add((owned) callback);
175 Thread.create<void*>(run, false);
177 // Wait for background thread to schedule our callback
184 public Gee.HashMap<int,string> checkString(string contents)
187 var valac = "valac " ;
189 context = new Vala.CodeContext ();
190 Vala.CodeContext.push (context);
192 context.experimental = false;
193 context.experimental_non_null = false;
205 for (int i = 2; i <= ver; i += 2) {
206 context.add_define ("VALA_0_%d".printf (i));
215 var vapidirs = ((Project.Gtk)this.file.project).vapidirs();
216 // what's the current version of vala???
219 vapidirs += Path.get_dirname (context.get_vapi_path("glib-2.0")) ;
221 for(var i =0 ; i < vapidirs.length; i++) {
222 valac += " --vapidir=" + vapidirs[i];
226 // or context.get_vapi_path("glib-2.0"); // should return path..
227 context.vapi_directories = vapidirs;
228 context.report.enable_warnings = true;
229 context.metadata_directories = { };
230 context.gir_directories = {};
231 context.thread = true;
234 this.report = new ValaSourceReport();;
235 context.report = this.report;
238 context.basedir = "/tmp"; //Posix.realpath (".");
240 context.directory = context.basedir;
243 // add default packages:
244 //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
245 context.profile = Vala.Profile.GOBJECT;
247 var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
248 context.root.add_using_directive (ns_ref);
250 var source_file = new Vala.SourceFile (
252 Vala.SourceFileType.SOURCE,
253 "~~~~~testfile.vala",
256 source_file.add_using_directive (ns_ref);
257 context.add_source_file (source_file);
259 // add all the files (except the current one) - this.file.path
260 var pr = ((Project.Gtk)this.file.project);
261 if (this.file.build_module.length > 0) {
262 var cg = pr.compilegroups.get(this.file.build_module);
263 for (var i = 0; i < cg.sources.size; i++) {
264 var path = pr.resolve_path(
265 pr.resolve_path_combine_path(pr.firstPath(),cg.sources.get(i)));
267 if (!FileUtils.test(path, FileTest.EXISTS)) {
271 if (path == this.file.path.replace(".bjs", ".vala")) {
275 if (FileUtils.test(path, FileTest.IS_DIR)) {
278 //print("Add source file %s\n", path);
282 if (Regex.match_simple("\\.c$", path)) {
283 context.add_c_source_file(path);
288 var xsf = new Vala.SourceFile (
290 Vala.SourceFileType.SOURCE,
293 xsf.add_using_directive (ns_ref);
294 context.add_source_file(xsf);
298 // default.. packages..
299 context.add_external_package ("glib-2.0");
300 context.add_external_package ("gobject-2.0");
301 // user defined ones..
303 var dcg = pr.compilegroups.get("_default_");
304 for (var i = 0; i < dcg.packages.size; i++) {
305 valac += " --pkg " + dcg.packages.get(i);
306 context.add_external_package (dcg.packages.get(i));
309 //Vala.Config.PACKAGE_SUFFIX.substring (1)
311 // add the modules...
315 //context.add_external_package ("libvala-0.24");
320 //add_documented_files (context, settings.source_files);
322 Vala.Parser parser = new Vala.Parser ();
323 parser.parse (context);
324 //gir_parser.parse (context);
325 if (context.report.get_errors () > 0) {
326 print("parse got errors");
327 ((ValaSourceReport)context.report).dump();
328 Vala.CodeContext.pop ();
329 return this.report.line_errors;
336 if (context.report.get_errors () > 0) {
337 print("check got errors");
338 ((ValaSourceReport)context.report).dump();
339 Vala.CodeContext.pop ();
340 return this.report.line_errors;
344 context.codegen = new Vala.GDBusServerModule ();
347 context.output = "/tmp/testbuild";
348 valac += " -o " +context.output;
349 context.codegen.emit (context);
351 var ccompiler = new Vala.CCodeCompiler ();
352 var cc_command = Environment.get_variable ("CC");
353 var pkg_config_command = Environment.get_variable ("PKG_CONFIG");
355 ccompiler.compile (context, cc_command, new string[] { }, pkg_config_command);
357 ccompiler.compile (context, cc_command, new string[] { });
361 Vala.CodeContext.pop ();
362 print("%s\n", valac);
364 return this.report.line_errors;
373 int main (string[] args) {
375 var a = new ValaSource(file);
376 a.create_valac_tree();