2 // valac TreeBuilder.vala --pkg libvala-0.24 --pkg posix -o /tmp/treebuilder
6 public class ValaSourceReport : Vala.Report {
11 public Gee.HashMap<int,string> line_errors;
13 public ValaSourceReport()
16 this.line_errors = new Gee.HashMap<int,string> ();
19 public override void err (Vala.SourceReference? source, string message) {
23 //stderr.printf ("My error: %s\n", message);
25 if (source.file.filename != "~~~~~testfile.vala") {
26 print ("Other file: Got error error: %d: %s\n", source.begin.line, message);
30 if (line_errors.has_key(source.begin.line)) {
31 pre = line_errors.get(source.begin.line) + "\n";
34 line_errors.set(source.begin.line, pre + message);
35 print ("Test file: Got error error: %d: %s\n", source.begin.line, message);
39 var iter = this.line_errors.map_iterator();
41 print ("%d : %s\n\n", iter.get_key(), iter.get_value());
47 public class ValaSource : Vala.CodeVisitor {
49 Vala.CodeContext context;
50 ValaSourceReport report;
51 JsRender.JsRender file;
52 public ValaSource(JsRender.JsRender file) {
58 public void dumpCode(string str) {
59 var ls = str.split("\n");
60 for (var i=0;i < ls.length; i++) {
61 print("%d : %s\n", i+1, ls[i]);
65 public Gee.HashMap<int,string> checkFile()
67 return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
70 public async Gee.HashMap<int,string> checkFileWithNodePropChange(
76 Gee.HashMap<int,string> ret = new Gee.HashMap<int,string> ();
77 var hash = ptype == "listener" ? node.listeners : node.props;
79 // untill we get a smarter renderer..
80 // we have some scenarios where changing the value does not work
81 if (prop == "* xns" || prop == "xtype") {
86 var old = hash.get(prop);
87 var newval = "/*--VALACHECK-START--*/ " + val ;
89 hash.set(prop, newval);
90 var tmpstring = JsRender.NodeToVala.mungeFile(this.file);
91 var bits = tmpstring.split("/*--VALACHECK-START--*/");
93 if (bits.length > 0) {
94 offset = bits[0].split("\n").length +1;
96 //this.dumpCode(tmpstring);
97 //print("offset %d\n", offset);
98 yield this.checkStringThread(tmpstring);
102 var iter = this.report.line_errors.map_iterator();
103 while (iter.next()) {
104 // print("%d : %s\n",iter.get_key() - offset, iter.get_value());
105 // we have to prefix the error with the fake line number
106 // so that it's a unique mark..
107 ret.set(iter.get_key() - offset,
108 "%d : %s".printf(iter.get_key() - offset,iter.get_value()));
114 public async Gee.HashMap<int,string> checkStringThread(string contents)
116 SourceFunc callback = checkStringThread.callback;
117 var ret = new Gee.HashMap<int,string>();
118 ThreadFunc<void*> run = () => {
120 // Pass back result and schedule callback
121 ret = this.checkString(contents);
122 Idle.add((owned) callback);
125 Thread.create<void*>(run, false);
127 // Wait for background thread to schedule our callback
134 public Gee.HashMap<int,string> checkString(string contents)
137 var valac = "valac " ;
139 context = new Vala.CodeContext ();
140 Vala.CodeContext.push (context);
142 context.experimental = false;
143 context.experimental_non_null = false;
144 var vapidirs = ((Project.Gtk)this.file.project).vapidirs();
145 // what's the current version of vala???
148 vapidirs += Path.get_dirname (context.get_vapi_path("glib-2.0")) ;
150 for(var i =0 ; i < vapidirs.length; i++) {
151 valac += " --vapidir=" + vapidirs[i];
155 // or context.get_vapi_path("glib-2.0"); // should return path..
156 context.vapi_directories = vapidirs;
157 context.report.enable_warnings = true;
158 context.metadata_directories = { };
159 context.gir_directories = {};
160 context.thread = true;
163 this.report = new ValaSourceReport();;
164 context.report = this.report;
167 context.basedir = Posix.realpath (".");
169 context.directory = context.basedir;
172 // add default packages:
173 //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
174 context.profile = Vala.Profile.GOBJECT;
176 var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
177 context.root.add_using_directive (ns_ref);
179 var source_file = new Vala.SourceFile (
181 Vala.SourceFileType.SOURCE,
182 "~~~~~testfile.vala",
185 source_file.add_using_directive (ns_ref);
186 context.add_source_file (source_file);
188 // add all the files (except the current one) - this.file.path
189 var pr = ((Project.Gtk)this.file.project);
190 if (this.file.build_module.length > 0) {
191 var cg = pr.compilegroups.get(this.file.build_module);
192 for (var i = 0; i < cg.sources.size; i++) {
193 var path = pr.resolve_path(
194 pr.resolve_path_combine_path(pr.firstPath(),cg.sources.get(i)));
196 if (!FileUtils.test(path, FileTest.EXISTS)) {
200 if (path == this.file.path.replace(".bjs", ".vala")) {
204 if (FileUtils.test(path, FileTest.IS_DIR)) {
207 //print("Add source file %s\n", path);
209 var xsf = new Vala.SourceFile (
211 Vala.SourceFileType.SOURCE,
214 xsf.add_using_directive (ns_ref);
217 // default.. packages..
218 context.add_external_package ("glib-2.0");
219 context.add_external_package ("gobject-2.0");
220 // user defined ones..
222 var dcg = pr.compilegroups.get("_default_");
223 for (var i = 0; i < dcg.packages.size; i++) {
224 valac += " --pkg " + dcg.packages.get(i);
225 context.add_external_package (dcg.packages.get(i));
228 //Vala.Config.PACKAGE_SUFFIX.substring (1)
230 // add the modules...
234 //context.add_external_package ("libvala-0.24");
239 //add_documented_files (context, settings.source_files);
241 Vala.Parser parser = new Vala.Parser ();
242 parser.parse (context);
243 //gir_parser.parse (context);
244 if (context.report.get_errors () > 0) {
245 print("parse got errors");
246 ((ValaSourceReport)context.report).dump();
247 Vala.CodeContext.pop ();
248 return this.report.line_errors;
255 if (context.report.get_errors () > 0) {
256 print("check got errors");
257 ((ValaSourceReport)context.report).dump();
258 Vala.CodeContext.pop ();
259 return this.report.line_errors;
263 context.codegen = new Vala.GDBusServerModule ();
266 context.output = "/tmp/testbuild";
267 valac += " -o " +context.output;
268 context.codegen.emit (context);
270 var ccompiler = new Vala.CCodeCompiler ();
271 var cc_command = Environment.get_variable ("CC");
272 var pkg_config_command = Environment.get_variable ("PKG_CONFIG");
273 ccompiler.compile (context, cc_command, new string[] { }, pkg_config_command);
275 Vala.CodeContext.pop ();
276 print("%s\n", valac);
278 return this.report.line_errors;
287 int main (string[] args) {
289 var a = new ValaSource(file);
290 a.create_valac_tree();