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