| 1 |
|
|---|
| 2 | #include <boost/spirit/include/qi.hpp>
|
|---|
| 3 | #include <boost/spirit/include/phoenix_operator.hpp>
|
|---|
| 4 |
|
|---|
| 5 | #include "map.hpp"
|
|---|
| 6 |
|
|---|
| 7 | namespace qi = boost::spirit::qi;
|
|---|
| 8 | namespace qi_repo = boost::spirit::repository::qi;
|
|---|
| 9 | namespace ascii = boost::spirit::ascii;
|
|---|
| 10 |
|
|---|
| 11 | int main(int argc, char **argv)
|
|---|
| 12 | {
|
|---|
| 13 | using boost::spirit::ascii::space;
|
|---|
| 14 | typedef std::string::const_iterator iter_type;
|
|---|
| 15 | using qi::_a;
|
|---|
| 16 | using qi::_1;
|
|---|
| 17 |
|
|---|
| 18 | qi::rule<iter_type, ascii::space_type>
|
|---|
| 19 | id = qi::lexeme[*(ascii::alnum | '_')],
|
|---|
| 20 | one = id, two = id >> ',' >> id;
|
|---|
| 21 |
|
|---|
| 22 | typedef qi::rule<iter_type, ascii::space_type> term_type;
|
|---|
| 23 |
|
|---|
| 24 | std::map<int, term_type*> keymap;
|
|---|
| 25 | keymap[1] = &one;
|
|---|
| 26 | keymap[2] = &two;
|
|---|
| 27 |
|
|---|
| 28 | qi::rule<iter_type, ascii::space_type, qi::locals<term_type*> >
|
|---|
| 29 | start = *(qi_repo::map(keymap)[qi::int_][_a = _1] >> qi::lazy(*_a));
|
|---|
| 30 |
|
|---|
| 31 | std::string str = "1 only\n1 again\n2 first,second\n0 ugh";
|
|---|
| 32 | iter_type iter = str.begin();
|
|---|
| 33 | iter_type end = str.end();
|
|---|
| 34 |
|
|---|
| 35 | bool r = phrase_parse(iter, end, start, space);
|
|---|
| 36 |
|
|---|
| 37 | if (r && iter == end) {
|
|---|
| 38 | std::cout << "-------------------------\n";
|
|---|
| 39 | std::cout << "Parsing succeeded\n";
|
|---|
| 40 | std::cout << "-------------------------\n";
|
|---|
| 41 | } else {
|
|---|
| 42 | std::string rest(iter, end);
|
|---|
| 43 | std::cout << "-------------------------\n";
|
|---|
| 44 | std::cout << "Parsing failed\n";
|
|---|
| 45 | std::cout << "stopped at: \": " << rest << "\"\n";
|
|---|
| 46 | std::cout << "-------------------------\n";
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | return 0;
|
|---|
| 50 | }
|
|---|