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: %s\n", 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);
38 var iter = this.line_errors.map_iterator();
40 print ("%d : %s\n\n", iter.get_key(), iter.get_value());
46 public class ValaSource : Vala.CodeVisitor {
48 Vala.CodeContext context;
49 ValaSourceReport report;
50 JsRender.JsRender file;
51 public ValaSource(JsRender.JsRender file) {
57 public void dumpCode(string str) {
58 var ls = str.split("\n");
59 for (var i=0;i < ls.length; i++) {
60 print("%d : %s\n", i+1, ls[i]);
64 public Gee.HashMap<int,string> checkFile()
66 return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
69 public Gee.HashMap<int,string> checkFileWithNodePropChange(
75 Gee.HashMap<int,string> ret = new Gee.HashMap<int,string> ();
76 var hash = ptype == "listener" ? node.listeners : node.props;
78 // untill we get a smarter renderer..
79 // we have some scenarios where changing the value does not work
80 if (prop == "* xns" || prop == "xtype") {
85 var old = hash.get(prop);
86 var newval = "/*--VALACHECK-START--*/ " + val ;
88 hash.set(prop, newval);
89 var tmpstring = JsRender.NodeToVala.mungeFile(this.file);
90 var bits = tmpstring.split("/*--VALACHECK-START--*/");
92 if (bits.length > 0) {
93 offset = bits[0].split("\n").length +1;
95 //this.dumpCode(tmpstring);
96 //print("offset %d\n", offset);
97 this.checkString(tmpstring);
101 var iter = this.report.line_errors.map_iterator();
102 while (iter.next()) {
103 // print("%d : %s\n",iter.get_key() - offset, iter.get_value());
104 // we have to prefix the error with the fake line number
105 // so that it's a unique mark..
106 ret.set(iter.get_key() - offset,
107 "%d : %s".printf(iter.get_key() - offset,iter.get_value()));
113 public Gee.HashMap<int,string> checkString(string contents)
117 context = new Vala.CodeContext ();
118 Vala.CodeContext.push (context);
120 context.experimental = false;
121 context.experimental_non_null = false;
122 var vapidirs = ((Project.Gtk)this.file.project).vapidirs();
123 vapidirs += "/usr/share/vala-0.24/vapi" ;
125 // or context.get_vapi_path("glib-2.0"); // should return path..
126 context.vapi_directories = vapidirs;
127 context.report.enable_warnings = true;
128 context.metadata_directories = { };
129 context.gir_directories = {};
130 this.report = new ValaSourceReport();;
131 context.report = this.report;
134 context.basedir = Posix.realpath (".");
136 context.directory = context.basedir;
139 // add default packages:
140 //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
141 context.profile = Vala.Profile.GOBJECT;
143 var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
144 context.root.add_using_directive (ns_ref);
146 var source_file = new Vala.SourceFile (
148 Vala.SourceFileType.SOURCE,
149 "~~~~~testfile.vala",
152 source_file.add_using_directive (ns_ref);
153 context.add_source_file (source_file);
155 // add all the files (except the current one) - this.file.path
156 var pr = ((Project.Gtk)this.file.project);
157 if (this.file.build_module.length > 0) {
158 var cg = pr.compilegroups.get(this.file.build_module);
159 for (var i = 0; i < cg.sources.size; i++) {
160 var path = pr.resolve_path(
161 pr.resolve_path_combine_path(pr.firstPath(),cg.sources.get(i)));
162 if (path == this.file.path) {
165 print("Add source file %s\n", path);
166 var xsf = new Vala.SourceFile (
168 Vala.SourceFileType.SOURCE,
171 xsf.add_using_directive (ns_ref);
174 // default.. packages..
175 context.add_external_package ("glib-2.0");
176 context.add_external_package ("gobject-2.0");
177 // user defined ones..
179 var dcg = pr.compilegroups.get("_default");
180 for (var i = 0; i < dcg.packages.size; i++) {
181 context.add_external_package (dcg.packages.get(i));
185 //Vala.Config.PACKAGE_SUFFIX.substring (1)
187 // add the modules...
191 //context.add_external_package ("libvala-0.24");
196 //add_documented_files (context, settings.source_files);
198 Vala.Parser parser = new Vala.Parser ();
199 parser.parse (context);
200 //gir_parser.parse (context);
201 if (context.report.get_errors () > 0) {
202 print("parse got errors");
203 ((ValaSourceReport)context.report).dump();
204 Vala.CodeContext.pop ();
205 return this.report.line_errors;
212 if (context.report.get_errors () > 0) {
213 print("check got errors");
214 ((ValaSourceReport)context.report).dump();
215 Vala.CodeContext.pop ();
216 return this.report.line_errors;
220 Vala.CodeContext.pop ();
223 return this.report.line_errors;
232 int main (string[] args) {
234 var a = new ValaSource(file);
235 a.create_valac_tree();