mercredi 30 juin 2021

Boost::spirit::qi parse alternative variant

I need to parse string with parameters A and B. Order of the parameters not defined. I.e. string can be present as one of the next formats

A="value1",B="value2"
B="value1",A="value2"

Part of my code you can see below. But in that code I can parse only A="value1",B="value2" variant. Could I modify this code to parse the first and second variants together? Yes, I can add alternative condition ("|"). But what if I need to parse new C and D parameters.

using Iterator = std::string::const_iterator;
qi::rule<Iterator, std::string()> quotedStringParser;
quotedStringParser %= ('"' >> +(qi::char_ - '"') >> '"');

std::string A;
std::string B;
bool isImport = false;

if (!qi::parse(begin(line), end(line),
    ("A=" >> quotedStringParser[px::ref(A) = qi::_1] >> ',' >> "B=" >> quotedStringParser[px::ref(B) = qi::_1]) >> qi::eoi
)) {
    return false;
}

Aucun commentaire:

Enregistrer un commentaire