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