src/Palete/VapiParser.vala
[app.Builder.js] / src / Palete / VapiParser.vala
1
2
3  // valac -g  --pkg libvala-0.26 --pkg libxml-2.0 --pkg gee-1.0 --pkg json-glib-1.0 --pkg gobject-introspection-1.0 --pkg gtk+-3.0  ../Application.vala  VapiParser.vala Gir.vala -o /tmp/vdoc
4
5 namespace Palete {
6          
7          
8          
9
10         public class VapiParser : Vala.CodeVisitor {
11                 
12                 
13                 public static  Gee.HashMap<string,Gir> cache = null;
14                 
15                 
16                 public static GirObject?  factory(string ns) {
17                         if (cache == null) {
18                                 cache = new Gee.HashMap<string,Gir>();
19                         }
20                         var ret = cache.get(ns);
21                         if (ret == null) {
22
23                                 var add = new GirObject("Namespace",ns);
24                                 
25                                 
26                                 
27                                 cache.set(ns, add);
28                         
29                                 var iter = add.classes.map_iterator();
30                                 while(iter.next()) {
31                                         iter.get_value().overlayParent();
32                                 }
33                                 // loop again and add the ctor properties.
34                                 iter = add.classes.map_iterator();
35                                 while(iter.next()) {
36                                         iter.get_value().overlayCtorProperties();
37                                 }       
38
39                                 
40                                 ret = cache.get(ns);
41                         }
42                          
43
44                         return ret;
45                         
46                 }
47                 Vala.CodeContext context;
48                 public VapiParser() {
49                         base();
50                         if (cache == null) {
51                                 cache =  new Gee.HashMap<string,Gir>();
52                         }
53                 }
54                 
55                 
56                 
57                 public override void visit_namespace (Vala.Namespace element) 
58                 {
59                         if (element == null) {
60                                 
61                                 return;
62                         }
63                         print("parsing namespace %s\n", element.name);
64                         if (element.name == null) {
65                                 element.accept_children(this); // catch sub namespaces..
66                                 return;
67                         }
68                         
69                         var g = new Gir.new_empty(element.name);
70                         cache.set(element.name, g);
71                         
72                         
73                         foreach(var c in element.get_classes()) {
74                                 this.add_class(g, c);
75                         }
76                         foreach(var c in element.get_enums()) {
77                                 this.add_enum(g, c);
78                         }
79                         
80                         
81                         element.accept_children(this); // catch sub namespaces..
82                         
83                         
84                 }
85                 
86                 
87                 public void add_enum(GirObject parent, Vala.Enum cls)
88                 {
89                 
90                         var c = new GirObject("Enum",   cls.name);
91                         parent.consts.set(cls.name, c);
92                         c.ns = parent.name;
93                         
94                         c.gparent = parent;
95                         
96                         foreach(var e in cls.get_values()) {
97                                 var em = new GirObject("EnumMember",e.name);
98                                 em.gparent = c;
99                                 em.ns = c.ns;
100                                 em.type  = e.type_reference == null ||  e.type_reference.data_type == null ? "" : e.type_reference.data_type.get_full_name();
101                                 // unlikely to get value..
102                                 //c.value = element->get_prop("value");
103                                 c.consts.set(e.name,em);
104                         }
105                         
106                         
107                         
108                          
109                 }
110                 
111                 
112                 public void add_class(GirObject parent, Vala.Class cls)
113                 {
114                 
115                         var c = new GirObject("Class", parent.name + "." + cls.name);
116                         parent.classes.set(cls.name, c);
117                         c.ns = parent.name;
118                         c.parent = cls.base_class == null ? "" : cls.base_class.get_full_name() ;  // extends...
119                         c.gparent = parent;
120                         
121                         foreach(var p in cls.get_properties()) {
122                                 this.add_property(c, p);
123                         }
124                         // methods...
125                         foreach(var p in cls.get_signals()) {
126                                 this.add_signal(c, p);
127                         }
128                         
129                         foreach(var p in cls.get_methods()) {
130                                 // skip static methods..
131                                 if (p.binding != Vala.MemberBinding.INSTANCE &&
132                                         !(p is Vala.CreationMethod)
133                                 ) {
134                                         continue;
135                                 }
136                                 
137                                 this.add_method(c, p);
138                         }
139                         
140                         if (cls.base_class != null) {
141                                 c.inherits.add(cls.base_class.get_full_name());
142                         }
143                         foreach(var p in cls.get_base_types()) {
144                                 if (p.data_type != null) {
145                                         c.implements.add(p.data_type.get_full_name());
146                                 }
147                         }
148                           
149                         
150                         
151                          
152                 }
153                 public void add_property(GirObject parent, Vala.Property prop)
154                 {
155                         var c = new GirObject("Prop",prop.name);
156                         c.gparent = parent;
157                         c.ns = parent.ns;
158                         c.propertyof = parent.name;
159                         c.type  = prop.property_type.data_type == null ? "" : prop.property_type.data_type.get_full_name();
160                         parent.props.set(prop.name,c);
161                         
162                 }
163                 public void add_signal(GirObject parent, Vala.Signal sig)
164                 {
165                         var c = new GirObject("Signal",sig.name);
166                         c.gparent = parent;
167                         c.ns = parent.ns;
168                         
169                         if (sig.return_type.data_type != null) {
170                                 //print("creating return type on signal %s\n", sig.name);
171                                 var cc = new GirObject("Return", "return-value");
172                                 cc.gparent = c;
173                                 cc.ns = c.ns;
174                                 cc.type  =  sig.return_type.data_type.get_full_name();
175                                 c.return_value = cc;
176                         }
177                         parent.signals.set(sig.name,c);
178                         
179                         var params =  sig.get_parameters() ;
180                         if (params.size < 1) {
181                                 return;
182                         }
183                         var cc = new GirObject("Paramset",sig.name); // what's the name on this?
184                         cc.gparent = c;
185                         cc.ns = c.ns;
186                         c.paramset = cc;
187                         
188                         
189                         foreach(var p in params) {
190                                 this.add_param(cc, p);
191                         }
192                         
193                 }       
194                 
195                 public void add_method(GirObject parent, Vala.Method met)
196                 {
197                         var n = met.name == null ? parent.name : "";
198                         var ty  = "Method";
199                         if (met is Vala.CreationMethod && n == "") {
200                                 n = ".new";
201                                 ty = "Ctor";
202                         }
203                         
204                         var c = new GirObject(ty,n);
205                         c.gparent = parent;
206                         c.ns = parent.ns;
207                         
208                         if (met.return_type.data_type != null) {
209                                 //print("creating return type on method %s\n", met.name);
210                                 var cc = new GirObject("Return", "return-value");
211                                 cc.gparent = c;
212                                 cc.ns = c.ns;
213                                 cc.type  =  met.return_type.data_type.get_full_name();
214                                 c.return_value = cc;
215                         }
216                         if (met is Vala.CreationMethod) {
217                                 parent.ctors.set(c.name,c);
218                         } else {
219                                 parent.methods.set(met.name,c);
220                         }
221                         
222                         var params =  met.get_parameters() ;
223                         if (params.size < 1) {
224                                 return;
225                         }
226                         var cc = new GirObject("Paramset",met.name); // what's the name on this?
227                         cc.gparent = c;
228                         cc.ns = c.ns;
229                         c.paramset = cc;
230                         
231                         
232                         foreach(var p in params) {
233                                 if (p.name == null) {
234                                         continue;
235                                 }
236                                 this.add_param(cc, p);
237                         }
238                         
239                 }
240                 
241                 public void add_param(GirObject parent, Vala.Parameter pam)
242                 {
243                         var c = new GirObject("Param",pam.name);
244                         c.gparent = parent;
245                         c.ns = parent.ns;
246                         parent.params.add(c);
247                         c.type = pam.variable_type.data_type == null ? "" : pam.variable_type.data_type.get_full_name();
248                         // this.checkParamOverride(c);   - this is an old kludge for Gir files..
249                         
250                 }
251                 
252                 public void create_valac_tree( )
253                 {
254                         // init context:
255                         context = new Vala.CodeContext ();
256                         Vala.CodeContext.push (context);
257                 
258                         context.experimental = false;
259                         context.experimental_non_null = false;
260                         
261 #if VALA_0_28
262                         var ver=28;
263 #elif VALA_0_26 
264                         var ver=26;
265 #elif VALA_0_24
266                         var ver=24;
267 #elif VALA_0_22 
268                         var ver=22;
269 #endif
270                         
271                         for (int i = 2; i <= ver; i += 2) {
272                                 context.add_define ("VALA_0_%d".printf (i));
273                         }
274                         
275                          
276                         //var vapidirs = ((Project.Gtk)this.file.project).vapidirs();
277                         // what's the current version of vala???
278                         
279                         
280                         //vapidirs +=  Path.get_dirname (context.get_vapi_path("glib-2.0")) ;
281                          
282                                 
283                         
284                         // or context.get_vapi_path("glib-2.0"); // should return path..
285                         //context.vapi_directories = vapidirs;
286                         context.report.enable_warnings = true;
287                         context.metadata_directories = { };
288                         context.gir_directories = {};
289                         context.thread = true;
290                         
291                         
292                         //this.report = new ValaSourceReport(this.file);
293                         //context.report = this.report;
294                         
295                         
296                         context.basedir = "/tmp"; //Posix.realpath (".");
297                 
298                         context.directory = context.basedir;
299                 
300
301                         // add default packages:
302                         //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
303                         context.profile = Vala.Profile.GOBJECT;
304                          
305                         var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
306                         context.root.add_using_directive (ns_ref);
307  
308                         // default.. packages..
309                         context.add_external_package ("glib-2.0"); 
310                         context.add_external_package ("gobject-2.0");
311                         context.add_external_package ("gdk-3.0");
312                         context.add_external_package ("atk");
313                         context.add_external_package ("gdk-x11-3.0");
314                         context.add_external_package ("x11");
315                         context.add_external_package ("gtk+-3.0");
316                         // user defined ones..
317                         //context.add_package ("Gtk");
318                   
319                         //var vfile = new Vala.SourceFile (context, Vala.SourceFileType.PACKAGE, "/usr/share/vala-0.26/vapi/gtk+-3.0.vapi");
320                         //context.add_source_file (vfile);
321                          
322                         //context.add_external_package ("libvala-0.24");
323                         
324                          
325                 
326                         //add_documented_files (context, settings.source_files);
327                 
328                         Vala.Parser parser = new Vala.Parser ();
329                         parser.parse (context);
330                         //gir_parser.parse (context);
331                         if (context.report.get_errors () > 0) {
332                                 print("parse got errors");
333                                  
334                                 
335                                 Vala.CodeContext.pop ();
336                                 return ;
337                         }
338
339
340                         
341                         // check context:
342                         context.check ();
343                         if (context.report.get_errors () > 0) {
344                                 print("check got errors");
345                                  
346                                 Vala.CodeContext.pop ();
347                                  
348                                 return;
349                                 
350                         }
351                          
352                         Vala.CodeContext.pop ();
353                          
354                         context.accept(this);
355                         
356                         // dump the tree for Gtk?
357                         
358                         
359                         print("%s\n", cache.get("Gtk").asJSONString());
360                         print("ALL OK?\n");
361                  
362                 }
363         //
364                 // startpoint:
365                 //
366          
367         }
368 }
369  
370 int main (string[] args) {
371
372         var a = new Palete.VapiParser( );
373         a.create_valac_tree();
374         return 0;
375 }
376  
377
378