Boost.Spirit.Qi で文字列のパース
{文字列}={値}
の様に文字列が含まれている構文をパースする場合、
+qi::char_ >> '=' >> qi::int_
では、パースに失敗します。
+qi::char_ が、'=' にマッチしないよう設定する必要があるので、
+(qi::char_ - '=') >> '=' >> qi::int_
これでパースに成功します。
[ソース]
#include <boost/spirit/include/qi.hpp> #include <iostream> #include <string> namespace qi = boost::spirit::qi; template <typename P> void test_parser(char const* input, P const& p, bool full_match = true){ using boost::spirit::qi::parse; char const* f(input); char const* l(f + strlen(f)); if (parse(f, l, p) && (!full_match || (f == l))) std::cout << "ok" << std::endl; else std::cout << "fail" << std::endl; } int main(){ test_parser("boost=14700", +qi::char_ >> '=' >> qi::int_); test_parser("boost=14700", +(qi::char_ - '=') >> '=' >> qi::int_); return 0; }
[出力]
fail ok
[boost]
- ver 1.47.0