06be35e70de79954e7452abbb309b863abef6d67
[app.Builder.js] / Palete / ValaSource.vala
1
2 // valac TreeBuilder.vala --pkg libvala-0.24 --pkg posix -o /tmp/treebuilder
3
4 namespace Palete {
5         
6         public class ValaSourceReport  : Vala.Report {
7
8
9                 
10
11                 public Gee.HashMap<int,string> line_errors;
12
13                 public ValaSourceReport()
14                 {
15                         base();
16                         this.line_errors = new Gee.HashMap<int,string> ();
17                 }
18                 
19                 public override void err (Vala.SourceReference? source, string message) {
20                         errors++;
21                         if (source == null) {
22                                 return;
23                                 //stderr.printf ("My error: %s\n", message);
24                         }
25                         if (source.file.filename != "~~~~~testfile.vala") {
26                                 return;
27                         }
28                         var pre = "";
29                         if (line_errors.has_key(source.begin.line)) {
30                                 pre = line_errors.get(source.begin.line) + "\n";
31                                 
32                         }
33                         line_errors.set(source.begin.line, pre +  message);
34                 }
35                 public void dump()
36                 {
37                         var iter = this.line_errors.map_iterator();
38                         while (iter.next()) {
39                                 print ("%d : %s\n\n", iter.get_key(), iter.get_value());
40                         }
41                 }
42
43         }
44
45         public class ValaSource : Vala.CodeVisitor {
46
47                 Vala.CodeContext context;
48                 ValaSourceReport report;
49                 JsRender.JsRender file; 
50                 public ValaSource(JsRender.JsRender file) {
51                         base();
52                         this.file = file;
53                         
54
55                 }
56                 public void dumpCode(string str) {
57                         var ls = str.split("\n");
58                         for (var i=0;i < ls.length; i++) {
59                                 print("%d : %s\n", i+1, ls[i]);
60                         }
61                 }
62                 
63                 public Gee.HashMap<int,string> checkFile()
64                 {
65                         return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
66                 }
67
68                 public Gee.HashMap<int,string> checkFileWithNodePropChange(
69                            JsRender.Node node, 
70                            string prop,
71                            string ptype,
72                            string val)
73                 {
74
75                         var hash = ptype == "listener" ? node.listeners : node.props;
76                         var old = hash.get(prop);
77                         var newval = "/*--VALACHECK-START--*/ " + val ;
78                         
79                         hash.set(prop, newval);
80                         var tmpstring = JsRender.NodeToVala.mungeFile(this.file);
81                         var bits = tmpstring.split("/*--VALACHECK-START--*/");
82                         var offset =0;
83                         if (bits.length > 0) {
84                                 offset = bits[0].split("\n").length +1;
85                         }
86                         //this.dumpCode(tmpstring);
87                         //print("offset %d\n", offset);
88                         this.checkString(tmpstring);
89                         hash.set(prop, old);
90                         // modify report
91                         Gee.HashMap<int,string> ret = new Gee.HashMap<int,string> ();
92                         var iter = this.report.line_errors.map_iterator();
93                         while (iter.next()) {
94                                 // print("%d : %s\n",iter.get_key() - offset, iter.get_value());
95                                 // we have to prefix the error with the fake line number 
96                                 // so that it's a unique mark..
97                                  ret.set(iter.get_key() - offset, 
98                                        "%d : %s".printf(iter.get_key() - offset,iter.get_value()));
99                         }
100                         return ret;
101                         
102                 }
103                 
104         public Gee.HashMap<int,string> checkString(string contents)
105        {
106                         // init context:
107
108                         context = new Vala.CodeContext ();
109                         Vala.CodeContext.push (context);
110                 
111                         context.experimental = false;
112                         context.experimental_non_null = false;
113                         context.vapi_directories = { "/usr/share/vala-0.24/vapi" };
114                         context.report.enable_warnings = true;
115                         context.metadata_directories = { };
116                         context.gir_directories = {};
117                         this.report = new ValaSourceReport();;
118                         context.report = this.report;
119                 
120                         context.basedir = Posix.realpath (".");
121                 
122                         context.directory = context.basedir;
123                 
124
125                         // add default packages:
126                         //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
127                                 context.profile = Vala.Profile.GOBJECT;
128                         //      context.add_define ("GOBJECT");
129                         //}
130                         //add_depencies (context, settings.packages);
131                         //if (reporter.errors > 0) {
132                         //      return context;
133                         //}
134
135                         var source_file = new Vala.SourceFile (
136                                 context, 
137                                 Vala.SourceFileType.SOURCE, 
138                                 "~~~~~testfile.vala",
139                                contents
140                         );
141                          //Vala.Config.BUILD_VERSION
142                         context.add_external_package ("glib-2.0");
143                         context.add_external_package ("gobject-2.0");
144                         context.add_external_package ("libvala-0.24");
145                         var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
146                         source_file.add_using_directive (ns_ref);
147                         context.root.add_using_directive (ns_ref);
148                         context.add_source_file (source_file);
149
150                 
151                         //add_documented_files (context, settings.source_files);
152                 
153                         Vala.Parser parser = new Vala.Parser ();
154                         parser.parse (context);
155                         //gir_parser.parse (context);
156                         if (context.report.get_errors () > 0) {
157                                 print("parse got errors");
158                                 ((ValaSourceReport)context.report).dump();
159                                 Vala.CodeContext.pop ();
160                                 return this.report.line_errors;
161                         }
162
163
164                         /*
165                         // check context:
166                         context.check ();
167                         if (context.report.get_errors () > 0) {
168                                 print("check got errors");
169                                 ((ValaSourceReport)context.report).dump();
170                                 Vala.CodeContext.pop ();
171                                 return this.report.line_errors;
172                                 
173                         }
174                         */
175                         Vala.CodeContext.pop ();
176                         
177                         print("ALL OK?\n");
178                         return this.report.line_errors;
179                 }
180         //
181                 // startpoint:
182                 //
183          
184         }
185 }
186 /*
187 int main (string[] args) {
188
189         var a = new ValaSource(file);
190         a.create_valac_tree();
191         return 0;
192 }
193 */
194
195