c++ - How to capture character without consuming it in boost::spirit::qi -
i'm using boost::spirit::qi
parse "template" format looks this:
/path/to/:somewhere:/nifty.json
where :somewhere:
represents string identified name somewhere
(the name can series of characters between 2 :
characters). have working parser this, want create 1 additional improvement.
i know character follows :somewhere:
placeholder (in case /
). rest of parser still needs know /
, consume part of next section.
how can "read" /
after :somewhere:
without consuming rest of parser see , consume it.
as sehe mentioned can done using lookahead parser operator &, if want emit character you'll need boost.phoenix, qi::locals , qi::attr.
for example:
#include <boost/fusion/include/std_pair.hpp> #include <boost/spirit/include/phoenix.hpp> #include <boost/spirit/include/qi.hpp> #include <iostream> #include <string> namespace qi = boost::spirit::qi; int main(int argc, char** argv) { std::string input("foo:/bar"); std::pair<char, std::string> output; std::string::const_iterator begin = input.begin(), end = input.end(); qi::rule<std::string::const_iterator, qi::locals<char>, std::pair<char, std::string>()> duplicate = "foo" >> qi::omit[ &(":" >> qi::char_[qi::_a = qi::_1]) ] >> qi::attr(qi::_a) >> ":" >> *qi::char_; bool r = qi::parse(begin, end, duplicate, output); std::cout << std::boolalpha << r << " " << (begin == end) << " '" << output.first << "' \"" << output.second << "\"" << std::endl; homecoming 0; }
this outputs:
true true '/' "/bar"
c++ boost boost-spirit boost-spirit-qi
No comments:
Post a Comment