First public contribution.
1 /*=============================================================================
2 Copyright (c) 2003 Hartmut Kaiser
3 http://spirit.sourceforge.net/
5 Use, modification and distribution is subject to the Boost Software
6 License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 http://www.boost.org/LICENSE_1_0.txt)
8 =============================================================================*/
9 #ifndef BOOST_SPIRIT_SELECT_HPP
10 #define BOOST_SPIRIT_SELECT_HPP
12 #include <boost/preprocessor/repeat.hpp>
13 #include <boost/preprocessor/enum.hpp>
14 #include <boost/preprocessor/enum_params.hpp>
15 #include <boost/preprocessor/enum_params_with_defaults.hpp>
16 #include <boost/preprocessor/inc.hpp>
17 #include <boost/preprocessor/cat.hpp>
18 #include <boost/preprocessor/facilities/intercept.hpp>
20 #include <boost/spirit/core/parser.hpp>
22 #include <boost/spirit/phoenix/tuples.hpp>
24 ///////////////////////////////////////////////////////////////////////////////
26 // Spirit predefined maximum number of possible embedded select_p parsers.
27 // It should NOT be greater than PHOENIX_LIMIT!
29 ///////////////////////////////////////////////////////////////////////////////
30 #if !defined(BOOST_SPIRIT_SELECT_LIMIT)
31 #define BOOST_SPIRIT_SELECT_LIMIT PHOENIX_LIMIT
32 #endif // !defined(BOOST_SPIRIT_SELECT_LIMIT)
34 ///////////////////////////////////////////////////////////////////////////////
36 // ensure BOOST_SPIRIT_SELECT_LIMIT <= PHOENIX_LIMIT and
37 // BOOST_SPIRIT_SELECT_LIMIT > 0
38 // BOOST_SPIRIT_SELECT_LIMIT <= 15
40 // [Pushed this down a little to make CW happy with BOOST_STATIC_ASSERT]
41 // [Otherwise, it complains: 'boost_static_assert_test_42' redefined]
43 ///////////////////////////////////////////////////////////////////////////////
44 BOOST_STATIC_ASSERT(BOOST_SPIRIT_SELECT_LIMIT <= PHOENIX_LIMIT);
45 BOOST_STATIC_ASSERT(BOOST_SPIRIT_SELECT_LIMIT > 0);
46 BOOST_STATIC_ASSERT(BOOST_SPIRIT_SELECT_LIMIT <= 15);
48 ///////////////////////////////////////////////////////////////////////////////
50 // Calculate the required amount of tuple members rounded up to the nearest
51 // integer dividable by 3
53 ///////////////////////////////////////////////////////////////////////////////
54 #if BOOST_SPIRIT_SELECT_LIMIT > 12
55 #define BOOST_SPIRIT_SELECT_LIMIT_A 15
56 #elif BOOST_SPIRIT_SELECT_LIMIT > 9
57 #define BOOST_SPIRIT_SELECT_LIMIT_A 12
58 #elif BOOST_SPIRIT_SELECT_LIMIT > 6
59 #define BOOST_SPIRIT_SELECT_LIMIT_A 9
60 #elif BOOST_SPIRIT_SELECT_LIMIT > 3
61 #define BOOST_SPIRIT_SELECT_LIMIT_A 6
63 #define BOOST_SPIRIT_SELECT_LIMIT_A 3
66 ///////////////////////////////////////////////////////////////////////////////
67 namespace boost { namespace spirit {
69 ///////////////////////////////////////////////////////////////////////////////
71 // The select_default_no_fail and select_default_fail structs are used to
72 // distinguish two different behaviours for the select_parser in case that not
73 // any of the given sub-parsers match.
75 // If the select_parser is used with the select_default_no_fail behaviour,
76 // then in case of no matching sub-parser the whole select_parser returns an
77 // empty match and the value -1.
79 // If the select_parser is used with the select_default_fail behaviour, then
80 // in case of no matching sub-parser the whole select_parser fails to match at
83 ///////////////////////////////////////////////////////////////////////////////
84 struct select_default_no_fail {};
85 struct select_default_fail {};
87 }} // namespace boost::spirit
89 ///////////////////////////////////////////////////////////////////////////////
90 #include <boost/spirit/dynamic/impl/select.ipp>
92 ///////////////////////////////////////////////////////////////////////////////
93 namespace boost { namespace spirit {
95 ///////////////////////////////////////////////////////////////////////////////
96 template <typename TupleT, typename BehaviourT, typename T>
98 : public parser<select_parser<TupleT, BehaviourT, T> >
100 typedef select_parser<TupleT, BehaviourT, T> self_t;
102 select_parser(TupleT const &t_)
106 template <typename ScannerT>
109 typedef typename match_result<ScannerT, T>::type type;
112 template <typename ScannerT>
113 typename parser_result<self_t, ScannerT>::type
114 parse(ScannerT const& scan) const
116 typedef typename parser_result<self_t, ScannerT>::type result_t;
118 if (!scan.at_end()) {
119 return impl::parse_tuple_element<
120 TupleT::length, result_t, TupleT, BehaviourT>::do_(t, scan);
122 return impl::select_match_gen<result_t, BehaviourT>::do_(scan);
128 ///////////////////////////////////////////////////////////////////////////////
129 template <typename BehaviourT, typename T = int>
130 struct select_parser_gen {
132 ///////////////////////////////////////////////////////////////////////////
134 // This generates different select_parser_gen::operator()() functions with
135 // an increasing number of parser parameters:
137 // template <typename ParserT0, ...>
140 // typename impl::as_embedded_parser<ParserT0>::type,
146 // operator()(ParserT0 const &p0, ...) const
148 // typedef impl::as_embedded_parser<ParserT0> parser_t0;
151 // typedef phoenix::tuple<
155 // typedef select_parser<tuple_t, BehaviourT, T> result_t;
157 // return result_t(tuple_t(
158 // parser_t0::convert(p0),
163 // The number of generated functions depends on the maximum tuple member
164 // limit defined by the PHOENIX_LIMIT pp constant.
166 ///////////////////////////////////////////////////////////////////////////
167 #define BOOST_SPIRIT_SELECT_EMBEDDED(z, N, _) \
168 typename impl::as_embedded_parser<BOOST_PP_CAT(ParserT, N)>::type \
170 #define BOOST_SPIRIT_SELECT_EMBEDDED_TYPEDEF(z, N, _) \
171 typedef impl::as_embedded_parser<BOOST_PP_CAT(ParserT, N)> \
172 BOOST_PP_CAT(parser_t, N); \
174 #define BOOST_SPIRIT_SELECT_CONVERT(z, N, _) \
175 BOOST_PP_CAT(parser_t, N)::convert(BOOST_PP_CAT(p, N)) \
178 #define BOOST_SPIRIT_SELECT_PARSER(z, N, _) \
180 BOOST_PP_ENUM_PARAMS_Z(z, BOOST_PP_INC(N), typename ParserT) \
184 BOOST_PP_ENUM_ ## z(BOOST_PP_INC(N), \
185 BOOST_SPIRIT_SELECT_EMBEDDED, _) \
191 BOOST_PP_ENUM_BINARY_PARAMS_Z(z, BOOST_PP_INC(N), \
195 BOOST_PP_REPEAT_ ## z(BOOST_PP_INC(N), \
196 BOOST_SPIRIT_SELECT_EMBEDDED_TYPEDEF, _) \
198 typedef phoenix::tuple< \
199 BOOST_PP_ENUM_BINARY_PARAMS_Z(z, BOOST_PP_INC(N), \
200 typename parser_t, ::type BOOST_PP_INTERCEPT) \
202 typedef select_parser<tuple_t, BehaviourT, T> result_t; \
204 return result_t(tuple_t( \
205 BOOST_PP_ENUM_ ## z(BOOST_PP_INC(N), \
206 BOOST_SPIRIT_SELECT_CONVERT, _) \
211 BOOST_PP_REPEAT(BOOST_SPIRIT_SELECT_LIMIT_A,
212 BOOST_SPIRIT_SELECT_PARSER, _)
214 #undef BOOST_SPIRIT_SELECT_PARSER
215 #undef BOOST_SPIRIT_SELECT_CONVERT
216 #undef BOOST_SPIRIT_SELECT_EMBEDDED_TYPEDEF
217 #undef BOOST_SPIRIT_SELECT_EMBEDDED
218 ///////////////////////////////////////////////////////////////////////////
221 ///////////////////////////////////////////////////////////////////////////////
223 // Predefined parser generator helper objects
225 ///////////////////////////////////////////////////////////////////////////////
226 select_parser_gen<select_default_no_fail> const select_p =
227 select_parser_gen<select_default_no_fail>();
229 select_parser_gen<select_default_fail> const select_fail_p =
230 select_parser_gen<select_default_fail>();
232 #undef BOOST_SPIRIT_SELECT_LIMIT_A
234 ///////////////////////////////////////////////////////////////////////////////
235 }} // namespace boost::spirit
237 #endif // BOOST_SPIRIT_SELECT_HPP