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