src/JsRender/NodeToVala.vala
[app.Builder.js] / src / JsRender / NodeToVala.vala
1 /**
2  * 
3  * Code to convert node tree to Javascript...
4  * 
5  * usage : x = (new JsRender.NodeToJs(node)).munge();
6  * 
7  * 
8  * 
9  * 
10  * 
11 */
12
13
14
15
16 public class JsRender.NodeToVala : Object {
17
18         Node node;
19
20         int depth;
21         string inpad;
22         string pad;
23         string ipad;
24         string cls;
25         string xcls;
26         
27         string ret;
28         
29         int cur_line;
30
31         Gee.ArrayList<string> ignoreList;
32         Gee.ArrayList<string> ignoreWrappedList; 
33         Gee.ArrayList<string> myvars;
34         Gee.ArrayList<Node> vitems; // top level items
35         NodeToVala top;
36         JsRender file;
37         
38         /* 
39          * ctor - just initializes things
40          * - wraps a render node 
41          */
42         public NodeToVala( Node node,  int depth, NodeToVala? parent) 
43         {
44
45                 
46                 this.node = node;
47                 this.depth = depth;
48                 this.inpad = string.nfill(depth > 0 ? 4 : 0, ' ');
49                 this.pad = this.inpad + "    ";
50                 this.ipad = this.inpad + "        ";
51                 this.cls = node.xvala_cls;
52                 this.xcls = node.xvala_xcls;
53                 this.ret = "";
54                 this.cur_line = parent == null ? 0 : parent.cur_line;
55                 node.line = this.cur_line;
56                 
57                 this.top = parent == null ? this : parent.top;
58                 this.ignoreList = new Gee.ArrayList<string>();
59                 this.ignoreWrappedList  = new Gee.ArrayList<string>();
60                 this.myvars = new Gee.ArrayList<string>();
61                 this.vitems = new Gee.ArrayList<Node>();
62                 this.file = null;
63         }
64
65         public int vcnt = 0;
66         string toValaNS(Node item)
67         {
68                 var ns = item.get("xns") ;
69                 if (ns == "GtkSource") {
70                         return "Gtk.Source";
71                 }
72                 return ns + ".";
73         }
74         public void  toValaName(Node item, int depth =0) 
75         {
76                 this.vcnt++;
77
78                 var ns =  this.toValaNS(item) ;
79                 var cls = ns + item.get("xtype");
80                 
81                 
82                 item.xvala_cls = cls;
83                 
84                 
85                 string id = item.get("id").length > 0 ?
86                         item.get("id") :  "%s%d".printf(item.get("xtype"), this.vcnt);
87
88                 
89                 
90                 
91                 if (id[0] == '*' || id[0] == '+') {
92                         item.xvala_xcls = "Xcls_" + id.substring(1);
93                 } else {
94                         item.xvala_xcls = "Xcls_" + id;
95                 }
96                         
97                 
98                 item.xvala_id =  id;
99                 if (depth > 0) {                        
100                         this.vitems.add(item);
101                 } else if (!item.props.has_key("id")) {
102                         // use the file name..
103                         item.xvala_xcls =  this.file.name;
104                         // is id used?
105                         item.xvala_id = this.file.name;
106
107                 }
108                 // loop children..
109                                                                                                                            
110                 if (item.items.size < 1) {
111                         return;
112                 }
113                 for(var i =0;i<item.items.size;i++) {
114                         this.toValaName(item.items.get(i), depth+1);
115                 }
116                                           
117         }
118         /**
119          *  Main entry point to convert a file into a string..
120          */
121         public static string mungeFile(JsRender file) 
122         {
123                 if (file.tree == null) {
124                         return "";
125                 }
126
127                 var n = new NodeToVala(file.tree, 0, null);
128                 n.file = file;
129                 n.vcnt = 0;
130                 
131                 n.toValaName(file.tree);
132                 
133                 
134                 GLib.debug("top cls %s / xlcs %s\n ",file.tree.xvala_cls,file.tree.xvala_cls); 
135                 n.cls = file.tree.xvala_cls;
136                 n.xcls = file.tree.xvala_xcls;
137                 return n.munge();
138                 
139
140         }
141         
142         public string munge ( )
143         {
144                 //return this.mungeToString(this.node);
145
146                 this.ignore("pack");
147                 this.ignore("init");
148                 this.ignore("xns");
149                 this.ignore("xtype");
150                 this.ignore("id");
151                 
152                 this.globalVars();
153                 this.classHeader();
154                 this.addSingleton();
155                 this.addTopProperties();
156                 this.addMyVars();
157                 this.addPlusProperties();
158                 this.addValaCtor();
159                 this.addUnderThis();
160                 this.addWrappedCtor();
161
162                 this.addInitMyVars();
163                 this.addWrappedProperties();
164                 this.addChildren();
165                 this.addInit();
166                 this.addListeners();
167                 this.addEndCtor();
168                 this.addUserMethods();
169                 this.iterChildren();
170                 
171                 return this.ret;
172                  
173                          
174         } 
175         public string mungeChild(  Node cnode)
176         {
177                 var x = new  NodeToVala(cnode,  this.depth+1, this);
178                 return x.munge();
179         }
180         public void addLine(string str)
181         {
182                 this.cur_line++;
183                 this.ret += str;
184         }
185         
186         
187         public void globalVars()
188         {
189                 if (this.depth > 0) {
190                         return;
191                 }
192                 // Global Vars..
193                 //this.ret += this.inpad + "public static " + this.xcls + "  " + this.node.xvala_id+ ";\n\n";
194
195                 this.addLine("%sstatic %s _%s;".printf(this.inpad, this.xcls, this.node.xvala_id));
196                 this.addLine("");
197                 //this.ret += this.inpad + "static " + this.xcls + "  _" + this.node.xvala_id+ ";\n\n";
198                                 
199                                 
200         }
201
202         void classHeader()
203         {
204                            
205                         // class header..
206                         // class xxx {   WrappedGtk  el; }
207                         this.ret += inpad + "public class " + this.xcls + " : Object \n" + this.inpad + "{\n";
208                         this.ret +=  this.pad + "public " + this.cls + " el;\n";
209
210                           
211                         this.ret += this.pad + "private " + this.top.xcls + "  _this;\n\n";
212                         
213                         
214                         
215                         // singleton
216         }
217         void addSingleton() 
218         {
219                         if (depth > 0) {
220                         return;
221                 }
222                         this.ret += pad + "public static " + xcls + " singleton()\n" + 
223                                 this.pad + "{\n" +
224                                 this.ipad + "if (_" + this.node.xvala_id  + " == null) {\n" +
225                                 this.ipad + "    _" + this.node.xvala_id + "= new "+ this.xcls + "();\n" + // what about args?
226                         this.ipad + "}\n" +
227                         this.ipad + "return _" + this.node.xvala_id +";\n" + 
228                                 this.pad + "}\n";
229         }
230                         
231
232         void addTopProperties()
233         {
234                 if (this.depth > 0) {
235                         return;
236                 }
237                 // properties - global..??
238
239                 var iter = this.vitems.list_iterator();
240                 while(iter.next()) {
241                         var n = iter.get();
242
243                          
244                                         if (!n.props.has_key("id") || n.xvala_id.length < 0) {
245                                                 continue;
246                                                 
247                                         }
248                                         if (n.xvala_id[0] == '*') {
249                                                 continue;
250                                         }
251                                         if (n.xvala_id[0] == '+') {
252                                                 continue;
253                                         }
254                                         this.ret += this.pad + "public " + n.xvala_xcls + " " + n.xvala_id + ";\n";
255                                 }
256                                 
257         }
258          
259                 void addMyVars()
260         {
261                 this.ret += "\n" + this.ipad + "// my vars (def)\n";
262                         
263
264  
265                 var cls = Palete.Gir.factoryFqn(this.node.fqn());
266                    
267                 if (cls == null) {
268                         return;
269                 }
270           
271                 
272                         // Key = TYPE:name
273                 var iter = this.node.props.map_iterator();
274                 while (iter.next()) {
275                                 var k = iter.get_key();
276                         if (this.shouldIgnore(k)) {
277                                 continue;
278                         }
279                         var vv = k.strip().split(" ");
280                         // user defined method
281                         if (vv[0] == "|") {
282                                 continue;
283                         }
284                         if (vv[0] == "*") {
285                                 continue;
286                         }
287                                 
288                                 if (vv[0] == "@") {
289                                         this.ret += this.pad + "public signal" + k.substring(1)  + " "  + iter.get_value() + ";\n";
290                                 this.ignore(k);
291                                 continue;
292                                 }
293                         var min = (vv[0] == "$" || vv[0] == "#") ? 3 : 2; 
294                         if (vv.length < min) {
295                                 // skip 'old js style properties without a type'
296                                 continue;
297                         }
298                         
299                         var kname = vv[vv.length-1];
300
301                         if (this.shouldIgnore(kname)) {
302                                 continue;
303                         }
304                         
305                         // is it a class property...
306                         if (cls.props.has_key(kname) && vv[0] != "#") {
307                                 continue;
308                         }
309                         
310                         this.myvars.add(k);
311
312                                 
313                         this.ret += this.pad + "public " + 
314                                 (k[0] == '$' || k[0] == '#' ? k.substring(2) : k ) + ";\n";
315                                 
316                         this.ignore(k);
317                         
318                                 
319                 }
320         }
321         
322                         // if id of child is '+' then it's a property of this..
323                 void addPlusProperties()
324         {
325                         if (this.node.items.size < 1) {
326                           return;
327                 }
328                 var iter = this.node.items.list_iterator();
329                 while (iter.next()) {
330                         var ci = iter.get();
331                                         
332                                         if (ci.xvala_id[0] != '+') {
333                                                 continue; // skip generation of children?
334                                                 
335                                         }
336                                         this.ret += this.pad + "public " + ci.xvala_xcls + " " + ci.xvala_id.substring(1) + ";\n";
337                                                            
338                                         
339                                 }
340         }
341
342         void addValaCtor()
343         {
344                         
345                         
346                         // .vala props.. 
347                         
348                         string[] cargs = {};
349                         var cargs_str = "";
350                         // ctor..
351                         this.ret += "\n" + this.pad + "// ctor \n";
352                         if (this.node.has("* args")) {
353                                 // not sure what this is supposed to be ding..
354                         
355                                 cargs_str = ", " + this.node.get("* args");
356                                 //var ar = this.node.get("* args");.split(",");
357                                 //for (var ari =0; ari < ar.length; ari++) {
358                                         //      cargs +=  (ar[ari].trim().split(" ").pop();
359                                           // }
360                                 }
361                 
362                         if (this.depth < 1) {
363                                 this.ret += this.pad + "public " + this.xcls + "(" + 
364                                         cargs_str +")\n" + this.pad + "{\n";
365                         } else {
366                                         
367                                                 //code 
368                                         
369                                 this.ret+= this.pad + "public " + this.xcls + "(" + 
370                                         this.top.xcls + " _owner " + cargs_str + ")\n" + this.pad + "{\n";
371                         }
372                         
373
374         }
375         void addUnderThis() 
376         {
377                         // public static?
378                         if (depth < 1) {
379                         this.ret += this.ipad + "_this = this;\n";
380                         return;
381                 }
382                 this.ret+= this.ipad + "_this = _owner;\n";
383
384                 if (this.node.props.has_key("id")
385                         &&
386                         this.node.xvala_id != "" 
387                         && 
388                         this.node.xvala_id[0] != '*' 
389                         && 
390                         this.node.xvala_id[0] != '+' 
391                         ) {
392                                 this.ret+= this.ipad + "_this." + node.xvala_id  + " = this;\n";
393                    
394                 }
395                                 
396                                 
397    
398         }
399
400         void addWrappedCtor()
401         {
402                 // wrapped ctor..
403                 // this may need to look up properties to fill in the arguments..
404                 // introspection does not workk..... - as things like gtkmessagedialog
405                 /*
406                 if (cls == 'Gtk.Table') {
407
408                 var methods = this.palete.getPropertiesFor(cls, 'methods');
409
410                 print(JSON.stringify(this.palete.proplist[cls], null,4));
411                 Seed.quit();
412                 }
413                 */
414                 if (this.node.has("* ctor")) {
415                         
416                         
417                                 this.ret +=  this.ipad + "this.el = " + this.node.get("* ctor")+ ";\n";
418                         return;
419                 }
420                 // the ctor arguments...
421
422                 // see what the 
423                 //var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".newv");
424                 //if (default_ctor == null) {
425                         var  default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".new");
426
427                 //}
428                 if (default_ctor != null && default_ctor.paramset != null && default_ctor.paramset.params.size > 0) {
429                         string[] args  = {};
430                         var iter =default_ctor.paramset.params.list_iterator();
431                         while (iter.next()) {
432                                 var n = iter.get().name;
433                                 if (!this.node.has(n)) {
434
435                                         if (iter.get().type.contains("int")) {
436                                                 args += "0";
437                                                 continue;
438                                         }
439                                         if (iter.get().type.contains("float")) {
440                                                 args += "0f";
441                                                 continue;
442                                         }
443                                         if (iter.get().type.contains("bool")) {
444                                                 args += "true"; // always default to true?
445                                                 continue;
446                                         }
447                                         // any other types???
448                                         
449                                         args += "null";
450                                         continue;
451                                 }
452                                 this.ignoreWrapped(n);
453                                 this.ignore(n);
454                                 
455                                 var v = this.node.get(n);
456
457                                 if (iter.get().type == "string") {
458                                         v = "\"" +  v.escape("") + "\"";
459                                 }
460                                 if (v == "TRUE" || v == "FALSE") {
461                                         v = v.down();
462                                 }
463
464                                 
465                                 args += v;
466
467                         }
468                         this.ret += this.ipad + "this.el = new " + cls + "( "+ string.joinv(", ",args) + " );\n" ;
469                         return;
470                         
471                 }
472                 
473                 
474                                 this.ret += this.ipad + "this.el = new " + this.cls + "();\n";
475
476                         
477         }
478
479         void addInitMyVars()
480         {
481                         //var meths = this.palete.getPropertiesFor(item['|xns'] + '.' + item.xtype, 'methods');
482                         //print(JSON.stringify(meths,null,4));Seed.quit();
483                         
484                         
485                         
486                         // initialize.. my vars..
487                 this.ret += "\n" + this.ipad + "// my vars (dec)\n";
488                 
489                 var iter = this.myvars.list_iterator();
490                 while(iter.next()) {
491                         
492                                 var k = iter.get();
493                         
494                                 var ar  = k.strip().split(" ");
495                         var kname = ar[ar.length-1];
496                         
497                                 var v = this.node.props.get(k);
498                         // ignore signals.. 
499                                 if (v.length < 1) {
500                                                 continue; 
501                                 }
502                         if (v == "FALSE" || v == "TRUE") {
503                                 v = v.down();
504                         }
505 //FIXME -- check for raw string.. "string XXXX"
506                         
507                         // if it's a string...
508                         
509                                 this.ret += this.ipad + "this." + kname + " = " +   v +";\n";
510                         }
511         }
512
513         
514
515
516         
517         void addWrappedProperties()
518         {
519                 var cls = Palete.Gir.factoryFqn(this.node.fqn());
520                 if (cls == null) {
521                         return;
522                 }
523                         // what are the properties of this class???
524                 this.ret += "\n" + this.ipad + "// set gobject values\n";
525
526                 var iter = cls.props.map_iterator();
527                 while (iter.next()) {
528                         var p = iter.get_key();
529                         print("Check Write %s\n", p);
530                         if (!this.node.has(p)) {
531                                 continue;
532                         }
533                         if (this.shouldIgnoreWrapped(p)) {
534                                 continue;
535                         }
536                         
537                                 this.ignore(p);
538                         var v = this.node.get(p);
539
540                         var nodekey = this.node.get_key(p);
541
542                         // user defined properties.
543                         if (nodekey[0] == '#') {
544                                 continue;
545                         }
546                                 
547
548                         
549                         var is_raw = nodekey[0] == '$';
550                         
551                         // what's the type.. - if it's a string.. then we quote it..
552                         if (iter.get_value().type == "string" && !is_raw) {
553                                  v = "\"" +  v.escape("") + "\"";
554                         }
555                         if (v == "TRUE" || v == "FALSE") {
556                                 v = v.down();
557                         }
558                         if (iter.get_value().type == "float" && v[v.length-1] != 'f') {
559                                 v += "f";
560                         }
561                         
562                         
563                         this.ret += "%sthis.el.%s = %s;\n".printf(ipad,p,v); // // %s,  iter.get_value().type);
564                                         
565                            // got a property..
566                            
567
568                 }
569                 
570         }
571
572         void addChildren()
573         {
574                                 //code
575                 if (this.node.items.size < 1) {
576                         return;
577                 }
578                          
579                         var iter = this.node.items.list_iterator();
580                 var i = -1;
581                 while (iter.next()) {
582                         i++;
583                                 
584                                         var ci = iter.get();
585
586                         if (ci.xvala_id[0] == '*') {
587                                                 continue; // skip generation of children?
588                                         }
589                                         
590                                         var xargs = "";
591                                         if (ci.has("* args")) {
592                                                 
593                                                 var ar = ci.get("* args").split(",");
594                                                 for (var ari = 0 ; ari < ar.length; ari++ ) {
595                                         var arg = ar[ari].split(" ");
596                                                                 xargs += "," + arg[arg.length -1];
597                                                 }
598                                         }
599                                         
600                                         this.ret += this.ipad + "var child_" + "%d".printf(i) + " = new " + ci.xvala_xcls +
601                                         "( _this " + xargs + ");\n" ;
602                                         
603                                         this.ret+= this.ipad + "child_" + "%d".printf(i) +".ref();\n"; // we need to reference increase unnamed children...
604                                         
605                                         if (ci.has("* prop")) {
606                                                 this.ret+= ipad + "this.el." + ci.get("* prop") + " = child_" + "%d".printf(i) + ".el;\n";
607                                                 continue;
608                                         }
609
610                         // not sure why we have 'true' in pack?!?
611                                         if (!ci.has("pack") || ci.get("pack").down() == "false" || ci.get("pack").down() == "true") {
612                                                 continue;
613                                         }
614                                         
615                                         string[]  packing =  { "add" };
616                         if (ci.has("pack")) {
617                                 packing = ci.get("pack").split(",");
618                         }
619                                         
620                                         var pack = packing[0];
621                         this.ret += this.ipad + "this.el." + pack.strip() + " (  child_" + "%d".printf(i) + ".el " +
622                                                            (packing.length > 1 ? 
623                                                                 (", " + string.joinv(",", packing).substring(pack.length+1))
624                                                         :
625                                                                         ""
626                                                                 ) + " );\n";
627                         
628                                                           
629                                         if (ci.xvala_id[0] != '+') {
630                                                 continue; // skip generation of children?
631                                                                 
632                                         }
633                                         this.ret+= this.ipad + "this." + ci.xvala_id.substring(1) + " =  child_" + "%d".printf(i) +  ";\n";
634                                                   
635                 }
636         }
637
638         void addInit()
639         {
640
641                 
642                 if (!this.node.has("init")) {
643                                 return;
644                 }
645                 this.ret+= "\n" + ipad + "// init method \n";
646         
647                 this.ret+= "\n" + ipad + this.padMultiline(ipad, this.node.get("init")) + "\n";
648
649          }
650          void addListeners()
651          {
652                 if (this.node.listeners.size < 1) {
653                         return;
654                 }
655                                 
656                         
657                         
658                 this.ret+= "\n" + ipad + "// listeners \n";
659
660                 var iter = this.node.listeners.map_iterator();
661                 while (iter.next()) {
662                         var k = iter.get_key();
663                         var v = iter.get_value();
664                                         this.ret+= this.ipad + "this.el." + k + ".connect( " + 
665                                         this.padMultiline(this.ipad,v) +");\n"; 
666                                         
667                                 }
668         }    
669                 void addEndCtor()
670         {
671                         
672                         
673                         
674                         // end ctor..
675                         this.ret+= this.pad + "}\n";
676         }
677
678
679         /*
680  * Standardize this crap...
681  * 
682  * standard properties (use to set)
683  *          If they are long values show the dialog..
684  *
685  * someprop : ....
686  * bool is_xxx  :: can show a pulldown.. (true/false)
687  * string html  
688  * $ string html  = string with value interpolated eg. baseURL + ".." 
689  *  Clutter.ActorAlign x_align  (typed)  -- shows pulldowns if type is ENUM? 
690  * $ untypedvalue = javascript untyped value...  
691  * _ string html ... = translatable..
692
693  * 
694  * object properties (not part of the GOjbect being wrapped?
695  * # Gee.ArrayList<Xcls_fileitem> fileitems
696  * 
697  * signals
698  * @ void open 
699  * 
700  * methods -- always text editor..
701  * | void clearFiles
702  * | someJSmethod
703  * 
704  * specials
705  * * prop -- string
706  * * args  -- string
707  * * ctor -- string
708  * * init -- big string?
709  * 
710  * event handlers (listeners)
711  *   just shown 
712  * 
713  * -----------------
714  * special ID values
715  *  +XXXX -- indicates it's a instance property / not glob...
716  *  *XXXX -- skip writing glob property (used as classes that can be created...)
717  * 
718  * 
719  */
720          
721         void addUserMethods()
722         {
723                         
724                 this.ret+= "\n" + pad + "// user defined functions \n";  
725                         
726                         // user defined functions...
727                 var iter = this.node.props.map_iterator();
728                 while(iter.next()) {
729                                 var k = iter.get_key();
730                         if (this.shouldIgnore(k)) {
731                                 continue;
732                         }
733                         // HOW TO DETERIME if its a method?            
734                                 if (k[0] != '|') {
735                                                 //strbuilder("\n" + pad + "// skip " + k + " - not pipe \n"); 
736                                                 continue;
737                         }       
738                                 // function in the format of {type} (args) { .... }
739                                 var kk = k.substring(2);
740                                 var vv = iter.get_value();
741                                 this.ret += this.pad + "public " + kk + " " + this.padMultiline(this.pad, vv) + "\n";
742                         
743                                 
744                         }
745         }
746
747         void iterChildren()
748         {
749                         
750                         if (this.depth > 0) {
751                         this.ret+= this.inpad + "}\n";
752                         }
753                 
754                 var iter = this.node.items.list_iterator();
755                 var i = -1;
756                 while (iter.next()) {
757                                 this.ret += this.mungeChild(iter.get());
758                 }
759                          
760                         if (this.depth < 1) {
761                                 this.ret+= this.inpad + "}\n";
762                         }
763                         
764                 }
765
766         string padMultiline(string pad, string str)
767         {
768                 var ar = str.strip().split("\n");
769                 return string.joinv("\n" + pad , ar);
770         }
771         
772         void ignore(string i) {
773                 this.ignoreList.add(i);
774                 
775         }
776         void ignoreWrapped(string i) {
777                 this.ignoreWrappedList.add(i);
778                 
779         }
780         bool shouldIgnore(string i)
781         {
782                 return ignoreList.contains(i);
783         }
784         bool shouldIgnoreWrapped(string i)
785         {
786                 return ignoreWrappedList.contains(i);
787         }
788         
789 }
790         
791          
792         
793         
794
795