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 signal %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                                 this.add_param(cc, p);
157                         }
158                         
159                 }
160                 
161                 public void add_param(GirObject parent, Vala.Parameter pam)
162                 {
163                         var c = new GirObject("Param",pam.name);
164                         c.gparent = parent;
165                         c.ns = parent.ns;
166                         parent.params.add(c);
167                         c.type = pam.variable_type.data_type == null ? "" : pam.variable_type.data_type.get_full_name();
168                         // this.checkParamOverride(c);   - this is an old kludge for Gir files..
169                         
170                 }
171                 
172                 public void create_valac_tree( )
173                 {
174                         // init context:
175                         context = new Vala.CodeContext ();
176                         Vala.CodeContext.push (context);
177                 
178                         context.experimental = false;
179                         context.experimental_non_null = false;
180                         
181 #if VALA_0_28
182                         var ver=28;
183 #elif VALA_0_26 
184                         var ver=26;
185 #elif VALA_0_24
186                         var ver=24;
187 #elif VALA_0_22 
188                         var ver=22;
189 #endif
190                         
191                         for (int i = 2; i <= ver; i += 2) {
192                                 context.add_define ("VALA_0_%d".printf (i));
193                         }
194                         
195                          
196                         //var vapidirs = ((Project.Gtk)this.file.project).vapidirs();
197                         // what's the current version of vala???
198                         
199                         
200                         //vapidirs +=  Path.get_dirname (context.get_vapi_path("glib-2.0")) ;
201                          
202                                 
203                         
204                         // or context.get_vapi_path("glib-2.0"); // should return path..
205                         //context.vapi_directories = vapidirs;
206                         context.report.enable_warnings = true;
207                         context.metadata_directories = { };
208                         context.gir_directories = {};
209                         context.thread = true;
210                         
211                         
212                         //this.report = new ValaSourceReport(this.file);
213                         //context.report = this.report;
214                         
215                         
216                         context.basedir = "/tmp"; //Posix.realpath (".");
217                 
218                         context.directory = context.basedir;
219                 
220
221                         // add default packages:
222                         //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
223                         context.profile = Vala.Profile.GOBJECT;
224                          
225                         var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
226                         context.root.add_using_directive (ns_ref);
227  
228                         // default.. packages..
229                         context.add_external_package ("glib-2.0"); 
230                         context.add_external_package ("gobject-2.0");
231                         context.add_external_package ("gdk-3.0");
232                         context.add_external_package ("atk");
233                         context.add_external_package ("gdk-x11-3.0");
234                         context.add_external_package ("x11");
235                         
236                         // user defined ones..
237                         //context.add_package ("Gtk");
238                   
239                         var vfile = new Vala.SourceFile (context, Vala.SourceFileType.PACKAGE, "/usr/share/vala-0.26/vapi/gtk+-3.0.vapi");
240                         context.add_source_file (vfile);
241                          
242                         //context.add_external_package ("libvala-0.24");
243                         
244                          
245                 
246                         //add_documented_files (context, settings.source_files);
247                 
248                         Vala.Parser parser = new Vala.Parser ();
249                         parser.parse (context);
250                         //gir_parser.parse (context);
251                         if (context.report.get_errors () > 0) {
252                                 print("parse got errors");
253                                  
254                                 
255                                 Vala.CodeContext.pop ();
256                                 return ;
257                         }
258
259
260                         
261                         // check context:
262                         context.check ();
263                         if (context.report.get_errors () > 0) {
264                                 print("check got errors");
265                                  
266                                 Vala.CodeContext.pop ();
267                                  
268                                 return;
269                                 
270                         }
271                          
272                         Vala.CodeContext.pop ();
273                          
274                         context.accept(this);
275                         
276                         // dump the tree for Gtk?
277                         
278                         
279                         print("%s\n", Gir.cache.get("Gtk").asJSONString());
280                         print("ALL OK?\n");
281                  
282                 }
283         //
284                 // startpoint:
285                 //
286          
287         }
288 }
289  
290 int main (string[] args) {
291
292         var a = new Palete.VapiParser( );
293         a.create_valac_tree();
294         return 0;
295 }
296  
297
298