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