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