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