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