49ab8c73c814fe133f5aaa29d1932165185431cc
[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 Gee.HashMap<int,string> checkFile()
57                 {
58                         return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
59                 }
60
61                 public Gee.HashMap<int,string> checkFileWithNodePropChange(
62                            JsRender.Node node, 
63                            string prop, string val)
64                 {
65                         var old = node.props.get(prop);
66                         var newval = "/*--VALACHECK-START--*/ " + val + " /*--VALACHECK-START--*/";
67                         node.props.set(prop, newval);
68                         var tmpstring = JsRender.NodeToVala.mungeFile(this.file);
69                         var bits = tmpstring.split(newval);
70                         var offset =0;
71                         if (bits.length > 1) {
72                                 offset = bits[0].split("\n").length;
73                         }
74                         this.checkString(tmpstring);
75                         node.props.set(prop, old);
76                         // modify report
77                         Gee.HashMap<int,string> ret = new Gee.HashMap<int,string> ();
78                         var iter = this.report.line_errors.map_iterator();
79                         while (iter.next()) {
80                                  ret.set(iter.get_key() - offset, iter.get_value());
81                         }
82                         return ret;
83                         
84                 }
85                 
86         public Gee.HashMap<int,string> checkString(string contents)
87        {
88                         // init context:
89
90                         context = new Vala.CodeContext ();
91                         Vala.CodeContext.push (context);
92                 
93                         context.experimental = false;
94                         context.experimental_non_null = false;
95                         context.vapi_directories = { "/usr/share/vala-0.24/vapi" };
96                         context.report.enable_warnings = true;
97                         context.metadata_directories = { };
98                         context.gir_directories = {};
99                         this.report = new ValaSourceReport();;
100                         context.report = this.report;
101                 
102                         context.basedir = Posix.realpath (".");
103                 
104                         context.directory = context.basedir;
105                 
106
107                         // add default packages:
108                         //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
109                                 context.profile = Vala.Profile.GOBJECT;
110                         //      context.add_define ("GOBJECT");
111                         //}
112                         //add_depencies (context, settings.packages);
113                         //if (reporter.errors > 0) {
114                         //      return context;
115                         //}
116
117                         var source_file = new Vala.SourceFile (
118                                 context, 
119                                 Vala.SourceFileType.SOURCE, 
120                                 "~~~~~testfile.vala",
121                                contents
122                         );
123                          
124                         context.add_external_package ("glib-2.0");
125                         context.add_external_package ("gobject-2.0");
126                         context.add_external_package ("libvala-0.24");
127                         var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
128                         source_file.add_using_directive (ns_ref);
129                         context.root.add_using_directive (ns_ref);
130                         context.add_source_file (source_file);
131
132                 
133                         //add_documented_files (context, settings.source_files);
134                 
135                         Vala.Parser parser = new Vala.Parser ();
136                         parser.parse (context);
137                         //gir_parser.parse (context);
138                         if (context.report.get_errors () > 0) {
139                                 print("parse got errors");
140                                 ((ValaSourceReport)context.report).dump();
141                                 Vala.CodeContext.pop ();
142                                 return this.report.line_errors;
143                         }
144
145
146
147                         // check context:
148                         context.check ();
149                         if (context.report.get_errors () > 0) {
150                                 print("check got errors");
151                                 ((ValaSourceReport)context.report).dump();
152                                 Vala.CodeContext.pop ();
153                                 return this.report.line_errors;
154                                 
155                         }
156                         Vala.CodeContext.pop ();
157                         print("ALL OK?\n");
158                         return this.report.line_errors;
159                 }
160         //
161                 // startpoint:
162                 //
163          
164         }
165 }
166 /*
167 int main (string[] args) {
168
169         var a = new ValaSource(file);
170         a.create_valac_tree();
171         return 0;
172 }
173 */
174
175