src/Palete/VapiParser.vala
[app.Builder.js] / src / Palete / VapiParser.vala
index 83bfe2e..fe90622 100644 (file)
 
 namespace Palete {
         
-       
+        
         
 
        public class VapiParser : Vala.CodeVisitor {
-
-               Vala.CodeContext context;
+               
+               
+               
+               
+               
+               Vala.CodeContext context;
                public VapiParser() {
                        base();
+                       if (Gir.cache == null) {
+                               Gir.cache =  new Gee.HashMap<string,Gir>();
+                       }
+               }
+               
+               
+               
+               public override void visit_namespace (Vala.Namespace element) 
+               {
+                       if (element == null) {
+                               
+                               return;
+                       }
+                       print("parsing namespace %s\n", element.name);
+                       if (element.name == null) {
+                               element.accept_children(this); // catch sub namespaces..
+                               return;
+                       }
+                       
+                       var g = new Gir.new_empty(element.name);
+                       Gir.cache.set(element.name, g);
+                       
+                       
+                       foreach(var c in element.get_classes()) {
+                               this.add_class(g, c);
+                       }
+                       
+                       element.accept_children(this); // catch sub namespaces..
+                       
+                       
+               }
+               
+               public void add_class(GirObject parent, Vala.Class cls)
+               {
+               
+                       var c = new GirObject("Class", parent.name + "." + cls.name);
+                       parent.classes.set(cls.name, c);
+                       c.ns = parent.name;
+                       c.parent = cls.base_class == null ? "" : cls.base_class.get_full_name() ;  // extends...
+                       c.gparent = parent;
+                       
+                       foreach(var p in cls.get_properties()) {
+                               this.add_property(c, p);
+                       }
+                       // methods...
+                       foreach(var p in cls.get_signals()) {
+                               this.add_signal(c, p);
+                       }
+                       
+                       foreach(var p in cls.get_methods()) {
+                               // skip static methods..
+                               if (p.binding != Vala.MemberBinding.INSTANCE) {
+                                       continue;
+                               }
+                               
+                               this.add_method(c, p);
+                       }
+                       
+                       if (cls.base_class != null) {
+                               c.inherits.add(cls.base_class.get_full_name());
+                       }
+                       foreach(var p in cls.get_base_types()) {
+                               if (p.data_type != null) {
+                                       c.implements.add(p.data_type.get_full_name());
+                               }
+                       }
+                         
+                       
+                       
+                        
+               }
+               public void add_property(GirObject parent, Vala.Property prop)
+               {
+                       var c = new GirObject("Prop",prop.name);
+                       c.gparent = parent;
+                       c.ns = parent.ns;
+                       c.propertyof = parent.name;
+                       c.type  = prop.property_type.data_type == null ? "" : prop.property_type.data_type.get_full_name();
+                       parent.props.set(prop.name,c);
+                       
+               }
+               public void add_signal(GirObject parent, Vala.Signal sig)
+               {
+                       var c = new GirObject("Signal",sig.name);
+                       c.gparent = parent;
+                       c.ns = parent.ns;
+                       
+                       if (sig.return_type.data_type != null) {
+                               //print("creating return type on signal %s\n", sig.name);
+                               var cc = new GirObject("Return", "return-value");
+                               cc.gparent = c;
+                               cc.ns = c.ns;
+                               cc.type  =  sig.return_type.data_type.get_full_name();
+                               c.return_value = cc;
+                       }
+                       parent.signals.set(sig.name,c);
+                       
+                       var params =  sig.get_parameters() ;
+                       if (params.size < 1) {
+                               return;
+                       }
+                       var cc = new GirObject("Paramset",sig.name); // what's the name on this?
+                       cc.gparent = c;
+                       cc.ns = c.ns;
+                       c.paramset = cc;
+                       
+                       
+                       foreach(var p in params) {
+                               this.add_param(cc, p);
+                       }
+                       
+               }       
+               
+               public void add_method(GirObject parent, Vala.Method met)
+               {
+                       
+                       
+                       var c = new GirObject("Method",met.name == null ? parent.name : "");
+                       c.gparent = parent;
+                       c.ns = parent.ns;
+                       
+                       if (met.return_type.data_type != null) {
+                               //print("creating return type on method %s\n", met.name);
+                               var cc = new GirObject("Return", "return-value");
+                               cc.gparent = c;
+                               cc.ns = c.ns;
+                               cc.type  =  met.return_type.data_type.get_full_name();
+                               c.return_value = cc;
+                       }
+                       if (met is Vala.CreationMethod) {
+                               
+                       } else {
+                       
+                               parent.methods.set(met.name,c);
+                       }
+                       
+                       var params =  met.get_parameters() ;
+                       if (params.size < 1) {
+                               return;
+                       }
+                       var cc = new GirObject("Paramset",met.name); // what's the name on this?
+                       cc.gparent = c;
+                       cc.ns = c.ns;
+                       c.paramset = cc;
+                       
+                       
+                       foreach(var p in params) {
+                               if (p.name == null) {
+                                       continue;
+                               }
+                               this.add_param(cc, p);
+                       }
                        
                }
-                 
                
-               public void checkString(string contents)
+               public void add_param(GirObject parent, Vala.Parameter pam)
+               {
+                       var c = new GirObject("Param",pam.name);
+                       c.gparent = parent;
+                       c.ns = parent.ns;
+                       parent.params.add(c);
+                       c.type = pam.variable_type.data_type == null ? "" : pam.variable_type.data_type.get_full_name();
+                       // this.checkParamOverride(c);   - this is an old kludge for Gir files..
+                       
+               }
+               
+               public void create_valac_tree( )
                {
                        // init context:
                        context = new Vala.CodeContext ();
@@ -45,10 +211,7 @@ namespace Palete {
                        
                        
                        //vapidirs +=  Path.get_dirname (context.get_vapi_path("glib-2.0")) ;
-                       
-                       //for(var i =0 ; i < vapidirs.length; i++) {
-                       //      valac += " --vapidir=" + vapidirs[i];
-                       //}
+                        
                                
                        
                        // or context.get_vapi_path("glib-2.0"); // should return path..
@@ -59,8 +222,8 @@ namespace Palete {
                        context.thread = true;
                        
                        
-                       this.report = new ValaSourceReport(this.file);
-                       context.report = this.report;
+                       //this.report = new ValaSourceReport(this.file);
+                       //context.report = this.report;
                        
                        
                        context.basedir = "/tmp"; //Posix.realpath (".");
@@ -74,76 +237,24 @@ namespace Palete {
                         
                        var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
                        context.root.add_using_directive (ns_ref);
-
-                       var source_file = new Vala.SourceFile (
-                               context, 
-                               Vala.SourceFileType.SOURCE, 
-                                       "~~~~~testfile.vala",
-                                       contents
-                       );
-                       source_file.add_using_directive (ns_ref);
-                       context.add_source_file (source_file);
-                       
-               // add all the files (except the current one) - this.file.path
-               var pr = ((Project.Gtk)this.file.project);
-               if (this.file.build_module.length > 0) {
-                               var cg =  pr.compilegroups.get(this.file.build_module);
-                               for (var i = 0; i < cg.sources.size; i++) {
-                                       var path = pr.resolve_path(
-                                                       pr.resolve_path_combine_path(pr.firstPath(),cg.sources.get(i)));
-                                                       
-                                       if (!FileUtils.test(path, FileTest.EXISTS)) {
-                                               continue;
-                                       }       
-                        
-                                       if (path == this.file.path.replace(".bjs", ".vala")) {
-                                               valac += " " + path;
-                                               continue;
-                                       }
-                                       if (FileUtils.test(path, FileTest.IS_DIR)) {
-                                               continue;
-                                       }
-                                       //print("Add source file %s\n", path);
-                                       
-                                       valac += " " + path;
-                                       
-                                       if (Regex.match_simple("\\.c$", path)) {
-                                               context.add_c_source_file(path);
-                                               continue;
-                                       }
-                                       
-                                       
-                                       var xsf = new Vala.SourceFile (
-                                               context,
-                                               Vala.SourceFileType.SOURCE, 
-                                               path
-                                       );
-                                       xsf.add_using_directive (ns_ref);
-                                       context.add_source_file(xsf);
-                                       
-                               }
-                       }
                        // default.. packages..
                        context.add_external_package ("glib-2.0"); 
                        context.add_external_package ("gobject-2.0");
-                       // user defined ones..
-                       
-               var dcg = pr.compilegroups.get("_default_");
-               for (var i = 0; i < dcg.packages.size; i++) {
-                               valac += " --pkg " + dcg.packages.get(i);
-                               context.add_external_package (dcg.packages.get(i));
-                       }
-               
-                        //Vala.Config.PACKAGE_SUFFIX.substring (1)
-                       
-                       // add the modules...
-                       
-                       
+                       context.add_external_package ("gdk-3.0");
+                       context.add_external_package ("atk");
+                       context.add_external_package ("gdk-x11-3.0");
+                       context.add_external_package ("x11");
                        
+                       // user defined ones..
+                       //context.add_package ("Gtk");
+                 
+                       var vfile = new Vala.SourceFile (context, Vala.SourceFileType.PACKAGE, "/usr/share/vala-0.26/vapi/gtk+-3.0.vapi");
+                       context.add_source_file (vfile);
+                        
                        //context.add_external_package ("libvala-0.24");
                        
-                       this.report.compile_notice("START", "", 0, "");
-
+                        
                
                        //add_documented_files (context, settings.source_files);
                
@@ -152,11 +263,10 @@ namespace Palete {
                        //gir_parser.parse (context);
                        if (context.report.get_errors () > 0) {
                                print("parse got errors");
-                               ((ValaSourceReport)context.report).dump();
+                                
                                
                                Vala.CodeContext.pop ();
-                               this.report.compile_notice("END", "", 0, "");
-                               return this.report.line_errors;
+                               return ;
                        }
 
 
@@ -165,35 +275,23 @@ namespace Palete {
                        context.check ();
                        if (context.report.get_errors () > 0) {
                                print("check got errors");
-                               ((ValaSourceReport)context.report).dump();
+                                
                                Vala.CodeContext.pop ();
-                               this.report.compile_notice("END", "", 0, "");
-                               return this.report.line_errors;
+                                
+                               return;
                                
                        }
-                       
-                       context.codegen = new Vala.GDBusServerModule ();
-                       
                         
-                       context.output = "/tmp/testbuild";
-                       valac += " -o " +context.output;
-                       context.codegen.emit (context);
-                       /*
-                       var ccompiler = new Vala.CCodeCompiler ();
-                       var cc_command = Environment.get_variable ("CC");
-                       var pkg_config_command = Environment.get_variable ("PKG_CONFIG");
-#if VALA_0_28
-                       ccompiler.compile (context, cc_command, new string[] { }, pkg_config_command);
-#else
-                       ccompiler.compile (context, cc_command, new string[] { });
-#endif
-                       */
                        Vala.CodeContext.pop ();
-                       this.report.compile_notice("END", "", 0, "");
-                       print("%s\n", valac);
+                        
+                       context.accept(this);
+                       
+                       // dump the tree for Gtk?
+                       
+                       
+                       print("%s\n", Gir.cache.get("Gtk").asJSONString());
                        print("ALL OK?\n");
-                       return this.report.line_errors;
+                
                }
        //
                // startpoint:
@@ -201,13 +299,13 @@ namespace Palete {
         
        }
 }
-/*
 int main (string[] args) {
 
-       var a = new ValaSource(file);
+       var a = new Palete.VapiParser( );
        a.create_valac_tree();
        return 0;
 }
-*/