fundamental.ipp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*=============================================================================
  2. Copyright (c) 2002-2003 Hartmut Kaiser
  3. http://spirit.sourceforge.net/
  4. Use, modification and distribution is subject to the Boost Software
  5. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. #if !defined(BOOST_SPIRIT_FUNDAMENTAL_IPP)
  9. #define BOOST_SPIRIT_FUNDAMENTAL_IPP
  10. #include <boost/mpl/int.hpp>
  11. namespace boost { namespace spirit {
  12. BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
  13. #if defined(BOOST_MSVC) && (BOOST_MSVC <= 1300)
  14. BOOST_SPIRIT_DEPENDENT_TEMPLATE_WRAPPER2(count_wrapper, count);
  15. #endif // defined(BOOST_MSVC) && (BOOST_MSVC <= 1300)
  16. namespace impl
  17. {
  18. ///////////////////////////////////////////////////////////////////////////
  19. //
  20. // Helper template for counting the number of nodes contained in a
  21. // given parser type.
  22. // All parser_category type parsers are counted as nodes.
  23. //
  24. ///////////////////////////////////////////////////////////////////////////
  25. template <typename CategoryT>
  26. struct nodes;
  27. template <>
  28. struct nodes<plain_parser_category> {
  29. template <typename ParserT, typename LeafCountT>
  30. struct count {
  31. // __BORLANDC__ == 0x0561 isn't happy with BOOST_STATIC_CONSTANT
  32. enum { value = (LeafCountT::value + 1) };
  33. };
  34. };
  35. #if defined(BOOST_MSVC) && (BOOST_MSVC <= 1300)
  36. template <>
  37. struct nodes<unary_parser_category> {
  38. template <typename ParserT, typename LeafCountT>
  39. struct count {
  40. typedef typename ParserT::subject_t subject_t;
  41. typedef typename subject_t::parser_category_t subject_category_t;
  42. typedef nodes<subject_category_t> nodes_t;
  43. typedef typename count_wrapper<nodes_t>
  44. ::template result_<subject_t, LeafCountT> count_t;
  45. BOOST_STATIC_CONSTANT(int, value = count_t::value + 1);
  46. };
  47. };
  48. template <>
  49. struct nodes<action_parser_category> {
  50. template <typename ParserT, typename LeafCountT>
  51. struct count {
  52. typedef typename ParserT::subject_t subject_t;
  53. typedef typename subject_t::parser_category_t subject_category_t;
  54. typedef nodes<subject_category_t> nodes_t;
  55. typedef typename count_wrapper<nodes_t>
  56. ::template result_<subject_t, LeafCountT> count_t;
  57. BOOST_STATIC_CONSTANT(int, value = count_t::value + 1);
  58. };
  59. };
  60. template <>
  61. struct nodes<binary_parser_category> {
  62. template <typename ParserT, typename LeafCountT>
  63. struct count {
  64. typedef typename ParserT::left_t left_t;
  65. typedef typename ParserT::right_t right_t;
  66. typedef typename left_t::parser_category_t left_category_t;
  67. typedef typename right_t::parser_category_t right_category_t;
  68. typedef nodes<left_category_t> left_nodes_t;
  69. typedef typename count_wrapper<left_nodes_t>
  70. ::template result_<left_t, LeafCountT> left_count_t;
  71. typedef nodes<right_category_t> right_nodes_t;
  72. typedef typename count_wrapper<right_nodes_t>
  73. ::template result_<right_t, LeafCountT> right_count_t;
  74. BOOST_STATIC_CONSTANT(int,
  75. value = (left_count_t::value + right_count_t::value + 1));
  76. };
  77. };
  78. #else
  79. template <>
  80. struct nodes<unary_parser_category> {
  81. template <typename ParserT, typename LeafCountT>
  82. struct count {
  83. typedef typename ParserT::subject_t subject_t;
  84. typedef typename subject_t::parser_category_t subject_category_t;
  85. // __BORLANDC__ == 0x0561 isn't happy with BOOST_STATIC_CONSTANT
  86. enum { value = (nodes<subject_category_t>
  87. ::template count<subject_t, LeafCountT>::value + 1) };
  88. };
  89. };
  90. template <>
  91. struct nodes<action_parser_category> {
  92. template <typename ParserT, typename LeafCountT>
  93. struct count {
  94. typedef typename ParserT::subject_t subject_t;
  95. typedef typename subject_t::parser_category_t subject_category_t;
  96. // __BORLANDC__ == 0x0561 isn't happy with BOOST_STATIC_CONSTANT
  97. enum { value = (nodes<subject_category_t>
  98. ::template count<subject_t, LeafCountT>::value + 1) };
  99. };
  100. };
  101. template <>
  102. struct nodes<binary_parser_category> {
  103. template <typename ParserT, typename LeafCountT>
  104. struct count {
  105. typedef typename ParserT::left_t left_t;
  106. typedef typename ParserT::right_t right_t;
  107. typedef typename left_t::parser_category_t left_category_t;
  108. typedef typename right_t::parser_category_t right_category_t;
  109. typedef count self_t;
  110. // __BORLANDC__ == 0x0561 isn't happy with BOOST_STATIC_CONSTANT
  111. enum {
  112. leftcount = (nodes<left_category_t>
  113. ::template count<left_t, LeafCountT>::value),
  114. rightcount = (nodes<right_category_t>
  115. ::template count<right_t, LeafCountT>::value),
  116. value = ((self_t::leftcount) + (self_t::rightcount) + 1)
  117. };
  118. };
  119. };
  120. #endif
  121. ///////////////////////////////////////////////////////////////////////////
  122. //
  123. // Helper template for counting the number of leaf nodes contained in a
  124. // given parser type.
  125. // Only plain_parser_category type parsers are counted as leaf nodes.
  126. //
  127. ///////////////////////////////////////////////////////////////////////////
  128. template <typename CategoryT>
  129. struct leafs;
  130. template <>
  131. struct leafs<plain_parser_category> {
  132. template <typename ParserT, typename LeafCountT>
  133. struct count {
  134. // __BORLANDC__ == 0x0561 isn't happy with BOOST_STATIC_CONSTANT
  135. enum { value = (LeafCountT::value + 1) };
  136. };
  137. };
  138. #if defined(BOOST_MSVC) && (BOOST_MSVC <= 1300)
  139. template <>
  140. struct leafs<unary_parser_category> {
  141. template <typename ParserT, typename LeafCountT>
  142. struct count {
  143. typedef typename ParserT::subject_t subject_t;
  144. typedef typename subject_t::parser_category_t subject_category_t;
  145. typedef leafs<subject_category_t> nodes_t;
  146. typedef typename count_wrapper<nodes_t>
  147. ::template result_<subject_t, LeafCountT> count_t;
  148. BOOST_STATIC_CONSTANT(int, value = count_t::value);
  149. };
  150. };
  151. template <>
  152. struct leafs<action_parser_category> {
  153. template <typename ParserT, typename LeafCountT>
  154. struct count {
  155. typedef typename ParserT::subject_t subject_t;
  156. typedef typename subject_t::parser_category_t subject_category_t;
  157. typedef leafs<subject_category_t> nodes_t;
  158. typedef typename count_wrapper<nodes_t>
  159. ::template result_<subject_t, LeafCountT> count_t;
  160. BOOST_STATIC_CONSTANT(int, value = count_t::value);
  161. };
  162. };
  163. template <>
  164. struct leafs<binary_parser_category> {
  165. template <typename ParserT, typename LeafCountT>
  166. struct count {
  167. typedef typename ParserT::left_t left_t;
  168. typedef typename ParserT::right_t right_t;
  169. typedef typename left_t::parser_category_t left_category_t;
  170. typedef typename right_t::parser_category_t right_category_t;
  171. typedef leafs<left_category_t> left_nodes_t;
  172. typedef typename count_wrapper<left_nodes_t>
  173. ::template result_<left_t, LeafCountT> left_count_t;
  174. typedef leafs<right_category_t> right_nodes_t;
  175. typedef typename count_wrapper<right_nodes_t>
  176. ::template result_<right_t, LeafCountT> right_count_t;
  177. BOOST_STATIC_CONSTANT(int,
  178. value = (left_count_t::value + right_count_t::value));
  179. };
  180. };
  181. #else
  182. template <>
  183. struct leafs<unary_parser_category> {
  184. template <typename ParserT, typename LeafCountT>
  185. struct count {
  186. typedef typename ParserT::subject_t subject_t;
  187. typedef typename subject_t::parser_category_t subject_category_t;
  188. // __BORLANDC__ == 0x0561 isn't happy with BOOST_STATIC_CONSTANT
  189. enum { value = (leafs<subject_category_t>
  190. ::template count<subject_t, LeafCountT>::value) };
  191. };
  192. };
  193. template <>
  194. struct leafs<action_parser_category> {
  195. template <typename ParserT, typename LeafCountT>
  196. struct count {
  197. typedef typename ParserT::subject_t subject_t;
  198. typedef typename subject_t::parser_category_t subject_category_t;
  199. // __BORLANDC__ == 0x0561 isn't happy with BOOST_STATIC_CONSTANT
  200. enum { value = (leafs<subject_category_t>
  201. ::template count<subject_t, LeafCountT>::value) };
  202. };
  203. };
  204. template <>
  205. struct leafs<binary_parser_category> {
  206. template <typename ParserT, typename LeafCountT>
  207. struct count {
  208. typedef typename ParserT::left_t left_t;
  209. typedef typename ParserT::right_t right_t;
  210. typedef typename left_t::parser_category_t left_category_t;
  211. typedef typename right_t::parser_category_t right_category_t;
  212. typedef count self_t;
  213. // __BORLANDC__ == 0x0561 isn't happy with BOOST_STATIC_CONSTANT
  214. enum {
  215. leftcount = (leafs<left_category_t>
  216. ::template count<left_t, LeafCountT>::value),
  217. rightcount = (leafs<right_category_t>
  218. ::template count<right_t, LeafCountT>::value),
  219. value = (self_t::leftcount + self_t::rightcount)
  220. };
  221. };
  222. };
  223. #endif
  224. } // namespace impl
  225. ///////////////////////////////////////////////////////////////////////////////
  226. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  227. }} // namespace boost::spirit
  228. #endif // !defined(BOOST_SPIRIT_FUNDAMENTAL_IPP)