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) {
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(element.name);
39 Gir.cache.set(element.name, g);
42 foreach(var c in element.get_classes()) {
45 foreach(var c in element.get_enums()) {
48 foreach(var c in element.get_interfaces()) {
49 this.add_interface(g, c);
52 element.accept_children(this); // catch sub namespaces..
58 public void add_enum(GirObject parent, Vala.Enum cls)
61 var c = new GirObject("Enum", cls.name);
62 parent.consts.set(cls.name, c);
67 foreach(var e in cls.get_values()) {
68 var em = new GirObject("EnumMember",e.name);
71 em.type = e.type_reference == null || e.type_reference.data_type == null ? "" : e.type_reference.data_type.get_full_name();
72 // unlikely to get value..
73 //c.value = element->get_prop("value");
74 c.consts.set(e.name,em);
82 public void add_interface(GirObject parent, Vala.Interface cls)
85 var c = new GirObject("Interface", 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());
124 public void add_class(GirObject parent, Vala.Class cls)
127 var c = new GirObject("Class", parent.name + "." + cls.name);
128 parent.classes.set(cls.name, c);
130 c.parent = cls.base_class == null ? "" : cls.base_class.get_full_name() ; // extends...
133 foreach(var p in cls.get_properties()) {
134 this.add_property(c, p);
137 foreach(var p in cls.get_signals()) {
138 this.add_signal(c, p);
141 foreach(var p in cls.get_methods()) {
142 // skip static methods..
143 if (p.binding != Vala.MemberBinding.INSTANCE &&
144 !(p is Vala.CreationMethod)
149 this.add_method(c, p);
152 if (cls.base_class != null) {
153 c.inherits.add(cls.base_class.get_full_name());
155 foreach(var p in cls.get_base_types()) {
156 if (p.data_type != null) {
157 c.implements.add(p.data_type.get_full_name());
165 public void add_property(GirObject parent, Vala.Property prop)
167 var c = new GirObject("Prop",prop.name);
170 c.propertyof = parent.name;
171 c.type = prop.property_type.data_type == null ? "" : prop.property_type.data_type.get_full_name();
172 parent.props.set(prop.name,c);
175 public void add_signal(GirObject parent, Vala.Signal sig)
177 var c = new GirObject("Signal",sig.name);
181 if (sig.return_type.data_type != null) {
182 //print("creating return type on signal %s\n", sig.name);
183 var cc = new GirObject("Return", "return-value");
186 cc.type = sig.return_type.data_type.get_full_name();
189 parent.signals.set(sig.name,c);
191 var params = sig.get_parameters() ;
192 if (params.size < 1) {
195 var cc = new GirObject("Paramset",sig.name); // what's the name on this?
201 foreach(var p in params) {
202 this.add_param(cc, p);
207 public void add_method(GirObject parent, Vala.Method met)
209 var n = met.name == null ? "" : met.name;
211 if (met is Vala.CreationMethod) {
213 if(n == "" || n == ".new") {
218 //print("add_method : %s\n", n);
220 var c = new GirObject(ty,n);
224 if (met.return_type.data_type != null) {
225 //print("creating return type on method %s\n", met.name);
226 var cc = new GirObject("Return", "return-value");
229 cc.type = met.return_type.data_type.get_full_name();
232 if (met is Vala.CreationMethod) {
233 parent.ctors.set(c.name,c);
235 parent.methods.set(met.name,c);
238 var params = met.get_parameters() ;
239 if (params.size < 1) {
242 var cc = new GirObject("Paramset",met.name); // what's the name on this?
248 foreach(var p in params) {
249 if (p.name == null && !p.ellipsis) {
252 this.add_param(cc, p);
257 public void add_param(GirObject parent, Vala.Parameter pam)
264 var c = new GirObject("Param",n);
267 parent.params.add(c);
270 c.type = pam.variable_type.data_type == null ? "" : pam.variable_type.data_type.get_full_name();
272 Gir.checkParamOverride(c);
276 public void create_valac_tree( )
279 context = new Vala.CodeContext ();
280 Vala.CodeContext.push (context);
282 context.experimental = false;
283 context.experimental_non_null = false;
295 for (int i = 2; i <= ver; i += 2) {
296 context.add_define ("VALA_0_%d".printf (i));
300 //var vapidirs = ((Project.Gtk)this.file.project).vapidirs();
301 // what's the current version of vala???
304 //vapidirs += Path.get_dirname (context.get_vapi_path("glib-2.0")) ;
306 var vapidirs = context.vapi_directories;
308 vapidirs += (BuilderApplication.configDirectory() + "/resources/vapi");
309 context.vapi_directories = vapidirs;
310 // or context.get_vapi_path("glib-2.0"); // should return path..
311 //context.vapi_directories = vapidirs;
312 context.report.enable_warnings = true;
313 context.metadata_directories = { };
314 context.gir_directories = {};
315 context.thread = true;
318 //this.report = new ValaSourceReport(this.file);
319 //context.report = this.report;
322 context.basedir = "/tmp"; //Posix.realpath (".");
324 context.directory = context.basedir;
327 // add default packages:
328 //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
329 context.profile = Vala.Profile.GOBJECT;
331 var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
332 context.root.add_using_directive (ns_ref);
334 context.add_external_package ("glib-2.0");
335 context.add_external_package ("gobject-2.0");
337 // core packages we are interested in for the builder..
338 // some of these may fail... - we probalby need a better way to handle this..
340 context.add_external_package ("gtk+-3.0");
341 context.add_external_package ("libsoup-2.4");
342 if (!context.add_external_package ("webkit2gtk-4.0")) {
343 context.add_external_package ("webkit2gtk-3.0");
345 context.add_external_package ("clutter-gtk-1.0");
346 context.add_external_package ("gdl-3.0");
347 context.add_external_package ("gtksourceview-3.0");
348 context.add_external_package ("vte-2.90"); //??? -- hopefullly that works..
349 //add_documented_files (context, settings.source_files);
351 Vala.Parser parser = new Vala.Parser ();
352 parser.parse (context);
353 //gir_parser.parse (context);
354 if (context.report.get_errors () > 0) {
356 throw new VapiParserError.PARSE_FAILED("failed parse VAPIS, so we can not write file correctly");
358 print("parse got errors");
361 Vala.CodeContext.pop ();
369 if (context.report.get_errors () > 0) {
370 print("check got errors");
371 throw new VapiParserError.PARSE_FAILED("failed check VAPIS, so we can not write file correctly");
372 Vala.CodeContext.pop ();
380 context.accept(this);
383 // dump the tree for Gtk?
385 Vala.CodeContext.pop ();
397 int main (string[] args) {
399 var g = Palete.Gir.factoryFqn("Gtk.SourceView");
400 print("%s\n", g.asJSONString());