Giving the code
class Test {
static function main() {
var i = 1;
trace(m('some before $i some after')); // some before 1 some after
}
static macro function m(e: haxe.macro.Expr)
{
trace(e); // { expr => EConst(CString(some before $i some after)), pos => #pos(Test.hx:4: characters 12-39) }
// trace the name of referenced var
/*
trace();
*/
return macro $e;
}
}
what should I place in commented out code to trace()
the name of the variable used inside an interpolated String
expression without manual parsing of the string constant?
What you want is formatString
:
class Test {
#if !macro
static function main() {
var i = 1;
trace(m('some before $i some after')); // some before 1 some after
}
#end
static macro function m(e: haxe.macro.Expr)
{
switch e.expr {
case EConst(CString(s)):
trace(haxe.macro.MacroStringTools.formatString(s, e.pos));
default:
}
return e;
}
}