My task is to parse a bracketed string, like
[foo | bar | foobar], to a vector of std::strings.
In this case,
the vector should end up with the contents {"foo" , "bar", "foobar"}.
These brackets can be nested. For example, the given bracketed string
[[john | doe] | [ bob | dylan]]
would become { "[john | doe]" , "[bob | dylan] }"
The best I could manage so far is
int main(int argc, char ** argv)
{
const std::string input {argv[1]};
std::vector<std::string> res;
qi::phrase_parse(input.cbegin(), input.cend(),
'['
>> *qi::lexeme[ +(qi::char_ - '|') >> '|']
> -qi::lexeme[ +(qi::char_ - ']') >> ']' ],
qi::space ,
res);
for (const auto& v: res)
std::cout << v <<std::endl;
return 0;
}
which fails miserably for the nested case. Can somebody please point me in the right direction?
Note #1: Nested cases can be more than one.
Note #2: I welcome any simpler solutions, even without using Boost Spirit.