Imported Upstream version 1.57.0
[platform/upstream/boost.git] / boost / spirit / home / classic / utility / grammar_def.hpp
1 /*=============================================================================
2     Copyright (c) 2003 Hartmut Kaiser
3     Copyright (c) 2003 Joel de Guzman
4     http://spirit.sourceforge.net/
5
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
11
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>
25
26 ///////////////////////////////////////////////////////////////////////////////
27 //
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.
34 //
35 //  It should NOT be greater than PHOENIX_LIMIT!
36 //
37 ///////////////////////////////////////////////////////////////////////////////
38 #if !defined(BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT)
39 #define BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT PHOENIX_LIMIT
40 #endif
41
42 ///////////////////////////////////////////////////////////////////////////////
43 //
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
47 //
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);
52
53 //////////////////////////////////////////////////////////////////////////////
54 namespace boost { namespace spirit {
55
56 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
57
58 struct same {};
59
60 ///////////////////////////////////////////////////////////////////////////////
61 namespace impl {
62
63     ///////////////////////////////////////////////////////////////////////////
64     //
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.
67     //
68     ///////////////////////////////////////////////////////////////////////////
69     template <typename T0, typename T = T0>
70     struct make_const_pointer {
71
72     private:
73         // T0 shouldn't be of type 'same'
74         BOOST_STATIC_ASSERT((!boost::is_same<T0, same>::value));
75
76         typedef typename boost::mpl::if_c<
77                     boost::is_same<T, same>::value,
78                     T0 const *,
79                     T const *
80                 >::type
81             ptr_type;
82
83     public:
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,
89                     ::phoenix::nil_t,
90                     ptr_type
91                 >::type
92             type;
93     };
94
95     ///////////////////////////////////////////////////////////////////////////
96     template <int N, typename ElementT>
97     struct assign_zero_to_tuple_member {
98
99         template <typename TupleT>
100         static void do_(TupleT &t) { t[::phoenix::tuple_index<N>()] = 0; }
101     };
102
103     template <int N>
104     struct assign_zero_to_tuple_member<N, ::phoenix::nil_t> {
105
106         template <typename TupleT>
107         static void do_(TupleT& /*t*/) {}
108     };
109
110     struct phoenix_nil_type {
111
112         typedef ::phoenix::nil_t type;
113     };
114
115     template <int N>
116     struct init_tuple_member {
117
118         template <typename TupleT>
119         static void
120         do_(TupleT &t)
121         {
122             typedef typename boost::mpl::eval_if_c<
123                         (N < TupleT::length),
124                         ::phoenix::tuple_element<N, TupleT>,
125                         phoenix_nil_type
126                     >::type
127                 element_type;
128
129             assign_zero_to_tuple_member<N, element_type>::do_(t);
130         }
131     };
132
133 ///////////////////////////////////////////////////////////////////////////////
134 }   // namespace impl
135
136 ///////////////////////////////////////////////////////////////////////////////
137 //
138 //  grammar_def class
139 //
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:
143 //
144 //          rule<> const &start() const;
145 //
146 //      and
147 //
148 //          template <int N>
149 //          rule<> const *get_start_parser() const;
150 //
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.
154 //
155 ///////////////////////////////////////////////////////////////////////////////
156 template <
157     typename T,
158     BOOST_PP_ENUM_PARAMS(
159         BOOST_PP_DEC(BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT_A), typename T)
160 >
161 class grammar_def {
162
163 private:
164     ///////////////////////////////////////////////////////////////////////////
165     //
166     //  This generates the full tuple type from the given template parameters
167     //  T, T0, ...
168     //
169     //      typedef ::phoenix::tuple<
170     //              typename impl::make_const_pointer<T>::type,
171     //              typename impl::make_const_pointer<T, T0>::type,
172     //              ...
173     //          > tuple_t;
174     //
175     ///////////////////////////////////////////////////////////////////////////
176     #define BOOST_SPIRIT_GRAMMARDEF_TUPLE_PARAM(z, N, _) \
177         typename impl::make_const_pointer<T, BOOST_PP_CAT(T, N)>::type \
178         /**/
179
180     typedef ::phoenix::tuple<
181             typename impl::make_const_pointer<T>::type,
182             BOOST_PP_ENUM(
183                 BOOST_PP_DEC(BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT_A),
184                 BOOST_SPIRIT_GRAMMARDEF_TUPLE_PARAM,
185                 _
186             )
187         > tuple_t;
188
189     #undef BOOST_SPIRIT_GRAMMARDEF_TUPLE_PARAM
190     ///////////////////////////////////////////////////////////////////////////
191
192 protected:
193     ///////////////////////////////////////////////////////////////////////////
194     //
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:
198     //
199     //      template <typename TC0, ...>
200     //      void start_parsers (TC0 const &t0, ...)
201     //      {
202     //          using ::phoenix::tuple_index_names::_1;
203     //          t[_1] = &t0;
204     //          ...
205     //      }
206     //
207     //      where a TC0 const* must be convertible to a T0 const*
208     //
209     ///////////////////////////////////////////////////////////////////////////
210     #define BOOST_SPIRIT_GRAMMARDEF_ENUM_PARAMS(z, N, _) \
211         BOOST_PP_CAT(TC, N) const &BOOST_PP_CAT(t, N) \
212         /**/
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); \
216         /**/
217     #define BOOST_SPIRIT_GRAMMARDEF_ENUM_START(z, N, _) \
218         template <BOOST_PP_ENUM_PARAMS_Z(z, BOOST_PP_INC(N), typename TC)> \
219         void \
220         start_parsers(BOOST_PP_ENUM_ ## z(BOOST_PP_INC(N), \
221             BOOST_SPIRIT_GRAMMARDEF_ENUM_PARAMS, _) ) \
222         { \
223             BOOST_PP_REPEAT_ ## z(BOOST_PP_INC(N), \
224                 BOOST_SPIRIT_GRAMMARDEF_ENUM_ASSIGN, _) \
225         } \
226         /**/
227
228     BOOST_PP_REPEAT(
229         BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT_A,
230         BOOST_SPIRIT_GRAMMARDEF_ENUM_START, _)
231
232     #undef BOOST_SPIRIT_GRAMMARDEF_ENUM_START
233     #undef BOOST_SPIRIT_GRAMMARDEF_ENUM_ASSIGN
234     #undef BOOST_SPIRIT_GRAMMARDEF_ENUM_PARAMS
235     ///////////////////////////////////////////////////////////////////////////
236
237     ///////////////////////////////////////////////////////////////////////////
238     //
239     //  This generates some initialization code, which allows to initialize all
240     //  used tuple members to 0 (zero):
241     //
242     //      t[_1] = 0;
243     //      impl::init_tuple_member<1>::do_(t);
244     //      ...
245     //
246     ///////////////////////////////////////////////////////////////////////////
247     #define BOOST_SPIRIT_GRAMMARDEF_ENUM_INIT(z, N, _) \
248         impl::init_tuple_member<N>::do_(t); \
249         /**/
250
251     grammar_def()
252     {
253         using ::phoenix::tuple_index_names::_1;
254         t[_1] = 0;
255         BOOST_PP_REPEAT_FROM_TO(
256             1, BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT_A,
257             BOOST_SPIRIT_GRAMMARDEF_ENUM_INIT, _)
258     }
259
260     #undef BOOST_SPIRIT_GRAMMARDEF_ENUM_INIT
261     ///////////////////////////////////////////////////////////////////////////
262
263 public:
264     T const &
265     start() const
266     {
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]);
273         return *t[_1];
274     }
275
276     template <int N>
277     typename ::phoenix::tuple_element<N, tuple_t>::crtype
278     get_start_parser() const
279     {
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
282     //  grammar.
283         BOOST_STATIC_ASSERT(N > 0 && N < tuple_t::length);
284
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>()]);
293
294         return t[::phoenix::tuple_index<N>()];
295     }
296
297 private:
298     tuple_t t;
299 };
300
301 #undef BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT_A
302
303 BOOST_SPIRIT_CLASSIC_NAMESPACE_END
304
305 }} // namespace BOOST_SPIRIT_CLASSIC_NS
306
307 #endif // BOOST_SPIRIT_GRAMMAR_DEF_HPP