Boost.Spirit.Qi の test_parser
Qi の Reference に書かれている test_parser の実装はこれかな?
[ソース]
#include <boost/spirit/include/qi.hpp> #include <iostream> 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; } template <typename P> void test_phrase_parser(char const* input, P const& p, bool full_match = true){ using boost::spirit::qi::phrase_parse; using boost::spirit::qi::ascii::space; char const* f(input); char const* l(f + strlen(f)); if (phrase_parse(f, l, p, space) && (!full_match || (f == l))) std::cout << "ok" << std::endl; else std::cout << "fail" << std::endl; } // パースした結果を取得する場合 template <typename P, typename T> void test_parser_attr( char const* input, P const& p, T& attr, bool full_match = true){ using boost::spirit::qi::parse; char const* f(input); char const* l(f + strlen(f)); if (parse(f, l, p, attr) && (!full_match || (f == l))) std::cout << "ok" << std::endl; else std::cout << "fail" << std::endl; } template <typename P, typename T> void test_phrase_parser_attr( char const* input, P const& p, T& attr, bool full_match = true){ using boost::spirit::qi::phrase_parse; using boost::spirit::qi::ascii::space; char const* f(input); char const* l(f + strlen(f)); if (phrase_parse(f, l, p, space, attr) && (!full_match || (f == l))) std::cout << "ok" << std::endl; else std::cout << "fail" << std::endl; } int main(){ namespace qi = boost::spirit::qi; test_parser("x", qi::char_('x')); test_parser("z", qi::char_('x')); std::cout << "\n"; int value = 0; test_parser_attr("{100}", '{' >> qi::int_ >> '}', value); std::cout << value << std::endl; return 0; }
[出力]
ok fail ok 100
ちょっとしたコードを試す場合には便利そうですね。
[boost]
- ver 1.47.0