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