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