Chris@16
|
1 /*=============================================================================
|
Chris@16
|
2 Copyright (c) 2003 Hartmut Kaiser
|
Chris@16
|
3 http://spirit.sourceforge.net/
|
Chris@16
|
4
|
Chris@16
|
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
7 =============================================================================*/
|
Chris@16
|
8 #ifndef BOOST_SPIRIT_SELECT_HPP
|
Chris@16
|
9 #define BOOST_SPIRIT_SELECT_HPP
|
Chris@16
|
10
|
Chris@16
|
11 #include <boost/preprocessor/repeat.hpp>
|
Chris@16
|
12 #include <boost/preprocessor/enum.hpp>
|
Chris@16
|
13 #include <boost/preprocessor/enum_params.hpp>
|
Chris@16
|
14 #include <boost/preprocessor/enum_params_with_defaults.hpp>
|
Chris@16
|
15 #include <boost/preprocessor/inc.hpp>
|
Chris@16
|
16 #include <boost/preprocessor/cat.hpp>
|
Chris@16
|
17 #include <boost/preprocessor/facilities/intercept.hpp>
|
Chris@16
|
18
|
Chris@16
|
19 #include <boost/spirit/home/classic/namespace.hpp>
|
Chris@16
|
20 #include <boost/spirit/home/classic/core/parser.hpp>
|
Chris@16
|
21
|
Chris@16
|
22 #include <boost/spirit/home/classic/phoenix/tuples.hpp>
|
Chris@16
|
23
|
Chris@16
|
24 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
25 //
|
Chris@16
|
26 // Spirit predefined maximum number of possible embedded select_p parsers.
|
Chris@16
|
27 // It should NOT be greater than PHOENIX_LIMIT!
|
Chris@16
|
28 //
|
Chris@16
|
29 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
30 #if !defined(BOOST_SPIRIT_SELECT_LIMIT)
|
Chris@16
|
31 #define BOOST_SPIRIT_SELECT_LIMIT PHOENIX_LIMIT
|
Chris@16
|
32 #endif // !defined(BOOST_SPIRIT_SELECT_LIMIT)
|
Chris@16
|
33
|
Chris@16
|
34 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
35 //
|
Chris@16
|
36 // ensure BOOST_SPIRIT_SELECT_LIMIT <= PHOENIX_LIMIT and
|
Chris@16
|
37 // BOOST_SPIRIT_SELECT_LIMIT > 0
|
Chris@16
|
38 // BOOST_SPIRIT_SELECT_LIMIT <= 15
|
Chris@16
|
39 //
|
Chris@16
|
40 // [Pushed this down a little to make CW happy with BOOST_STATIC_ASSERT]
|
Chris@16
|
41 // [Otherwise, it complains: 'boost_static_assert_test_42' redefined]
|
Chris@16
|
42 //
|
Chris@16
|
43 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
44 BOOST_STATIC_ASSERT(BOOST_SPIRIT_SELECT_LIMIT <= PHOENIX_LIMIT);
|
Chris@16
|
45 BOOST_STATIC_ASSERT(BOOST_SPIRIT_SELECT_LIMIT > 0);
|
Chris@16
|
46 BOOST_STATIC_ASSERT(BOOST_SPIRIT_SELECT_LIMIT <= 15);
|
Chris@16
|
47
|
Chris@16
|
48 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
49 //
|
Chris@16
|
50 // Calculate the required amount of tuple members rounded up to the nearest
|
Chris@16
|
51 // integer dividable by 3
|
Chris@16
|
52 //
|
Chris@16
|
53 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
54 #if BOOST_SPIRIT_SELECT_LIMIT > 12
|
Chris@16
|
55 #define BOOST_SPIRIT_SELECT_LIMIT_A 15
|
Chris@16
|
56 #elif BOOST_SPIRIT_SELECT_LIMIT > 9
|
Chris@16
|
57 #define BOOST_SPIRIT_SELECT_LIMIT_A 12
|
Chris@16
|
58 #elif BOOST_SPIRIT_SELECT_LIMIT > 6
|
Chris@16
|
59 #define BOOST_SPIRIT_SELECT_LIMIT_A 9
|
Chris@16
|
60 #elif BOOST_SPIRIT_SELECT_LIMIT > 3
|
Chris@16
|
61 #define BOOST_SPIRIT_SELECT_LIMIT_A 6
|
Chris@16
|
62 #else
|
Chris@16
|
63 #define BOOST_SPIRIT_SELECT_LIMIT_A 3
|
Chris@16
|
64 #endif
|
Chris@16
|
65
|
Chris@16
|
66 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
67 namespace boost { namespace spirit {
|
Chris@16
|
68
|
Chris@16
|
69 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
|
Chris@16
|
70
|
Chris@16
|
71 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
72 //
|
Chris@16
|
73 // The select_default_no_fail and select_default_fail structs are used to
|
Chris@16
|
74 // distinguish two different behaviours for the select_parser in case that not
|
Chris@16
|
75 // any of the given sub-parsers match.
|
Chris@16
|
76 //
|
Chris@16
|
77 // If the select_parser is used with the select_default_no_fail behaviour,
|
Chris@16
|
78 // then in case of no matching sub-parser the whole select_parser returns an
|
Chris@16
|
79 // empty match and the value -1.
|
Chris@16
|
80 //
|
Chris@16
|
81 // If the select_parser is used with the select_default_fail behaviour, then
|
Chris@16
|
82 // in case of no matching sub-parser the whole select_parser fails to match at
|
Chris@16
|
83 // all.
|
Chris@16
|
84 //
|
Chris@16
|
85 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
86 struct select_default_no_fail {};
|
Chris@16
|
87 struct select_default_fail {};
|
Chris@16
|
88
|
Chris@16
|
89 BOOST_SPIRIT_CLASSIC_NAMESPACE_END
|
Chris@16
|
90
|
Chris@16
|
91 }} // namespace BOOST_SPIRIT_CLASSIC_NS
|
Chris@16
|
92
|
Chris@16
|
93 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
94 #include <boost/spirit/home/classic/dynamic/impl/select.ipp>
|
Chris@16
|
95
|
Chris@16
|
96 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
97 namespace boost { namespace spirit {
|
Chris@16
|
98
|
Chris@16
|
99 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
|
Chris@16
|
100
|
Chris@16
|
101 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
102 template <typename TupleT, typename BehaviourT, typename T>
|
Chris@16
|
103 struct select_parser
|
Chris@16
|
104 : public parser<select_parser<TupleT, BehaviourT, T> >
|
Chris@16
|
105 {
|
Chris@16
|
106 typedef select_parser<TupleT, BehaviourT, T> self_t;
|
Chris@16
|
107
|
Chris@16
|
108 select_parser(TupleT const &t_)
|
Chris@16
|
109 : t(t_)
|
Chris@16
|
110 {}
|
Chris@16
|
111
|
Chris@16
|
112 template <typename ScannerT>
|
Chris@16
|
113 struct result
|
Chris@16
|
114 {
|
Chris@16
|
115 typedef typename match_result<ScannerT, T>::type type;
|
Chris@16
|
116 };
|
Chris@16
|
117
|
Chris@16
|
118 template <typename ScannerT>
|
Chris@16
|
119 typename parser_result<self_t, ScannerT>::type
|
Chris@16
|
120 parse(ScannerT const& scan) const
|
Chris@16
|
121 {
|
Chris@16
|
122 typedef typename parser_result<self_t, ScannerT>::type result_t;
|
Chris@16
|
123
|
Chris@16
|
124 if (!scan.at_end()) {
|
Chris@16
|
125 return impl::parse_tuple_element<
|
Chris@16
|
126 TupleT::length, result_t, TupleT, BehaviourT>::do_(t, scan);
|
Chris@16
|
127 }
|
Chris@16
|
128 return impl::select_match_gen<result_t, BehaviourT>::do_(scan);
|
Chris@16
|
129 }
|
Chris@16
|
130
|
Chris@16
|
131 TupleT const t;
|
Chris@16
|
132 };
|
Chris@16
|
133
|
Chris@16
|
134 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
135 template <typename BehaviourT, typename T = int>
|
Chris@16
|
136 struct select_parser_gen {
|
Chris@16
|
137
|
Chris@16
|
138 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
139 //
|
Chris@16
|
140 // This generates different select_parser_gen::operator()() functions with
|
Chris@16
|
141 // an increasing number of parser parameters:
|
Chris@16
|
142 //
|
Chris@16
|
143 // template <typename ParserT0, ...>
|
Chris@16
|
144 // select_parser<
|
Chris@16
|
145 // ::phoenix::tuple<
|
Chris@16
|
146 // typename impl::as_embedded_parser<ParserT0>::type,
|
Chris@16
|
147 // ...
|
Chris@16
|
148 // >,
|
Chris@16
|
149 // BehaviourT,
|
Chris@16
|
150 // T
|
Chris@16
|
151 // >
|
Chris@16
|
152 // operator()(ParserT0 const &p0, ...) const
|
Chris@16
|
153 // {
|
Chris@16
|
154 // typedef impl::as_embedded_parser<ParserT0> parser_t0;
|
Chris@16
|
155 // ...
|
Chris@16
|
156 //
|
Chris@16
|
157 // typedef ::phoenix::tuple<
|
Chris@16
|
158 // parser_t0::type,
|
Chris@16
|
159 // ...
|
Chris@16
|
160 // > tuple_t;
|
Chris@16
|
161 // typedef select_parser<tuple_t, BehaviourT, T> result_t;
|
Chris@16
|
162 //
|
Chris@16
|
163 // return result_t(tuple_t(
|
Chris@16
|
164 // parser_t0::convert(p0),
|
Chris@16
|
165 // ...
|
Chris@16
|
166 // ));
|
Chris@16
|
167 // }
|
Chris@16
|
168 //
|
Chris@16
|
169 // The number of generated functions depends on the maximum tuple member
|
Chris@16
|
170 // limit defined by the PHOENIX_LIMIT pp constant.
|
Chris@16
|
171 //
|
Chris@16
|
172 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
173 #define BOOST_SPIRIT_SELECT_EMBEDDED(z, N, _) \
|
Chris@16
|
174 typename impl::as_embedded_parser<BOOST_PP_CAT(ParserT, N)>::type \
|
Chris@16
|
175 /**/
|
Chris@16
|
176 #define BOOST_SPIRIT_SELECT_EMBEDDED_TYPEDEF(z, N, _) \
|
Chris@16
|
177 typedef impl::as_embedded_parser<BOOST_PP_CAT(ParserT, N)> \
|
Chris@16
|
178 BOOST_PP_CAT(parser_t, N); \
|
Chris@16
|
179 /**/
|
Chris@16
|
180 #define BOOST_SPIRIT_SELECT_CONVERT(z, N, _) \
|
Chris@16
|
181 BOOST_PP_CAT(parser_t, N)::convert(BOOST_PP_CAT(p, N)) \
|
Chris@16
|
182 /**/
|
Chris@16
|
183
|
Chris@16
|
184 #define BOOST_SPIRIT_SELECT_PARSER(z, N, _) \
|
Chris@16
|
185 template < \
|
Chris@16
|
186 BOOST_PP_ENUM_PARAMS_Z(z, BOOST_PP_INC(N), typename ParserT) \
|
Chris@16
|
187 > \
|
Chris@16
|
188 select_parser< \
|
Chris@16
|
189 ::phoenix::tuple< \
|
Chris@16
|
190 BOOST_PP_ENUM_ ## z(BOOST_PP_INC(N), \
|
Chris@16
|
191 BOOST_SPIRIT_SELECT_EMBEDDED, _) \
|
Chris@16
|
192 >, \
|
Chris@16
|
193 BehaviourT, \
|
Chris@16
|
194 T \
|
Chris@16
|
195 > \
|
Chris@16
|
196 operator()( \
|
Chris@16
|
197 BOOST_PP_ENUM_BINARY_PARAMS_Z(z, BOOST_PP_INC(N), \
|
Chris@16
|
198 ParserT, const &p) \
|
Chris@16
|
199 ) const \
|
Chris@16
|
200 { \
|
Chris@16
|
201 BOOST_PP_REPEAT_ ## z(BOOST_PP_INC(N), \
|
Chris@16
|
202 BOOST_SPIRIT_SELECT_EMBEDDED_TYPEDEF, _) \
|
Chris@16
|
203 \
|
Chris@16
|
204 typedef ::phoenix::tuple< \
|
Chris@16
|
205 BOOST_PP_ENUM_BINARY_PARAMS_Z(z, BOOST_PP_INC(N), \
|
Chris@16
|
206 typename parser_t, ::type BOOST_PP_INTERCEPT) \
|
Chris@16
|
207 > tuple_t; \
|
Chris@16
|
208 typedef select_parser<tuple_t, BehaviourT, T> result_t; \
|
Chris@16
|
209 \
|
Chris@16
|
210 return result_t(tuple_t( \
|
Chris@16
|
211 BOOST_PP_ENUM_ ## z(BOOST_PP_INC(N), \
|
Chris@16
|
212 BOOST_SPIRIT_SELECT_CONVERT, _) \
|
Chris@16
|
213 )); \
|
Chris@16
|
214 } \
|
Chris@16
|
215 /**/
|
Chris@16
|
216
|
Chris@16
|
217 BOOST_PP_REPEAT(BOOST_SPIRIT_SELECT_LIMIT_A,
|
Chris@16
|
218 BOOST_SPIRIT_SELECT_PARSER, _)
|
Chris@16
|
219
|
Chris@16
|
220 #undef BOOST_SPIRIT_SELECT_PARSER
|
Chris@16
|
221 #undef BOOST_SPIRIT_SELECT_CONVERT
|
Chris@16
|
222 #undef BOOST_SPIRIT_SELECT_EMBEDDED_TYPEDEF
|
Chris@16
|
223 #undef BOOST_SPIRIT_SELECT_EMBEDDED
|
Chris@16
|
224 ///////////////////////////////////////////////////////////////////////////
|
Chris@16
|
225 };
|
Chris@16
|
226
|
Chris@16
|
227 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
228 //
|
Chris@16
|
229 // Predefined parser generator helper objects
|
Chris@16
|
230 //
|
Chris@16
|
231 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
232 select_parser_gen<select_default_no_fail> const select_p =
|
Chris@16
|
233 select_parser_gen<select_default_no_fail>();
|
Chris@16
|
234
|
Chris@16
|
235 select_parser_gen<select_default_fail> const select_fail_p =
|
Chris@16
|
236 select_parser_gen<select_default_fail>();
|
Chris@16
|
237
|
Chris@16
|
238 #undef BOOST_SPIRIT_SELECT_LIMIT_A
|
Chris@16
|
239
|
Chris@16
|
240 ///////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
241 BOOST_SPIRIT_CLASSIC_NAMESPACE_END
|
Chris@16
|
242
|
Chris@16
|
243 }} // namespace BOOST_SPIRIT_CLASSIC_NS
|
Chris@16
|
244
|
Chris@16
|
245 #endif // BOOST_SPIRIT_SELECT_HPP
|