src/Palete/ValaSource.vala
[app.Builder.js] / src / Palete / ValaSource.vala
1
2 // valac TreeBuilder.vala --pkg libvala-0.24 --pkg posix -o /tmp/treebuilder
3
4 namespace Palete {
5         
6         public class ValaSourceReport  : Vala.Report {
7
8
9                 
10
11                 public Gee.HashMap<int,string> line_errors;
12
13                 public ValaSourceReport()
14                 {
15                         base();
16                         this.line_errors = new Gee.HashMap<int,string> ();
17                 }
18                 
19                 public override void err (Vala.SourceReference? source, string message) {
20                         errors++;
21                         if (source == null) {
22                                 return;
23                                 //stderr.printf ("My error: %s\n", message);
24                         }
25                         if (source.file.filename != "~~~~~testfile.vala") {
26                                 print ("Other file: Got error error: %s\n", message);
27                                 return;
28                         }
29                         var pre = "";
30                         if (line_errors.has_key(source.begin.line)) {
31                                 pre = line_errors.get(source.begin.line) + "\n";
32                                 
33                         }
34                         line_errors.set(source.begin.line, pre +  message);
35                 }
36                 public void dump()
37                 {
38                         var iter = this.line_errors.map_iterator();
39                         while (iter.next()) {
40                                 print ("%d : %s\n\n", iter.get_key(), iter.get_value());
41                         }
42                 }
43
44         }
45
46         public class ValaSource : Vala.CodeVisitor {
47
48                 Vala.CodeContext context;
49                 ValaSourceReport report;
50                 JsRender.JsRender file; 
51                 public ValaSource(JsRender.JsRender file) {
52                         base();
53                         this.file = file;
54                         
55
56                 }
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]);
61                         }
62                 }
63                 
64                 public Gee.HashMap<int,string> checkFile()
65                 {
66                         return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
67                 }
68
69                 public Gee.HashMap<int,string> checkFileWithNodePropChange(
70                            JsRender.Node node, 
71                            string prop,
72                            string ptype,
73                            string val)
74                 {
75                         Gee.HashMap<int,string> ret = new Gee.HashMap<int,string> ();
76                         var hash = ptype == "listener" ? node.listeners : node.props;
77                         
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") {
81                                 return ret;
82                         }
83                                 
84                         
85                         var old = hash.get(prop);
86                         var newval = "/*--VALACHECK-START--*/ " + val ;
87                         
88                         hash.set(prop, newval);
89                         var tmpstring = JsRender.NodeToVala.mungeFile(this.file);
90                         var bits = tmpstring.split("/*--VALACHECK-START--*/");
91                         var offset =0;
92                         if (bits.length > 0) {
93                                 offset = bits[0].split("\n").length +1;
94                         }
95                         //this.dumpCode(tmpstring);
96                         //print("offset %d\n", offset);
97                         this.checkString(tmpstring);
98                         hash.set(prop, old);
99                         // modify report
100                         
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()));
108                         }
109                         return ret;
110                         
111                 }
112                 
113                 public Gee.HashMap<int,string> checkString(string contents)
114         {
115                         // init context:
116                         
117                         context = new Vala.CodeContext ();
118                         Vala.CodeContext.push (context);
119                 
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" ;
124
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;
132                         
133                         
134                         context.basedir = Posix.realpath (".");
135                 
136                         context.directory = context.basedir;
137                 
138
139                         // add default packages:
140                         //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
141                         context.profile = Vala.Profile.GOBJECT;
142                          
143                         var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
144                         context.root.add_using_directive (ns_ref);
145
146                         var source_file = new Vala.SourceFile (
147                                 context, 
148                                 Vala.SourceFileType.SOURCE, 
149                                         "~~~~~testfile.vala",
150                                         contents
151                         );
152                         source_file.add_using_directive (ns_ref);
153                         context.add_source_file (source_file);
154                         
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) {
163                                                 continue;
164                                         }
165                                         print("Add source file %s\n", path);
166                                         var xsf = new Vala.SourceFile (
167                                                 context,
168                                                 Vala.SourceFileType.SOURCE, 
169                                                 path
170                                         );
171                                         xsf.add_using_directive (ns_ref);
172                                 }
173                         }
174                         // default.. packages..
175                         context.add_external_package ("glib-2.0"); 
176                         context.add_external_package ("gobject-2.0");
177                         // user defined ones..
178                         
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));
182                                 
183                         }
184                 
185                          //Vala.Config.PACKAGE_SUFFIX.substring (1)
186                         
187                         // add the modules...
188                         
189                         
190                         
191                         //context.add_external_package ("libvala-0.24");
192                         
193                         
194
195                 
196                         //add_documented_files (context, settings.source_files);
197                 
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;
206                         }
207
208
209                         /*
210                         // check context:
211                         context.check ();
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;
217                                 
218                         }
219                         */
220                         Vala.CodeContext.pop ();
221                         
222                         print("ALL OK?\n");
223                         return this.report.line_errors;
224                 }
225         //
226                 // startpoint:
227                 //
228          
229         }
230 }
231 /*
232 int main (string[] args) {
233
234         var a = new ValaSource(file);
235         a.create_valac_tree();
236         return 0;
237 }
238 */
239
240