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