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