c++ - How do I suppress skipping in boost::spirit for parsing a quoted string? -
this question has answer here:
- boost spirit skipper issues 1 answer
i'm trying implement simple parser using boost::spirit (among other things) accepts strings in double quotes, e.g. "hello, world". here's stripped down program illustrates issue:
#include <iostream> #include <boost/iostreams/filtering_stream.hpp> #include <boost/spirit/include/qi.hpp> #include <boost/spirit/include/support_istream_iterator.hpp> namespace qi = boost::spirit::qi; static void print(const std::vector<char>& v) { std::cout << std::string(v.begin(), v.end()) << '\n'; } template<typename iterator> class grammar : public qi::grammar<iterator, boost::spirit::ascii::space_type> { typedef boost::spirit::ascii::space_type space_type; qi::rule<iterator, space_type> start; qi::rule<iterator, std::vector<char>()> string; qi::rule<iterator, char()> unescapedchar; public: grammar() : grammar::base_type(start) { start = string[&print]; string %= qi::lexeme['"' >> qi::no_skip[*unescapedchar] >> '"']; unescapedchar %= qi::char_ - qi::lit('"'); } }; int main() { const std::string s("\"how brown cow\""); boost::iostreams::filtering_istream fs(boost::make_iterator_range(s.begin(), s.end()));; typedef boost::spirit::istream_iterator iterator; qi::phrase_parse( iterator(fs), iterator(), grammar<iterator>(), boost::spirit::ascii::space); return 0; }
in program unescapedchar
rule defines allowable characters within string (anything double quotes) , string
rule kleene star of unescapedchar
within double quotes.
the output of program when passed "how brown cow"
(with quotes part of string) hownowbrowncow
. std::vector<char>
attribute of string
missing spaces. want spaces in parsed result.
neither unescapedchar
nor string
rule declarations specify skip type. tried use lexeme
, no_skip
directives both individually , (as shown above). have tried boost 1.49 , boost 1.55. in each case output has no embedded spaces.
in actual parser want skip spaces, not within quoted strings. doing incorrectly?
i think problem input stream skipping whitespace
see http://en.cppreference.com/w/cpp/io/manip/skipws
adding
fs.unsetf(std::ios::skipws);
fixes things
see live on coliru
Comments
Post a Comment