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