src/Palete/VapiParser.vala
[app.Builder.js] / src / Palete / VapiParser.vala
1
2
3  // valac -g  --pkg libvala-0.26 --pkg libxml-2.0 --pkg gee-1.0 --pkg json-glib-1.0 --pkg gobject-introspection-1.0 --pkg gtk+-3.0  ../Application.vala  VapiParser.vala Gir.vala -o /tmp/vdoc
4
5 namespace Palete {
6          
7          
8          
9
10         public class VapiParser : Vala.CodeVisitor {
11                 
12                 
13                 public static  Gee.HashMap<string,Gir> cache = null;
14                 
15                 
16                 
17                 Vala.CodeContext context;
18                 public VapiParser() {
19                         base();
20                         if (cache == null) {
21                                 cache =  new Gee.HashMap<string,Gir>();
22                         }
23                 }
24                 
25                 
26                 
27                 public override void visit_namespace (Vala.Namespace element) 
28                 {
29                         if (element == null) {
30                                 
31                                 return;
32                         }
33                         print("parsing namespace %s\n", element.name);
34                         if (element.name == null) {
35                                 element.accept_children(this); // catch sub namespaces..
36                                 return;
37                         }
38                         
39                         var g = new Gir.new_empty(element.name);
40                         cache.set(element.name, g);
41                         
42                         
43                         foreach(var c in element.get_classes()) {
44                                 this.add_class(g, c);
45                         }
46                         foreach(var c in element.get_enums()) {
47                                 this.add_enum(g, c);
48                         }
49                         
50                         
51                         element.accept_children(this); // catch sub namespaces..
52                         
53                         
54                 }
55                 
56                 
57                 public void add_enum(GirObject parent, Vala.Enum cls)
58                 {
59                 
60                         var c = new GirObject("Enum",   cls.name);
61                         parent.consts.set(cls.name, c);
62                         c.ns = parent.name;
63                         
64                         c.gparent = parent;
65                         
66                         foreach(var e in cls.get_values()) {
67                                 var em = new GirObject("EnumMember",e.name);
68                                 em.gparent = c;
69                                 em.ns = c.ns;
70                                 em.type  = e.type_reference == null ||  e.type_reference.data_type == null ? "" : e.type_reference.data_type.get_full_name();
71                                 // unlikely to get value..
72                                 //c.value = element->get_prop("value");
73                                 c.consts.set(e.name,em);
74                         }
75                         
76                         
77                         
78                          
79                 }
80                 
81                 
82                 public void add_class(GirObject parent, Vala.Class cls)
83                 {
84                 
85                         var c = new GirObject("Class", parent.name + "." + cls.name);
86                         parent.classes.set(cls.name, c);
87                         c.ns = parent.name;
88                         c.parent = cls.base_class == null ? "" : cls.base_class.get_full_name() ;  // extends...
89                         c.gparent = parent;
90                         
91                         foreach(var p in cls.get_properties()) {
92                                 this.add_property(c, p);
93                         }
94                         // methods...
95                         foreach(var p in cls.get_signals()) {
96                                 this.add_signal(c, p);
97                         }
98                         
99                         foreach(var p in cls.get_methods()) {
100                                 // skip static methods..
101                                 if (p.binding != Vala.MemberBinding.INSTANCE &&
102                                         !(p is Vala.CreationMethod)
103                                 ) {
104                                         continue;
105                                 }
106                                 
107                                 this.add_method(c, p);
108                         }
109                         
110                         if (cls.base_class != null) {
111                                 c.inherits.add(cls.base_class.get_full_name());
112                         }
113                         foreach(var p in cls.get_base_types()) {
114                                 if (p.data_type != null) {
115                                         c.implements.add(p.data_type.get_full_name());
116                                 }
117                         }
118                           
119                         
120                         
121                          
122                 }
123                 public void add_property(GirObject parent, Vala.Property prop)
124                 {
125                         var c = new GirObject("Prop",prop.name);
126                         c.gparent = parent;
127                         c.ns = parent.ns;
128                         c.propertyof = parent.name;
129                         c.type  = prop.property_type.data_type == null ? "" : prop.property_type.data_type.get_full_name();
130                         parent.props.set(prop.name,c);
131                         
132                 }
133                 public void add_signal(GirObject parent, Vala.Signal sig)
134                 {
135                         var c = new GirObject("Signal",sig.name);
136                         c.gparent = parent;
137                         c.ns = parent.ns;
138                         
139                         if (sig.return_type.data_type != null) {
140                                 //print("creating return type on signal %s\n", sig.name);
141                                 var cc = new GirObject("Return", "return-value");
142                                 cc.gparent = c;
143                                 cc.ns = c.ns;
144                                 cc.type  =  sig.return_type.data_type.get_full_name();
145                                 c.return_value = cc;
146                         }
147                         parent.signals.set(sig.name,c);
148                         
149                         var params =  sig.get_parameters() ;
150                         if (params.size < 1) {
151                                 return;
152                         }
153                         var cc = new GirObject("Paramset",sig.name); // what's the name on this?
154                         cc.gparent = c;
155                         cc.ns = c.ns;
156                         c.paramset = cc;
157                         
158                         
159                         foreach(var p in params) {
160                                 this.add_param(cc, p);
161                         }
162                         
163                 }       
164                 
165                 public void add_method(GirObject parent, Vala.Method met)
166                 {
167                         var n = met.name == null ? parent.name : "";
168                         var ty  = "Method";
169                         if (met is Vala.CreationMethod && n == "") {
170                                 n = ".new";
171                                 ty = "Ctor";
172                         }
173                         
174                         var c = new GirObject(ty,n);
175                         c.gparent = parent;
176                         c.ns = parent.ns;
177                         
178                         if (met.return_type.data_type != null) {
179                                 //print("creating return type on method %s\n", met.name);
180                                 var cc = new GirObject("Return", "return-value");
181                                 cc.gparent = c;
182                                 cc.ns = c.ns;
183                                 cc.type  =  met.return_type.data_type.get_full_name();
184                                 c.return_value = cc;
185                         }
186                         if (met is Vala.CreationMethod) {
187                                 parent.ctors.set(c.name,c);
188                         } else {
189                                 parent.methods.set(met.name,c);
190                         }
191                         
192                         var params =  met.get_parameters() ;
193                         if (params.size < 1) {
194                                 return;
195                         }
196                         var cc = new GirObject("Paramset",met.name); // what's the name on this?
197                         cc.gparent = c;
198                         cc.ns = c.ns;
199                         c.paramset = cc;
200                         
201                         
202                         foreach(var p in params) {
203                                 if (p.name == null) {
204                                         continue;
205                                 }
206                                 this.add_param(cc, p);
207                         }
208                         
209                 }
210                 
211                 public void add_param(GirObject parent, Vala.Parameter pam)
212                 {
213                         var c = new GirObject("Param",pam.name);
214                         c.gparent = parent;
215                         c.ns = parent.ns;
216                         parent.params.add(c);
217                         c.type = pam.variable_type.data_type == null ? "" : pam.variable_type.data_type.get_full_name();
218                         // this.checkParamOverride(c);   - this is an old kludge for Gir files..
219                         
220                 }
221                 
222                 public void create_valac_tree( )
223                 {
224                         // init context:
225                         context = new Vala.CodeContext ();
226                         Vala.CodeContext.push (context);
227                 
228                         context.experimental = false;
229                         context.experimental_non_null = false;
230                         
231 #if VALA_0_28
232                         var ver=28;
233 #elif VALA_0_26 
234                         var ver=26;
235 #elif VALA_0_24
236                         var ver=24;
237 #elif VALA_0_22 
238                         var ver=22;
239 #endif
240                         
241                         for (int i = 2; i <= ver; i += 2) {
242                                 context.add_define ("VALA_0_%d".printf (i));
243                         }
244                         
245                          
246                         //var vapidirs = ((Project.Gtk)this.file.project).vapidirs();
247                         // what's the current version of vala???
248                         
249                         
250                         //vapidirs +=  Path.get_dirname (context.get_vapi_path("glib-2.0")) ;
251                          
252                                 
253                         
254                         // or context.get_vapi_path("glib-2.0"); // should return path..
255                         //context.vapi_directories = vapidirs;
256                         context.report.enable_warnings = true;
257                         context.metadata_directories = { };
258                         context.gir_directories = {};
259                         context.thread = true;
260                         
261                         
262                         //this.report = new ValaSourceReport(this.file);
263                         //context.report = this.report;
264                         
265                         
266                         context.basedir = "/tmp"; //Posix.realpath (".");
267                 
268                         context.directory = context.basedir;
269                 
270
271                         // add default packages:
272                         //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
273                         context.profile = Vala.Profile.GOBJECT;
274                          
275                         var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
276                         context.root.add_using_directive (ns_ref);
277  
278                         // default.. packages..
279                         context.add_external_package ("glib-2.0"); 
280                         context.add_external_package ("gobject-2.0");
281                         context.add_external_package ("gdk-3.0");
282                         context.add_external_package ("atk");
283                         context.add_external_package ("gdk-x11-3.0");
284                         context.add_external_package ("x11");
285                         context.add_external_package ("gtk+-3.0");
286                         // user defined ones..
287                         //context.add_package ("Gtk");
288                   
289                         //var vfile = new Vala.SourceFile (context, Vala.SourceFileType.PACKAGE, "/usr/share/vala-0.26/vapi/gtk+-3.0.vapi");
290                         //context.add_source_file (vfile);
291                          
292                         //context.add_external_package ("libvala-0.24");
293                         
294                          
295                 
296                         //add_documented_files (context, settings.source_files);
297                 
298                         Vala.Parser parser = new Vala.Parser ();
299                         parser.parse (context);
300                         //gir_parser.parse (context);
301                         if (context.report.get_errors () > 0) {
302                                 print("parse got errors");
303                                  
304                                 
305                                 Vala.CodeContext.pop ();
306                                 return ;
307                         }
308
309
310                         
311                         // check context:
312                         context.check ();
313                         if (context.report.get_errors () > 0) {
314                                 print("check got errors");
315                                  
316                                 Vala.CodeContext.pop ();
317                                  
318                                 return;
319                                 
320                         }
321                          
322                         Vala.CodeContext.pop ();
323                          
324                         context.accept(this);
325                         
326                         // dump the tree for Gtk?
327                         
328                         
329                         print("%s\n", cache.get("Gtk").asJSONString());
330                         print("ALL OK?\n");
331                  
332                 }
333         //
334                 // startpoint:
335                 //
336          
337         }
338 }
339  
340 int main (string[] args) {
341
342         var a = new Palete.VapiParser( );
343         a.create_valac_tree();
344         return 0;
345 }
346  
347
348