within a semantic action I want to get the iterator, preferably the entire iterator range from the first to last parsed character. When using the raw
directive I could simply get it with _attr(context)
. I guessed that _where(context)
does this, but it only returns an empty range whose begin iterator points to the character after the parsed substring.
Sample code:
#include <boost/spirit/home/x3.hpp>
#include <iostream>
#include <utility>
namespace x3 = boost::spirit::x3;
int main()
{
const auto action = [](auto &ctx)
{
auto range = x3::_where(ctx);
std::cout << range.size() << '\n';
std::cout << "range start: " << static_cast<const void*>(&*range.begin()) << '\n';
};
const auto rule = x3::int_[action];
const std::string input = "432";
std::cout << "string start: " << static_cast<const void*>(input.data()) << '\n';
int output;
x3::phrase_parse(input.begin(), input.end(), rule, x3::space, output);
std::cout << output << '\n';
}
Output
string start: 0x7ffd65f337c0
0
range start: 0x7ffd65f337c3
432
The length of the range is 0 and begin() of it points to the end of string. When I expand the input string the range covers the remaining unparsed substring.
How can I get the iterator range that contains the parsed substring?