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                                 return;
27                         }
28                         var pre = "";
29                         if (line_errors.has_key(source.begin.line)) {
30                                 pre = line_errors.get(source.begin.line) + "\n";
31                                 
32                         }
33                         line_errors.set(source.begin.line, pre +  message);
34                 }
35                 public void dump()
36                 {
37                         var iter = this.line_errors.map_iterator();
38                         while (iter.next()) {
39                                 print ("%d : %s\n\n", iter.get_key(), iter.get_value());
40                         }
41                 }
42
43         }
44
45         public class ValaSource : Vala.CodeVisitor {
46
47                 Vala.CodeContext context;
48                 ValaSourceReport report;
49                 JsRender.JsRender file; 
50                 public ValaSource(JsRender.JsRender file) {
51                         base();
52                         this.file = file;
53                         
54
55                 }
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]);
60                         }
61                 }
62                 
63                 public Gee.HashMap<int,string> checkFile()
64                 {
65                         return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
66                 }
67
68                 public Gee.HashMap<int,string> checkFileWithNodePropChange(
69                            JsRender.Node node, 
70                            string prop,
71                            string ptype,
72                            string val)
73                 {
74                         Gee.HashMap<int,string> ret = new Gee.HashMap<int,string> ();
75                         var hash = ptype == "listener" ? node.listeners : node.props;
76                         
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") {
80                                 return ret;
81                         }
82                                 
83                         
84                         var old = hash.get(prop);
85                         var newval = "/*--VALACHECK-START--*/ " + val ;
86                         
87                         hash.set(prop, newval);
88                         var tmpstring = JsRender.NodeToVala.mungeFile(this.file);
89                         var bits = tmpstring.split("/*--VALACHECK-START--*/");
90                         var offset =0;
91                         if (bits.length > 0) {
92                                 offset = bits[0].split("\n").length +1;
93                         }
94                         //this.dumpCode(tmpstring);
95                         //print("offset %d\n", offset);
96                         this.checkString(tmpstring);
97                         hash.set(prop, old);
98                         // modify report
99                         
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()));
107                         }
108                         return ret;
109                         
110                 }
111                 
112                 public Gee.HashMap<int,string> checkString(string contents)
113         {
114                         // init context:
115                         
116                         context = new Vala.CodeContext ();
117                         Vala.CodeContext.push (context);
118                 
119                         context.experimental = false;
120                         context.experimental_non_null = false;
121                         var vapidirs = ((Project.Gtk)this.file.project).vapidirs();
122                         vapidirs +=  "/usr/share/vala-0.24/vapi" ;
123
124                         // or context.get_vapi_path("glib-2.0"); // should return path..
125                         context.vapi_directories = vapidirs;
126                         context.report.enable_warnings = true;
127                         context.metadata_directories = { };
128                         context.gir_directories = {};
129                         this.report = new ValaSourceReport();;
130                         context.report = this.report;
131                         
132                         
133                         context.basedir = Posix.realpath (".");
134                 
135                         context.directory = context.basedir;
136                 
137
138                         // add default packages:
139                         //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
140                                 context.profile = Vala.Profile.GOBJECT;
141                         //      context.add_define ("GOBJECT");
142                         //}
143                         //add_depencies (context, settings.packages);
144                         //if (reporter.errors > 0) {
145                         //      return context;
146                         //}
147                         var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
148                         
149                         var source_file = new Vala.SourceFile (
150                                 context, 
151                                 Vala.SourceFileType.SOURCE, 
152                                         "~~~~~testfile.vala",
153                                         contents
154                         );
155                 
156                 // add all the files (except the current one) - this.file.path
157                 var pr = ((Project.Gtk)this.file.project);
158                 if (this.file.build_module.length > 0) {
159                                 var cg =  pr.compilegroups.get(this.file.build_module);
160                                 for (var i = 0; i < cg.sources.size; i++) {
161                                         var path = pr.resolve_path(
162                                 pr.resolve_path_combine_path(pr.firstPath(),cg.sources.get(i)));
163                                         if (path == this.file.path) {
164                                                 continue;
165                                         }
166                                         print("Add source file %s\n", path);
167                                         var xsf = new Vala.SourceFile (
168                                                 context,
169                                                 Vala.SourceFileType.SOURCE, 
170                                                 path
171                                         );
172                                         xsf.add_using_directive (ns_ref);
173                                 }
174                         }
175                 
176                 
177                          //Vala.Config.PACKAGE_SUFFIX.substring (1)
178                         
179                         // add the modules...
180                         
181                         context.add_external_package ("glib-2.0");
182                         context.add_external_package ("gobject-2.0");
183                         context.add_external_package ("libvala-0.24");
184                         
185                         source_file.add_using_directive (ns_ref);
186                         context.root.add_using_directive (ns_ref);
187                         context.add_source_file (source_file);
188
189                 
190                         //add_documented_files (context, settings.source_files);
191                 
192                         Vala.Parser parser = new Vala.Parser ();
193                         parser.parse (context);
194                         //gir_parser.parse (context);
195                         if (context.report.get_errors () > 0) {
196                                 print("parse got errors");
197                                 ((ValaSourceReport)context.report).dump();
198                                 Vala.CodeContext.pop ();
199                                 return this.report.line_errors;
200                         }
201
202
203                         /*
204                         // check context:
205                         context.check ();
206                         if (context.report.get_errors () > 0) {
207                                 print("check got errors");
208                                 ((ValaSourceReport)context.report).dump();
209                                 Vala.CodeContext.pop ();
210                                 return this.report.line_errors;
211                                 
212                         }
213                         */
214                         Vala.CodeContext.pop ();
215                         
216                         print("ALL OK?\n");
217                         return this.report.line_errors;
218                 }
219         //
220                 // startpoint:
221                 //
222          
223         }
224 }
225 /*
226 int main (string[] args) {
227
228         var a = new ValaSource(file);
229         a.create_valac_tree();
230         return 0;
231 }
232 */
233
234