boost::spirit::karma output of string in quotation marks -
i trying escape string in quotation marks using boost::spirit::karma. works fine if it's string. however, string in boost::variant in std::vector, not. printing string work however, not quite understand why.
line (1) works fine, doesn't want. line (2) should it, doesn't.
#include <iostream> #include <string> #include <boost/variant.hpp> #include <boost/spirit/include/karma.hpp> namespace karma = boost::spirit::karma; typedef std::vector<boost::variant<int, std::string>> parameterlist; typedef boost::variant<int, std::string, parameterlist> parameter; main() { using karma::int_; using boost::spirit::ascii::string; using karma::eol; using karma::lit; std::string generated; std::back_insert_iterator<std::string> sink(generated); // (1) karma::rule<std::back_insert_iterator<std::string>, parameterlist()> parameterlistrule = (int_ | string) % lit(", "); // works! // (2) //karma::rule<std::back_insert_iterator<std::string>, parameterlist()> parameterlistrule = (int_ | (lit('"') << string << lit('"'))) % lit(", "); // not work karma::rule<std::back_insert_iterator<std::string>, parameter()> parameterrule = (int_ | (lit('"') << string << lit('"')) | parameterlistrule) << eol; // work, though escapes string in pair of quotation marks karma::generate(sink, parameterrule, 1); // works karma::generate(sink, parameterrule, "foo"); // works karma::generate(sink, parameterrule, parameter(parameterlist{1, "foo"})); // works using rule (1), not (2) std::cout << generated; }
if iterate data types, should iterate rules.
#include <iostream> #include <string> #include <boost/variant.hpp> #include <boost/spirit/include/karma.hpp> namespace karma = boost::spirit::karma; typedef boost::variant<int, std::string> item; typedef std::vector<item> parameterlist; typedef boost::variant<int, std::string, parameterlist> parameter; int main() { using karma::int_; using boost::spirit::ascii::string; using karma::eol; using karma::lit; std::string generated; std::back_insert_iterator<std::string> sink(generated); karma::rule<std::back_insert_iterator<std::string>, item()> itemrule = int_ | (lit('"') << string << lit('"')); karma::rule<std::back_insert_iterator<std::string>, parameterlist()> parameterlistrule = itemrule % lit(", "); karma::rule<std::back_insert_iterator<std::string>, parameter()> parameterrule = (int_ | (lit('"') << string << lit('"')) | parameterlistrule) << eol; karma::generate(sink, parameterrule, 1); karma::generate(sink, parameterrule, "foo"); karma::generate(sink, parameterrule, parameter(parameterlist {1, "foo"})); std::cout << generated; return 0; }
Comments
Post a Comment