Dynamically combine Boost.Spirit.Qi rules at runtime (arbitrary number of alternatives)

一世执手 提交于 2019-12-02 18:26:38
n1ckp

Yes I'm not sure to really understand how it work internally but you do not copy all the rules in your for loop (only the left one) so this seems to work:

std::vector<qi::rule<iterator_t, void(int&)>> rules;

rules.push_back(qi::char_('a')[qi::_r1 = px::val(0)]);
rules.push_back(qi::char_('b')[qi::_r1 = px::val(1)]);
rules.push_back(qi::char_('c')[qi::_r1 = px::val(2)]);

std::vector<qi::rule<iterator_t, void(int&)>>::iterator
 i(rules.begin()), last(rules.end());

qi::rule<iterator_t, int()> temp;

for(; i!=last; ++i)
{
qi::rule<iterator_t, int()> tmp = (*i)(qi::_val)[no_op];

    temp = temp.copy() | tmp.copy();
}

qi::rule<iterator_t, void(int&)> grammar;

grammar = temp[qi::_r1 = qi::_1];

// Tests

int intres1;
int intres2;
int intres3;

bool res1 = parse_full("a", grammar(px::ref(intres1)) );
bool res2 = parse_full("b", grammar(px::ref(intres2)) );
bool res3 = parse_full("c", grammar(px::ref(intres3)) );
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!