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                 public void add_interface(GirObject parent, Vala.Interface cls)
81                 {
82                 
83                         var c = new GirObject("Interface", parent.name + "." + cls.name);
84                         parent.classes.set(cls.name, c);
85                         c.ns = parent.name;
86                         //c.parent = cls.base_class == null ? "" : cls.base_class.get_full_name() ;  // extends...
87                         c.gparent = parent;
88                         
89                         foreach(var p in cls.get_properties()) {
90                                 this.add_property(c, p);
91                         }
92                         // methods...
93                         foreach(var p in cls.get_signals()) {
94                                 this.add_signal(c, p);
95                         }
96                         
97                         foreach(var p in cls.get_methods()) {
98                                 // skip static methods..
99                                 if (p.binding != Vala.MemberBinding.INSTANCE &&
100                                         !(p is Vala.CreationMethod)
101                                 ) {
102                                         continue;
103                                 }
104                                 
105                                 this.add_method(c, p);
106                         }
107                         
108                         //if (cls.base_class != null) {
109                         //      c.inherits.add(cls.base_class.get_full_name());
110                         //}
111                         //foreach(var p in cls.get_base_types()) {
112                         //      if (p.data_type != null) {
113                         //              c.implements.add(p.data_type.get_full_name());
114                         //      }
115                         //}
116                           
117                         
118                         
119                          
120                 }
121                 
122                 public void add_class(GirObject parent, Vala.Class cls)
123                 {
124                 
125                         var c = new GirObject("Class", parent.name + "." + cls.name);
126                         parent.classes.set(cls.name, c);
127                         c.ns = parent.name;
128                         c.parent = cls.base_class == null ? "" : cls.base_class.get_full_name() ;  // extends...
129                         c.gparent = parent;
130                         
131                         foreach(var p in cls.get_properties()) {
132                                 this.add_property(c, p);
133                         }
134                         // methods...
135                         foreach(var p in cls.get_signals()) {
136                                 this.add_signal(c, p);
137                         }
138                         
139                         foreach(var p in cls.get_methods()) {
140                                 // skip static methods..
141                                 if (p.binding != Vala.MemberBinding.INSTANCE &&
142                                         !(p is Vala.CreationMethod)
143                                 ) {
144                                         continue;
145                                 }
146                                 
147                                 this.add_method(c, p);
148                         }
149                         
150                         if (cls.base_class != null) {
151                                 c.inherits.add(cls.base_class.get_full_name());
152                         }
153                         foreach(var p in cls.get_base_types()) {
154                                 if (p.data_type != null) {
155                                         c.implements.add(p.data_type.get_full_name());
156                                 }
157                         }
158                           
159                         
160                         
161                          
162                 }
163                 public void add_property(GirObject parent, Vala.Property prop)
164                 {
165                         var c = new GirObject("Prop",prop.name);
166                         c.gparent = parent;
167                         c.ns = parent.ns;
168                         c.propertyof = parent.name;
169                         c.type  = prop.property_type.data_type == null ? "" : prop.property_type.data_type.get_full_name();
170                         parent.props.set(prop.name,c);
171                         
172                 }
173                 public void add_signal(GirObject parent, Vala.Signal sig)
174                 {
175                         var c = new GirObject("Signal",sig.name);
176                         c.gparent = parent;
177                         c.ns = parent.ns;
178                         
179                         if (sig.return_type.data_type != null) {
180                                 //print("creating return type on signal %s\n", sig.name);
181                                 var cc = new GirObject("Return", "return-value");
182                                 cc.gparent = c;
183                                 cc.ns = c.ns;
184                                 cc.type  =  sig.return_type.data_type.get_full_name();
185                                 c.return_value = cc;
186                         }
187                         parent.signals.set(sig.name,c);
188                         
189                         var params =  sig.get_parameters() ;
190                         if (params.size < 1) {
191                                 return;
192                         }
193                         var cc = new GirObject("Paramset",sig.name); // what's the name on this?
194                         cc.gparent = c;
195                         cc.ns = c.ns;
196                         c.paramset = cc;
197                         
198                         
199                         foreach(var p in params) {
200                                 this.add_param(cc, p);
201                         }
202                         
203                 }       
204                 
205                 public void add_method(GirObject parent, Vala.Method met)
206                 {
207                         var n = met.name == null ? "" : met.name;
208                         var ty  = "Method";
209                         if (met is Vala.CreationMethod) {
210                                 ty = "Ctor";
211                                 if(n == "" || n == ".new") {
212                                         n = "new";
213                                 }
214                                 
215                         }
216                         
217                         var c = new GirObject(ty,n);
218                         c.gparent = parent;
219                         c.ns = parent.ns;
220                         
221                         if (met.return_type.data_type != null) {
222                                 //print("creating return type on method %s\n", met.name);
223                                 var cc = new GirObject("Return", "return-value");
224                                 cc.gparent = c;
225                                 cc.ns = c.ns;
226                                 cc.type  =  met.return_type.data_type.get_full_name();
227                                 c.return_value = cc;
228                         }
229                         if (met is Vala.CreationMethod) {
230                                 parent.ctors.set(c.name,c);
231                         } else {
232                                 parent.methods.set(met.name,c);
233                         }
234                         
235                         var params =  met.get_parameters() ;
236                         if (params.size < 1) {
237                                 return;
238                         }
239                         var cc = new GirObject("Paramset",met.name); // what's the name on this?
240                         cc.gparent = c;
241                         cc.ns = c.ns;
242                         c.paramset = cc;
243                         
244                         
245                         foreach(var p in params) {
246                                 if (p.name == null) {
247                                         continue;
248                                 }
249                                 this.add_param(cc, p);
250                         }
251                         
252                 }
253                 
254                 public void add_param(GirObject parent, Vala.Parameter pam)
255                 {
256                         var c = new GirObject("Param",pam.name);
257                         c.gparent = parent;
258                         c.ns = parent.ns;
259                         parent.params.add(c);
260                         c.type = pam.variable_type.data_type == null ? "" : pam.variable_type.data_type.get_full_name();
261                         Gir.checkParamOverride(c); 
262                         
263                 }
264                 
265                 public void create_valac_tree( )
266                 {
267                         // init context:
268                         context = new Vala.CodeContext ();
269                         Vala.CodeContext.push (context);
270                 
271                         context.experimental = false;
272                         context.experimental_non_null = false;
273                         
274 #if VALA_0_28
275                         var ver=28;
276 #elif VALA_0_26 
277                         var ver=26;
278 #elif VALA_0_24
279                         var ver=24;
280 #elif VALA_0_22 
281                         var ver=22;
282 #endif
283                         
284                         for (int i = 2; i <= ver; i += 2) {
285                                 context.add_define ("VALA_0_%d".printf (i));
286                         }
287                         
288                          
289                         //var vapidirs = ((Project.Gtk)this.file.project).vapidirs();
290                         // what's the current version of vala???
291                         
292                         
293                         //vapidirs +=  Path.get_dirname (context.get_vapi_path("glib-2.0")) ;
294                          
295                                 
296                         
297                         // or context.get_vapi_path("glib-2.0"); // should return path..
298                         //context.vapi_directories = vapidirs;
299                         context.report.enable_warnings = true;
300                         context.metadata_directories = { };
301                         context.gir_directories = {};
302                         context.thread = true;
303                         
304                         
305                         //this.report = new ValaSourceReport(this.file);
306                         //context.report = this.report;
307                         
308                         
309                         context.basedir = "/tmp"; //Posix.realpath (".");
310                 
311                         context.directory = context.basedir;
312                 
313
314                         // add default packages:
315                         //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
316                         context.profile = Vala.Profile.GOBJECT;
317                          
318                         var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
319                         context.root.add_using_directive (ns_ref);
320                         
321                         context.add_external_package ("glib-2.0"); 
322                         context.add_external_package ("gobject-2.0");
323                         
324                         // core packages we are interested in for the builder..
325                         // some of these may fail... - we probalby need a better way to handle this..
326                         
327                         context.add_external_package ("gtk+-3.0");
328                         if (!context.add_external_package ("webkit2gtk-4.0")) {
329                                 context.add_external_package ("webkit2gtk-3.0");
330                         }
331                         context.add_external_package ("clutter-gtk-1.0");
332                         context.add_external_package ("gdl-3.0");
333                         context.add_external_package ("gtksourceview-3.0");
334                         context.add_external_package ("vte-2.90"); //??? -- hopefullly that works..
335                         //add_documented_files (context, settings.source_files);
336                 
337                         Vala.Parser parser = new Vala.Parser ();
338                         parser.parse (context);
339                         //gir_parser.parse (context);
340                         if (context.report.get_errors () > 0) {
341                                 print("parse got errors");
342                                  
343                                 
344                                 Vala.CodeContext.pop ();
345                                 return ;
346                         }
347
348
349                         
350                         // check context:
351                         context.check ();
352                         if (context.report.get_errors () > 0) {
353                                 print("check got errors");
354                                  
355                                 Vala.CodeContext.pop ();
356                                  
357                                 return;
358                                 
359                         }
360                          
361                         Vala.CodeContext.pop ();
362                          
363                         context.accept(this);
364                         
365                         // dump the tree for Gtk?
366                         
367                         
368                         
369                         print("ALL OK?\n");
370                  
371                 }
372         //
373                 // startpoint:
374                 //
375          
376         }
377 }
378  /*
379 int main (string[] args) {
380         
381         var g = Palete.Gir.factoryFqn("Gtk.Dialog");
382         print("%s\n", g.asJSONString());
383         
384         return 0;
385 }
386  */
387  
388  
389
390