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