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