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