How to use spirit's qi attributes properly?

96 views Asked by At

I want to parse something like "a=1;b=2;{c=3;d=4;}e=5;f=6;". My problem is how to handle the block within the '{}' chars. The values within the block shall be parsed (but not propagated to attribute of r1) if ignoreBlock is true.

using namespace boost::spirit;
using namespace std;
bool ignoreBlock = true;
qi::rule<std::string::iterator, vector<pair<string, int>>()> r1;
qi::rule<std::string::iterator, pair<string, int>()> r2;
qi::rule<std::string::iterator, vector<pair<string, int>>()> r3;
r1 = *(r2 | (qi::eps(phoenix::ref(ignoreBlock)==true) >> qi::omit[r3]) | r3);
r2 = qi::as<string>()[+qi::alnum] >> '=' >> qi::int_ >> ';';
r3 = '{' >> *r2 >> '}';
string s = { "a=1;b=2;{c=3;d=4;}e=5;f=6;"};
vector<pair<string, int>> v;
phrase_parse(s.begin(), s.end(), r1,  boost::spirit::ascii::space, v);

This code causes some error like: 'static_cast': cannot convert from const std::vector<std::pair<std::string,int>,std::allocator<_Ty>>' to 'std::pair<std::string,int>'

How do I handle such conditional parts correctly?

1

There are 1 answers

1
Caleth On

Why do you bind ignoreBlock in the rules, when you don't have anything changing it during parsing?

qi::rule<std::string::iterator, vector<pair<string, int>>()> r1;
qi::rule<std::string::iterator, pair<string, int>()> r2;
r2 = qi::as<string>()[+qi::alnum] >> '=' >> qi::int_ >> ';';

if (ignoreBlock)
{
    qi::rule<std::string::iterator, void()> r3;
    r1 = *(r2 | r3);
    r3 = '{' >> *r2 >> '}';
}
else
{
    r1 = *(r2 | qi::lit('{') | qi::lit('}'));
}

This doesn't reject unbalanced parentheses, but can be adapted to.