I wrote a semantic action for my Boost Spirit Lexer to convert escape sequences in strings to what they stand for. It works perfectly and I want to convert it to a Boost Phoenix expression, but can't get that one to compile.
Here is what works:
// the semantic action
struct ConvertEscapes
{
template <typename ItT, typename IdT, typename CtxT>
void operator () (ItT& start, ItT& end, lex::pass_flags& matched, IdT& id, CtxT& ctx)
{
static boost::wregex escapeRgx(L"(\\\\r)|(\\\\n)|(\\\\t)|(\\\\\\\\)|(\\\\\")");
static std::wstring escapeRepl = L"(?1\r)(?2\n)(?3\t)(?4\\\\)(?5\")";
static std::wstring wval; // static b/c set_value doesn't seem to copy
auto const& val = ctx.get_value();
wval.assign(val.begin(), val.end());
wval = boost::regex_replace(wval,
escapeRgx,
escapeRepl,
boost::match_default | boost::format_all);
ctx.set_value(wval);
}
};
// the token declaration
lex::token_def<std::wstring, wchar_t> literal_str;
// the token definition
literal_str = L"\\\"([^\\\\\"]|(\\\\.))*\\\""; // string with escapes
// adding it to the lexer
this->self += literal_str [ ConvertEscapes() ];
This is what I tried to convert it:
this->self += literal_str
[
lex::_val = boost::regex_replace(lex::_val /* this is the place I can't figure out */,
boost::wregex(L"(\\\\r)|(\\\\n)|
(\\\\t)|(\\\\\\\\)|(\\\\\")"),
L"(?1\r)(?2\n)(?3\t)(?4\\\\)(?5\")",
boost::match_default | boost::format_all)
];
A wstring
can't be constructed from _val
. _val
also doesn't have begin()
or end()
, how is it supposed to be used anyway?
This std::wstring(lex::_start, lex::_end)
fails, too, because those arguments aren't recognized as iterators.
In this question, I found phoenix::construct<std::wstring>(lex::_start, lex::_end)
, but this also doesn't really result in a wstring
.
How do I get either a string or a pair of wchar_t
iterators for the current token?