I was trying to define a parser where the rules are not fully pre-defined, i.e. they contain a variable part. This was no problem with Spirit Qi, but I was not able to implement it due to the static nature of X3. I tried the with directive, which is unfortunately undocumented, but without luck so far. The only examples I found so far are inside a lambda expression.
I constructed a simple example to demonstrate the issue: parsing integers where the separator is given as a parameter.
#include <boost/spirit/home/x3.hpp>
#include <iostream>
namespace x3 = boost::spirit::x3;
namespace parsing {
x3::rule<struct parser> parser {"parser"};
//struct separator {};
char separator(',');
//auto parser_def = x3::int_ % x3::lit(x3::get<separator>(/* context */)); // candidate function template not viable: requires single argument 'context'
auto parser_def = x3::int_ % x3::lit(separator);
BOOST_SPIRIT_DEFINE(parser)
}
void parse(const std::string &data, const char separator) {
using namespace std;
//auto parser = x3::with<parsing::separator>(ref(separator)) [parsing::parser] >> x3::eoi;
auto parser = parsing::parser >> x3::eoi;
if (x3::parse(data.begin(), data.end(), parser))
cout << "Parse succeeded\n";
else
cout << "Parse failed\n";
}
int main() {
parse("1 2 3", ' ');
parse("1,2,3", ',');
parse("1;2;3", ';');
}
I commented out the parts where I tried to use the with directive.
Is this currently possible with X3? Has anyone done this before?