src/Palete/VapiParser.vala
[app.Builder.js] / src / Palete / VapiParser.vala
1
2
3 // valac VapiParser.vala --pkg libvala-0.24 --pkg posix -o /tmp/treebuilder
4
5 namespace Palete {
6          
7          
8          
9
10         public class VapiParser : Vala.CodeVisitor {
11                 
12                 static  Gee.HashMap<string,Gir> cache = null;
13                 
14                 
15                 
16                 Vala.CodeContext context;
17                 public VapiParser() {
18                         base();
19                         if (cache == null) {
20                                 cache =  new Gee.HashMap<string,Gir>();
21                         }
22                 }
23                 
24                 
25                 
26                 public override void visit_namespace (Vala.Namespace element) 
27                 {
28                         if (element == null) {
29                                 
30                                 return;
31                         }
32                         print("parsing namespace %s\n", element.name);
33                         if (element.name == null) {
34                                 element.accept_children(this); // catch sub namespaces..
35                                 return;
36                         }
37                         
38                         var g = new Gir.new_empty(element.name);
39                         cache.set(element.name, g);
40                         
41                         
42                         foreach(var c in element.get_classes()) {
43                                 this.add_class(g, c);
44                         }
45                         
46                         element.accept_children(this); // catch sub namespaces..
47                         
48                         
49                 }
50                 
51                 public void add_class(GirObject parent, Vala.Class cls)
52                 {
53                 
54                         var c = new GirObject("Class", parent.name + "." + cls.name);
55                         parent.classes.set(cls.name, c);
56                         c.ns = parent.name;
57                         c.parent = cls.base_class == null ? "" : cls.base_class.get_full_name() ;  // extends...
58                         c.gparent = parent;
59                         
60                         foreach(var p in cls.get_properties()) {
61                                 this.add_property(c, p);
62                         }
63                         // methods...
64                         
65                          
66                 }
67                 public void add_property(GirObject parent, Vala.Property prop)
68                 {
69                         var c = new GirObject("Prop",prop.name);
70                         c.gparent = parent;
71                         c.ns = parent.ns;
72                         c.propertyof = parent.name;
73                         c.type  = prop.property_type.data_type.get_full_name();
74                         parent.props.set(prop.name,c);
75                         
76                 }
77                           
78                 
79                 public void create_valac_tree( )
80                 {
81                         // init context:
82                         context = new Vala.CodeContext ();
83                         Vala.CodeContext.push (context);
84                 
85                         context.experimental = false;
86                         context.experimental_non_null = false;
87                         
88 #if VALA_0_28
89                         var ver=28;
90 #elif VALA_0_26 
91                         var ver=26;
92 #elif VALA_0_24
93                         var ver=24;
94 #elif VALA_0_22 
95                         var ver=22;
96 #endif
97                         
98                         for (int i = 2; i <= ver; i += 2) {
99                                 context.add_define ("VALA_0_%d".printf (i));
100                         }
101                         
102                          
103                         //var vapidirs = ((Project.Gtk)this.file.project).vapidirs();
104                         // what's the current version of vala???
105                         
106                         
107                         //vapidirs +=  Path.get_dirname (context.get_vapi_path("glib-2.0")) ;
108                          
109                                 
110                         
111                         // or context.get_vapi_path("glib-2.0"); // should return path..
112                         //context.vapi_directories = vapidirs;
113                         context.report.enable_warnings = true;
114                         context.metadata_directories = { };
115                         context.gir_directories = {};
116                         context.thread = true;
117                         
118                         
119                         //this.report = new ValaSourceReport(this.file);
120                         //context.report = this.report;
121                         
122                         
123                         context.basedir = "/tmp"; //Posix.realpath (".");
124                 
125                         context.directory = context.basedir;
126                 
127
128                         // add default packages:
129                         //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
130                         context.profile = Vala.Profile.GOBJECT;
131                          
132                         var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
133                         context.root.add_using_directive (ns_ref);
134  
135                         // default.. packages..
136                         context.add_external_package ("glib-2.0"); 
137                         context.add_external_package ("gobject-2.0");
138                         context.add_external_package ("gdk-3.0");
139                         context.add_external_package ("atk");
140                         context.add_external_package ("gdk-x11-3.0");
141                         context.add_external_package ("x11");
142                         
143                         // user defined ones..
144                         //context.add_package ("Gtk");
145                   
146                         var vfile = new Vala.SourceFile (context, Vala.SourceFileType.PACKAGE, "/usr/share/vala-0.26/vapi/gtk+-3.0.vapi");
147                         context.add_source_file (vfile);
148                          
149                         //context.add_external_package ("libvala-0.24");
150                         
151                          
152                 
153                         //add_documented_files (context, settings.source_files);
154                 
155                         Vala.Parser parser = new Vala.Parser ();
156                         parser.parse (context);
157                         //gir_parser.parse (context);
158                         if (context.report.get_errors () > 0) {
159                                 print("parse got errors");
160                                  
161                                 
162                                 Vala.CodeContext.pop ();
163                                 return ;
164                         }
165
166
167                         
168                         // check context:
169                         context.check ();
170                         if (context.report.get_errors () > 0) {
171                                 print("check got errors");
172                                  
173                                 Vala.CodeContext.pop ();
174                                  
175                                 return;
176                                 
177                         }
178                          
179                         Vala.CodeContext.pop ();
180                          
181                         context.accept(this);
182                         
183                         // dump the tree for Gtk?
184                         
185                         
186                         print("%s\n", cache.get("Gtk").asJSONString());
187                         print("ALL OK?\n");
188                  
189                 }
190         //
191                 // startpoint:
192                 //
193          
194         }
195 }
196  
197 int main (string[] args) {
198
199         var a = new Palete.VapiParser( );
200         a.create_valac_tree();
201         return 0;
202 }
203  
204
205