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