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