grammar_def.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*=============================================================================
  2. Copyright (c) 2003 Hartmut Kaiser
  3. Copyright (c) 2003 Joel de Guzman
  4. http://spirit.sourceforge.net/
  5. Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. #if !defined(BOOST_SPIRIT_GRAMMAR_DEF_HPP)
  9. #define BOOST_SPIRIT_GRAMMAR_DEF_HPP
  10. #include <boost/mpl/if.hpp>
  11. #include <boost/mpl/eval_if.hpp>
  12. #include <boost/type_traits/is_same.hpp>
  13. #include <boost/preprocessor/arithmetic/inc.hpp>
  14. #include <boost/preprocessor/arithmetic/dec.hpp>
  15. #include <boost/preprocessor/enum.hpp>
  16. #include <boost/preprocessor/enum_params.hpp>
  17. #include <boost/preprocessor/repeat.hpp>
  18. #include <boost/preprocessor/repeat_from_to.hpp>
  19. #include <boost/spirit/home/classic/namespace.hpp>
  20. #include <boost/spirit/home/classic/phoenix/tuples.hpp>
  21. #include <boost/spirit/home/classic/core/assert.hpp>
  22. #include <boost/spirit/home/classic/utility/grammar_def_fwd.hpp>
  23. ///////////////////////////////////////////////////////////////////////////////
  24. //
  25. // Spirit predefined maximum grammar start parser limit. This limit defines
  26. // the maximum number of possible different parsers exposed from a
  27. // particular grammar. This number defaults to 3.
  28. // The actual maximum is rounded up in multiples of 3. Thus, if this value
  29. // is 4, the actual limit is 6. The ultimate maximum limit in this
  30. // implementation is 15.
  31. //
  32. // It should NOT be greater than PHOENIX_LIMIT!
  33. //
  34. ///////////////////////////////////////////////////////////////////////////////
  35. #if !defined(BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT)
  36. #define BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT PHOENIX_LIMIT
  37. #endif
  38. ///////////////////////////////////////////////////////////////////////////////
  39. //
  40. // ensure BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT <= PHOENIX_LIMIT and
  41. // BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT <= 15 and
  42. // BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT > 0
  43. //
  44. ///////////////////////////////////////////////////////////////////////////////
  45. BOOST_STATIC_ASSERT(BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT <= PHOENIX_LIMIT);
  46. BOOST_STATIC_ASSERT(BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT <= 15);
  47. BOOST_STATIC_ASSERT(BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT > 0);
  48. //////////////////////////////////////////////////////////////////////////////
  49. namespace boost { namespace spirit {
  50. BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
  51. struct same {};
  52. ///////////////////////////////////////////////////////////////////////////////
  53. namespace impl {
  54. ///////////////////////////////////////////////////////////////////////////
  55. //
  56. // The make_const_pointer meta function allows to generate a T const*
  57. // needed to store the pointer to a given start parser from a grammar.
  58. //
  59. ///////////////////////////////////////////////////////////////////////////
  60. template <typename T0, typename T = T0>
  61. struct make_const_pointer {
  62. private:
  63. // T0 shouldn't be of type 'same'
  64. BOOST_STATIC_ASSERT((!boost::is_same<T0, same>::value));
  65. typedef typename boost::mpl::if_c<
  66. boost::is_same<T, same>::value,
  67. T0 const *,
  68. T const *
  69. >::type
  70. ptr_type;
  71. public:
  72. // If the type in question is phoenix::nil_t, then the returned type
  73. // is still phoenix::nil_t, otherwise a constant pointer type to the
  74. // inspected type is returned.
  75. typedef typename boost::mpl::if_c<
  76. boost::is_same<T, ::phoenix::nil_t>::value,
  77. ::phoenix::nil_t,
  78. ptr_type
  79. >::type
  80. type;
  81. };
  82. ///////////////////////////////////////////////////////////////////////////
  83. template <int N, typename ElementT>
  84. struct assign_zero_to_tuple_member {
  85. template <typename TupleT>
  86. static void do_(TupleT &t) { t[::phoenix::tuple_index<N>()] = 0; }
  87. };
  88. template <int N>
  89. struct assign_zero_to_tuple_member<N, ::phoenix::nil_t> {
  90. template <typename TupleT>
  91. static void do_(TupleT& /*t*/) {}
  92. };
  93. struct phoenix_nil_type {
  94. typedef ::phoenix::nil_t type;
  95. };
  96. template <int N>
  97. struct init_tuple_member {
  98. template <typename TupleT>
  99. static void
  100. do_(TupleT &t)
  101. {
  102. typedef typename boost::mpl::eval_if_c<
  103. (N < TupleT::length),
  104. ::phoenix::tuple_element<N, TupleT>,
  105. phoenix_nil_type
  106. >::type
  107. element_type;
  108. assign_zero_to_tuple_member<N, element_type>::do_(t);
  109. }
  110. };
  111. ///////////////////////////////////////////////////////////////////////////////
  112. } // namespace impl
  113. ///////////////////////////////////////////////////////////////////////////////
  114. //
  115. // grammar_def class
  116. //
  117. // This class may be used as a base class for the embedded definition
  118. // class inside the grammar<> derived user grammar.
  119. // It exposes the two functions needed for start rule access:
  120. //
  121. // rule<> const &start() const;
  122. //
  123. // and
  124. //
  125. // template <int N>
  126. // rule<> const *get_start_parser() const;
  127. //
  128. // Additionally it exposes a set o 'start_parsers' functions, which are to
  129. // be called by the user to define the parsers to use as start parsers
  130. // of the given grammar.
  131. //
  132. ///////////////////////////////////////////////////////////////////////////////
  133. template <
  134. typename T,
  135. BOOST_PP_ENUM_PARAMS(
  136. BOOST_PP_DEC(BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT_A), typename T)
  137. >
  138. class grammar_def {
  139. private:
  140. ///////////////////////////////////////////////////////////////////////////
  141. //
  142. // This generates the full tuple type from the given template parameters
  143. // T, T0, ...
  144. //
  145. // typedef ::phoenix::tuple<
  146. // typename impl::make_const_pointer<T>::type,
  147. // typename impl::make_const_pointer<T, T0>::type,
  148. // ...
  149. // > tuple_t;
  150. //
  151. ///////////////////////////////////////////////////////////////////////////
  152. #define BOOST_SPIRIT_GRAMMARDEF_TUPLE_PARAM(z, N, _) \
  153. typename impl::make_const_pointer<T, BOOST_PP_CAT(T, N)>::type \
  154. /**/
  155. typedef ::phoenix::tuple<
  156. typename impl::make_const_pointer<T>::type,
  157. BOOST_PP_ENUM(
  158. BOOST_PP_DEC(BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT_A),
  159. BOOST_SPIRIT_GRAMMARDEF_TUPLE_PARAM,
  160. _
  161. )
  162. > tuple_t;
  163. #undef BOOST_SPIRIT_GRAMMARDEF_TUPLE_PARAM
  164. ///////////////////////////////////////////////////////////////////////////
  165. protected:
  166. ///////////////////////////////////////////////////////////////////////////
  167. //
  168. // This generates a sequence of 'start_parsers' functions with increasing
  169. // number of arguments, which allow to initialize the tuple members with
  170. // the pointers to the start parsers of the grammar:
  171. //
  172. // template <typename TC0, ...>
  173. // void start_parsers (TC0 const &t0, ...)
  174. // {
  175. // using ::phoenix::tuple_index_names::_1;
  176. // t[_1] = &t0;
  177. // ...
  178. // }
  179. //
  180. // where a TC0 const* must be convertible to a T0 const*
  181. //
  182. ///////////////////////////////////////////////////////////////////////////
  183. #define BOOST_SPIRIT_GRAMMARDEF_ENUM_PARAMS(z, N, _) \
  184. BOOST_PP_CAT(TC, N) const &BOOST_PP_CAT(t, N) \
  185. /**/
  186. #define BOOST_SPIRIT_GRAMMARDEF_ENUM_ASSIGN(z, N, _) \
  187. using ::phoenix::tuple_index_names::BOOST_PP_CAT(_, BOOST_PP_INC(N)); \
  188. t[BOOST_PP_CAT(_, BOOST_PP_INC(N))] = &BOOST_PP_CAT(t, N); \
  189. /**/
  190. #define BOOST_SPIRIT_GRAMMARDEF_ENUM_START(z, N, _) \
  191. template <BOOST_PP_ENUM_PARAMS_Z(z, BOOST_PP_INC(N), typename TC)> \
  192. void \
  193. start_parsers(BOOST_PP_ENUM_ ## z(BOOST_PP_INC(N), \
  194. BOOST_SPIRIT_GRAMMARDEF_ENUM_PARAMS, _) ) \
  195. { \
  196. BOOST_PP_REPEAT_ ## z(BOOST_PP_INC(N), \
  197. BOOST_SPIRIT_GRAMMARDEF_ENUM_ASSIGN, _) \
  198. } \
  199. /**/
  200. BOOST_PP_REPEAT(
  201. BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT_A,
  202. BOOST_SPIRIT_GRAMMARDEF_ENUM_START, _)
  203. #undef BOOST_SPIRIT_GRAMMARDEF_ENUM_START
  204. #undef BOOST_SPIRIT_GRAMMARDEF_ENUM_ASSIGN
  205. #undef BOOST_SPIRIT_GRAMMARDEF_ENUM_PARAMS
  206. ///////////////////////////////////////////////////////////////////////////
  207. ///////////////////////////////////////////////////////////////////////////
  208. //
  209. // This generates some initialization code, which allows to initialize all
  210. // used tuple members to 0 (zero):
  211. //
  212. // t[_1] = 0;
  213. // impl::init_tuple_member<1>::do_(t);
  214. // ...
  215. //
  216. ///////////////////////////////////////////////////////////////////////////
  217. #define BOOST_SPIRIT_GRAMMARDEF_ENUM_INIT(z, N, _) \
  218. impl::init_tuple_member<N>::do_(t); \
  219. /**/
  220. grammar_def()
  221. {
  222. using ::phoenix::tuple_index_names::_1;
  223. t[_1] = 0;
  224. BOOST_PP_REPEAT_FROM_TO(
  225. 1, BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT_A,
  226. BOOST_SPIRIT_GRAMMARDEF_ENUM_INIT, _)
  227. }
  228. #undef BOOST_SPIRIT_GRAMMARDEF_ENUM_INIT
  229. ///////////////////////////////////////////////////////////////////////////
  230. public:
  231. T const &
  232. start() const
  233. {
  234. // If the following assertion is fired, you have probably forgot to call
  235. // the start_parser() function from inside the constructor of your
  236. // embedded definition class to initialize the start parsers to be exposed
  237. // from your grammar.
  238. using ::phoenix::tuple_index_names::_1;
  239. BOOST_SPIRIT_ASSERT(0 != t[_1]);
  240. return *t[_1];
  241. }
  242. template <int N>
  243. typename ::phoenix::tuple_element<N, tuple_t>::crtype
  244. get_start_parser() const
  245. {
  246. // If the following expression yields a compiler error, you have probably
  247. // tried to access a start rule, which isn't exposed as such from your
  248. // grammar.
  249. BOOST_STATIC_ASSERT(N > 0 && N < tuple_t::length);
  250. // If the following assertion is fired, you have probably forgot to call
  251. // the start_parser() function from inside the constructor of your
  252. // embedded definition class to initialize the start parsers to be exposed
  253. // from your grammar.
  254. // Another reason may be, that there is a count mismatch between
  255. // the number of template parameters to the grammar_def<> class and the
  256. // number of parameters used while calling start_parsers().
  257. BOOST_SPIRIT_ASSERT(0 != t[::phoenix::tuple_index<N>()]);
  258. return t[::phoenix::tuple_index<N>()];
  259. }
  260. private:
  261. tuple_t t;
  262. };
  263. #undef BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT_A
  264. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  265. }} // namespace BOOST_SPIRIT_CLASSIC_NS
  266. #endif // BOOST_SPIRIT_GRAMMAR_DEF_HPP