3 // valac -g --pkg libvala-0.26 --pkg gee-1.0 --pkg json-glib-1.0 --pkg gtk+-3.0 VapiParser.vala Gir.vala GirObject.vala -o /tmp/vdoc
7 public errordomain VapiParserError {
12 public class VapiParser : Vala.CodeVisitor {
14 Vala.CodeContext context;
18 // should not really happen..
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) {
34 //print("parsing namespace %s\n", element.name);
35 if (element.name == null) {
36 element.accept_children(this); // catch sub namespaces..
39 this.add_namespace(null, element);
41 public void add_namespace(GirObject? parent, Vala.Namespace element)
45 var g = new GirObject("Package",element.name) ;
47 Gir.cache.set(element.name, (Gir)g);
49 // we add it as a class of the package.. even though its a namespace..
50 parent.classes.set(element.name, g);
54 foreach(var c in element.get_classes()) {
57 foreach(var c in element.get_enums()) {
60 foreach(var c in element.get_interfaces()) {
61 this.add_interface(g, c);
63 foreach(var c in element.get_namespaces()) {
64 this.add_namespace(g, c);
66 foreach(var c in element.get_methods()) {
67 this.add_method(g, c);
69 element.accept_children(this); // catch sub namespaces..
75 public void add_enum(GirObject parent, Vala.Enum cls)
78 var c = new GirObject("Enum", cls.name);
79 parent.consts.set(cls.name, c);
84 foreach(var e in cls.get_values()) {
85 var em = new GirObject("EnumMember",e.name);
88 em.type = e.type_reference == null || e.type_reference.data_type == null ? "" : e.type_reference.data_type.get_full_name();
89 // unlikely to get value..
90 //c.value = element->get_prop("value");
91 c.consts.set(e.name,em);
99 public void add_interface(GirObject parent, Vala.Interface cls)
102 var c = new GirObject("Interface", parent.name + "." + cls.name);
103 parent.classes.set(cls.name, c);
105 //c.parent = cls.base_class == null ? "" : cls.base_class.get_full_name() ; // extends...
108 foreach(var p in cls.get_properties()) {
109 this.add_property(c, p);
112 foreach(var p in cls.get_signals()) {
113 this.add_signal(c, p);
116 foreach(var p in cls.get_methods()) {
117 // skip static methods..
118 if (p.binding != Vala.MemberBinding.INSTANCE &&
119 !(p is Vala.CreationMethod)
124 this.add_method(c, p);
127 //if (cls.base_class != null) {
128 // c.inherits.add(cls.base_class.get_full_name());
130 //foreach(var p in cls.get_base_types()) {
131 // if (p.data_type != null) {
132 // c.implements.add(p.data_type.get_full_name());
141 public void add_class(GirObject parent, Vala.Class cls)
144 var c = new GirObject("Class", parent.name + "." + cls.name);
145 parent.classes.set(cls.name, c);
147 c.parent = cls.base_class == null ? "" : cls.base_class.get_full_name() ; // extends...
150 foreach(var p in cls.get_properties()) {
151 this.add_property(c, p);
154 foreach(var p in cls.get_signals()) {
155 this.add_signal(c, p);
158 foreach(var p in cls.get_methods()) {
159 // skip static methods..
160 if (p.binding != Vala.MemberBinding.INSTANCE &&
161 !(p is Vala.CreationMethod)
166 this.add_method(c, p);
169 if (cls.base_class != null) {
170 c.inherits.add(cls.base_class.get_full_name());
172 foreach(var p in cls.get_base_types()) {
173 if (p.data_type != null) {
174 c.implements.add(p.data_type.get_full_name());
182 public void add_property(GirObject parent, Vala.Property prop)
184 var c = new GirObject("Prop",prop.name);
187 c.propertyof = parent.name;
189 c.type = prop.property_type.data_type == null ? "" : prop.property_type.data_type.get_full_name();
190 parent.props.set(prop.name,c);
193 public void add_signal(GirObject parent, Vala.Signal sig)
195 var c = new GirObject("Signal",sig.name);
199 if (sig.return_type.data_type != null) {
200 //print("creating return type on signal %s\n", sig.name);
201 var cc = new GirObject("Return", "return-value");
204 cc.type = sig.return_type.data_type.get_full_name();
207 parent.signals.set(sig.name,c);
209 var params = sig.get_parameters() ;
210 if (params.size < 1) {
213 var cc = new GirObject("Paramset",sig.name); // what's the name on this?
219 foreach(var p in params) {
220 this.add_param(cc, p);
225 public void add_method(GirObject parent, Vala.Method met)
227 var n = met.name == null ? "" : met.name;
229 if (met is Vala.CreationMethod) {
231 if(n == "" || n == ".new") {
236 //print("add_method : %s\n", n);
238 var c = new GirObject(ty,n);
242 if (met.return_type.data_type != null) {
243 //print("creating return type on method %s\n", met.name);
244 var cc = new GirObject("Return", "return-value");
247 cc.type = met.return_type.data_type.get_full_name();
250 if (met is Vala.CreationMethod) {
251 parent.ctors.set(c.name,c);
253 parent.methods.set(met.name,c);
256 var params = met.get_parameters() ;
257 if (params.size < 1) {
260 var cc = new GirObject("Paramset",met.name); // what's the name on this?
266 foreach(var p in params) {
267 if (p.name == null && !p.ellipsis) {
270 var pp = this.add_param(cc, p);
271 c.sig += (c.sig == "(" ? "" : ",");
272 c.sig += " " + (pp.direction == "in" ? "" : pp.direction) + " " + pp.type + " " + pp.name;
274 c.sig += (c.sig == "(" ? ")" : " )");
278 public GirObject add_param(GirObject parent, Vala.Parameter pam)
285 var c = new GirObject("Param",n);
289 switch (pam.direction) {
290 case Vala.ParameterDirection.IN:
293 case Vala.ParameterDirection.OUT:
296 case Vala.ParameterDirection.REF:
301 parent.params.add(c);
304 c.type = pam.variable_type.data_type == null ? "" : pam.variable_type.data_type.get_full_name();
306 Gir.checkParamOverride(c);
311 public void create_valac_tree( )
314 context = new Vala.CodeContext ();
315 Vala.CodeContext.push (context);
317 context.experimental = false;
318 context.experimental_non_null = false;
333 for (int i = 2; i <= ver; i += 2) {
334 context.add_define ("VALA_0_%d".printf (i));
338 //var vapidirs = ((Project.Gtk)this.file.project).vapidirs();
339 // what's the current version of vala???
342 //vapidirs += Path.get_dirname (context.get_vapi_path("glib-2.0")) ;
344 var vapidirs = context.vapi_directories;
346 vapidirs += (BuilderApplication.configDirectory() + "/resources/vapi");
347 context.vapi_directories = vapidirs;
348 // or context.get_vapi_path("glib-2.0"); // should return path..
349 //context.vapi_directories = vapidirs;
350 context.report.enable_warnings = true;
351 context.metadata_directories = { };
352 context.gir_directories = {};
353 context.thread = true;
356 //this.report = new ValaSourceReport(this.file);
357 //context.report = this.report;
360 context.basedir = "/tmp"; //Posix.realpath (".");
362 context.directory = context.basedir;
365 // add default packages:
366 //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
367 context.profile = Vala.Profile.GOBJECT;
369 var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
370 context.root.add_using_directive (ns_ref);
372 context.add_external_package ("glib-2.0");
373 context.add_external_package ("gobject-2.0");
375 // core packages we are interested in for the builder..
376 // some of these may fail... - we probalby need a better way to handle this..
378 context.add_external_package ("gtk+-3.0");
379 context.add_external_package ("libsoup-2.4");
380 if (!context.add_external_package ("webkit2gtk-4.0")) {
381 context.add_external_package ("webkit2gtk-3.0");
383 // these are supposed to be in the 'deps' file, but it's not getting read..
384 context.add_external_package ("cogl-1.0");
385 context.add_external_package ("json-glib-1.0");
386 context.add_external_package ("clutter-gtk-1.0");
390 context.add_external_package ("gdl-3.0");
391 context.add_external_package ("gtksourceview-3.0");
392 context.add_external_package ("vte-2.90"); //??? -- hopefullly that works..
393 //add_documented_files (context, settings.source_files);
395 Vala.Parser parser = new Vala.Parser ();
396 parser.parse (context);
397 //gir_parser.parse (context);
398 if (context.report.get_errors () > 0) {
400 throw new VapiParserError.PARSE_FAILED("failed parse VAPIS, so we can not write file correctly");
402 print("parse got errors");
405 Vala.CodeContext.pop ();
413 if (context.report.get_errors () > 0) {
414 print("check got errors");
415 throw new VapiParserError.PARSE_FAILED("failed check VAPIS, so we can not write file correctly");
416 Vala.CodeContext.pop ();
424 context.accept(this);
427 // dump the tree for Gtk?
429 Vala.CodeContext.pop ();
441 int main (string[] args) {
443 var g = Palete.Gir.factoryFqn("Gtk.SourceView");
444 print("%s\n", g.asJSONString());