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