select.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*=============================================================================
  2. Copyright (c) 2003 Hartmut Kaiser
  3. http://spirit.sourceforge.net/
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. =============================================================================*/
  7. #ifndef BOOST_SPIRIT_SELECT_HPP
  8. #define BOOST_SPIRIT_SELECT_HPP
  9. #include <boost/preprocessor/repeat.hpp>
  10. #include <boost/preprocessor/enum.hpp>
  11. #include <boost/preprocessor/enum_params.hpp>
  12. #include <boost/preprocessor/enum_params_with_defaults.hpp>
  13. #include <boost/preprocessor/inc.hpp>
  14. #include <boost/preprocessor/cat.hpp>
  15. #include <boost/preprocessor/facilities/intercept.hpp>
  16. #include <boost/spirit/home/classic/namespace.hpp>
  17. #include <boost/spirit/home/classic/core/parser.hpp>
  18. #include <boost/spirit/home/classic/phoenix/tuples.hpp>
  19. ///////////////////////////////////////////////////////////////////////////////
  20. //
  21. // Spirit predefined maximum number of possible embedded select_p parsers.
  22. // It should NOT be greater than PHOENIX_LIMIT!
  23. //
  24. ///////////////////////////////////////////////////////////////////////////////
  25. #if !defined(BOOST_SPIRIT_SELECT_LIMIT)
  26. #define BOOST_SPIRIT_SELECT_LIMIT PHOENIX_LIMIT
  27. #endif // !defined(BOOST_SPIRIT_SELECT_LIMIT)
  28. ///////////////////////////////////////////////////////////////////////////////
  29. //
  30. // ensure BOOST_SPIRIT_SELECT_LIMIT <= PHOENIX_LIMIT and
  31. // BOOST_SPIRIT_SELECT_LIMIT > 0
  32. // BOOST_SPIRIT_SELECT_LIMIT <= 15
  33. //
  34. // [Pushed this down a little to make CW happy with BOOST_STATIC_ASSERT]
  35. // [Otherwise, it complains: 'boost_static_assert_test_42' redefined]
  36. //
  37. ///////////////////////////////////////////////////////////////////////////////
  38. BOOST_STATIC_ASSERT(BOOST_SPIRIT_SELECT_LIMIT <= PHOENIX_LIMIT);
  39. BOOST_STATIC_ASSERT(BOOST_SPIRIT_SELECT_LIMIT > 0);
  40. BOOST_STATIC_ASSERT(BOOST_SPIRIT_SELECT_LIMIT <= 15);
  41. ///////////////////////////////////////////////////////////////////////////////
  42. //
  43. // Calculate the required amount of tuple members rounded up to the nearest
  44. // integer dividable by 3
  45. //
  46. ///////////////////////////////////////////////////////////////////////////////
  47. #if BOOST_SPIRIT_SELECT_LIMIT > 12
  48. #define BOOST_SPIRIT_SELECT_LIMIT_A 15
  49. #elif BOOST_SPIRIT_SELECT_LIMIT > 9
  50. #define BOOST_SPIRIT_SELECT_LIMIT_A 12
  51. #elif BOOST_SPIRIT_SELECT_LIMIT > 6
  52. #define BOOST_SPIRIT_SELECT_LIMIT_A 9
  53. #elif BOOST_SPIRIT_SELECT_LIMIT > 3
  54. #define BOOST_SPIRIT_SELECT_LIMIT_A 6
  55. #else
  56. #define BOOST_SPIRIT_SELECT_LIMIT_A 3
  57. #endif
  58. ///////////////////////////////////////////////////////////////////////////////
  59. namespace boost { namespace spirit {
  60. BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
  61. ///////////////////////////////////////////////////////////////////////////////
  62. //
  63. // The select_default_no_fail and select_default_fail structs are used to
  64. // distinguish two different behaviours for the select_parser in case that not
  65. // any of the given sub-parsers match.
  66. //
  67. // If the select_parser is used with the select_default_no_fail behaviour,
  68. // then in case of no matching sub-parser the whole select_parser returns an
  69. // empty match and the value -1.
  70. //
  71. // If the select_parser is used with the select_default_fail behaviour, then
  72. // in case of no matching sub-parser the whole select_parser fails to match at
  73. // all.
  74. //
  75. ///////////////////////////////////////////////////////////////////////////////
  76. struct select_default_no_fail {};
  77. struct select_default_fail {};
  78. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  79. }} // namespace BOOST_SPIRIT_CLASSIC_NS
  80. ///////////////////////////////////////////////////////////////////////////////
  81. #include <boost/spirit/home/classic/dynamic/impl/select.ipp>
  82. ///////////////////////////////////////////////////////////////////////////////
  83. namespace boost { namespace spirit {
  84. BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
  85. ///////////////////////////////////////////////////////////////////////////////
  86. template <typename TupleT, typename BehaviourT, typename T>
  87. struct select_parser
  88. : public parser<select_parser<TupleT, BehaviourT, T> >
  89. {
  90. typedef select_parser<TupleT, BehaviourT, T> self_t;
  91. select_parser(TupleT const &t_)
  92. : t(t_)
  93. {}
  94. template <typename ScannerT>
  95. struct result
  96. {
  97. typedef typename match_result<ScannerT, T>::type type;
  98. };
  99. template <typename ScannerT>
  100. typename parser_result<self_t, ScannerT>::type
  101. parse(ScannerT const& scan) const
  102. {
  103. typedef typename parser_result<self_t, ScannerT>::type result_t;
  104. if (!scan.at_end()) {
  105. return impl::parse_tuple_element<
  106. TupleT::length, result_t, TupleT, BehaviourT>::do_(t, scan);
  107. }
  108. return impl::select_match_gen<result_t, BehaviourT>::do_(scan);
  109. }
  110. TupleT const t;
  111. };
  112. ///////////////////////////////////////////////////////////////////////////////
  113. template <typename BehaviourT, typename T = int>
  114. struct select_parser_gen {
  115. ///////////////////////////////////////////////////////////////////////////
  116. //
  117. // This generates different select_parser_gen::operator()() functions with
  118. // an increasing number of parser parameters:
  119. //
  120. // template <typename ParserT0, ...>
  121. // select_parser<
  122. // ::phoenix::tuple<
  123. // typename impl::as_embedded_parser<ParserT0>::type,
  124. // ...
  125. // >,
  126. // BehaviourT,
  127. // T
  128. // >
  129. // operator()(ParserT0 const &p0, ...) const
  130. // {
  131. // typedef impl::as_embedded_parser<ParserT0> parser_t0;
  132. // ...
  133. //
  134. // typedef ::phoenix::tuple<
  135. // parser_t0::type,
  136. // ...
  137. // > tuple_t;
  138. // typedef select_parser<tuple_t, BehaviourT, T> result_t;
  139. //
  140. // return result_t(tuple_t(
  141. // parser_t0::convert(p0),
  142. // ...
  143. // ));
  144. // }
  145. //
  146. // The number of generated functions depends on the maximum tuple member
  147. // limit defined by the PHOENIX_LIMIT pp constant.
  148. //
  149. ///////////////////////////////////////////////////////////////////////////
  150. #define BOOST_SPIRIT_SELECT_EMBEDDED(z, N, _) \
  151. typename impl::as_embedded_parser<BOOST_PP_CAT(ParserT, N)>::type \
  152. /**/
  153. #define BOOST_SPIRIT_SELECT_EMBEDDED_TYPEDEF(z, N, _) \
  154. typedef impl::as_embedded_parser<BOOST_PP_CAT(ParserT, N)> \
  155. BOOST_PP_CAT(parser_t, N); \
  156. /**/
  157. #define BOOST_SPIRIT_SELECT_CONVERT(z, N, _) \
  158. BOOST_PP_CAT(parser_t, N)::convert(BOOST_PP_CAT(p, N)) \
  159. /**/
  160. #define BOOST_SPIRIT_SELECT_PARSER(z, N, _) \
  161. template < \
  162. BOOST_PP_ENUM_PARAMS_Z(z, BOOST_PP_INC(N), typename ParserT) \
  163. > \
  164. select_parser< \
  165. ::phoenix::tuple< \
  166. BOOST_PP_ENUM_ ## z(BOOST_PP_INC(N), \
  167. BOOST_SPIRIT_SELECT_EMBEDDED, _) \
  168. >, \
  169. BehaviourT, \
  170. T \
  171. > \
  172. operator()( \
  173. BOOST_PP_ENUM_BINARY_PARAMS_Z(z, BOOST_PP_INC(N), \
  174. ParserT, const &p) \
  175. ) const \
  176. { \
  177. BOOST_PP_REPEAT_ ## z(BOOST_PP_INC(N), \
  178. BOOST_SPIRIT_SELECT_EMBEDDED_TYPEDEF, _) \
  179. \
  180. typedef ::phoenix::tuple< \
  181. BOOST_PP_ENUM_BINARY_PARAMS_Z(z, BOOST_PP_INC(N), \
  182. typename parser_t, ::type BOOST_PP_INTERCEPT) \
  183. > tuple_t; \
  184. typedef select_parser<tuple_t, BehaviourT, T> result_t; \
  185. \
  186. return result_t(tuple_t( \
  187. BOOST_PP_ENUM_ ## z(BOOST_PP_INC(N), \
  188. BOOST_SPIRIT_SELECT_CONVERT, _) \
  189. )); \
  190. } \
  191. /**/
  192. BOOST_PP_REPEAT(BOOST_SPIRIT_SELECT_LIMIT_A,
  193. BOOST_SPIRIT_SELECT_PARSER, _)
  194. #undef BOOST_SPIRIT_SELECT_PARSER
  195. #undef BOOST_SPIRIT_SELECT_CONVERT
  196. #undef BOOST_SPIRIT_SELECT_EMBEDDED_TYPEDEF
  197. #undef BOOST_SPIRIT_SELECT_EMBEDDED
  198. ///////////////////////////////////////////////////////////////////////////
  199. };
  200. ///////////////////////////////////////////////////////////////////////////////
  201. //
  202. // Predefined parser generator helper objects
  203. //
  204. ///////////////////////////////////////////////////////////////////////////////
  205. select_parser_gen<select_default_no_fail> const select_p =
  206. select_parser_gen<select_default_no_fail>();
  207. select_parser_gen<select_default_fail> const select_fail_p =
  208. select_parser_gen<select_default_fail>();
  209. #undef BOOST_SPIRIT_SELECT_LIMIT_A
  210. ///////////////////////////////////////////////////////////////////////////////
  211. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  212. }} // namespace BOOST_SPIRIT_CLASSIC_NS
  213. #endif // BOOST_SPIRIT_SELECT_HPP