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