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