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