src/Palete/VapiParser.vala
[app.Builder.js] / src / Palete / VapiParser.vala
1
2
3  // valac -g  --pkg libvala-0.26  --pkg gee-1.0 --pkg json-glib-1.0  --pkg gtk+-3.0   VapiParser.vala Gir.vala GirObject.vala -o /tmp/vdoc
4
5 namespace Palete {
6          
7          
8          
9
10         public class VapiParser : Vala.CodeVisitor {
11                 
12                 Vala.CodeContext context;
13                  
14                 public VapiParser() {
15                         base();
16                         // should not really happen..
17                         if (Gir.cache == null) {
18                                 Gir.cache =  new Gee.HashMap<string,Gir>();
19                         }
20                 }
21                 
22                 
23                 
24                 public override void visit_namespace (Vala.Namespace element) 
25                 {
26                         if (element == null) {
27                                 
28                                 return;
29                         }
30                         print("parsing namespace %s\n", element.name);
31                         if (element.name == null) {
32                                 element.accept_children(this); // catch sub namespaces..
33                                 return;
34                         }
35                         
36                         var g = new Gir(element.name);
37                         Gir.cache.set(element.name, g);
38                         
39                         
40                         foreach(var c in element.get_classes()) {
41                                 this.add_class(g, c);
42                         }
43                         foreach(var c in element.get_enums()) {
44                                 this.add_enum(g, c);
45                         }
46                         foreach(var c in element.get_interfaces()) {
47                                 this.add_interface(g, c);
48                         }
49                         
50                         element.accept_children(this); // catch sub namespaces..
51                         
52                         
53                 }
54                 
55                 
56                 public void add_enum(GirObject parent, Vala.Enum cls)
57                 {
58                 
59                         var c = new GirObject("Enum",   cls.name);
60                         parent.consts.set(cls.name, c);
61                         c.ns = parent.name;
62                         
63                         c.gparent = parent;
64                         
65                         foreach(var e in cls.get_values()) {
66                                 var em = new GirObject("EnumMember",e.name);
67                                 em.gparent = c;
68                                 em.ns = c.ns;
69                                 em.type  = e.type_reference == null ||  e.type_reference.data_type == null ? "" : e.type_reference.data_type.get_full_name();
70                                 // unlikely to get value..
71                                 //c.value = element->get_prop("value");
72                                 c.consts.set(e.name,em);
73                         }
74                         
75                         
76                         
77                          
78                 }
79                 
80                 
81                 public void add_class(GirObject parent, Vala.Class cls)
82                 {
83                 
84                         var c = new GirObject("Class", parent.name + "." + cls.name);
85                         parent.classes.set(cls.name, c);
86                         c.ns = parent.name;
87                         c.parent = cls.base_class == null ? "" : cls.base_class.get_full_name() ;  // extends...
88                         c.gparent = parent;
89                         
90                         foreach(var p in cls.get_properties()) {
91                                 this.add_property(c, p);
92                         }
93                         // methods...
94                         foreach(var p in cls.get_signals()) {
95                                 this.add_signal(c, p);
96                         }
97                         
98                         foreach(var p in cls.get_methods()) {
99                                 // skip static methods..
100                                 if (p.binding != Vala.MemberBinding.INSTANCE &&
101                                         !(p is Vala.CreationMethod)
102                                 ) {
103                                         continue;
104                                 }
105                                 
106                                 this.add_method(c, p);
107                         }
108                         
109                         if (cls.base_class != null) {
110                                 c.inherits.add(cls.base_class.get_full_name());
111                         }
112                         foreach(var p in cls.get_base_types()) {
113                                 if (p.data_type != null) {
114                                         c.implements.add(p.data_type.get_full_name());
115                                 }
116                         }
117                           
118                         
119                         
120                          
121                 }
122                 public void add_property(GirObject parent, Vala.Property prop)
123                 {
124                         var c = new GirObject("Prop",prop.name);
125                         c.gparent = parent;
126                         c.ns = parent.ns;
127                         c.propertyof = parent.name;
128                         c.type  = prop.property_type.data_type == null ? "" : prop.property_type.data_type.get_full_name();
129                         parent.props.set(prop.name,c);
130                         
131                 }
132                 public void add_signal(GirObject parent, Vala.Signal sig)
133                 {
134                         var c = new GirObject("Signal",sig.name);
135                         c.gparent = parent;
136                         c.ns = parent.ns;
137                         
138                         if (sig.return_type.data_type != null) {
139                                 //print("creating return type on signal %s\n", sig.name);
140                                 var cc = new GirObject("Return", "return-value");
141                                 cc.gparent = c;
142                                 cc.ns = c.ns;
143                                 cc.type  =  sig.return_type.data_type.get_full_name();
144                                 c.return_value = cc;
145                         }
146                         parent.signals.set(sig.name,c);
147                         
148                         var params =  sig.get_parameters() ;
149                         if (params.size < 1) {
150                                 return;
151                         }
152                         var cc = new GirObject("Paramset",sig.name); // what's the name on this?
153                         cc.gparent = c;
154                         cc.ns = c.ns;
155                         c.paramset = cc;
156                         
157                         
158                         foreach(var p in params) {
159                                 this.add_param(cc, p);
160                         }
161                         
162                 }       
163                 
164                 public void add_method(GirObject parent, Vala.Method met)
165                 {
166                         var n = met.name == null ? parent.name : "";
167                         var ty  = "Method";
168                         if (met is Vala.CreationMethod && n == "") {
169                                 n = ".new";
170                                 ty = "Ctor";
171                         }
172                         
173                         var c = new GirObject(ty,n);
174                         c.gparent = parent;
175                         c.ns = parent.ns;
176                         
177                         if (met.return_type.data_type != null) {
178                                 //print("creating return type on method %s\n", met.name);
179                                 var cc = new GirObject("Return", "return-value");
180                                 cc.gparent = c;
181                                 cc.ns = c.ns;
182                                 cc.type  =  met.return_type.data_type.get_full_name();
183                                 c.return_value = cc;
184                         }
185                         if (met is Vala.CreationMethod) {
186                                 parent.ctors.set(c.name,c);
187                         } else {
188                                 parent.methods.set(met.name,c);
189                         }
190                         
191                         var params =  met.get_parameters() ;
192                         if (params.size < 1) {
193                                 return;
194                         }
195                         var cc = new GirObject("Paramset",met.name); // what's the name on this?
196                         cc.gparent = c;
197                         cc.ns = c.ns;
198                         c.paramset = cc;
199                         
200                         
201                         foreach(var p in params) {
202                                 if (p.name == null) {
203                                         continue;
204                                 }
205                                 this.add_param(cc, p);
206                         }
207                         
208                 }
209                 
210                 public void add_param(GirObject parent, Vala.Parameter pam)
211                 {
212                         var c = new GirObject("Param",pam.name);
213                         c.gparent = parent;
214                         c.ns = parent.ns;
215                         parent.params.add(c);
216                         c.type = pam.variable_type.data_type == null ? "" : pam.variable_type.data_type.get_full_name();
217                         // this.checkParamOverride(c);   - this is an old kludge for Gir files..
218                         
219                 }
220                 
221                 public void create_valac_tree( )
222                 {
223                         // init context:
224                         context = new Vala.CodeContext ();
225                         Vala.CodeContext.push (context);
226                 
227                         context.experimental = false;
228                         context.experimental_non_null = false;
229                         
230 #if VALA_0_28
231                         var ver=28;
232 #elif VALA_0_26 
233                         var ver=26;
234 #elif VALA_0_24
235                         var ver=24;
236 #elif VALA_0_22 
237                         var ver=22;
238 #endif
239                         
240                         for (int i = 2; i <= ver; i += 2) {
241                                 context.add_define ("VALA_0_%d".printf (i));
242                         }
243                         
244                          
245                         //var vapidirs = ((Project.Gtk)this.file.project).vapidirs();
246                         // what's the current version of vala???
247                         
248                         
249                         //vapidirs +=  Path.get_dirname (context.get_vapi_path("glib-2.0")) ;
250                          
251                                 
252                         
253                         // or context.get_vapi_path("glib-2.0"); // should return path..
254                         //context.vapi_directories = vapidirs;
255                         context.report.enable_warnings = true;
256                         context.metadata_directories = { };
257                         context.gir_directories = {};
258                         context.thread = true;
259                         
260                         
261                         //this.report = new ValaSourceReport(this.file);
262                         //context.report = this.report;
263                         
264                         
265                         context.basedir = "/tmp"; //Posix.realpath (".");
266                 
267                         context.directory = context.basedir;
268                 
269
270                         // add default packages:
271                         //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
272                         context.profile = Vala.Profile.GOBJECT;
273                          
274                         var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
275                         context.root.add_using_directive (ns_ref);
276                         
277                         context.add_external_package ("glib-2.0"); 
278                         context.add_external_package ("gobject-2.0");
279                         
280                         // core packages we are interested in for the builder..
281                         // some of these may fail... - we probalby need a better way to handle this..
282                         
283                         context.add_external_package ("gtk+-3.0");
284                         if (!context.add_external_package ("webkit2gtk-4.0")) {
285                                 context.add_external_package ("webkit2gtk-3.0");
286                         }
287                         context.add_external_package ("clutter-gtk-1.0");
288                         context.add_external_package ("gdl-3.0");
289                         context.add_external_package ("gtksourceview-3.0");
290                         context.add_external_package ("vte-2.90"); //??? -- hopefullly that works..
291                         //add_documented_files (context, settings.source_files);
292                 
293                         Vala.Parser parser = new Vala.Parser ();
294                         parser.parse (context);
295                         //gir_parser.parse (context);
296                         if (context.report.get_errors () > 0) {
297                                 print("parse got errors");
298                                  
299                                 
300                                 Vala.CodeContext.pop ();
301                                 return ;
302                         }
303
304
305                         
306                         // check context:
307                         context.check ();
308                         if (context.report.get_errors () > 0) {
309                                 print("check got errors");
310                                  
311                                 Vala.CodeContext.pop ();
312                                  
313                                 return;
314                                 
315                         }
316                          
317                         Vala.CodeContext.pop ();
318                          
319                         context.accept(this);
320                         
321                         // dump the tree for Gtk?
322                         
323                         
324                         
325                         print("ALL OK?\n");
326                  
327                 }
328         //
329                 // startpoint:
330                 //
331          
332         }
333 }
334 /*
335 int main (string[] args) {
336         
337         var g = Palete.Gir.factory("Gdk");
338         print("%s\n", g.asJSONString());
339         
340         return 0;
341 }
342 * */
343  
344  
345
346