What is the simplest way to make a semantic action that extracts a string from a typical identifier parser based on boost::spirit::x3::lexeme
?
I thought it might be possible to bypass needing to unpack the attribute and just use iterators into the input stream but apparently x3::_where
does not do what I think it does.
The following yields output
being empty. I expected it to contain "foobar_hello".
namespace x3 = boost::spirit::x3;
using x3::_where;
using x3::lexeme;
using x3::alpha;
auto ctx_to_string = [&](auto& ctx) {
_val(ctx) = std::string(_where(ctx).begin(), _where(ctx).end());
};
x3::rule<class identifier_rule_, std::string> const identifier_rule = "identifier_rule";
auto const identifier_rule_def = lexeme[(x3::alpha | '_') >> *(x3::alnum | '_')][ctx_to_string];
BOOST_SPIRIT_DEFINE(identifier_rule);
int main()
{
std::string input = "foobar_hello";
std::string output;
auto result = x3::parse(input.begin(), input.end(), identifier_rule, output);
}
Do I need to somehow extract the string from the boost::fusion objects in x3::_attr(ctx)
or am I doing something wrong?