1 | /*============================================================================= |
---|
2 | Copyright (c) 2003 Sam Nabialek |
---|
3 | Copyright (c) 2003-2004 Joel de Guzman |
---|
4 | http://spirit.sourceforge.net/ |
---|
5 | |
---|
6 | Use, modification and distribution is subject to the Boost Software |
---|
7 | License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
---|
8 | http://www.boost.org/LICENSE_1_0.txt) |
---|
9 | =============================================================================*/ |
---|
10 | #include <iostream> |
---|
11 | |
---|
12 | #define BOOST_SPIRIT_DEBUG |
---|
13 | |
---|
14 | #include <boost/spirit/core.hpp> |
---|
15 | #include <boost/spirit/error_handling.hpp> |
---|
16 | #include <boost/spirit/iterator.hpp> |
---|
17 | #include <boost/spirit/symbols.hpp> |
---|
18 | #include <boost/spirit/utility.hpp> |
---|
19 | |
---|
20 | using namespace boost::spirit; |
---|
21 | |
---|
22 | template <typename Rule> |
---|
23 | struct SetRest |
---|
24 | { |
---|
25 | SetRest(Rule& the_rule) |
---|
26 | : m_the_rule(the_rule) |
---|
27 | { |
---|
28 | } |
---|
29 | |
---|
30 | void operator()(Rule* new_rule) const |
---|
31 | { |
---|
32 | m_the_rule = *new_rule; |
---|
33 | } |
---|
34 | |
---|
35 | private: |
---|
36 | |
---|
37 | Rule& m_the_rule; |
---|
38 | }; |
---|
39 | |
---|
40 | |
---|
41 | struct nabialek_trick : public grammar<nabialek_trick> |
---|
42 | { |
---|
43 | template <typename ScannerT> |
---|
44 | struct definition |
---|
45 | { |
---|
46 | typedef rule<ScannerT> rule_t; |
---|
47 | |
---|
48 | rule_t name; |
---|
49 | rule_t line; |
---|
50 | rule_t rest; |
---|
51 | rule_t main; |
---|
52 | rule_t one; |
---|
53 | rule_t two; |
---|
54 | |
---|
55 | symbols<rule_t*> continuations; |
---|
56 | |
---|
57 | definition(nabialek_trick const& self) |
---|
58 | { |
---|
59 | name = lexeme_d[repeat_p(1,20)[alnum_p | '_']]; |
---|
60 | |
---|
61 | one = name; |
---|
62 | two = name >> ',' >> name; |
---|
63 | |
---|
64 | continuations.add |
---|
65 | ("one", &one) |
---|
66 | ("two", &two) |
---|
67 | ; |
---|
68 | |
---|
69 | line = continuations[SetRest<rule_t>(rest)] >> rest; |
---|
70 | main = *line; |
---|
71 | |
---|
72 | BOOST_SPIRIT_DEBUG_RULE(name); |
---|
73 | BOOST_SPIRIT_DEBUG_RULE(line); |
---|
74 | BOOST_SPIRIT_DEBUG_RULE(rest); |
---|
75 | BOOST_SPIRIT_DEBUG_RULE(main); |
---|
76 | BOOST_SPIRIT_DEBUG_RULE(one); |
---|
77 | BOOST_SPIRIT_DEBUG_RULE(two); |
---|
78 | } |
---|
79 | |
---|
80 | rule_t const& |
---|
81 | start() const |
---|
82 | { |
---|
83 | return main; |
---|
84 | } |
---|
85 | }; |
---|
86 | }; |
---|
87 | |
---|
88 | int |
---|
89 | main() |
---|
90 | { |
---|
91 | nabialek_trick g; |
---|
92 | parse("one only\none again\ntwo first,second", g, space_p); |
---|
93 | return 0; |
---|
94 | } |
---|