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