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