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                 Vala.CodeContext context;
13                 public VapiParser() {
14                         base();
15                         
16                 }
17                   
18                 
19                 public void checkString(string contents)
20                 {
21                         // init context:
22                         context = new Vala.CodeContext ();
23                         Vala.CodeContext.push (context);
24                 
25                         context.experimental = false;
26                         context.experimental_non_null = false;
27                         
28 #if VALA_0_28
29                         var ver=28;
30 #elif VALA_0_26 
31                         var ver=26;
32 #elif VALA_0_24
33                         var ver=24;
34 #elif VALA_0_22 
35                         var ver=22;
36 #endif
37                         
38                         for (int i = 2; i <= ver; i += 2) {
39                                 context.add_define ("VALA_0_%d".printf (i));
40                         }
41                         
42                          
43                         //var vapidirs = ((Project.Gtk)this.file.project).vapidirs();
44                         // what's the current version of vala???
45                         
46                         
47                         //vapidirs +=  Path.get_dirname (context.get_vapi_path("glib-2.0")) ;
48                         
49                         //for(var i =0 ; i < vapidirs.length; i++) {
50                         //      valac += " --vapidir=" + vapidirs[i];
51                         //}
52                                 
53                         
54                         // or context.get_vapi_path("glib-2.0"); // should return path..
55                         //context.vapi_directories = vapidirs;
56                         context.report.enable_warnings = true;
57                         context.metadata_directories = { };
58                         context.gir_directories = {};
59                         context.thread = true;
60                         
61                         
62                         this.report = new ValaSourceReport(this.file);
63                         context.report = this.report;
64                         
65                         
66                         context.basedir = "/tmp"; //Posix.realpath (".");
67                 
68                         context.directory = context.basedir;
69                 
70
71                         // add default packages:
72                         //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
73                         context.profile = Vala.Profile.GOBJECT;
74                          
75                         var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
76                         context.root.add_using_directive (ns_ref);
77
78                         var source_file = new Vala.SourceFile (
79                                 context, 
80                                 Vala.SourceFileType.SOURCE, 
81                                         "~~~~~testfile.vala",
82                                         contents
83                         );
84                         source_file.add_using_directive (ns_ref);
85                         context.add_source_file (source_file);
86                         
87                 // add all the files (except the current one) - this.file.path
88                 var pr = ((Project.Gtk)this.file.project);
89                 if (this.file.build_module.length > 0) {
90                                 var cg =  pr.compilegroups.get(this.file.build_module);
91                                 for (var i = 0; i < cg.sources.size; i++) {
92                                         var path = pr.resolve_path(
93                                                         pr.resolve_path_combine_path(pr.firstPath(),cg.sources.get(i)));
94                                                         
95                                         if (!FileUtils.test(path, FileTest.EXISTS)) {
96                                                 continue;
97                                         }       
98                          
99                                         if (path == this.file.path.replace(".bjs", ".vala")) {
100                                                 valac += " " + path;
101                                                 continue;
102                                         }
103                                         if (FileUtils.test(path, FileTest.IS_DIR)) {
104                                                 continue;
105                                         }
106                                         //print("Add source file %s\n", path);
107                                         
108                                         valac += " " + path;
109                                         
110                                         if (Regex.match_simple("\\.c$", path)) {
111                                                 context.add_c_source_file(path);
112                                                 continue;
113                                         }
114                                         
115                                         
116                                         var xsf = new Vala.SourceFile (
117                                                 context,
118                                                 Vala.SourceFileType.SOURCE, 
119                                                 path
120                                         );
121                                         xsf.add_using_directive (ns_ref);
122                                         context.add_source_file(xsf);
123                                         
124                                 }
125                         }
126                         // default.. packages..
127                         context.add_external_package ("glib-2.0"); 
128                         context.add_external_package ("gobject-2.0");
129                         // user defined ones..
130                         
131                 var dcg = pr.compilegroups.get("_default_");
132                 for (var i = 0; i < dcg.packages.size; i++) {
133                                 valac += " --pkg " + dcg.packages.get(i);
134                                 context.add_external_package (dcg.packages.get(i));
135                         }
136                 
137                          //Vala.Config.PACKAGE_SUFFIX.substring (1)
138                         
139                         // add the modules...
140                         
141                         
142                         
143                         //context.add_external_package ("libvala-0.24");
144                         
145                         this.report.compile_notice("START", "", 0, "");
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                                 ((ValaSourceReport)context.report).dump();
156                                 
157                                 Vala.CodeContext.pop ();
158                                 this.report.compile_notice("END", "", 0, "");
159                                 return this.report.line_errors;
160                         }
161
162
163                         
164                         // check context:
165                         context.check ();
166                         if (context.report.get_errors () > 0) {
167                                 print("check got errors");
168                                 ((ValaSourceReport)context.report).dump();
169                                 Vala.CodeContext.pop ();
170                                 this.report.compile_notice("END", "", 0, "");
171                                 return this.report.line_errors;
172                                 
173                         }
174                         
175                         context.codegen = new Vala.GDBusServerModule ();
176                         
177                          
178                         context.output = "/tmp/testbuild";
179                         valac += " -o " +context.output;
180                         context.codegen.emit (context);
181                         /*
182                         var ccompiler = new Vala.CCodeCompiler ();
183                         var cc_command = Environment.get_variable ("CC");
184                         var pkg_config_command = Environment.get_variable ("PKG_CONFIG");
185 #if VALA_0_28
186                         ccompiler.compile (context, cc_command, new string[] { }, pkg_config_command);
187 #else
188                         ccompiler.compile (context, cc_command, new string[] { });
189 #endif
190                         */
191  
192                         Vala.CodeContext.pop ();
193                         this.report.compile_notice("END", "", 0, "");
194                         print("%s\n", valac);
195                         print("ALL OK?\n");
196                         return this.report.line_errors;
197                 }
198         //
199                 // startpoint:
200                 //
201          
202         }
203 }
204 /*
205 int main (string[] args) {
206
207         var a = new ValaSource(file);
208         a.create_valac_tree();
209         return 0;
210 }
211 */
212
213