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