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