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 + "\n";
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.addLine(inpad + "public class " + this.xcls + " : Object \n" + this.inpad + "{");
208                         
209                         this.addLine(this.pad + "public " + this.cls + " el;");
210
211                           
212                         this.addLine(this.pad + "private " + this.top.xcls + "  _this;)";
213                         this.addLine();
214                         
215                         
216                         
217                         // singleton
218         }
219         void addSingleton() 
220         {
221                 if (depth > 0) {
222                         return;
223                 }
224                 this.addLine(pad + "public static " + xcls + " singleton()");
225                 this.addLine(this.pad + "{")
226                 this.addLine(this.ipad +    "if (_" + this.node.xvala_id  + " == null) {");
227                 this.addLine(this.ipad +    "    _" + this.node.xvala_id + "= new "+ this.xcls + "();");  // what about args?
228                 this.addLine(this.ipad +    "}");
229                 this.addLine(this.ipad +    "return _" + this.node.xvala_id +";");
230                 this.addLine(this.pad + "}");
231         }
232                         
233
234         void addTopProperties()
235         {
236                 if (this.depth > 0) {
237                         return;
238                 }
239                 // properties - global..??
240
241                 var iter = this.vitems.list_iterator();
242                 while(iter.next()) {
243                         var n = iter.get();
244
245                          
246                                         if (!n.props.has_key("id") || n.xvala_id.length < 0) {
247                                                 continue;
248                                                 
249                                         }
250                                         if (n.xvala_id[0] == '*') {
251                                                 continue;
252                                         }
253                                         if (n.xvala_id[0] == '+') {
254                                                 continue;
255                                         }
256                                         this.addLine(this.pad + "public " + n.xvala_xcls + " " + n.xvala_id + ";");
257                                         
258                                 }
259                                 
260         }
261          
262                 void addMyVars()
263         {
264                 this.ret += "\n" + this.ipad + "// my vars (def)\n";
265                         
266
267  
268                 var cls = Palete.Gir.factoryFqn(this.node.fqn());
269                    
270                 if (cls == null) {
271                         return;
272                 }
273           
274                 
275                         // Key = TYPE:name
276                 var iter = this.node.props.map_iterator();
277                 while (iter.next()) {
278                                 var k = iter.get_key();
279                         if (this.shouldIgnore(k)) {
280                                 continue;
281                         }
282                         var vv = k.strip().split(" ");
283                         // user defined method
284                         if (vv[0] == "|") {
285                                 continue;
286                         }
287                         if (vv[0] == "*") {
288                                 continue;
289                         }
290                                 
291                                 if (vv[0] == "@") {
292                                         this.ret += this.pad + "public signal" + k.substring(1)  + " "  + iter.get_value() + ";\n";
293                                 this.ignore(k);
294                                 continue;
295                                 }
296                         var min = (vv[0] == "$" || vv[0] == "#") ? 3 : 2; 
297                         if (vv.length < min) {
298                                 // skip 'old js style properties without a type'
299                                 continue;
300                         }
301                         
302                         var kname = vv[vv.length-1];
303
304                         if (this.shouldIgnore(kname)) {
305                                 continue;
306                         }
307                         
308                         // is it a class property...
309                         if (cls.props.has_key(kname) && vv[0] != "#") {
310                                 continue;
311                         }
312                         
313                         this.myvars.add(k);
314
315                                 
316                         this.ret += this.pad + "public " + 
317                                 (k[0] == '$' || k[0] == '#' ? k.substring(2) : k ) + ";\n";
318                                 
319                         this.ignore(k);
320                         
321                                 
322                 }
323         }
324         
325                         // if id of child is '+' then it's a property of this..
326                 void addPlusProperties()
327         {
328                         if (this.node.items.size < 1) {
329                           return;
330                 }
331                 var iter = this.node.items.list_iterator();
332                 while (iter.next()) {
333                         var ci = iter.get();
334                                         
335                                         if (ci.xvala_id[0] != '+') {
336                                                 continue; // skip generation of children?
337                                                 
338                                         }
339                                         this.ret += this.pad + "public " + ci.xvala_xcls + " " + ci.xvala_id.substring(1) + ";\n";
340                                                            
341                                         
342                                 }
343         }
344
345         void addValaCtor()
346         {
347                         
348                         
349                         // .vala props.. 
350                         
351                         string[] cargs = {};
352                         var cargs_str = "";
353                         // ctor..
354                         this.ret += "\n" + this.pad + "// ctor \n";
355                         if (this.node.has("* args")) {
356                                 // not sure what this is supposed to be ding..
357                         
358                                 cargs_str = ", " + this.node.get("* args");
359                                 //var ar = this.node.get("* args");.split(",");
360                                 //for (var ari =0; ari < ar.length; ari++) {
361                                         //      cargs +=  (ar[ari].trim().split(" ").pop();
362                                           // }
363                                 }
364                 
365                         if (this.depth < 1) {
366                                 this.ret += this.pad + "public " + this.xcls + "(" + 
367                                         cargs_str +")\n" + this.pad + "{\n";
368                         } else {
369                                         
370                                                 //code 
371                                         
372                                 this.ret+= this.pad + "public " + this.xcls + "(" + 
373                                         this.top.xcls + " _owner " + cargs_str + ")\n" + this.pad + "{\n";
374                         }
375                         
376
377         }
378         void addUnderThis() 
379         {
380                         // public static?
381                         if (depth < 1) {
382                         this.ret += this.ipad + "_this = this;\n";
383                         return;
384                 }
385                 this.ret+= this.ipad + "_this = _owner;\n";
386
387                 if (this.node.props.has_key("id")
388                         &&
389                         this.node.xvala_id != "" 
390                         && 
391                         this.node.xvala_id[0] != '*' 
392                         && 
393                         this.node.xvala_id[0] != '+' 
394                         ) {
395                                 this.ret+= this.ipad + "_this." + node.xvala_id  + " = this;\n";
396                    
397                 }
398                                 
399                                 
400    
401         }
402
403         void addWrappedCtor()
404         {
405                 // wrapped ctor..
406                 // this may need to look up properties to fill in the arguments..
407                 // introspection does not workk..... - as things like gtkmessagedialog
408                 /*
409                 if (cls == 'Gtk.Table') {
410
411                 var methods = this.palete.getPropertiesFor(cls, 'methods');
412
413                 print(JSON.stringify(this.palete.proplist[cls], null,4));
414                 Seed.quit();
415                 }
416                 */
417                 if (this.node.has("* ctor")) {
418                         
419                         
420                                 this.ret +=  this.ipad + "this.el = " + this.node.get("* ctor")+ ";\n";
421                         return;
422                 }
423                 // the ctor arguments...
424
425                 // see what the 
426                 //var default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".newv");
427                 //if (default_ctor == null) {
428                         var  default_ctor = Palete.Gir.factoryFqn(this.node.fqn() + ".new");
429
430                 //}
431                 if (default_ctor != null && default_ctor.paramset != null && default_ctor.paramset.params.size > 0) {
432                         string[] args  = {};
433                         var iter =default_ctor.paramset.params.list_iterator();
434                         while (iter.next()) {
435                                 var n = iter.get().name;
436                                 if (!this.node.has(n)) {
437
438                                         if (iter.get().type.contains("int")) {
439                                                 args += "0";
440                                                 continue;
441                                         }
442                                         if (iter.get().type.contains("float")) {
443                                                 args += "0f";
444                                                 continue;
445                                         }
446                                         if (iter.get().type.contains("bool")) {
447                                                 args += "true"; // always default to true?
448                                                 continue;
449                                         }
450                                         // any other types???
451                                         
452                                         args += "null";
453                                         continue;
454                                 }
455                                 this.ignoreWrapped(n);
456                                 this.ignore(n);
457                                 
458                                 var v = this.node.get(n);
459
460                                 if (iter.get().type == "string") {
461                                         v = "\"" +  v.escape("") + "\"";
462                                 }
463                                 if (v == "TRUE" || v == "FALSE") {
464                                         v = v.down();
465                                 }
466
467                                 
468                                 args += v;
469
470                         }
471                         this.ret += this.ipad + "this.el = new " + cls + "( "+ string.joinv(", ",args) + " );\n" ;
472                         return;
473                         
474                 }
475                 
476                 
477                                 this.ret += this.ipad + "this.el = new " + this.cls + "();\n";
478
479                         
480         }
481
482         void addInitMyVars()
483         {
484                         //var meths = this.palete.getPropertiesFor(item['|xns'] + '.' + item.xtype, 'methods');
485                         //print(JSON.stringify(meths,null,4));Seed.quit();
486                         
487                         
488                         
489                         // initialize.. my vars..
490                 this.ret += "\n" + this.ipad + "// my vars (dec)\n";
491                 
492                 var iter = this.myvars.list_iterator();
493                 while(iter.next()) {
494                         
495                                 var k = iter.get();
496                         
497                                 var ar  = k.strip().split(" ");
498                         var kname = ar[ar.length-1];
499                         
500                                 var v = this.node.props.get(k);
501                         // ignore signals.. 
502                                 if (v.length < 1) {
503                                                 continue; 
504                                 }
505                         if (v == "FALSE" || v == "TRUE") {
506                                 v = v.down();
507                         }
508 //FIXME -- check for raw string.. "string XXXX"
509                         
510                         // if it's a string...
511                         
512                                 this.ret += this.ipad + "this." + kname + " = " +   v +";\n";
513                         }
514         }
515
516         
517
518
519         
520         void addWrappedProperties()
521         {
522                 var cls = Palete.Gir.factoryFqn(this.node.fqn());
523                 if (cls == null) {
524                         return;
525                 }
526                         // what are the properties of this class???
527                 this.ret += "\n" + this.ipad + "// set gobject values\n";
528
529                 var iter = cls.props.map_iterator();
530                 while (iter.next()) {
531                         var p = iter.get_key();
532                         print("Check Write %s\n", p);
533                         if (!this.node.has(p)) {
534                                 continue;
535                         }
536                         if (this.shouldIgnoreWrapped(p)) {
537                                 continue;
538                         }
539                         
540                                 this.ignore(p);
541                         var v = this.node.get(p);
542
543                         var nodekey = this.node.get_key(p);
544
545                         // user defined properties.
546                         if (nodekey[0] == '#') {
547                                 continue;
548                         }
549                                 
550
551                         
552                         var is_raw = nodekey[0] == '$';
553                         
554                         // what's the type.. - if it's a string.. then we quote it..
555                         if (iter.get_value().type == "string" && !is_raw) {
556                                  v = "\"" +  v.escape("") + "\"";
557                         }
558                         if (v == "TRUE" || v == "FALSE") {
559                                 v = v.down();
560                         }
561                         if (iter.get_value().type == "float" && v[v.length-1] != 'f') {
562                                 v += "f";
563                         }
564                         
565                         
566                         this.ret += "%sthis.el.%s = %s;\n".printf(ipad,p,v); // // %s,  iter.get_value().type);
567                                         
568                            // got a property..
569                            
570
571                 }
572                 
573         }
574
575         void addChildren()
576         {
577                                 //code
578                 if (this.node.items.size < 1) {
579                         return;
580                 }
581                          
582                         var iter = this.node.items.list_iterator();
583                 var i = -1;
584                 while (iter.next()) {
585                         i++;
586                                 
587                                         var ci = iter.get();
588
589                         if (ci.xvala_id[0] == '*') {
590                                                 continue; // skip generation of children?
591                                         }
592                                         
593                                         var xargs = "";
594                                         if (ci.has("* args")) {
595                                                 
596                                                 var ar = ci.get("* args").split(",");
597                                                 for (var ari = 0 ; ari < ar.length; ari++ ) {
598                                         var arg = ar[ari].split(" ");
599                                                                 xargs += "," + arg[arg.length -1];
600                                                 }
601                                         }
602                                         
603                                         this.ret += this.ipad + "var child_" + "%d".printf(i) + " = new " + ci.xvala_xcls +
604                                         "( _this " + xargs + ");\n" ;
605                                         
606                                         this.ret+= this.ipad + "child_" + "%d".printf(i) +".ref();\n"; // we need to reference increase unnamed children...
607                                         
608                                         if (ci.has("* prop")) {
609                                                 this.ret+= ipad + "this.el." + ci.get("* prop") + " = child_" + "%d".printf(i) + ".el;\n";
610                                                 continue;
611                                         }
612
613                         // not sure why we have 'true' in pack?!?
614                                         if (!ci.has("pack") || ci.get("pack").down() == "false" || ci.get("pack").down() == "true") {
615                                                 continue;
616                                         }
617                                         
618                                         string[]  packing =  { "add" };
619                         if (ci.has("pack")) {
620                                 packing = ci.get("pack").split(",");
621                         }
622                                         
623                                         var pack = packing[0];
624                         this.ret += this.ipad + "this.el." + pack.strip() + " (  child_" + "%d".printf(i) + ".el " +
625                                                            (packing.length > 1 ? 
626                                                                 (", " + string.joinv(",", packing).substring(pack.length+1))
627                                                         :
628                                                                         ""
629                                                                 ) + " );\n";
630                         
631                                                           
632                                         if (ci.xvala_id[0] != '+') {
633                                                 continue; // skip generation of children?
634                                                                 
635                                         }
636                                         this.ret+= this.ipad + "this." + ci.xvala_id.substring(1) + " =  child_" + "%d".printf(i) +  ";\n";
637                                                   
638                 }
639         }
640
641         void addInit()
642         {
643
644                 
645                 if (!this.node.has("init")) {
646                                 return;
647                 }
648                 this.ret+= "\n" + ipad + "// init method \n";
649         
650                 this.ret+= "\n" + ipad + this.padMultiline(ipad, this.node.get("init")) + "\n";
651
652          }
653          void addListeners()
654          {
655                 if (this.node.listeners.size < 1) {
656                         return;
657                 }
658                                 
659                         
660                         
661                 this.ret+= "\n" + ipad + "// listeners \n";
662
663                 var iter = this.node.listeners.map_iterator();
664                 while (iter.next()) {
665                         var k = iter.get_key();
666                         var v = iter.get_value();
667                                         this.ret+= this.ipad + "this.el." + k + ".connect( " + 
668                                         this.padMultiline(this.ipad,v) +");\n"; 
669                                         
670                                 }
671         }    
672                 void addEndCtor()
673         {
674                         
675                         
676                         
677                         // end ctor..
678                         this.ret+= this.pad + "}\n";
679         }
680
681
682         /*
683  * Standardize this crap...
684  * 
685  * standard properties (use to set)
686  *          If they are long values show the dialog..
687  *
688  * someprop : ....
689  * bool is_xxx  :: can show a pulldown.. (true/false)
690  * string html  
691  * $ string html  = string with value interpolated eg. baseURL + ".." 
692  *  Clutter.ActorAlign x_align  (typed)  -- shows pulldowns if type is ENUM? 
693  * $ untypedvalue = javascript untyped value...  
694  * _ string html ... = translatable..
695
696  * 
697  * object properties (not part of the GOjbect being wrapped?
698  * # Gee.ArrayList<Xcls_fileitem> fileitems
699  * 
700  * signals
701  * @ void open 
702  * 
703  * methods -- always text editor..
704  * | void clearFiles
705  * | someJSmethod
706  * 
707  * specials
708  * * prop -- string
709  * * args  -- string
710  * * ctor -- string
711  * * init -- big string?
712  * 
713  * event handlers (listeners)
714  *   just shown 
715  * 
716  * -----------------
717  * special ID values
718  *  +XXXX -- indicates it's a instance property / not glob...
719  *  *XXXX -- skip writing glob property (used as classes that can be created...)
720  * 
721  * 
722  */
723          
724         void addUserMethods()
725         {
726                         
727                 this.ret+= "\n" + pad + "// user defined functions \n";  
728                         
729                         // user defined functions...
730                 var iter = this.node.props.map_iterator();
731                 while(iter.next()) {
732                                 var k = iter.get_key();
733                         if (this.shouldIgnore(k)) {
734                                 continue;
735                         }
736                         // HOW TO DETERIME if its a method?            
737                                 if (k[0] != '|') {
738                                                 //strbuilder("\n" + pad + "// skip " + k + " - not pipe \n"); 
739                                                 continue;
740                         }       
741                                 // function in the format of {type} (args) { .... }
742                                 var kk = k.substring(2);
743                                 var vv = iter.get_value();
744                                 this.ret += this.pad + "public " + kk + " " + this.padMultiline(this.pad, vv) + "\n";
745                         
746                                 
747                         }
748         }
749
750         void iterChildren()
751         {
752                         
753                         if (this.depth > 0) {
754                         this.ret+= this.inpad + "}\n";
755                         }
756                 
757                 var iter = this.node.items.list_iterator();
758                 var i = -1;
759                 while (iter.next()) {
760                                 this.ret += this.mungeChild(iter.get());
761                 }
762                          
763                         if (this.depth < 1) {
764                                 this.ret+= this.inpad + "}\n";
765                         }
766                         
767                 }
768
769         string padMultiline(string pad, string str)
770         {
771                 var ar = str.strip().split("\n");
772                 return string.joinv("\n" + pad , ar);
773         }
774         
775         void ignore(string i) {
776                 this.ignoreList.add(i);
777                 
778         }
779         void ignoreWrapped(string i) {
780                 this.ignoreWrappedList.add(i);
781                 
782         }
783         bool shouldIgnore(string i)
784         {
785                 return ignoreList.contains(i);
786         }
787         bool shouldIgnoreWrapped(string i)
788         {
789                 return ignoreWrappedList.contains(i);
790         }
791         
792 }
793         
794          
795         
796         
797
798