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