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