3 // valac VapiParser.vala --pkg libvala-0.24 --pkg posix -o /tmp/treebuilder
10 public class VapiParser : Vala.CodeVisitor {
16 Vala.CodeContext context;
19 if (Gir.cache == null) {
20 Gir.cache = new Gee.HashMap<string,Gir>();
26 public override void visit_namespace (Vala.Namespace element)
28 if (element == null) {
32 print("parsing namespace %s\n", element.name);
33 if (element.name == null) {
34 element.accept_children(this); // catch sub namespaces..
38 var g = new Gir.new_empty(element.name);
39 Gir.cache.set(element.name, g);
42 foreach(var c in element.get_classes()) {
46 element.accept_children(this); // catch sub namespaces..
51 public void add_class(GirObject parent, Vala.Class cls)
54 var c = new GirObject("Class", parent.name + "." + cls.name);
55 parent.classes.set(cls.name, c);
57 c.parent = cls.base_class == null ? "" : cls.base_class.get_full_name() ; // extends...
60 foreach(var p in cls.get_properties()) {
61 this.add_property(c, p);
64 foreach(var p in cls.get_signals()) {
65 this.add_signal(c, p);
68 foreach(var p in cls.get_methods()) {
69 // skip static methods..
70 if (p.binding != Vala.MemberBinding.INSTANCE &&
71 !(p is Vala.CreationMethod)
76 this.add_method(c, p);
79 if (cls.base_class != null) {
80 c.inherits.add(cls.base_class.get_full_name());
82 foreach(var p in cls.get_base_types()) {
83 if (p.data_type != null) {
84 c.implements.add(p.data_type.get_full_name());
92 public void add_property(GirObject parent, Vala.Property prop)
94 var c = new GirObject("Prop",prop.name);
97 c.propertyof = parent.name;
98 c.type = prop.property_type.data_type == null ? "" : prop.property_type.data_type.get_full_name();
99 parent.props.set(prop.name,c);
102 public void add_signal(GirObject parent, Vala.Signal sig)
104 var c = new GirObject("Signal",sig.name);
108 if (sig.return_type.data_type != null) {
109 //print("creating return type on signal %s\n", sig.name);
110 var cc = new GirObject("Return", "return-value");
113 cc.type = sig.return_type.data_type.get_full_name();
116 parent.signals.set(sig.name,c);
118 var params = sig.get_parameters() ;
119 if (params.size < 1) {
122 var cc = new GirObject("Paramset",sig.name); // what's the name on this?
128 foreach(var p in params) {
129 this.add_param(cc, p);
134 public void add_method(GirObject parent, Vala.Method met)
136 var n = met.name == null ? parent.name : "";
137 if (met is Vala.CreationMethod && n == "") {
141 var c = new GirObject("Method",met.name == null ? parent.name : "");
145 if (met.return_type.data_type != null) {
146 //print("creating return type on method %s\n", met.name);
147 var cc = new GirObject("Return", "return-value");
150 cc.type = met.return_type.data_type.get_full_name();
153 if (met is Vala.CreationMethod) {
154 parent.ctors.set(c.name,c);
156 parent.methods.set(met.name,c);
159 var params = met.get_parameters() ;
160 if (params.size < 1) {
163 var cc = new GirObject("Paramset",met.name); // what's the name on this?
169 foreach(var p in params) {
170 if (p.name == null) {
173 this.add_param(cc, p);
178 public void add_param(GirObject parent, Vala.Parameter pam)
180 var c = new GirObject("Param",pam.name);
183 parent.params.add(c);
184 c.type = pam.variable_type.data_type == null ? "" : pam.variable_type.data_type.get_full_name();
185 // this.checkParamOverride(c); - this is an old kludge for Gir files..
189 public void create_valac_tree( )
192 context = new Vala.CodeContext ();
193 Vala.CodeContext.push (context);
195 context.experimental = false;
196 context.experimental_non_null = false;
208 for (int i = 2; i <= ver; i += 2) {
209 context.add_define ("VALA_0_%d".printf (i));
213 //var vapidirs = ((Project.Gtk)this.file.project).vapidirs();
214 // what's the current version of vala???
217 //vapidirs += Path.get_dirname (context.get_vapi_path("glib-2.0")) ;
221 // or context.get_vapi_path("glib-2.0"); // should return path..
222 //context.vapi_directories = vapidirs;
223 context.report.enable_warnings = true;
224 context.metadata_directories = { };
225 context.gir_directories = {};
226 context.thread = true;
229 //this.report = new ValaSourceReport(this.file);
230 //context.report = this.report;
233 context.basedir = "/tmp"; //Posix.realpath (".");
235 context.directory = context.basedir;
238 // add default packages:
239 //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
240 context.profile = Vala.Profile.GOBJECT;
242 var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
243 context.root.add_using_directive (ns_ref);
245 // default.. packages..
246 context.add_external_package ("glib-2.0");
247 context.add_external_package ("gobject-2.0");
248 context.add_external_package ("gdk-3.0");
249 context.add_external_package ("atk");
250 context.add_external_package ("gdk-x11-3.0");
251 context.add_external_package ("x11");
253 // user defined ones..
254 //context.add_package ("Gtk");
256 var vfile = new Vala.SourceFile (context, Vala.SourceFileType.PACKAGE, "/usr/share/vala-0.26/vapi/gtk+-3.0.vapi");
257 context.add_source_file (vfile);
259 //context.add_external_package ("libvala-0.24");
263 //add_documented_files (context, settings.source_files);
265 Vala.Parser parser = new Vala.Parser ();
266 parser.parse (context);
267 //gir_parser.parse (context);
268 if (context.report.get_errors () > 0) {
269 print("parse got errors");
272 Vala.CodeContext.pop ();
280 if (context.report.get_errors () > 0) {
281 print("check got errors");
283 Vala.CodeContext.pop ();
289 Vala.CodeContext.pop ();
291 context.accept(this);
293 // dump the tree for Gtk?
296 print("%s\n", Gir.cache.get("Gtk").asJSONString());
307 int main (string[] args) {
309 var a = new Palete.VapiParser( );
310 a.create_valac_tree();