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;
330 for (int i = 2; i <= ver; i += 2) {
331 context.add_define ("VALA_0_%d".printf (i));
335 //var vapidirs = ((Project.Gtk)this.file.project).vapidirs();
336 // what's the current version of vala???
339 //vapidirs += Path.get_dirname (context.get_vapi_path("glib-2.0")) ;
341 var vapidirs = context.vapi_directories;
343 vapidirs += (BuilderApplication.configDirectory() + "/resources/vapi");
344 context.vapi_directories = vapidirs;
345 // or context.get_vapi_path("glib-2.0"); // should return path..
346 //context.vapi_directories = vapidirs;
347 context.report.enable_warnings = true;
348 context.metadata_directories = { };
349 context.gir_directories = {};
350 context.thread = true;
353 //this.report = new ValaSourceReport(this.file);
354 //context.report = this.report;
357 context.basedir = "/tmp"; //Posix.realpath (".");
359 context.directory = context.basedir;
362 // add default packages:
363 //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
364 context.profile = Vala.Profile.GOBJECT;
366 var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
367 context.root.add_using_directive (ns_ref);
369 context.add_external_package ("glib-2.0");
370 context.add_external_package ("gobject-2.0");
372 // core packages we are interested in for the builder..
373 // some of these may fail... - we probalby need a better way to handle this..
375 context.add_external_package ("gtk+-3.0");
376 context.add_external_package ("libsoup-2.4");
377 if (!context.add_external_package ("webkit2gtk-4.0")) {
378 context.add_external_package ("webkit2gtk-3.0");
380 context.add_external_package ("clutter-gtk-1.0");
381 context.add_external_package ("gdl-3.0");
382 context.add_external_package ("gtksourceview-3.0");
383 context.add_external_package ("vte-2.90"); //??? -- hopefullly that works..
384 //add_documented_files (context, settings.source_files);
386 Vala.Parser parser = new Vala.Parser ();
387 parser.parse (context);
388 //gir_parser.parse (context);
389 if (context.report.get_errors () > 0) {
391 throw new VapiParserError.PARSE_FAILED("failed parse VAPIS, so we can not write file correctly");
393 print("parse got errors");
396 Vala.CodeContext.pop ();
404 if (context.report.get_errors () > 0) {
405 print("check got errors");
406 throw new VapiParserError.PARSE_FAILED("failed check VAPIS, so we can not write file correctly");
407 Vala.CodeContext.pop ();
415 context.accept(this);
418 // dump the tree for Gtk?
420 Vala.CodeContext.pop ();
432 int main (string[] args) {
434 var g = Palete.Gir.factoryFqn("Gtk.SourceView");
435 print("%s\n", g.asJSONString());