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