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