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