sync
[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                         //print("add_method :  %s\n", n);
217                         
218                         var c = new GirObject(ty,n);
219                         c.gparent = parent;
220                         c.ns = parent.ns;
221                         
222                         if (met.return_type.data_type != null) {
223                                 //print("creating return type on method %s\n", met.name);
224                                 var cc = new GirObject("Return", "return-value");
225                                 cc.gparent = c;
226                                 cc.ns = c.ns;
227                                 cc.type  =  met.return_type.data_type.get_full_name();
228                                 c.return_value = cc;
229                         }
230                         if (met is Vala.CreationMethod) {
231                                 parent.ctors.set(c.name,c);
232                         } else {
233                                 parent.methods.set(met.name,c);
234                         }
235                         
236                         var params =  met.get_parameters() ;
237                         if (params.size < 1) {
238                                 return;
239                         }
240                         var cc = new GirObject("Paramset",met.name); // what's the name on this?
241                         cc.gparent = c;
242                         cc.ns = c.ns;
243                         c.paramset = cc;
244                         
245                         
246                         foreach(var p in params) {
247                                 if (p.name == null && !p.ellipsis) {
248                                         continue;
249                                 }
250                                 this.add_param(cc, p);
251                         }
252                         
253                 }
254                 
255                 public void add_param(GirObject parent, Vala.Parameter pam)
256                 {
257                         
258                         var n = pam.name;
259                         if (pam.ellipsis) {
260                                 n = "___";
261                         }
262                         var c = new GirObject("Param",n);
263                         c.gparent = parent;
264                         c.ns = parent.ns;
265                         parent.params.add(c);
266                         
267                         if (!pam.ellipsis) {
268                                 c.type = pam.variable_type.data_type == null ? "" : pam.variable_type.data_type.get_full_name();
269                         }
270                         Gir.checkParamOverride(c); 
271                         
272                 }
273                 
274                 public void create_valac_tree( )
275                 {
276                         // init context:
277                         context = new Vala.CodeContext ();
278                         Vala.CodeContext.push (context);
279                 
280                         context.experimental = false;
281                         context.experimental_non_null = false;
282                         
283 #if VALA_0_28
284                         var ver=28;
285 #elif VALA_0_26 
286                         var ver=26;
287 #elif VALA_0_24
288                         var ver=24;
289 #elif VALA_0_22 
290                         var ver=22;
291 #endif
292                         
293                         for (int i = 2; i <= ver; i += 2) {
294                                 context.add_define ("VALA_0_%d".printf (i));
295                         }
296                         
297                          
298                         //var vapidirs = ((Project.Gtk)this.file.project).vapidirs();
299                         // what's the current version of vala???
300                         
301                         
302                         //vapidirs +=  Path.get_dirname (context.get_vapi_path("glib-2.0")) ;
303                          
304                                 
305                         
306                         // or context.get_vapi_path("glib-2.0"); // should return path..
307                         //context.vapi_directories = vapidirs;
308                         context.report.enable_warnings = true;
309                         context.metadata_directories = { };
310                         context.gir_directories = {};
311                         context.thread = true;
312                         
313                         
314                         //this.report = new ValaSourceReport(this.file);
315                         //context.report = this.report;
316                         
317                         
318                         context.basedir = "/tmp"; //Posix.realpath (".");
319                 
320                         context.directory = context.basedir;
321                 
322
323                         // add default packages:
324                         //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
325                         context.profile = Vala.Profile.GOBJECT;
326                          
327                         var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
328                         context.root.add_using_directive (ns_ref);
329                         
330                         context.add_external_package ("glib-2.0"); 
331                         context.add_external_package ("gobject-2.0");
332                         
333                         // core packages we are interested in for the builder..
334                         // some of these may fail... - we probalby need a better way to handle this..
335                         
336                         context.add_external_package ("gtk+-3.0");
337                         if (!context.add_external_package ("webkit2gtk-4.0")) {
338                                 context.add_external_package ("webkit2gtk-3.0");
339                         }
340                         context.add_external_package ("clutter-gtk-1.0");
341                         context.add_external_package ("gdl-3.0");
342                         context.add_external_package ("gtksourceview-3.0");
343                         context.add_external_package ("vte-2.90"); //??? -- hopefullly that works..
344                         //add_documented_files (context, settings.source_files);
345                 
346                         Vala.Parser parser = new Vala.Parser ();
347                         parser.parse (context);
348                         //gir_parser.parse (context);
349                         if (context.report.get_errors () > 0) {
350                                 print("parse got errors");
351                                  
352                                 
353                                 Vala.CodeContext.pop ();
354                                 return ;
355                         }
356
357
358                         
359                         // check context:
360                         context.check ();
361                         if (context.report.get_errors () > 0) {
362                                 print("check got errors");
363                                  
364                                 Vala.CodeContext.pop ();
365                                  
366                                 return;
367                                 
368                         }
369                          
370                         Vala.CodeContext.pop ();
371                          
372                         context.accept(this);
373                         
374                         // dump the tree for Gtk?
375                         
376                         
377                         
378                         print("ALL OK?\n");
379                  
380                 }
381         //
382                 // startpoint:
383                 //
384          
385         }
386 }
387  /*
388 int main (string[] args) {
389         
390         var g = Palete.Gir.factoryFqn("Gtk.SourceView");
391         print("%s\n", g.asJSONString());
392         
393         return 0;
394 }
395  
396
397 */