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);
119 // setting id on top level class changes it classname..
120 // oddly enough we havent really thought about namespacing here.
122 } else if (!item.props.has_key("id")) {
123 // use the file name..
124 item.xvala_xcls = this.file.name;
126 item.xvala_id = this.file.name;
131 if (item.items.size < 1) {
134 for(var i =0;i<item.items.size;i++) {
135 this.toValaName(item.items.get(i), depth+1);
140 * Main entry point to convert a file into a string..
142 public static string mungeFile(JsRender file)
144 if (file.tree == null) {
148 var n = new NodeToVala(file, file.tree, 0, null);
152 n.toValaName(file.tree);
155 GLib.debug("top cls %s / xlcs %s\n ",file.tree.xvala_cls,file.tree.xvala_cls);
156 n.cls = file.tree.xvala_cls;
157 n.xcls = file.tree.xvala_xcls;
163 public string munge ( )
165 //return this.mungeToString(this.node);
170 this.ignore("xtype");
176 this.addTopProperties();
178 this.addPlusProperties();
181 this.addWrappedCtor();
183 this.addInitMyVars();
184 this.addWrappedProperties();
189 this.addUserMethods();
196 public string mungeChild( Node cnode)
198 var x = new NodeToVala(this.file, cnode, this.depth+1, this);
201 public void addLine(string str= "")
204 //this.ret += "/*%d*/ ".printf(this.cur_line-1) + str + "\n";
205 this.ret += str + "\n";
207 public void addMultiLine(string str= "")
210 this.cur_line += str.split("\n").length;
211 //this.ret += "/*%d*/ ".printf(l) + str + "\n";
212 this.ret += str + "\n";
216 public void globalVars()
218 if (this.depth > 0) {
221 // Global Vars..??? when did this get removed..?
222 //this.ret += this.inpad + "public static " + this.xcls + " " + this.node.xvala_id+ ";\n\n";
224 this.addLine(this.inpad + "static " + this.xcls + " _" + this.node.xvala_id+ ";");
233 // class xxx { WrappedGtk el; }
234 this.node.line_start = this.cur_line;
236 this.top.node.setNodeLine(this.cur_line, this.node);
238 this.addLine(inpad + "public class " + this.xcls + " : Object");
239 this.addLine(this.inpad + "{");
242 this.addLine(this.pad + "public " + this.cls + " el;");
244 this.addLine(this.pad + "private " + this.top.xcls + " _this;");
256 this.addLine(pad + "public static " + xcls + " singleton()");
257 this.addLine(this.pad + "{");
258 this.addLine(this.ipad + "if (_" + this.node.xvala_id + " == null) {");
259 this.addLine(this.ipad + " _" + this.node.xvala_id + "= new "+ this.xcls + "();"); // what about args?
260 this.addLine(this.ipad + "}");
261 this.addLine(this.ipad + "return _" + this.node.xvala_id +";");
262 this.addLine(this.pad + "}");
266 * when ID is used... on an element, it registeres a property on the top level...
267 * so that _this.ID always works..
270 void addTopProperties()
272 if (this.depth > 0) {
275 // properties - global..??
277 var iter = this.vitems.list_iterator();
282 if (!n.props.has_key("id") || n.xvala_id.length < 0) {
286 if (n.xvala_id[0] == '*') {
289 if (n.xvala_id[0] == '+') {
292 this.addLine(this.pad + "public " + n.xvala_xcls + " " + n.xvala_id + ";");
298 * create properties that are not 'part of the wrapped element.
305 GLib.debug("callinged addMhyVars");
308 this.addLine(this.ipad + "// my vars (def)");
312 var cls = Palete.Gir.factoryFqn((Project.Gtk) this.file.project, this.node.fqn());
315 GLib.debug("Gir factory failed to find class %s", this.node.fqn());
322 var iter = this.node.props.map_iterator();
323 while (iter.next()) {
325 var prop = iter.get_value();
327 if (this.shouldIgnore(prop.name)) {
331 // user defined method
332 if (prop.ptype == NodePropType.METHOD) {
335 if (prop.ptype == NodePropType.SPECIAL) {
339 if (prop.ptype == NodePropType.SIGNAL) {
340 this.node.setLine(this.cur_line, "p", prop.name);
341 this.addLine(this.pad + "public signal " + prop.rtype + " " + prop.name + " " + prop.val + ";");
343 this.ignore(prop.name);
347 GLib.debug("Got myvars: %s", prop.name.strip());
349 if (prop.rtype.strip().length < 1) {
353 // is it a class property...
354 if (cls.props.has_key(prop.name) && prop.ptype != NodePropType.USER) {
358 this.myvars.add(prop.name);
359 prop.start_line = this.cur_line;
361 this.node.setLine(this.cur_line, "p", prop.name);
363 this.addLine(this.pad + "public " + prop.rtype + " " + prop.name + ";"); // definer - does not include value.
366 prop.end_line = this.cur_line;
367 this.ignore(prop.name);
373 // if id of child is '+' then it's a property of this..
374 void addPlusProperties()
376 if (this.node.items.size < 1) {
379 var iter = this.node.items.list_iterator();
380 while (iter.next()) {
383 if (ci.xvala_id[0] != '+') {
384 continue; // skip generation of children?
388 this.addLine(this.pad + "public " + ci.xvala_xcls + " " + ci.xvala_id.substring(1) + ";");
394 * add the constructor definition..
406 this.addLine(this.pad + "// ctor");
408 if (this.node.has("* args")) {
409 // not sure what this is supposed to be ding..
411 cargs_str = this.node.get("* args");
412 //var ar = this.node.get("* args");.split(",");
413 //for (var ari =0; ari < ar.length; ari++) {
414 // cargs += (ar[ari].trim().split(" ").pop();
418 if (this.depth < 1) {
420 // top level - does not pass the top level element..
421 this.addLine(this.pad + "public " + this.xcls + "(" + cargs_str +")");
422 this.addLine(this.pad + "{");
424 if (cargs_str.length > 0) {
425 cargs_str = ", " + cargs_str;
427 // for sub classes = we passs the top level as _owner
428 this.addLine(this.pad + "public " + this.xcls + "(" + this.top.xcls + " _owner " + cargs_str + ")");
429 this.addLine(this.pad + "{");
435 * make sure _this is defined..
441 this.addLine( this.ipad + "_this = this;");
444 // for non top level = _this point to owner, and _this.ID is set
446 this.addLine( this.ipad + "_this = _owner;");
448 if (this.node.props.has_key("id")
450 this.node.xvala_id != ""
452 this.node.xvala_id[0] != '*'
454 this.node.xvala_id[0] != '+'
456 this.addLine( this.ipad + "_this." + node.xvala_id + " = this;");
462 * Initialize this.el to point to the wrapped element.
467 void addWrappedCtor()
470 // this may need to look up properties to fill in the arguments..
471 // introspection does not workk..... - as things like gtkmessagedialog
473 if (cls == 'Gtk.Table') {
475 var methods = this.palete.getPropertiesFor(cls, 'methods');
477 print(JSON.stringify(this.palete.proplist[cls], null,4));
481 if (this.node.has("* ctor")) {
482 this.node.setLine(this.cur_line, "p", "* ctor");
483 this.addLine(this.ipad + "this.el = " + this.node.get("* ctor")+ ";");
487 var default_ctor = Palete.Gir.factoryFqn((Project.Gtk) this.file.project, this.node.fqn() + ".new");
490 if (default_ctor != null && default_ctor.paramset != null && default_ctor.paramset.params.size > 0) {
492 var iter = default_ctor.paramset.params.list_iterator();
493 while (iter.next()) {
494 var n = iter.get().name;
495 GLib.debug("building CTOR ARGS: %s, %s", n, iter.get().is_varargs ? "VARARGS": "");
498 if (!this.node.has(n)) {
499 if (n == "___") { // for some reason our varargs are converted to '___' ...
504 if (iter.get().type.contains("int")) {
508 if (iter.get().type.contains("float")) {
512 if (iter.get().type.contains("bool")) {
513 args += "true"; // always default to true?
516 // any other types???
521 this.ignoreWrapped(n);
524 var v = this.node.get(n);
526 if (iter.get().type == "string") {
527 v = "\"" + v.escape("") + "\"";
529 if (v == "TRUE" || v == "FALSE") {
537 this.node.setLine(this.cur_line, "p", "* xtype");
539 this.addLine(this.ipad + "this.el = new " + cls + "( "+ string.joinv(", ",args) + " );") ;
543 this.node.setLine(this.cur_line, "p", "* xtype");;
545 this.addLine(this.ipad + "this.el = new " + this.cls + "();");
552 //var meths = this.palete.getPropertiesFor(item['|xns'] + '.' + item.xtype, 'methods');
553 //print(JSON.stringify(meths,null,4));Seed.quit();
557 // initialize.. my vars..
559 this.addLine( this.ipad + "// my vars (dec)");
561 var iter = this.myvars.list_iterator();
566 var ar = k.strip().split(" ");
567 var kname = ar[ar.length-1];
569 var prop = this.node.props.get(k);
571 var v = prop.val.strip();
576 // at this point start using
578 if (v == "FALSE" || v == "TRUE") {
581 //FIXME -- check for raw string.. "string XXXX"
583 // if it's a string...
585 prop.start_line = this.cur_line;
586 this.addLine(this.ipad + "this." + prop.name + " = " + v +";");
587 prop.end_line = this.cur_line;
595 void addWrappedProperties()
597 var cls = Palete.Gir.factoryFqn((Project.Gtk) this.file.project, this.node.fqn());
599 GLib.debug("Skipping wrapped properties - could not find class %s" , this.node.fqn());
602 // what are the properties of this class???
604 this.addLine(this.ipad + "// set gobject values");
607 var iter = cls.props.map_iterator();
608 while (iter.next()) {
609 var p = iter.get_key();
610 //print("Check Write %s\n", p);
611 if (!this.node.has(p)) {
614 if (this.shouldIgnoreWrapped(p)) {
621 var prop = this.node.get_prop(p);
624 // user defined properties.
625 if (prop.ptype == NodePropType.USER) {
631 var is_raw = prop.ptype == NodePropType.RAW;
633 // what's the type.. - if it's a string.. then we quote it..
634 if (iter.get_value().type == "string" && !is_raw) {
635 v = "\"" + v.escape("") + "\"";
637 if (v == "TRUE" || v == "FALSE") {
640 if (iter.get_value().type == "float" && v[v.length-1] != 'f') {
644 prop.start_line = this.cur_line;
645 this.addLine("%sthis.el.%s = %s;".printf(ipad,p,v)); // // %s, iter.get_value().type);
646 prop.end_line = this.cur_line;
654 * pack the children into the parent.
656 * if the child's id starts with '*' then it is not packed...
657 * - this allows you to define children and add them manually..
663 if (this.node.items.size < 1) {
667 var iter = this.node.items.list_iterator();
669 while (iter.next()) {
674 if (ci.xvala_id[0] == '*') {
675 continue; // skip generation of children?
679 if (ci.has("* args")) {
681 var ar = ci.get_prop("* args").val.split(",");
682 for (var ari = 0 ; ari < ar.length; ari++ ) {
683 var arg = ar[ari].split(" ");
684 xargs += "," + arg[arg.length -1];
687 // create the element..
688 this.addLine(this.ipad + "var child_" + "%d".printf(i) + " = new " + ci.xvala_xcls +
689 "( _this " + xargs + ");" );
691 // this is only needed if it does not have an ID???
692 this.addLine(this.ipad + "child_" + "%d".printf(i) +".ref();"); // we need to reference increase unnamed children...
694 if (ci.has("* prop")) {
695 this.addLine(ipad + "this.el." + ci.get_prop("* prop").val + " = child_" + "%d".printf(i) + ".el;");
700 // not sure why we have 'true' in pack?!?
701 if (!ci.has("* pack") || ci.get("* pack").down() == "false" || ci.get("* pack").down() == "true") {
705 string[] packing = { "add" };
706 if (ci.has("* pack")) {
707 packing = ci.get("* pack").split(",");
710 var pack = packing[0];
711 this.addLine(this.ipad + "this.el." + pack.strip() + " ( child_" + "%d".printf(i) + ".el " +
712 (packing.length > 1 ?
713 (", " + string.joinv(",", packing).substring(pack.length+1))
719 if (ci.xvala_id[0] != '+') {
720 continue; // skip generation of children?
723 // this.{id - without the '+'} = the element...
724 this.addLine(this.ipad + "this." + ci.xvala_id.substring(1) + " = child_" + "%d".printf(i) + ";");
733 if (!this.node.has("init")) {
737 this.addLine(ipad + "// init method");
739 this.node.setLine(this.cur_line, "p", "init");
741 this.addMultiLine(ipad + this.padMultiline(ipad, this.node.get("init")) );
746 if (this.node.listeners.size < 1) {
751 this.addLine(ipad + "//listeners");
755 var iter = this.node.listeners.map_iterator();
756 while (iter.next()) {
757 var k = iter.get_key();
758 var prop = iter.get_value();
761 prop.start_line = this.cur_line;
762 this.node.setLine(this.cur_line, "l", k);
763 this.addMultiLine(this.ipad + "this.el." + k + ".connect( " +
764 this.padMultiline(this.ipad,v) +");");
765 prop.end_line = this.cur_line;
772 this.addLine(this.pad + "}");
777 * Standardize this crap...
779 * standard properties (use to set)
780 * If they are long values show the dialog..
783 * bool is_xxx :: can show a pulldown.. (true/false)
785 * $ string html = string with value interpolated eg. baseURL + ".."
786 * Clutter.ActorAlign x_align (typed) -- shows pulldowns if type is ENUM?
787 * $ untypedvalue = javascript untyped value...
788 * _ string html ... = translatable..
791 * object properties (not part of the GOjbect being wrapped?
792 * # Gee.ArrayList<Xcls_fileitem> fileitems
797 * methods -- always text editor..
805 * * init -- big string?
807 * event handlers (listeners)
812 * +XXXX -- indicates it's a instance property / not glob...
813 * *XXXX -- skip writing glob property (used as classes that can be created...)
818 void addUserMethods()
821 this.addLine(this.pad + "// user defined functions");
823 // user defined functions...
824 var iter = this.node.props.map_iterator();
826 var prop = iter.get_value();
827 if (this.shouldIgnore(prop.name)) {
830 // HOW TO DETERIME if its a method?
831 if (prop.ptype != NodePropType.METHOD) {
832 //strbuilder("\n" + pad + "// skip " + k + " - not pipe \n");
836 // function in the format of {type} (args) { .... }
840 prop.start_line = this.cur_line;
841 this.node.setLine(this.cur_line, "p", prop.name);
842 this.addMultiLine(this.pad + "public " + prop.rtype + " " + prop.name + " " + this.padMultiline(this.pad, prop.val));;
843 prop.end_line = this.cur_line;
850 this.node.line_end = this.cur_line;
851 this.node.sortLines();
854 if (this.depth > 0) {
855 this.addLine(this.inpad + "}");
858 var iter = this.node.items.list_iterator();
860 while (iter.next()) {
861 this.addMultiLine(this.mungeChild(iter.get()));
864 if (this.depth < 1) {
865 this.addLine(this.inpad + "}");
870 string padMultiline(string pad, string str)
872 var ar = str.strip().split("\n");
873 return string.joinv("\n" + pad , ar);
876 void ignore(string i) {
877 this.ignoreList.add(i);
880 void ignoreWrapped(string i) {
881 this.ignoreWrappedList.add(i);
884 bool shouldIgnore(string i)
886 return ignoreList.contains(i);
888 bool shouldIgnoreWrapped(string i)
890 return ignoreWrappedList.contains(i);