Created
August 24, 2011 02:04
-
-
Save hattorix/1167135 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <boost/spirit/include/qi.hpp> | |
#include <boost/spirit/include/phoenix.hpp> | |
using namespace std; | |
using namespace boost::spirit; | |
namespace phx = boost::phoenix; | |
struct Test | |
{ | |
void func1() { cout << "func1()\n"; } | |
void func2() { cout << "func2()\n"; } | |
void func3() { cout << "func3()\n"; } | |
void func4() { cout << "func4()\n"; } | |
}; | |
template <typename Clazz> | |
void helper(Clazz& p, void (Clazz::*func)()) | |
{ | |
(p.*func)(); | |
} | |
template<typename Iterator> | |
struct MyGrammar : public qi::grammar<Iterator, ascii::space_type> | |
{ | |
typedef void (Test::*TestFunc)(); | |
qi::rule<Iterator, ascii::space_type> rule_; | |
qi::rule<Iterator, Test* (), ascii::space_type> creator_; | |
qi::rule<Iterator, TestFunc (), ascii::space_type> selector_; | |
MyGrammar() : MyGrammar::base_type(rule_) | |
{ | |
rule_ = creator_; | |
creator_ = | |
lit("test") [_val = phx::new_<Test>()] | |
>> selector_ [phx::bind(&helper<Test>, *_val, qi::_1)] | |
// !!NG!! | |
// >> selector_ [phx::bind(qi::_1, _val)] | |
; | |
selector_ = | |
lit("1") [_val = &Test::func1] | |
| lit("2") [_val = &Test::func2] | |
| lit("3") [_val = &Test::func3] | |
| lit("4") [_val = &Test::func4] | |
; | |
} | |
}; | |
int main(int argc, char** argv) | |
{ | |
if (argc != 2) { | |
cout << "invalid argument\n"; | |
return 1; | |
} | |
string str = argv[1]; | |
MyGrammar<string::iterator> syntax; | |
if (!qi::phrase_parse(str.begin(), str.end(), syntax, ascii::space)) | |
cout << "syntax error\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment