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