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