lundi 10 février 2020

Generic parser generator in boost::spirit::x3

I am trying to write generic parser generator in boost spirit. I have come up with following code:

auto attr_to_val = [](auto& ctx) { _val(ctx) = boost::fusion::at_c<2>(_attr(ctx)); };

auto parser_gen = [](const std::string a, auto&& p) {
    return((boost::spirit::x3::string(a) >> boost::spirit::x3::blank >> p)[attr_to_val]);
};

and tried to use it like this:

int a;
auto action = [&a](auto& ctx) { a = _val(ctx); };
auto parser = (parser_gen("aaa", boost::spirit::x3::uint_))[action];
parse(bar.begin(), bar.end(), parser);

but it gives a lot of errors about being unable to convert boost::fusion::deque to int. On the other hand when I change it a bit like that, which is IMHO equivalent to the expansion of above template code:

auto pars = (
    boost::spirit::x3::string("aaa") >>
    boost::spirit::x3::blank >> boost::spirit::x3::uint_)[attr_to_val];

int a;
auto action = [&a](auto& ctx) { a = _val(ctx); };
parse(bar.begin(), bar.end(), pars);

It is all fine. What am I doing wrong and how can I make parser_gen work?

Aucun commentaire:

Enregistrer un commentaire