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