grammar_def.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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)
  87. {
  88. ::phoenix::tuple_index<N> const idx;
  89. t[idx] = 0;
  90. }
  91. };
  92. template <int N>
  93. struct assign_zero_to_tuple_member<N, ::phoenix::nil_t> {
  94. template <typename TupleT>
  95. static void do_(TupleT& /*t*/) {}
  96. };
  97. struct phoenix_nil_type {
  98. typedef ::phoenix::nil_t type;
  99. };
  100. template <int N>
  101. struct init_tuple_member {
  102. template <typename TupleT>
  103. static void
  104. do_(TupleT &t)
  105. {
  106. typedef typename boost::mpl::eval_if_c<
  107. (N < TupleT::length),
  108. ::phoenix::tuple_element<N, TupleT>,
  109. phoenix_nil_type
  110. >::type
  111. element_type;
  112. assign_zero_to_tuple_member<N, element_type>::do_(t);
  113. }
  114. };
  115. ///////////////////////////////////////////////////////////////////////////////
  116. } // namespace impl
  117. ///////////////////////////////////////////////////////////////////////////////
  118. //
  119. // grammar_def class
  120. //
  121. // This class may be used as a base class for the embedded definition
  122. // class inside the grammar<> derived user grammar.
  123. // It exposes the two functions needed for start rule access:
  124. //
  125. // rule<> const &start() const;
  126. //
  127. // and
  128. //
  129. // template <int N>
  130. // rule<> const *get_start_parser() const;
  131. //
  132. // Additionally it exposes a set o 'start_parsers' functions, which are to
  133. // be called by the user to define the parsers to use as start parsers
  134. // of the given grammar.
  135. //
  136. ///////////////////////////////////////////////////////////////////////////////
  137. template <
  138. typename T,
  139. BOOST_PP_ENUM_PARAMS(
  140. BOOST_PP_DEC(BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT_A), typename T)
  141. >
  142. class grammar_def {
  143. private:
  144. ///////////////////////////////////////////////////////////////////////////
  145. //
  146. // This generates the full tuple type from the given template parameters
  147. // T, T0, ...
  148. //
  149. // typedef ::phoenix::tuple<
  150. // typename impl::make_const_pointer<T>::type,
  151. // typename impl::make_const_pointer<T, T0>::type,
  152. // ...
  153. // > tuple_t;
  154. //
  155. ///////////////////////////////////////////////////////////////////////////
  156. #define BOOST_SPIRIT_GRAMMARDEF_TUPLE_PARAM(z, N, _) \
  157. typename impl::make_const_pointer<T, BOOST_PP_CAT(T, N)>::type \
  158. /**/
  159. typedef ::phoenix::tuple<
  160. typename impl::make_const_pointer<T>::type,
  161. BOOST_PP_ENUM(
  162. BOOST_PP_DEC(BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT_A),
  163. BOOST_SPIRIT_GRAMMARDEF_TUPLE_PARAM,
  164. _
  165. )
  166. > tuple_t;
  167. #undef BOOST_SPIRIT_GRAMMARDEF_TUPLE_PARAM
  168. ///////////////////////////////////////////////////////////////////////////
  169. protected:
  170. ///////////////////////////////////////////////////////////////////////////
  171. //
  172. // This generates a sequence of 'start_parsers' functions with increasing
  173. // number of arguments, which allow to initialize the tuple members with
  174. // the pointers to the start parsers of the grammar:
  175. //
  176. // template <typename TC0, ...>
  177. // void start_parsers (TC0 const &t0, ...)
  178. // {
  179. // using ::phoenix::tuple_index_names::_1;
  180. // t[_1] = &t0;
  181. // ...
  182. // }
  183. //
  184. // where a TC0 const* must be convertible to a T0 const*
  185. //
  186. ///////////////////////////////////////////////////////////////////////////
  187. #define BOOST_SPIRIT_GRAMMARDEF_ENUM_PARAMS(z, N, _) \
  188. BOOST_PP_CAT(TC, N) const &BOOST_PP_CAT(t, N) \
  189. /**/
  190. #define BOOST_SPIRIT_GRAMMARDEF_ENUM_ASSIGN(z, N, _) \
  191. using ::phoenix::tuple_index_names::BOOST_PP_CAT(_, BOOST_PP_INC(N)); \
  192. t[BOOST_PP_CAT(_, BOOST_PP_INC(N))] = &BOOST_PP_CAT(t, N); \
  193. /**/
  194. #define BOOST_SPIRIT_GRAMMARDEF_ENUM_START(z, N, _) \
  195. template <BOOST_PP_ENUM_PARAMS_Z(z, BOOST_PP_INC(N), typename TC)> \
  196. void \
  197. start_parsers(BOOST_PP_ENUM_ ## z(BOOST_PP_INC(N), \
  198. BOOST_SPIRIT_GRAMMARDEF_ENUM_PARAMS, _) ) \
  199. { \
  200. BOOST_PP_REPEAT_ ## z(BOOST_PP_INC(N), \
  201. BOOST_SPIRIT_GRAMMARDEF_ENUM_ASSIGN, _) \
  202. } \
  203. /**/
  204. BOOST_PP_REPEAT(
  205. BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT_A,
  206. BOOST_SPIRIT_GRAMMARDEF_ENUM_START, _)
  207. #undef BOOST_SPIRIT_GRAMMARDEF_ENUM_START
  208. #undef BOOST_SPIRIT_GRAMMARDEF_ENUM_ASSIGN
  209. #undef BOOST_SPIRIT_GRAMMARDEF_ENUM_PARAMS
  210. ///////////////////////////////////////////////////////////////////////////
  211. ///////////////////////////////////////////////////////////////////////////
  212. //
  213. // This generates some initialization code, which allows to initialize all
  214. // used tuple members to 0 (zero):
  215. //
  216. // t[_1] = 0;
  217. // impl::init_tuple_member<1>::do_(t);
  218. // ...
  219. //
  220. ///////////////////////////////////////////////////////////////////////////
  221. #define BOOST_SPIRIT_GRAMMARDEF_ENUM_INIT(z, N, _) \
  222. impl::init_tuple_member<N>::do_(t); \
  223. /**/
  224. grammar_def()
  225. {
  226. using ::phoenix::tuple_index_names::_1;
  227. t[_1] = 0;
  228. BOOST_PP_REPEAT_FROM_TO(
  229. 1, BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT_A,
  230. BOOST_SPIRIT_GRAMMARDEF_ENUM_INIT, _)
  231. }
  232. #undef BOOST_SPIRIT_GRAMMARDEF_ENUM_INIT
  233. ///////////////////////////////////////////////////////////////////////////
  234. public:
  235. T const &
  236. start() const
  237. {
  238. // If the following assertion is fired, you have probably forgot to call
  239. // the start_parser() function from inside the constructor of your
  240. // embedded definition class to initialize the start parsers to be exposed
  241. // from your grammar.
  242. using ::phoenix::tuple_index_names::_1;
  243. BOOST_SPIRIT_ASSERT(0 != t[_1]);
  244. return *t[_1];
  245. }
  246. template <int N>
  247. typename ::phoenix::tuple_element<N, tuple_t>::crtype
  248. get_start_parser() const
  249. {
  250. // If the following expression yields a compiler error, you have probably
  251. // tried to access a start rule, which isn't exposed as such from your
  252. // grammar.
  253. BOOST_STATIC_ASSERT(N > 0 && N < tuple_t::length);
  254. ::phoenix::tuple_index<N> const idx;
  255. // If the following assertion is fired, you have probably forgot to call
  256. // the start_parser() function from inside the constructor of your
  257. // embedded definition class to initialize the start parsers to be exposed
  258. // from your grammar.
  259. // Another reason may be, that there is a count mismatch between
  260. // the number of template parameters to the grammar_def<> class and the
  261. // number of parameters used while calling start_parsers().
  262. BOOST_SPIRIT_ASSERT(0 != t[idx]);
  263. return t[idx];
  264. }
  265. private:
  266. tuple_t t;
  267. };
  268. #undef BOOST_SPIRIT_GRAMMAR_STARTRULE_TYPE_LIMIT_A
  269. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  270. }} // namespace BOOST_SPIRIT_CLASSIC_NS
  271. #endif // BOOST_SPIRIT_GRAMMAR_DEF_HPP