1 | #include <iostream>
|
---|
2 | #include <string>
|
---|
3 | #include <boost/spirit/include/qi.hpp>
|
---|
4 | #include <boost/spirit/include/support.hpp>
|
---|
5 | #include <boost/spirit/include/phoenix.hpp>
|
---|
6 |
|
---|
7 |
|
---|
8 | namespace parsers
|
---|
9 | {
|
---|
10 | namespace qi = boost::spirit::qi;
|
---|
11 | namespace ascii = boost::spirit::ascii;
|
---|
12 | namespace ph = boost::phoenix;
|
---|
13 |
|
---|
14 | template <class Iterator>
|
---|
15 | struct gram_1:qi::grammar<Iterator,std::string() >
|
---|
16 | {
|
---|
17 | gram_1():gram_1::base_type(start)
|
---|
18 | {
|
---|
19 | start %= qi::raw[(ascii::string("abc") >> ascii::string("def") )
|
---|
20 | [std::cout << qi::_1 << std::endl]];
|
---|
21 | }
|
---|
22 | qi::rule<Iterator,std::string()> start;
|
---|
23 | };
|
---|
24 |
|
---|
25 | template <class Iterator>
|
---|
26 | struct gram_2:qi::grammar<Iterator,std::string() >
|
---|
27 | {
|
---|
28 | gram_2():gram_2::base_type(start)
|
---|
29 | {
|
---|
30 | start %= qi::raw['"' >> *ascii::string("abc") >> '"'];
|
---|
31 | }
|
---|
32 | qi::rule<Iterator,std::string() > start;
|
---|
33 | };
|
---|
34 |
|
---|
35 | }
|
---|
36 |
|
---|
37 | template <class P>
|
---|
38 | bool do_parse(const char *p, const P & parser)
|
---|
39 | {
|
---|
40 | std::string in(p);
|
---|
41 | std::string::iterator first(in.begin());
|
---|
42 | std::string result;
|
---|
43 |
|
---|
44 | bool r = phrase_parse(first,in.end(),parser,result);
|
---|
45 | if( r)
|
---|
46 | {
|
---|
47 | std::cout << "parse of \"" << in << "\" produced $" << result << "$\n";
|
---|
48 | }
|
---|
49 | else
|
---|
50 | {
|
---|
51 | std::cout << "parse of \"" << in << "\" failed\n";
|
---|
52 | }
|
---|
53 | return r;
|
---|
54 | }
|
---|
55 |
|
---|
56 | int main()
|
---|
57 | {
|
---|
58 | char *av[] = {"","abcdef","\"abcabcabc\""};
|
---|
59 |
|
---|
60 | parsers::gram_1<std::string::iterator> g_1;
|
---|
61 | parsers::gram_2<std::string::iterator> g_2;
|
---|
62 |
|
---|
63 | do_parse(av[1],g_1);
|
---|
64 | do_parse(av[2],g_2);
|
---|
65 | return 0;
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|