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