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