resources/RooUsage.txt
[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          public errordomain VapiParserError {
8                 PARSE_FAILED 
9         }
10          
11
12         public class VapiParser : Vala.CodeVisitor {
13                 
14                 Vala.CodeContext context;
15                  
16                 public VapiParser() {
17                         base();
18                         // should not really happen..
19                         if (Gir.cache == null) {
20                                 Gir.cache =  new Gee.HashMap<string,Gir>();
21                         }
22                 }
23                 
24                 
25                 
26                 public override void visit_namespace (Vala.Namespace element) 
27                 {
28                         if (element == null) {
29                                 
30                                 return;
31                         }
32                         
33                         
34                         //print("parsing namespace %s\n", element.name);
35                         if (element.name == null) {
36                                 element.accept_children(this); // catch sub namespaces..
37                                 return;
38                         }
39                         this.add_namespace(null, element);
40                 }
41                 public void add_namespace(GirObject? parent, Vala.Namespace element)
42                 {
43                         
44                         
45                         var g = new GirObject("Package",element.name) ;
46                         if (parent == null) {
47                                 Gir.cache.set(element.name, (Gir)g);
48                         } else {
49                                 // we add it as a class of the package.. even though its a namespace..
50                                 parent.classes.set(element.name, g);
51                         }
52                         
53                         
54                         foreach(var c in element.get_classes()) {
55                                 this.add_class(g, c);
56                         }
57                         foreach(var c in element.get_enums()) {
58                                 this.add_enum(g, c);
59                         }
60                         foreach(var c in element.get_interfaces()) {
61                                 this.add_interface(g, c);
62                         }
63                         foreach(var c in element.get_namespaces()) {
64                                 this.add_namespace(g, c);
65                         }
66                         foreach(var c in element.get_methods()) {
67                                 this.add_method(g, c);
68                         }
69                         element.accept_children(this); // catch sub namespaces..
70                         
71                         
72                 }
73                  
74                 
75                 public void add_enum(GirObject parent, Vala.Enum cls)
76                 {
77                 
78                         var c = new GirObject("Enum",   cls.name);
79                         parent.consts.set(cls.name, c);
80                         c.ns = parent.name;
81                         
82                         c.gparent = parent;
83                         
84                         foreach(var e in cls.get_values()) {
85                                 var em = new GirObject("EnumMember",e.name);
86                                 em.gparent = c;
87                                 em.ns = c.ns;
88                                 em.type  = e.type_reference == null ||  e.type_reference.data_type == null ? "" : e.type_reference.data_type.get_full_name();
89                                 // unlikely to get value..
90                                 //c.value = element->get_prop("value");
91                                 c.consts.set(e.name,em);
92                         }
93                         
94                         
95                         
96                          
97                 }
98                 
99                 public void add_interface(GirObject parent, Vala.Interface cls)
100                 {
101                 
102                         var c = new GirObject("Interface", parent.name + "." + cls.name);
103                         parent.classes.set(cls.name, c);
104                         c.ns = parent.name;
105                         //c.parent = cls.base_class == null ? "" : cls.base_class.get_full_name() ;  // extends...
106                         c.gparent = parent;
107                         
108                         foreach(var p in cls.get_properties()) {
109                                 this.add_property(c, p);
110                         }
111                         // methods...
112                         foreach(var p in cls.get_signals()) {
113                                 this.add_signal(c, p);
114                         }
115                         
116                         foreach(var p in cls.get_methods()) {
117                                 // skip static methods..
118                                 if (p.binding != Vala.MemberBinding.INSTANCE &&
119                                         !(p is Vala.CreationMethod)
120                                 ) {
121                                         continue;
122                                 }
123                                 
124                                 this.add_method(c, p);
125                         }
126                         
127                         //if (cls.base_class != null) {
128                         //      c.inherits.add(cls.base_class.get_full_name());
129                         //}
130                         //foreach(var p in cls.get_base_types()) {
131                         //      if (p.data_type != null) {
132                         //              c.implements.add(p.data_type.get_full_name());
133                         //      }
134                         //}
135                           
136                         
137                         
138                          
139                 }
140                 
141                 public void add_class(GirObject parent, Vala.Class cls)
142                 {
143                 
144                         var c = new GirObject("Class", parent.name + "." + cls.name);
145                         parent.classes.set(cls.name, c);
146                         c.ns = parent.name;
147                         c.parent = cls.base_class == null ? "" : cls.base_class.get_full_name() ;  // extends...
148                         c.gparent = parent;
149                         
150                         foreach(var p in cls.get_properties()) {
151                                 this.add_property(c, p);
152                         }
153                         // methods...
154                         foreach(var p in cls.get_signals()) {
155                                 this.add_signal(c, p);
156                         }
157                         
158                         foreach(var p in cls.get_methods()) {
159                                 // skip static methods..
160                                 if (p.binding != Vala.MemberBinding.INSTANCE &&
161                                         !(p is Vala.CreationMethod)
162                                 ) {
163                                         continue;
164                                 }
165                                 
166                                 this.add_method(c, p);
167                         }
168                         
169                         if (cls.base_class != null) {
170                                 c.inherits.add(cls.base_class.get_full_name());
171                         }
172                         foreach(var p in cls.get_base_types()) {
173                                 if (p.data_type != null) {
174                                         c.implements.add(p.data_type.get_full_name());
175                                 }
176                         }
177                           
178                         
179                         
180                          
181                 }
182                 public void add_property(GirObject parent, Vala.Property prop)
183                 {
184                         var c = new GirObject("Prop",prop.name);
185                         c.gparent = parent;
186                         c.ns = parent.ns;
187                         c.propertyof = parent.name;
188                         
189                         c.type  = prop.property_type.data_type == null ? "" : prop.property_type.data_type.get_full_name();
190                         parent.props.set(prop.name,c);
191                         
192                 }
193                 public void add_signal(GirObject parent, Vala.Signal sig)
194                 {
195                         var c = new GirObject("Signal",sig.name);
196                         c.gparent = parent;
197                         c.ns = parent.ns;
198                         
199                         if (sig.return_type.data_type != null) {
200                                 //print("creating return type on signal %s\n", sig.name);
201                                 var cc = new GirObject("Return", "return-value");
202                                 cc.gparent = c;
203                                 cc.ns = c.ns;
204                                 cc.type  =  sig.return_type.data_type.get_full_name();
205                                 c.return_value = cc;
206                         }
207                         parent.signals.set(sig.name,c);
208                         
209                         var params =  sig.get_parameters() ;
210                         if (params.size < 1) {
211                                 return;
212                         }
213                         var cc = new GirObject("Paramset",sig.name); // what's the name on this?
214                         cc.gparent = c;
215                         cc.ns = c.ns;
216                         c.paramset = cc;
217                         
218                         
219                         foreach(var p in params) {
220                                 this.add_param(cc, p);
221                         }
222                         
223                 }       
224                 
225                 public void add_method(GirObject parent, Vala.Method met)
226                 {
227                         var n = met.name == null ? "" : met.name;
228                         var ty  = "Method";
229                         if (met is Vala.CreationMethod) {
230                                 ty = "Ctor";
231                                 if(n == "" || n == ".new") {
232                                         n = "new";
233                                 }
234                                 
235                         }
236                         //print("add_method :  %s\n", n);
237                         
238                         var c = new GirObject(ty,n);
239                         c.gparent = parent;
240                         c.ns = parent.ns;
241                         
242                         if (met.return_type.data_type != null) {
243                                 //print("creating return type on method %s\n", met.name);
244                                 var cc = new GirObject("Return", "return-value");
245                                 cc.gparent = c;
246                                 cc.ns = c.ns;
247                                 cc.type  =  met.return_type.data_type.get_full_name();
248                                 c.return_value = cc;
249                         }
250                         if (met is Vala.CreationMethod) {
251                                 parent.ctors.set(c.name,c);
252                         } else {
253                                 parent.methods.set(met.name,c);
254                         }
255                         
256                         var params =  met.get_parameters() ;
257                         if (params.size < 1) {
258                                 return;
259                         }
260                         var cc = new GirObject("Paramset",met.name); // what's the name on this?
261                         cc.gparent = c;
262                         cc.ns = c.ns;
263                         c.paramset = cc;
264                         c.sig = "(";
265                         
266                         foreach(var p in params) {
267                                 if (p.name == null && !p.ellipsis) {
268                                         continue;
269                                 }
270                                 var pp = this.add_param(cc, p);
271                                 c.sig += (c.sig == "(" ? "" : ",");
272                                 c.sig += " " + (pp.direction == "in" ? "" : pp.direction) + " " + pp.type + " " + pp.name;
273                         }
274                         c.sig += (c.sig == "(" ? ")" : " )");
275                         
276                 }
277                 
278                 public GirObject add_param(GirObject parent, Vala.Parameter pam)
279                 {
280                         
281                         var n = pam.name;
282                         if (pam.ellipsis) {
283                                 n = "___";
284                         }
285                         var c = new GirObject("Param",n);
286                         c.gparent = parent;
287                         c.ns = parent.ns;
288                         c.direction = "??";
289                         switch (pam.direction) {
290                                 case Vala.ParameterDirection.IN:
291                                         c.direction = "in";
292                                         break;
293                                 case Vala.ParameterDirection.OUT:
294                                         c.direction = "out";
295                                         break;
296                                 case Vala.ParameterDirection.REF:
297                                         c.direction = "ref";
298                                         break;
299                         }
300                         
301                         parent.params.add(c);
302                         
303                         if (!pam.ellipsis) {
304                                 c.type = pam.variable_type.data_type == null ? "" : pam.variable_type.data_type.get_full_name();
305                         }
306                         Gir.checkParamOverride(c); 
307                         return c;
308                         
309                 }
310                 
311                 public void create_valac_tree( )
312                 {
313                         // init context:
314                         context = new Vala.CodeContext ();
315                         Vala.CodeContext.push (context);
316                 
317                         context.experimental = false;
318                         context.experimental_non_null = false;
319 #if VALA_0_32
320                         var ver=32;                     
321 #elif VALA_0_30
322                         var ver=30;                     
323 #elif VALA_0_28
324                         var ver=28;
325 #elif VALA_0_26 
326                         var ver=26;
327 #elif VALA_0_24
328                         var ver=24;
329 #elif VALA_0_22 
330                         var ver=22;
331 #endif
332                         
333                         for (int i = 2; i <= ver; i += 2) {
334                                 context.add_define ("VALA_0_%d".printf (i));
335                         }
336                         
337                          
338                         //var vapidirs = ((Project.Gtk)this.file.project).vapidirs();
339                         // what's the current version of vala???
340                         
341                         
342                         //vapidirs +=  Path.get_dirname (context.get_vapi_path("glib-2.0")) ;
343                          
344                         var vapidirs = context.vapi_directories;
345                         
346                         vapidirs += (BuilderApplication.configDirectory() + "/resources/vapi");
347                         context.vapi_directories = vapidirs;
348                         // or context.get_vapi_path("glib-2.0"); // should return path..
349                         //context.vapi_directories = vapidirs;
350                         context.report.enable_warnings = true;
351                         context.metadata_directories = { };
352                         context.gir_directories = {};
353                         context.thread = true;
354                         
355                         
356                         //this.report = new ValaSourceReport(this.file);
357                         //context.report = this.report;
358                         
359                         
360                         context.basedir = "/tmp"; //Posix.realpath (".");
361                 
362                         context.directory = context.basedir;
363                 
364
365                         // add default packages:
366                         //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
367                         context.profile = Vala.Profile.GOBJECT;
368                          
369                         var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
370                         context.root.add_using_directive (ns_ref);
371                         
372                         context.add_external_package ("glib-2.0"); 
373                         context.add_external_package ("gobject-2.0");
374                         
375                         // core packages we are interested in for the builder..
376                         // some of these may fail... - we probalby need a better way to handle this..
377                         
378                         context.add_external_package ("gtk+-3.0");
379                         context.add_external_package ("libsoup-2.4");
380                         if (!context.add_external_package ("webkit2gtk-4.0")) {
381                                 context.add_external_package ("webkit2gtk-3.0");
382                         }
383                         // these are supposed to be in the 'deps' file, but it's not getting read..
384                         context.add_external_package ("cogl-1.0");
385                         context.add_external_package ("json-glib-1.0");
386                         context.add_external_package ("clutter-gtk-1.0");
387
388
389                     
390                         context.add_external_package ("gdl-3.0");
391                         context.add_external_package ("gtksourceview-3.0");
392                         context.add_external_package ("vte-2.90"); //??? -- hopefullly that works..
393                         //add_documented_files (context, settings.source_files);
394                 
395                         Vala.Parser parser = new Vala.Parser ();
396                         parser.parse (context);
397                         //gir_parser.parse (context);
398                         if (context.report.get_errors () > 0) {
399                                 
400                                 throw new VapiParserError.PARSE_FAILED("failed parse VAPIS, so we can not write file correctly");
401                                 
402                                 print("parse got errors");
403                                  
404                                 
405                                 Vala.CodeContext.pop ();
406                                 return ;
407                         }
408
409
410                         
411                         // check context:
412                         context.check ();
413                         if (context.report.get_errors () > 0) {
414                                 print("check got errors");
415                                  throw new VapiParserError.PARSE_FAILED("failed check VAPIS, so we can not write file correctly");
416                                 Vala.CodeContext.pop ();
417                                  
418                                 return;
419                                 
420                         }
421                          
422                         
423                          
424                         context.accept(this);
425                         
426                         context = null;
427                         // dump the tree for Gtk?
428                         
429                         Vala.CodeContext.pop ();
430                         
431                         print("ALL OK?\n");
432                  
433                 }
434         //
435                 // startpoint:
436                 //
437          
438         }
439 }
440  /*
441 int main (string[] args) {
442         
443         var g = Palete.Gir.factoryFqn("Gtk.SourceView");
444         print("%s\n", g.asJSONString());
445         
446         return 0;
447 }
448  
449
450 */