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, 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(
75 var hash = ptype == "listener" ? node.listeners : node.props;
76 var old = hash.get(prop);
77 var newval = "/*--VALACHECK-START--*/ " + val + " /*--VALACHECK-START--*/";
79 hash.set(prop, newval);
80 var tmpstring = JsRender.NodeToVala.mungeFile(this.file);
81 var bits = tmpstring.split(newval);
83 if (bits.length > 1) {
84 offset = bits[0].split("\n").length;
86 this.dumpCode(tmpstring);
87 print("offset %d\n", offset);
88 this.checkString(tmpstring);
91 Gee.HashMap<int,string> ret = new Gee.HashMap<int,string> ();
92 var iter = this.report.line_errors.map_iterator();
94 ret.set(iter.get_key() - offset, iter.get_value());
100 public Gee.HashMap<int,string> checkString(string contents)
104 context = new Vala.CodeContext ();
105 Vala.CodeContext.push (context);
107 context.experimental = false;
108 context.experimental_non_null = false;
109 context.vapi_directories = { "/usr/share/vala-0.24/vapi" };
110 context.report.enable_warnings = true;
111 context.metadata_directories = { };
112 context.gir_directories = {};
113 this.report = new ValaSourceReport();;
114 context.report = this.report;
116 context.basedir = Posix.realpath (".");
118 context.directory = context.basedir;
121 // add default packages:
122 //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
123 context.profile = Vala.Profile.GOBJECT;
124 // context.add_define ("GOBJECT");
126 //add_depencies (context, settings.packages);
127 //if (reporter.errors > 0) {
131 var source_file = new Vala.SourceFile (
133 Vala.SourceFileType.SOURCE,
134 "~~~~~testfile.vala",
138 context.add_external_package ("glib-2.0");
139 context.add_external_package ("gobject-2.0");
140 context.add_external_package ("libvala-0.24");
141 var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
142 source_file.add_using_directive (ns_ref);
143 context.root.add_using_directive (ns_ref);
144 context.add_source_file (source_file);
147 //add_documented_files (context, settings.source_files);
149 Vala.Parser parser = new Vala.Parser ();
150 parser.parse (context);
151 //gir_parser.parse (context);
152 if (context.report.get_errors () > 0) {
153 print("parse got errors");
154 ((ValaSourceReport)context.report).dump();
155 Vala.CodeContext.pop ();
156 return this.report.line_errors;
163 if (context.report.get_errors () > 0) {
164 print("check got errors");
165 ((ValaSourceReport)context.report).dump();
166 Vala.CodeContext.pop ();
167 return this.report.line_errors;
171 Vala.CodeContext.pop ();
174 return this.report.line_errors;
183 int main (string[] args) {
185 var a = new ValaSource(file);
186 a.create_valac_tree();