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