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") {
29 if (line_errors.has_key(source.begin.line)) {
30 pre = line_errors.get(source.begin.line) + "\n";
33 line_errors.set(source.begin.line, pre + message);
37 var iter = this.line_errors.map_iterator();
39 print ("%d : %s\n\n", iter.get_key(), iter.get_value());
45 public class ValaSource : Vala.CodeVisitor {
47 Vala.CodeContext context;
48 ValaSourceReport report;
49 JsRender.JsRender file;
50 public ValaSource(JsRender.JsRender file) {
56 public void dumpCode(string str) {
57 var ls = str.split("\n");
58 for (var i=0;i < ls.length; i++) {
59 print("%d : %s\n", i+1, ls[i]);
63 public Gee.HashMap<int,string> checkFile()
65 return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
68 public Gee.HashMap<int,string> checkFileWithNodePropChange(
74 Gee.HashMap<int,string> ret = new Gee.HashMap<int,string> ();
75 var hash = ptype == "listener" ? node.listeners : node.props;
77 // untill we get a smarter renderer..
78 // we have some scenarios where changing the value does not work
79 if (prop == "* xns" || prop == "xtype") {
84 var old = hash.get(prop);
85 var newval = "/*--VALACHECK-START--*/ " + val ;
87 hash.set(prop, newval);
88 var tmpstring = JsRender.NodeToVala.mungeFile(this.file);
89 var bits = tmpstring.split("/*--VALACHECK-START--*/");
91 if (bits.length > 0) {
92 offset = bits[0].split("\n").length +1;
94 //this.dumpCode(tmpstring);
95 //print("offset %d\n", offset);
96 this.checkString(tmpstring);
100 var iter = this.report.line_errors.map_iterator();
101 while (iter.next()) {
102 // print("%d : %s\n",iter.get_key() - offset, iter.get_value());
103 // we have to prefix the error with the fake line number
104 // so that it's a unique mark..
105 ret.set(iter.get_key() - offset,
106 "%d : %s".printf(iter.get_key() - offset,iter.get_value()));
112 public Gee.HashMap<int,string> checkString(string contents)
116 context = new Vala.CodeContext ();
117 Vala.CodeContext.push (context);
119 context.experimental = false;
120 context.experimental_non_null = false;
123 // or context.get_vapi_path("glib-2.0"); // should return path..
124 context.vapi_directories = { "/usr/share/vala-0.24/vapi" };
125 context.report.enable_warnings = true;
126 context.metadata_directories = { };
127 context.gir_directories = {};
128 this.report = new ValaSourceReport();;
129 context.report = this.report;
131 context.basedir = Posix.realpath (".");
133 context.directory = context.basedir;
136 // add default packages:
137 //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
138 context.profile = Vala.Profile.GOBJECT;
139 // context.add_define ("GOBJECT");
141 //add_depencies (context, settings.packages);
142 //if (reporter.errors > 0) {
146 var source_file = new Vala.SourceFile (
148 Vala.SourceFileType.SOURCE,
149 "~~~~~testfile.vala",
152 //Vala.Config.PACKAGE_SUFFIX.substring (1)
154 context.add_external_package ("glib-2.0");
155 context.add_external_package ("gobject-2.0");
156 context.add_external_package ("libvala-0.24");
157 var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
158 source_file.add_using_directive (ns_ref);
159 context.root.add_using_directive (ns_ref);
160 context.add_source_file (source_file);
163 //add_documented_files (context, settings.source_files);
165 Vala.Parser parser = new Vala.Parser ();
166 parser.parse (context);
167 //gir_parser.parse (context);
168 if (context.report.get_errors () > 0) {
169 print("parse got errors");
170 ((ValaSourceReport)context.report).dump();
171 Vala.CodeContext.pop ();
172 return this.report.line_errors;
179 if (context.report.get_errors () > 0) {
180 print("check got errors");
181 ((ValaSourceReport)context.report).dump();
182 Vala.CodeContext.pop ();
183 return this.report.line_errors;
187 Vala.CodeContext.pop ();
190 return this.report.line_errors;
199 int main (string[] args) {
201 var a = new ValaSource(file);
202 a.create_valac_tree();