3 // valac -g --pkg libvala-0.26 --pkg libxml-2.0 --pkg gee-1.0 --pkg json-glib-1.0 --pkg gobject-introspection-1.0 --pkg gtk+-3.0 ../Application.vala VapiParser.vala Gir.vala -o /tmp/vdoc
10 public class VapiParser : Vala.CodeVisitor {
13 public static Gee.HashMap<string,Gir> cache = null;
17 Vala.CodeContext context;
21 cache = new Gee.HashMap<string,Gir>();
27 public override void visit_namespace (Vala.Namespace element)
29 if (element == null) {
33 print("parsing namespace %s\n", element.name);
34 if (element.name == null) {
35 element.accept_children(this); // catch sub namespaces..
39 var g = new Gir.new_empty(element.name);
40 cache.set(element.name, g);
43 foreach(var c in element.get_classes()) {
46 foreach(var c in element.get_enums()) {
51 element.accept_children(this); // catch sub namespaces..
57 public void add_enum(GirObject parent, Vala.Enum cls)
60 var c = new GirObject("Enum", cls.name);
61 parent.consts.set(cls.name, c);
66 foreach(var e in cls.get_values()) {
67 var em = new GirObject("EnumMember",e.name);
70 em.type = e.type_reference == null || e.type_reference.data_type == null ? "" : e.type_reference.data_type.get_full_name();
71 // unlikely to get value..
72 //c.value = element->get_prop("value");
73 c.consts.set(e.name,em);
82 public void add_class(GirObject parent, Vala.Class cls)
85 var c = new GirObject("Class", parent.name + "." + cls.name);
86 parent.classes.set(cls.name, c);
88 c.parent = cls.base_class == null ? "" : cls.base_class.get_full_name() ; // extends...
91 foreach(var p in cls.get_properties()) {
92 this.add_property(c, p);
95 foreach(var p in cls.get_signals()) {
96 this.add_signal(c, p);
99 foreach(var p in cls.get_methods()) {
100 // skip static methods..
101 if (p.binding != Vala.MemberBinding.INSTANCE &&
102 !(p is Vala.CreationMethod)
107 this.add_method(c, p);
110 if (cls.base_class != null) {
111 c.inherits.add(cls.base_class.get_full_name());
113 foreach(var p in cls.get_base_types()) {
114 if (p.data_type != null) {
115 c.implements.add(p.data_type.get_full_name());
123 public void add_property(GirObject parent, Vala.Property prop)
125 var c = new GirObject("Prop",prop.name);
128 c.propertyof = parent.name;
129 c.type = prop.property_type.data_type == null ? "" : prop.property_type.data_type.get_full_name();
130 parent.props.set(prop.name,c);
133 public void add_signal(GirObject parent, Vala.Signal sig)
135 var c = new GirObject("Signal",sig.name);
139 if (sig.return_type.data_type != null) {
140 //print("creating return type on signal %s\n", sig.name);
141 var cc = new GirObject("Return", "return-value");
144 cc.type = sig.return_type.data_type.get_full_name();
147 parent.signals.set(sig.name,c);
149 var params = sig.get_parameters() ;
150 if (params.size < 1) {
153 var cc = new GirObject("Paramset",sig.name); // what's the name on this?
159 foreach(var p in params) {
160 this.add_param(cc, p);
165 public void add_method(GirObject parent, Vala.Method met)
167 var n = met.name == null ? parent.name : "";
169 if (met is Vala.CreationMethod && n == "") {
174 var c = new GirObject(ty,n);
178 if (met.return_type.data_type != null) {
179 //print("creating return type on method %s\n", met.name);
180 var cc = new GirObject("Return", "return-value");
183 cc.type = met.return_type.data_type.get_full_name();
186 if (met is Vala.CreationMethod) {
187 parent.ctors.set(c.name,c);
189 parent.methods.set(met.name,c);
192 var params = met.get_parameters() ;
193 if (params.size < 1) {
196 var cc = new GirObject("Paramset",met.name); // what's the name on this?
202 foreach(var p in params) {
203 if (p.name == null) {
206 this.add_param(cc, p);
211 public void add_param(GirObject parent, Vala.Parameter pam)
213 var c = new GirObject("Param",pam.name);
216 parent.params.add(c);
217 c.type = pam.variable_type.data_type == null ? "" : pam.variable_type.data_type.get_full_name();
218 // this.checkParamOverride(c); - this is an old kludge for Gir files..
222 public void create_valac_tree( )
225 context = new Vala.CodeContext ();
226 Vala.CodeContext.push (context);
228 context.experimental = false;
229 context.experimental_non_null = false;
241 for (int i = 2; i <= ver; i += 2) {
242 context.add_define ("VALA_0_%d".printf (i));
246 //var vapidirs = ((Project.Gtk)this.file.project).vapidirs();
247 // what's the current version of vala???
250 //vapidirs += Path.get_dirname (context.get_vapi_path("glib-2.0")) ;
254 // or context.get_vapi_path("glib-2.0"); // should return path..
255 //context.vapi_directories = vapidirs;
256 context.report.enable_warnings = true;
257 context.metadata_directories = { };
258 context.gir_directories = {};
259 context.thread = true;
262 //this.report = new ValaSourceReport(this.file);
263 //context.report = this.report;
266 context.basedir = "/tmp"; //Posix.realpath (".");
268 context.directory = context.basedir;
271 // add default packages:
272 //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
273 context.profile = Vala.Profile.GOBJECT;
275 var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
276 context.root.add_using_directive (ns_ref);
278 // default.. packages..
279 context.add_external_package ("glib-2.0");
280 context.add_external_package ("gobject-2.0");
281 context.add_external_package ("gdk-3.0");
282 context.add_external_package ("atk");
283 context.add_external_package ("gdk-x11-3.0");
284 context.add_external_package ("x11");
285 context.add_external_package ("gtk+-3.0");
286 // user defined ones..
287 //context.add_package ("Gtk");
289 //var vfile = new Vala.SourceFile (context, Vala.SourceFileType.PACKAGE, "/usr/share/vala-0.26/vapi/gtk+-3.0.vapi");
290 //context.add_source_file (vfile);
292 //context.add_external_package ("libvala-0.24");
296 //add_documented_files (context, settings.source_files);
298 Vala.Parser parser = new Vala.Parser ();
299 parser.parse (context);
300 //gir_parser.parse (context);
301 if (context.report.get_errors () > 0) {
302 print("parse got errors");
305 Vala.CodeContext.pop ();
313 if (context.report.get_errors () > 0) {
314 print("check got errors");
316 Vala.CodeContext.pop ();
322 Vala.CodeContext.pop ();
324 context.accept(this);
326 // dump the tree for Gtk?
329 print("%s\n", cache.get("Gtk").asJSONString());
340 int main (string[] args) {
342 var a = new Palete.VapiParser( );
343 a.create_valac_tree();