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