+/* ---------------------------xml ------------------------------ */
+
+%code {
+ static int xml_level = 0;
+};
+
+XML: XMLNODE {
+ multiname_t m = {QNAME, &stdns, 0, "XML"};
+ typedcode_t v;
+ v.c = 0;
+ v.c = abc_getlex2(v.c, &m);
+ v.c = code_append(v.c, node_read($1).c);
+ v.c = abc_construct(v.c, 1);
+ v.t = TYPE_XML;
+ $$ = mkcodenode(v);
+}
+
+OPEN : '<' {PASS_ALWAYS if(!xml_level++) tokenizer_begin_xml();}
+CLOSE : '>' {PASS_ALWAYS tokenizer_begin_xmltext();}
+CLOSE2 : {PASS_ALWAYS if(!--xml_level) tokenizer_end_xml(); else tokenizer_begin_xmltext();}
+
+XMLEXPR1 : '{' E {PASS_ALWAYS tokenizer_begin_xmltext();} '}' {
+ $$ = $2;
+}
+XMLEXPR2 : '{' E {PASS_ALWAYS tokenizer_begin_xml();} '}' {
+ $$ = $2;
+}
+XMLTEXT : {$$=mkstringnode("");}
+XMLTEXT : XMLTEXT XMLEXPR1 {
+ $$ = mkaddnode($1,$2);
+}
+XMLTEXT : XMLTEXT T_STRING {
+ char* str = string_cstr(&$2);
+ $$ = mkaddnode($1,mkstringnode(str));
+ free(str);
+}
+XMLTEXT : XMLTEXT '>' {
+ $$ = mkaddnode($1, mkstringnode(">"));
+}
+XML2 : XMLNODE XMLTEXT {
+ $$ = mkaddnode($1,$2);
+}
+XML2 : XML2 XMLNODE XMLTEXT {
+ $$ = mkaddnode($1, mkaddnode($2,$3));
+}
+XML_ID_OR_EXPR: T_IDENTIFIER {
+ $$ = mkstringnode($1);
+}
+XML_ID_OR_EXPR: XMLEXPR2 {
+ $$ = $1;
+}
+
+MAYBE_XMLATTRIBUTES: {
+ $$ = mkstringnode("");
+}
+MAYBE_XMLATTRIBUTES: XMLATTRIBUTES {
+ $$ = mkaddnode(mkstringnode(" "),$1);
+}
+
+XMLNODE : OPEN XML_ID_OR_EXPR MAYBE_XMLATTRIBUTES '/' CLOSE2 '>' {
+ //$$ = allocprintf("<%s%s/>", $2, $3, $5, $8);
+ $$ = mkaddnode(mkaddnode(mkaddnode(mkstringnode("<"),$2),$3),mkstringnode("/>"));
+}
+XMLNODE : OPEN XML_ID_OR_EXPR MAYBE_XMLATTRIBUTES CLOSE XMLTEXT '<' '/' XML_ID_OR_EXPR CLOSE2 '>' {
+ //$$ = allocprintf("<%s%s>%s</%s>", $2, $3, $5, $8);
+ $$ = mkaddnode(mkaddnode(mkaddnode(mkaddnode(mkaddnode(mkaddnode(mkaddnode(
+ mkstringnode("<"),$2),$3),mkstringnode(">")),$5),mkstringnode("</")),$8),mkstringnode(">"));
+}
+XMLNODE : OPEN XML_ID_OR_EXPR MAYBE_XMLATTRIBUTES CLOSE XMLTEXT XML2 '<' '/' XML_ID_OR_EXPR CLOSE2 '>' {
+ //$$ = allocprintf("<%s%s>%s%s</%s>", $2, $3, $5, $6, $9);
+ $$ = mkaddnode(mkaddnode(mkaddnode(mkaddnode(mkaddnode(mkaddnode(mkaddnode(mkaddnode(
+ mkstringnode("<"),$2),$3),mkstringnode(">")),$5),$6),mkstringnode("</")),$9),mkstringnode(">"));
+}
+
+XMLATTRIBUTES: XMLATTRIBUTE {
+ $$ = $1;
+}
+XMLATTRIBUTES: XMLATTRIBUTES XMLATTRIBUTE {
+ $$ = mkaddnode($1, mkaddnode(mkstringnode(" "),$2));
+}
+XMLATTRIBUTE: XMLEXPR2 {
+ $$ = $1;
+}
+XMLATTRIBUTE: XMLEXPR2 '=' T_STRING {
+ char* str = string_cstr(&$3);
+ $$ = mkaddnode($1, mkstringnode(concat2("=",str)));
+ free(str);
+}
+XMLATTRIBUTE: XMLEXPR2 '=' XMLEXPR2 {
+ $$ = mkaddnode($1, mkaddnode(mkstringnode("=\""), mkaddnode($3, mkstringnode("\""))));
+}
+XMLATTRIBUTE: T_IDENTIFIER '=' XMLEXPR2 {
+ $$ = mkaddnode(mkaddnode(mkstringnode(concat2($1,"=\"")), $3), mkstringnode("\""));
+}
+XMLATTRIBUTE: T_IDENTIFIER '=' T_STRING {
+ char* str = string_cstr(&$3);
+ $$=mkstringnode(allocprintf("%s=%s", $1,str));
+ free(str);
+ free($1);free((char*)$3.str);
+}
+