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