Skip to content

Instantly share code, notes, and snippets.

@binki
Last active January 4, 2017 17:51
Show Gist options
  • Save binki/a2488fb90ccade19664b726df8c27bc3 to your computer and use it in GitHub Desktop.
Save binki/a2488fb90ccade19664b726df8c27bc3 to your computer and use it in GitHub Desktop.
$ haxe --run XmlLiteral
XmlLiteral.hx:28: XmlUtil.createElement('x', ['y' => Std.string('uh, ${1+4}')], [XmlUtil.createElement('child1'), Xml.createPCData(Std.string('hi 2×4=${2*4}')), XmlUtil.createElement('child2', ['c2a1' => Std.string('2')])])
XmlLiteral.hx:29: ECall({ expr => EField({ expr => EConst(CIdent(XmlUtil)), pos => #pos(XmlLiteral.hx:15: characters 12-19) },createElement), pos => #pos(XmlLiteral.hx:15: characters 12-33) },[{ expr => EConst(CString(x)), pos => #pos(XmlLiteral.hx:16: characters 16-19) },{ expr => EArrayDecl([{ expr => EBinop(OpArrow,{ expr => EConst(CString(y)), pos => #pos(XmlLiteral.hx:17: characters 18-21) },{ expr => ECall({ expr => EField({ expr => EConst(CIdent(Std)), pos => #pos(XmlLiteral.hx:17: characters 25-28) },string), pos => #pos(XmlLiteral.hx:17: characters 25-35) },[{ expr => EConst(CString(uh, ${1+4})), pos => #pos(XmlLiteral.hx:17: characters 36-48) }]), pos => #pos(XmlLiteral.hx:17: characters 25-49) }), pos => #pos(XmlLiteral.hx:17: characters 18-49) }]), pos => #pos(XmlLiteral.hx:17: characters 16-52) },{ expr => EArrayDecl([{ expr => ECall({ expr => EField({ expr => EConst(CIdent(XmlUtil)), pos => #pos(XmlLiteral.hx:19: characters 20-27) },createElement), pos => #pos(XmlLiteral.hx:19: characters 20-41) },[{ expr => EConst(CString(child1)), pos => #pos(XmlLiteral.hx:19: characters 42-50) }]), pos => #pos(XmlLiteral.hx:19: characters 20-51) },{ expr => ECall({ expr => EField({ expr => EConst(CIdent(Xml)), pos => #pos(XmlLiteral.hx:20: characters 20-23) },createPCData), pos => #pos(XmlLiteral.hx:20: characters 20-36) },[{ expr => ECall({ expr => EField({ expr => EConst(CIdent(Std)), pos => #pos(XmlLiteral.hx:20: characters 37-40) },string), pos => #pos(XmlLiteral.hx:20: characters 37-47) },[{ expr => EConst(CString(hi 2×4=${2*4})), pos => #pos(XmlLiteral.hx:20: characters 48-63) }]), pos => #pos(XmlLiteral.hx:20: characters 37-64) }]), pos => #pos(XmlLiteral.hx:20: characters 20-65) },{ expr => ECall({ expr => EField({ expr => EConst(CIdent(XmlUtil)), pos => #pos(XmlLiteral.hx:21: characters 20-27) },createElement), pos => #pos(XmlLiteral.hx:21: characters 20-41) },[{ expr => EConst(CString(child2)), pos => #pos(XmlLiteral.hx:22: characters 24-32) },{ expr => EArrayDecl([{ expr => EBinop(OpArrow,{ expr => EConst(CString(c2a1)), pos => #pos(XmlLiteral.hx:23: characters 26-32) },{ expr => ECall({ expr => EField({ expr => EConst(CIdent(Std)), pos => #pos(XmlLiteral.hx:23: characters 36-39) },string), pos => #pos(XmlLiteral.hx:23: characters 36-46) },[{ expr => EConst(CString(2)), pos => #pos(XmlLiteral.hx:23: characters 47-50) }]), pos => #pos(XmlLiteral.hx:23: characters 36-51) }), pos => #pos(XmlLiteral.hx:23: characters 26-51) }]), pos => #pos(XmlLiteral.hx:23: characters 24-54) }]), pos => #pos(XmlLiteral.hx:21: lines 21-23) }]), pos => #pos(XmlLiteral.hx:18: lines 18-24) }])
<x y="uh, 5"><child1/>hi 2×4=8<child2 c2a1="2"/></x>
import haxe.ds.StringMap;
import haxe.macro.Expr;
import haxe.macro.Printer;
class XmlLiteral {
public static function main() {
// See https://gist.github.com/binki/9d09ea0ae8ea43788505af08844dae79
// It would be cool if the following:
// showExpression(<x y='uh, ${1+4}'><child1/>hi. 2×4=${2*4}<child2 c2a1="non-interpolated $tring{}"/></x>);
// Could become this:
showExpression(
XmlUtil.createElement(
'x',
[ 'y' => Std.string('uh, ${1+4}'), ],
[
XmlUtil.createElement('child1'),
Xml.createPCData(Std.string('hi 2×4=${2*4}')),
XmlUtil.createElement(
'child2',
[ 'c2a1' => Std.string('2'), ]),
]));
}
macro static function showExpression(e:Expr) {
trace(new Printer().printExpr(e));
trace(e.expr);
return macro {
Sys.stdout().writeString(Std.string($e));
Sys.stdout().writeString('\n');
};
}
}
class XmlUtil {
public static function createElement(name, ?attributes:StringMap<String>, ?children:Array<Xml>) {
var element = Xml.createElement(name);
if (attributes != null) {
for (attributeName in attributes.keys()) {
element.set(attributeName, attributes.get(attributeName));
}
}
if (children != null) {
for (child in children) {
element.addChild(child);
}
}
return element;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment