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