3 * Code to convert node tree to Vala...
5 * usage : x = (new JsRender.NodeToVala(node)).munge();
9 * pack - can we come up with a replacement?
10 - parent.child == child_widget -- actually uses getters and effectively does 'add'?
22 public class JsRender.NodeToVala : Object {
37 Gee.ArrayList<string> ignoreList;
38 Gee.ArrayList<string> ignoreWrappedList;
39 Gee.ArrayList<string> myvars;
40 Gee.ArrayList<Node> vitems; // top level items
45 * ctor - just initializes things
46 * - wraps a render node
48 public NodeToVala( JsRender file, Node node, int depth, NodeToVala? parent)
54 this.inpad = string.nfill(depth > 0 ? 4 : 0, ' ');
55 this.pad = this.inpad + " ";
56 this.ipad = this.inpad + " ";
57 this.cls = node.xvala_cls;
58 this.xcls = node.xvala_xcls;
60 this.cur_line = parent == null ? 0 : parent.cur_line;
63 this.top = parent == null ? this : parent.top;
64 this.ignoreList = new Gee.ArrayList<string>();
65 this.ignoreWrappedList = new Gee.ArrayList<string>();
66 this.myvars = new Gee.ArrayList<string>();
67 this.vitems = new Gee.ArrayList<Node>();
70 // initialize line data..
71 node.line_start = this.cur_line;
72 node.line_end = this.cur_line;
73 node.lines = new Gee.ArrayList<int>();
74 node.line_map = new Gee.HashMap<int,string>();
76 node.node_lines = new Gee.ArrayList<int>();
77 node.node_lines_map = new Gee.HashMap<int,Node>();
83 string toValaNS(Node item)
85 var ns = item.get("xns") ;
86 if (ns == "GtkSource") {
91 public void toValaName(Node item, int depth =0)
95 var ns = this.toValaNS(item) ;
96 var cls = ns + item.get("xtype");
102 string id = item.get("id").length > 0 ?
103 item.get("id") : "%s%d".printf(item.get("xtype"), this.vcnt);
108 if (id[0] == '*' || id[0] == '+') {
109 item.xvala_xcls = "Xcls_" + id.substring(1);
111 item.xvala_xcls = "Xcls_" + id;
117 this.vitems.add(item);
118 } else if (!item.props.has_key("id")) {
119 // use the file name..
120 item.xvala_xcls = this.file.name;
122 item.xvala_id = this.file.name;
127 if (item.items.size < 1) {
130 for(var i =0;i<item.items.size;i++) {
131 this.toValaName(item.items.get(i), depth+1);
136 * Main entry point to convert a file into a string..
138 public static string mungeFile(JsRender file)
140 if (file.tree == null) {
144 var n = new NodeToVala(file, file.tree, 0, null);
148 n.toValaName(file.tree);
151 GLib.debug("top cls %s / xlcs %s\n ",file.tree.xvala_cls,file.tree.xvala_cls);
152 n.cls = file.tree.xvala_cls;
153 n.xcls = file.tree.xvala_xcls;
159 public string munge ( )
161 //return this.mungeToString(this.node);
166 this.ignore("xtype");
172 this.addTopProperties();
174 this.addPlusProperties();
177 this.addWrappedCtor();
179 this.addInitMyVars();
180 this.addWrappedProperties();
185 this.addUserMethods();
192 public string mungeChild( Node cnode)
194 var x = new NodeToVala(this.file, cnode, this.depth+1, this);
197 public void addLine(string str= "")
200 //this.ret += "/*%d*/ ".printf(this.cur_line-1) + str + "\n";
201 this.ret += str + "\n";
203 public void addMultiLine(string str= "")
206 this.cur_line += str.split("\n").length;
207 //this.ret += "/*%d*/ ".printf(l) + str + "\n";
208 this.ret += str + "\n";
212 public void globalVars()
214 if (this.depth > 0) {
217 // Global Vars..??? when did this get removed..?
218 //this.ret += this.inpad + "public static " + this.xcls + " " + this.node.xvala_id+ ";\n\n";
220 this.addLine(this.inpad + "static " + this.xcls + " _" + this.node.xvala_id+ ";");
229 // class xxx { WrappedGtk el; }
230 this.node.line_start = this.cur_line;
232 this.top.node.setNodeLine(this.cur_line, this.node);
234 this.addLine(inpad + "public class " + this.xcls + " : Object");
235 this.addLine(this.inpad + "{");
238 this.addLine(this.pad + "public " + this.cls + " el;");
240 this.addLine(this.pad + "private " + this.top.xcls + " _this;");
252 this.addLine(pad + "public static " + xcls + " singleton()");
253 this.addLine(this.pad + "{");
254 this.addLine(this.ipad + "if (_" + this.node.xvala_id + " == null) {");
255 this.addLine(this.ipad + " _" + this.node.xvala_id + "= new "+ this.xcls + "();"); // what about args?
256 this.addLine(this.ipad + "}");
257 this.addLine(this.ipad + "return _" + this.node.xvala_id +";");
258 this.addLine(this.pad + "}");
262 * when ID is used... on an element, it registeres a property on the top level...
263 * so that _this.ID always works..
266 void addTopProperties()
268 if (this.depth > 0) {
271 // properties - global..??
273 var iter = this.vitems.list_iterator();
278 if (!n.props.has_key("id") || n.xvala_id.length < 0) {
282 if (n.xvala_id[0] == '*') {
285 if (n.xvala_id[0] == '+') {
288 this.addLine(this.pad + "public " + n.xvala_xcls + " " + n.xvala_id + ";");
294 * create properties that are not 'part of the wrapped element.
301 GLib.debug("callinged addMhyVars");
304 this.addLine(this.ipad + "// my vars (def)");
308 var cls = Palete.Gir.factoryFqn((Project.Gtk) this.file.project, this.node.fqn());
311 GLib.debug("Gir factory failed to find class %s", this.node.fqn());
318 var iter = this.node.props.map_iterator();
319 while (iter.next()) {
321 var prop = iter.get_value();
323 if (this.shouldIgnore(prop.name)) {
327 // user defined method
328 if (prop.ptype == NodePropType.METHOD) {
331 if (prop.ptype == NodePropType.SPECIAL) {
335 if (prop.ptype == NodePropType.SIGNAL) {
336 this.node.setLine(this.cur_line, "p", prop.name);
337 this.addLine(this.pad + "public signal " + prop.name + " " + prop.val + ";");
339 this.ignore(prop.name);
343 GLib.debug("Got myvars: %s", prop.name.strip());
345 if (prop.rtype.strip().length < 1) {
349 // is it a class property...
350 if (cls.props.has_key(prop.name) && prop.ptype != NodePropType.USER) {
354 this.myvars.add(prop.name);
355 prop.start_line = this.cur_line;
357 this.node.setLine(this.cur_line, "p", prop.name);
359 this.addLine(this.pad + "public " + prop.rtype + " " + prop.name + ";"); // definer - does not include value.
362 prop.end_line = this.cur_line;
363 this.ignore(prop.name);
369 // if id of child is '+' then it's a property of this..
370 void addPlusProperties()
372 if (this.node.items.size < 1) {
375 var iter = this.node.items.list_iterator();
376 while (iter.next()) {
379 if (ci.xvala_id[0] != '+') {
380 continue; // skip generation of children?
384 this.addLine(this.pad + "public " + ci.xvala_xcls + " " + ci.xvala_id.substring(1) + ";");
390 * add the constructor definition..
402 this.addLine(this.pad + "// ctor");
404 if (this.node.has("* args")) {
405 // not sure what this is supposed to be ding..
407 cargs_str = this.node.get("* args");
408 //var ar = this.node.get("* args");.split(",");
409 //for (var ari =0; ari < ar.length; ari++) {
410 // cargs += (ar[ari].trim().split(" ").pop();
414 if (this.depth < 1) {
416 // top level - does not pass the top level element..
417 this.addLine(this.pad + "public " + this.xcls + "(" + cargs_str +")");
418 this.addLine(this.pad + "{");
420 if (cargs_str.length > 0) {
421 cargs_str = ", " + cargs_str;
423 // for sub classes = we passs the top level as _owner
424 this.addLine(this.pad + "public " + this.xcls + "(" + this.top.xcls + " _owner " + cargs_str + ")");
425 this.addLine(this.pad + "{");
431 * make sure _this is defined..
437 this.addLine( this.ipad + "_this = this;");
440 // for non top level = _this point to owner, and _this.ID is set
442 this.addLine( this.ipad + "_this = _owner;");
444 if (this.node.props.has_key("id")
446 this.node.xvala_id != ""
448 this.node.xvala_id[0] != '*'
450 this.node.xvala_id[0] != '+'
452 this.addLine( this.ipad + "_this." + node.xvala_id + " = this;");
458 * Initialize this.el to point to the wrapped element.
463 void addWrappedCtor()
466 // this may need to look up properties to fill in the arguments..
467 // introspection does not workk..... - as things like gtkmessagedialog
469 if (cls == 'Gtk.Table') {
471 var methods = this.palete.getPropertiesFor(cls, 'methods');
473 print(JSON.stringify(this.palete.proplist[cls], null,4));
477 if (this.node.has("* ctor")) {
478 this.node.setLine(this.cur_line, "p", "* ctor");
479 this.addLine(this.ipad + "this.el = " + this.node.get("* ctor")+ ";");
483 var default_ctor = Palete.Gir.factoryFqn((Project.Gtk) this.file.project, this.node.fqn() + ".new");
486 if (default_ctor != null && default_ctor.paramset != null && default_ctor.paramset.params.size > 0) {
488 var iter = default_ctor.paramset.params.list_iterator();
489 while (iter.next()) {
490 var n = iter.get().name;
491 GLib.debug("building CTOR ARGS: %s, %s", n, iter.get().is_varargs ? "VARARGS": "");
494 if (!this.node.has(n)) {
495 if (n == "___") { // for some reason our varargs are converted to '___' ...
500 if (iter.get().type.contains("int")) {
504 if (iter.get().type.contains("float")) {
508 if (iter.get().type.contains("bool")) {
509 args += "true"; // always default to true?
512 // any other types???
517 this.ignoreWrapped(n);
520 var v = this.node.get(n);
522 if (iter.get().type == "string") {
523 v = "\"" + v.escape("") + "\"";
525 if (v == "TRUE" || v == "FALSE") {
533 this.node.setLine(this.cur_line, "p", "* xtype");
535 this.addLine(this.ipad + "this.el = new " + cls + "( "+ string.joinv(", ",args) + " );") ;
539 this.node.setLine(this.cur_line, "p", "* xtype");;
541 this.addLine(this.ipad + "this.el = new " + this.cls + "();");
548 //var meths = this.palete.getPropertiesFor(item['|xns'] + '.' + item.xtype, 'methods');
549 //print(JSON.stringify(meths,null,4));Seed.quit();
553 // initialize.. my vars..
555 this.addLine( this.ipad + "// my vars (dec)");
557 var iter = this.myvars.list_iterator();
562 var ar = k.strip().split(" ");
563 var kname = ar[ar.length-1];
565 var prop = this.node.props.get(k);
567 var v = prop.val.strip();
572 // at this point start using
574 if (v == "FALSE" || v == "TRUE") {
577 //FIXME -- check for raw string.. "string XXXX"
579 // if it's a string...
581 prop.start_line = this.cur_line;
582 this.addLine(this.ipad + "this." + prop.name + " = " + v +";");
583 prop.end_line = this.cur_line;
591 void addWrappedProperties()
593 var cls = Palete.Gir.factoryFqn((Project.Gtk) this.file.project, this.node.fqn());
595 GLib.debug("Skipping wrapped properties - could not find class %s" , this.node.fqn());
598 // what are the properties of this class???
600 this.addLine(this.ipad + "// set gobject values");
603 var iter = cls.props.map_iterator();
604 while (iter.next()) {
605 var p = iter.get_key();
606 //print("Check Write %s\n", p);
607 if (!this.node.has(p)) {
610 if (this.shouldIgnoreWrapped(p)) {
617 var prop = this.node.get_prop(p);
620 // user defined properties.
621 if (prop.ptype == NodePropType.USER) {
627 var is_raw = prop.ptype == NodePropType.RAW;
629 // what's the type.. - if it's a string.. then we quote it..
630 if (iter.get_value().type == "string" && !is_raw) {
631 v = "\"" + v.escape("") + "\"";
633 if (v == "TRUE" || v == "FALSE") {
636 if (iter.get_value().type == "float" && v[v.length-1] != 'f') {
640 prop.start_line = this.cur_line;
641 this.addLine("%sthis.el.%s = %s;".printf(ipad,p,v)); // // %s, iter.get_value().type);
642 prop.end_line = this.cur_line;
650 * pack the children into the parent.
652 * if the child's id starts with '*' then it is not packed...
653 * - this allows you to define children and add them manually..
659 if (this.node.items.size < 1) {
663 var iter = this.node.items.list_iterator();
665 while (iter.next()) {
670 if (ci.xvala_id[0] == '*') {
671 continue; // skip generation of children?
675 if (ci.has("* args")) {
677 var ar = ci.get_prop("* args").val.split(",");
678 for (var ari = 0 ; ari < ar.length; ari++ ) {
679 var arg = ar[ari].split(" ");
680 xargs += "," + arg[arg.length -1];
683 // create the element..
684 this.addLine(this.ipad + "var child_" + "%d".printf(i) + " = new " + ci.xvala_xcls +
685 "( _this " + xargs + ");" );
687 // this is only needed if it does not have an ID???
688 this.addLine(this.ipad + "child_" + "%d".printf(i) +".ref();"); // we need to reference increase unnamed children...
690 if (ci.has("* prop")) {
691 this.addLine(ipad + "this.el." + ci.get_prop("* prop").val + " = child_" + "%d".printf(i) + ".el;");
696 // not sure why we have 'true' in pack?!?
697 if (!ci.has("* pack") || ci.get("* pack").down() == "false" || ci.get("* pack").down() == "true") {
701 string[] packing = { "add" };
702 if (ci.has("* pack")) {
703 packing = ci.get("* pack").split(",");
706 var pack = packing[0];
707 this.addLine(this.ipad + "this.el." + pack.strip() + " ( child_" + "%d".printf(i) + ".el " +
708 (packing.length > 1 ?
709 (", " + string.joinv(",", packing).substring(pack.length+1))
715 if (ci.xvala_id[0] != '+') {
716 continue; // skip generation of children?
719 // this.{id - without the '+'} = the element...
720 this.addLine(this.ipad + "this." + ci.xvala_id.substring(1) + " = child_" + "%d".printf(i) + ";");
729 if (!this.node.has("init")) {
733 this.addLine(ipad + "// init method");
735 this.node.setLine(this.cur_line, "p", "init");
737 this.addMultiLine(ipad + this.padMultiline(ipad, this.node.get("init")) );
742 if (this.node.listeners.size < 1) {
747 this.addLine(ipad + "//listeners");
751 var iter = this.node.listeners.map_iterator();
752 while (iter.next()) {
753 var k = iter.get_key();
754 var prop = iter.get_value();
757 prop.start_line = this.cur_line;
758 this.node.setLine(this.cur_line, "l", k);
759 this.addMultiLine(this.ipad + "this.el." + k + ".connect( " +
760 this.padMultiline(this.ipad,v) +");");
761 prop.end_line = this.cur_line;
768 this.addLine(this.pad + "}");
773 * Standardize this crap...
775 * standard properties (use to set)
776 * If they are long values show the dialog..
779 * bool is_xxx :: can show a pulldown.. (true/false)
781 * $ string html = string with value interpolated eg. baseURL + ".."
782 * Clutter.ActorAlign x_align (typed) -- shows pulldowns if type is ENUM?
783 * $ untypedvalue = javascript untyped value...
784 * _ string html ... = translatable..
787 * object properties (not part of the GOjbect being wrapped?
788 * # Gee.ArrayList<Xcls_fileitem> fileitems
793 * methods -- always text editor..
801 * * init -- big string?
803 * event handlers (listeners)
808 * +XXXX -- indicates it's a instance property / not glob...
809 * *XXXX -- skip writing glob property (used as classes that can be created...)
814 void addUserMethods()
817 this.addLine(this.pad + "// user defined functions");
819 // user defined functions...
820 var iter = this.node.props.map_iterator();
822 var prop = iter.get_value();
823 if (this.shouldIgnore(prop.name)) {
826 // HOW TO DETERIME if its a method?
827 if (prop.ptype != NodePropType.METHOD) {
828 //strbuilder("\n" + pad + "// skip " + k + " - not pipe \n");
832 // function in the format of {type} (args) { .... }
836 prop.start_line = this.cur_line;
837 this.node.setLine(this.cur_line, "p", prop.name);
838 this.addMultiLine(this.pad + "public " + prop.rtype + " " + prop.name + " " + this.padMultiline(this.pad, prop.val));;
839 prop.end_line = this.cur_line;
846 this.node.line_end = this.cur_line;
847 this.node.sortLines();
850 if (this.depth > 0) {
851 this.addLine(this.inpad + "}");
854 var iter = this.node.items.list_iterator();
856 while (iter.next()) {
857 this.addMultiLine(this.mungeChild(iter.get()));
860 if (this.depth < 1) {
861 this.addLine(this.inpad + "}");
866 string padMultiline(string pad, string str)
868 var ar = str.strip().split("\n");
869 return string.joinv("\n" + pad , ar);
872 void ignore(string i) {
873 this.ignoreList.add(i);
876 void ignoreWrapped(string i) {
877 this.ignoreWrappedList.add(i);
880 bool shouldIgnore(string i)
882 return ignoreList.contains(i);
884 bool shouldIgnoreWrapped(string i)
886 return ignoreWrappedList.contains(i);