2 // valac TreeBuilder.vala --pkg libvala-0.24 --pkg posix -o /tmp/treebuilder
7 public class ValaSourceReport : Vala.Report {
9 public JsRender.JsRender file;
11 //public Gee.ArrayList<ValaSourceNotice> notices;
13 public Gee.HashMap<int,string> line_errors;
15 public void compile_notice(string type, string filename, int line, string message) {
17 this.file.compile_notice(type,filename,line,message);
23 public ValaSourceReport(JsRender.JsRender file)
27 this.line_errors = new Gee.HashMap<int,string> ();
28 //this.notices = new Gee.ArrayList<ValaSourceNotice>();
31 public override void warn (Vala.SourceReference? source, string message) {
35 //stderr.printf ("My error: %s\n", message);
38 if (source.file.filename != "~~~~~testfile.vala") {
39 this.compile_notice("WARN", source.file.filename , source.begin.line, message);
42 this.compile_notice("WARN", this.file.path, source.begin.line, message);
45 public override void depr (Vala.SourceReference? source, string message) {
49 //stderr.printf ("My error: %s\n", message);
52 if (source.file.filename != "~~~~~testfile.vala") {
53 this.compile_notice("DEPR", source.file.filename, source.begin.line, message);
56 this.compile_notice("DEPR", this.file.path, source.begin.line, message);
60 public override void err (Vala.SourceReference? source, string message) {
64 //stderr.printf ("My error: %s\n", message);
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);
72 if (line_errors.has_key(source.begin.line)) {
73 pre = line_errors.get(source.begin.line) + "\n";
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);
82 var iter = this.line_errors.map_iterator();
84 print ("%d : %s\n\n", iter.get_key(), iter.get_value());
90 public class ValaSource : Vala.CodeVisitor {
92 Vala.CodeContext context;
93 ValaSourceReport report;
94 JsRender.JsRender file;
95 public ValaSource(JsRender.JsRender file) {
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]);
108 public Gee.HashMap<int,string> checkFile()
110 return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
113 public async Gee.HashMap<int,string> checkFileWithNodePropChange(
119 Gee.HashMap<int,string> ret = new Gee.HashMap<int,string> ();
120 var hash = ptype == "listener" ? node.listeners : node.props;
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") {
129 var old = hash.get(prop);
130 var newval = "/*--VALACHECK-START--*/ " + val ;
132 hash.set(prop, newval);
133 var tmpstring = JsRender.NodeToVala.mungeFile(this.file);
135 //print("%s\n", tmpstring);
136 var bits = tmpstring.split("/*--VALACHECK-START--*/");
138 if (bits.length > 0) {
139 offset = bits[0].split("\n").length +1;
141 //this.dumpCode(tmpstring);
142 //print("offset %d\n", offset);
143 yield this.checkStringThread(tmpstring);
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()));
159 public async Gee.HashMap<int,string> checkStringThread(string contents)
161 SourceFunc callback = checkStringThread.callback;
162 var ret = new Gee.HashMap<int,string>();
163 ThreadFunc<void*> run = () => {
165 // Pass back result and schedule callback
166 ret = this.checkString(contents);
167 Idle.add((owned) callback);
170 Thread.create<void*>(run, false);
172 // Wait for background thread to schedule our callback
179 public Gee.HashMap<int,string> checkString(string contents)
182 var valac = "valac " ;
184 context = new Vala.CodeContext ();
185 Vala.CodeContext.push (context);
187 context.experimental = false;
188 context.experimental_non_null = false;
200 for (int i = 2; i <= ver; i += 2) {
201 context.add_define ("VALA_0_%d".printf (i));
210 var vapidirs = ((Project.Gtk)this.file.project).vapidirs();
211 // what's the current version of vala???
214 vapidirs += Path.get_dirname (context.get_vapi_path("glib-2.0")) ;
216 for(var i =0 ; i < vapidirs.length; i++) {
217 valac += " --vapidir=" + vapidirs[i];
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;
229 this.report = new ValaSourceReport(this.file);
230 context.report = this.report;
233 context.basedir = "/tmp"; //Posix.realpath (".");
235 context.directory = context.basedir;
238 // add default packages:
239 //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
240 context.profile = Vala.Profile.GOBJECT;
242 var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
243 context.root.add_using_directive (ns_ref);
245 var source_file = new Vala.SourceFile (
247 Vala.SourceFileType.SOURCE,
248 "~~~~~testfile.vala",
251 source_file.add_using_directive (ns_ref);
252 context.add_source_file (source_file);
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)));
262 if (!FileUtils.test(path, FileTest.EXISTS)) {
266 if (path == this.file.path.replace(".bjs", ".vala")) {
270 if (FileUtils.test(path, FileTest.IS_DIR)) {
273 //print("Add source file %s\n", path);
277 if (Regex.match_simple("\\.c$", path)) {
278 context.add_c_source_file(path);
283 var xsf = new Vala.SourceFile (
285 Vala.SourceFileType.SOURCE,
288 xsf.add_using_directive (ns_ref);
289 context.add_source_file(xsf);
293 // default.. packages..
294 context.add_external_package ("glib-2.0");
295 context.add_external_package ("gobject-2.0");
296 // user defined ones..
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));
304 //Vala.Config.PACKAGE_SUFFIX.substring (1)
306 // add the modules...
310 //context.add_external_package ("libvala-0.24");
312 this.report.compile_notice("START", "", 0, "");
315 //add_documented_files (context, settings.source_files);
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();
324 Vala.CodeContext.pop ();
325 this.report.compile_notice("END", "", 0, "");
326 return this.report.line_errors;
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;
342 //context.codegen = new Vala.GDBusServerModule ();
345 context.output = "/tmp/testbuild";
346 valac += " -o " +context.output;
347 //context.codegen.emit (context);
349 var ccompiler = new Vala.CCodeCompiler ();
350 var cc_command = Environment.get_variable ("CC");
351 var pkg_config_command = Environment.get_variable ("PKG_CONFIG");
353 ccompiler.compile (context, cc_command, new string[] { }, pkg_config_command);
355 ccompiler.compile (context, cc_command, new string[] { });
359 Vala.CodeContext.pop ();
360 //(new Vala.CodeNode()).get_error_types().clear();
361 //(new Vala.NullType()).get_type_arguments().clear();
362 (new Vala.NullType(null)).get_type_arguments().clear();
363 this.report.compile_notice("END", "", 0, "");
364 print("%s\n", valac);
366 return this.report.line_errors;
375 int main (string[] args) {
377 var a = new ValaSource(file);
378 a.create_valac_tree();