1 /*=============================================================================
2 Copyright (c) 2003 Hartmut Kaiser
3 Copyright (c) 2003 Joel de Guzman
4 http://spirit.sourceforge.net/
6 Distributed under the Boost Software License, Version 1.0. (See accompanying
7 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 =============================================================================*/
9 #if !defined(BOOST_SPIRIT_GRAMMAR_DEF_HPP)
10 #define BOOST_SPIRIT_GRAMMAR_DEF_HPP
12 #include <boost/mpl/if.hpp>
13 #include <boost/mpl/eval_if.hpp>
14 #include <boost/type_traits/is_same.hpp>
15 #include <boost/preprocessor/arithmetic/inc.hpp>
16 #include <boost/preprocessor/arithmetic/dec.hpp>
17 #include <boost/preprocessor/enum.hpp>
18 #include <boost/preprocessor/enum_params.hpp>
19 #include <boost/preprocessor/repeat.hpp>
20 #include <boost/preprocessor/repeat_from_to.hpp>
21 #include <boost/spirit/home/classic/namespace.hpp>
22 #include <boost/spirit/home/classic/phoenix/tuples.hpp>
23 #include <boost/spirit/home/classic/core/assert.hpp>
24 #include <boost/spirit/home/classic/utility/grammar_def_fwd.hpp>
26 ///////////////////////////////////////////////////////////////////////////////
28 // Spirit predefined maximum grammar start parser limit. This limit defines
29 // the maximum number of possible different parsers exposed from a
30 // particular grammar. This number defaults to 3.
31 // The actual maximum is rounded up in multiples of 3. Thus, if this value
32 // is 4, the actual limit is 6. The ultimate maximum limit in this
33 // implementation is 15.
35 // It should NOT be greater than PHOENIX_LIMIT!
37 ///////////////////////////////////////////////////////////////////////////////
38 #if !defined(BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT)
39 #define BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT PHOENIX_LIMIT
42 ///////////////////////////////////////////////////////////////////////////////
44 // ensure BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT <= PHOENIX_LIMIT and
45 // BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT <= 15 and
46 // BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT > 0
48 ///////////////////////////////////////////////////////////////////////////////
49 BOOST_STATIC_ASSERT(BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT <= PHOENIX_LIMIT);
50 BOOST_STATIC_ASSERT(BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT <= 15);
51 BOOST_STATIC_ASSERT(BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT > 0);
53 //////////////////////////////////////////////////////////////////////////////
54 namespace boost { namespace spirit {
56 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
60 ///////////////////////////////////////////////////////////////////////////////
63 ///////////////////////////////////////////////////////////////////////////
65 // The make_const_pointer meta function allows to generate a T const*
66 // needed to store the pointer to a given start parser from a grammar.
68 ///////////////////////////////////////////////////////////////////////////
69 template <typename T0, typename T = T0>
70 struct make_const_pointer {
73 // T0 shouldn't be of type 'same'
74 BOOST_STATIC_ASSERT((!boost::is_same<T0, same>::value));
76 typedef typename boost::mpl::if_c<
77 boost::is_same<T, same>::value,
84 // If the type in question is phoenix::nil_t, then the returned type
85 // is still phoenix::nil_t, otherwise a constant pointer type to the
86 // inspected type is returned.
87 typedef typename boost::mpl::if_c<
88 boost::is_same<T, ::phoenix::nil_t>::value,
95 ///////////////////////////////////////////////////////////////////////////
96 template <int N, typename ElementT>
97 struct assign_zero_to_tuple_member {
99 template <typename TupleT>
100 static void do_(TupleT &t) { t[::phoenix::tuple_index<N>()] = 0; }
104 struct assign_zero_to_tuple_member<N, ::phoenix::nil_t> {
106 template <typename TupleT>
107 static void do_(TupleT& /*t*/) {}
110 struct phoenix_nil_type {
112 typedef ::phoenix::nil_t type;
116 struct init_tuple_member {
118 template <typename TupleT>
122 typedef typename boost::mpl::eval_if_c<
123 (N < TupleT::length),
124 ::phoenix::tuple_element<N, TupleT>,
129 assign_zero_to_tuple_member<N, element_type>::do_(t);
133 ///////////////////////////////////////////////////////////////////////////////
136 ///////////////////////////////////////////////////////////////////////////////
140 // This class may be used as a base class for the embedded definition
141 // class inside the grammar<> derived user grammar.
142 // It exposes the two functions needed for start rule access:
144 // rule<> const &start() const;
149 // rule<> const *get_start_parser() const;
151 // Additionally it exposes a set o 'start_parsers' functions, which are to
152 // be called by the user to define the parsers to use as start parsers
153 // of the given grammar.
155 ///////////////////////////////////////////////////////////////////////////////
158 BOOST_PP_ENUM_PARAMS(
159 BOOST_PP_DEC(BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT_A), typename T)
164 ///////////////////////////////////////////////////////////////////////////
166 // This generates the full tuple type from the given template parameters
169 // typedef ::phoenix::tuple<
170 // typename impl::make_const_pointer<T>::type,
171 // typename impl::make_const_pointer<T, T0>::type,
175 ///////////////////////////////////////////////////////////////////////////
176 #define BOOST_SPIRIT_GRAMMARDEF_TUPLE_PARAM(z, N, _) \
177 typename impl::make_const_pointer<T, BOOST_PP_CAT(T, N)>::type \
180 typedef ::phoenix::tuple<
181 typename impl::make_const_pointer<T>::type,
183 BOOST_PP_DEC(BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT_A),
184 BOOST_SPIRIT_GRAMMARDEF_TUPLE_PARAM,
189 #undef BOOST_SPIRIT_GRAMMARDEF_TUPLE_PARAM
190 ///////////////////////////////////////////////////////////////////////////
193 ///////////////////////////////////////////////////////////////////////////
195 // This generates a sequence of 'start_parsers' functions with increasing
196 // number of arguments, which allow to initialize the tuple members with
197 // the pointers to the start parsers of the grammar:
199 // template <typename TC0, ...>
200 // void start_parsers (TC0 const &t0, ...)
202 // using ::phoenix::tuple_index_names::_1;
207 // where a TC0 const* must be convertible to a T0 const*
209 ///////////////////////////////////////////////////////////////////////////
210 #define BOOST_SPIRIT_GRAMMARDEF_ENUM_PARAMS(z, N, _) \
211 BOOST_PP_CAT(TC, N) const &BOOST_PP_CAT(t, N) \
213 #define BOOST_SPIRIT_GRAMMARDEF_ENUM_ASSIGN(z, N, _) \
214 using ::phoenix::tuple_index_names::BOOST_PP_CAT(_, BOOST_PP_INC(N)); \
215 t[BOOST_PP_CAT(_, BOOST_PP_INC(N))] = &BOOST_PP_CAT(t, N); \
217 #define BOOST_SPIRIT_GRAMMARDEF_ENUM_START(z, N, _) \
218 template <BOOST_PP_ENUM_PARAMS_Z(z, BOOST_PP_INC(N), typename TC)> \
220 start_parsers(BOOST_PP_ENUM_ ## z(BOOST_PP_INC(N), \
221 BOOST_SPIRIT_GRAMMARDEF_ENUM_PARAMS, _) ) \
223 BOOST_PP_REPEAT_ ## z(BOOST_PP_INC(N), \
224 BOOST_SPIRIT_GRAMMARDEF_ENUM_ASSIGN, _) \
229 BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT_A,
230 BOOST_SPIRIT_GRAMMARDEF_ENUM_START, _)
232 #undef BOOST_SPIRIT_GRAMMARDEF_ENUM_START
233 #undef BOOST_SPIRIT_GRAMMARDEF_ENUM_ASSIGN
234 #undef BOOST_SPIRIT_GRAMMARDEF_ENUM_PARAMS
235 ///////////////////////////////////////////////////////////////////////////
237 ///////////////////////////////////////////////////////////////////////////
239 // This generates some initialization code, which allows to initialize all
240 // used tuple members to 0 (zero):
243 // impl::init_tuple_member<1>::do_(t);
246 ///////////////////////////////////////////////////////////////////////////
247 #define BOOST_SPIRIT_GRAMMARDEF_ENUM_INIT(z, N, _) \
248 impl::init_tuple_member<N>::do_(t); \
253 using ::phoenix::tuple_index_names::_1;
255 BOOST_PP_REPEAT_FROM_TO(
256 1, BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT_A,
257 BOOST_SPIRIT_GRAMMARDEF_ENUM_INIT, _)
260 #undef BOOST_SPIRIT_GRAMMARDEF_ENUM_INIT
261 ///////////////////////////////////////////////////////////////////////////
267 // If the following assertion is fired, you have probably forgot to call
268 // the start_parser() function from inside the constructor of your
269 // embedded definition class to initialize the start parsers to be exposed
270 // from your grammar.
271 using ::phoenix::tuple_index_names::_1;
272 BOOST_SPIRIT_ASSERT(0 != t[_1]);
277 typename ::phoenix::tuple_element<N, tuple_t>::crtype
278 get_start_parser() const
280 // If the following expression yields a compiler error, you have probably
281 // tried to access a start rule, which isn't exposed as such from your
283 BOOST_STATIC_ASSERT(N > 0 && N < tuple_t::length);
285 // If the following assertion is fired, you have probably forgot to call
286 // the start_parser() function from inside the constructor of your
287 // embedded definition class to initialize the start parsers to be exposed
288 // from your grammar.
289 // Another reason may be, that there is a count mismatch between
290 // the number of template parameters to the grammar_def<> class and the
291 // number of parameters used while calling start_parsers().
292 BOOST_SPIRIT_ASSERT(0 != t[::phoenix::tuple_index<N>()]);
294 return t[::phoenix::tuple_index<N>()];
301 #undef BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT_A
303 BOOST_SPIRIT_CLASSIC_NAMESPACE_END
305 }} // namespace BOOST_SPIRIT_CLASSIC_NS
307 #endif // BOOST_SPIRIT_GRAMMAR_DEF_HPP