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