src/Palete/VapiParser.vala.c
[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 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                         var  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 == "string") {
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                         print("Check Write %s\n", p);
508                         if (!this.node.has(p)) {
509                                 continue;
510                         }
511                         if (this.shouldIgnoreWrapped(p)) {
512                                 continue;
513                         }
514                         
515                         this.ignore(p);
516                         var v = this.node.get(p);
517
518                         var nodekey = this.node.get_key(p);
519
520                         // user defined properties.
521                         if (nodekey[0] == '#') {
522                                 continue;
523                         }
524                                 
525
526                         
527                         var is_raw = nodekey[0] == '$';
528                         
529                         // what's the type.. - if it's a string.. then we quote it..
530                         if (iter.get_value().type == "string" && !is_raw) {
531                                  v = "\"" +  v.escape("") + "\"";
532                         }
533                         if (v == "TRUE" || v == "FALSE") {
534                                 v = v.down();
535                         }
536                         if (iter.get_value().type == "float" && v[v.length-1] != 'f') {
537                                 v += "f";
538                         }
539                         
540                         
541                         this.ret += "%sthis.el.%s = %s; // %s\n".printf(ipad,p,v,  iter.get_value().type);
542                             
543                        // got a property..
544                        
545
546                 }
547             
548         }
549
550         void addChildren()
551         {
552                 //code
553                 if (this.node.items.size < 1) {
554                         return;
555                 }
556              
557                 var iter = this.node.items.list_iterator();
558                 var i = -1;
559                 while (iter.next()) {
560                         i++;
561                 
562                         var ci = iter.get();
563
564                         if (ci.xvala_id[0] == '*') {
565                                 continue; // skip generation of children?
566                         }
567                     
568                         var xargs = "";
569                         if (ci.has("* args")) {
570                         
571                                 var ar = ci.get("* args").split(",");
572                                 for (var ari = 0 ; ari < ar.length; ari++ ) {
573                                         var arg = ar[ari].split(" ");
574                                         xargs += "," + arg[arg.length -1];
575                                 }
576                         }
577                     
578                         this.ret += this.ipad + "var child_" + "%d".printf(i) + " = new " + ci.xvala_xcls +
579                                         "( _this " + xargs + ");\n" ;
580                                     
581                         this.ret+= this.ipad + "child_" + "%d".printf(i) +".ref();\n"; // we need to reference increase unnamed children...
582                     
583                         if (ci.has("* prop")) {
584                                 this.ret+= ipad + "this.el." + ci.get("* prop") + " = child_" + "%d".printf(i) + ".el;\n";
585                                 continue;
586                         }
587
588                         // not sure why we have 'true' in pack?!?
589                         if (!ci.has("pack") || ci.get("pack").down() == "false" || ci.get("pack").down() == "true") {
590                                 continue;
591                         }
592                     
593                         string[]  packing =  { "add" };
594                         if (ci.has("pack")) {
595                                 packing = ci.get("pack").split(",");
596                         }
597                         
598                         var pack = packing[0];
599                         this.ret += this.ipad + "this.el." + pack.strip() + " (  child_" + "%d".printf(i) + ".el " +
600                                (packing.length > 1 ? 
601                                         (", " + string.joinv(",", packing).substring(pack.length+1))
602                                         :
603                                         ""
604                                 ) + " );\n";
605                         
606                               
607                         if (ci.xvala_id[0] != '+') {
608                                 continue; // skip generation of children?
609                                         
610                         }
611                         this.ret+= this.ipad + "this." + ci.xvala_id.substring(1) + " =  child_" + "%d".printf(i) +  ";\n";
612                           
613                 }
614         }
615
616         void addInit()
617         {
618
619             
620                 if (!this.node.has("init")) {
621                             return;
622                 }
623                 this.ret+= "\n" + ipad + "// init method \n";
624         
625                 this.ret+= "\n" + ipad + this.padMultiline(ipad, this.node.get("init")) + "\n";
626
627          }
628          void addListeners()
629          {
630                 if (this.node.listeners.size < 1) {
631                         return;
632                 }
633                             
634             
635             
636                 this.ret+= "\n" + ipad + "// listeners \n";
637
638                 var iter = this.node.listeners.map_iterator();
639                 while (iter.next()) {
640                         var k = iter.get_key();
641                         var v = iter.get_value();
642                         this.ret+= this.ipad + "this.el." + k + ".connect( " + 
643                                         this.padMultiline(this.ipad,v) +");\n"; 
644                     
645                 }
646         }    
647         void addEndCtor()
648         {
649             
650             
651             
652                 // end ctor..
653                 this.ret+= this.pad + "}\n";
654         }
655
656
657         /*
658  * Standardize this crap...
659  * 
660  * standard properties (use to set)
661  *          If they are long values show the dialog..
662  *
663  * someprop : ....
664  * bool is_xxx  :: can show a pulldown.. (true/false)
665  * string html  
666  * $ string html  = string with value interpolated eg. baseURL + ".." 
667  *  Clutter.ActorAlign x_align  (typed)  -- shows pulldowns if type is ENUM? 
668  * $ untypedvalue = javascript untyped value...  
669  * _ string html ... = translatable..
670
671  * 
672  * object properties (not part of the GOjbect being wrapped?
673  * # Gee.ArrayList<Xcls_fileitem> fileitems
674  * 
675  * signals
676  * @ void open 
677  * 
678  * methods -- always text editor..
679  * | void clearFiles
680  * | someJSmethod
681  * 
682  * specials
683  * * prop -- string
684  * * args  -- string
685  * * ctor -- string
686  * * init -- big string?
687  * 
688  * event handlers (listeners)
689  *   just shown 
690  * 
691  * -----------------
692  * special ID values
693  *  +XXXX -- indicates it's a instance property / not glob...
694  *  *XXXX -- skip writing glob property (used as classes that can be created...)
695  * 
696  * 
697  */
698          
699         void addUserMethods()
700         {
701             
702                 this.ret+= "\n" + pad + "// user defined functions \n";  
703             
704                 // user defined functions...
705                 var iter = this.node.props.map_iterator();
706                 while(iter.next()) {
707                         var k = iter.get_key();
708                         if (this.shouldIgnore(k)) {
709                                 continue;
710                         }
711                         // HOW TO DETERIME if its a method?            
712                         if (k[0] != '|') {
713                                 //strbuilder("\n" + pad + "// skip " + k + " - not pipe \n"); 
714                                 continue;
715                         }       
716                         // function in the format of {type} (args) { .... }
717                         var kk = k.substring(2);
718                         var vv = iter.get_value();
719                         this.ret += this.pad + "public " + kk + " " + this.padMultiline(this.pad, vv) + "\n";
720                         
721                 
722             }
723         }
724
725         void iterChildren()
726         {
727             
728                 if (this.depth > 0) {
729                         this.ret+= this.inpad + "}\n";
730                 }
731                 
732                 var iter = this.node.items.list_iterator();
733                 var i = -1;
734                 while (iter.next()) {
735                         this.ret += this.mungeChild(iter.get());
736                 }
737              
738                 if (this.depth < 1) {
739                         this.ret+= this.inpad + "}\n";
740                 }
741             
742         }
743
744         string padMultiline(string pad, string str)
745         {
746                 var ar = str.strip().split("\n");
747                 return string.joinv("\n" + pad , ar);
748         }
749         
750         void ignore(string i) {
751                 this.ignoreList.add(i);
752                 
753         }
754         void ignoreWrapped(string i) {
755                 this.ignoreWrappedList.add(i);
756                 
757         }
758         bool shouldIgnore(string i)
759         {
760                 return ignoreList.contains(i);
761         }
762         bool shouldIgnoreWrapped(string i)
763         {
764                 return ignoreWrappedList.contains(i);
765         }
766
767 }
768         
769          
770         
771         
772
773