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 Gee.HashMap<int,string> checkFile()
58 return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
61 public Gee.HashMap<int,string> checkFileWithNodePropChange(
63 string prop, string val)
65 var old = node.props.get(prop);
66 var newval = "/*--VALACHECK-START--*/ " + val + " /*--VALACHECK-START--*/";
67 node.props.set(prop, newval);
68 var tmpstring = JsRender.NodeToVala.mungeFile(this.file);
69 var bits = tmpstring.split(newval);
71 if (bits.length > 1) {
72 offset = bits[0].split("\n").length;
74 this.checkString(tmpstring);
75 node.props.set(prop, old);
77 Gee.HashMap<int,string> ret = new Gee.HashMap<int,string> ();
78 var iter = this.report.line_errors.map_iterator();
80 ret.set(iter.get_key() - offset, iter.get_value());
86 public Gee.HashMap<int,string> checkString(string contents)
90 context = new Vala.CodeContext ();
91 Vala.CodeContext.push (context);
93 context.experimental = false;
94 context.experimental_non_null = false;
95 context.vapi_directories = { "/usr/share/vala-0.24/vapi" };
96 context.report.enable_warnings = true;
97 context.metadata_directories = { };
98 context.gir_directories = {};
99 this.report = new ValaSourceReport();;
100 context.report = this.report;
102 context.basedir = Posix.realpath (".");
104 context.directory = context.basedir;
107 // add default packages:
108 //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
109 context.profile = Vala.Profile.GOBJECT;
110 // context.add_define ("GOBJECT");
112 //add_depencies (context, settings.packages);
113 //if (reporter.errors > 0) {
117 var source_file = new Vala.SourceFile (
119 Vala.SourceFileType.SOURCE,
120 "~~~~~testfile.vala",
124 context.add_external_package ("glib-2.0");
125 context.add_external_package ("gobject-2.0");
126 context.add_external_package ("libvala-0.24");
127 var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
128 source_file.add_using_directive (ns_ref);
129 context.root.add_using_directive (ns_ref);
130 context.add_source_file (source_file);
133 //add_documented_files (context, settings.source_files);
135 Vala.Parser parser = new Vala.Parser ();
136 parser.parse (context);
137 //gir_parser.parse (context);
138 if (context.report.get_errors () > 0) {
139 print("parse got errors");
140 ((ValaSourceReport)context.report).dump();
141 Vala.CodeContext.pop ();
142 return this.report.line_errors;
149 if (context.report.get_errors () > 0) {
150 print("check got errors");
151 ((ValaSourceReport)context.report).dump();
152 Vala.CodeContext.pop ();
153 return this.report.line_errors;
156 Vala.CodeContext.pop ();
158 return this.report.line_errors;
167 int main (string[] args) {
169 var a = new ValaSource(file);
170 a.create_valac_tree();