fundamental.ipp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. namespace impl
  14. {
  15. ///////////////////////////////////////////////////////////////////////////
  16. //
  17. // Helper template for counting the number of nodes contained in a
  18. // given parser type.
  19. // All parser_category type parsers are counted as nodes.
  20. //
  21. ///////////////////////////////////////////////////////////////////////////
  22. template <typename CategoryT>
  23. struct nodes;
  24. template <>
  25. struct nodes<plain_parser_category> {
  26. template <typename ParserT, typename LeafCountT>
  27. struct count {
  28. // __BORLANDC__ == 0x0561 isn't happy with BOOST_STATIC_CONSTANT
  29. enum { value = (LeafCountT::value + 1) };
  30. };
  31. };
  32. template <>
  33. struct nodes<unary_parser_category> {
  34. template <typename ParserT, typename LeafCountT>
  35. struct count {
  36. typedef typename ParserT::subject_t subject_t;
  37. typedef typename subject_t::parser_category_t subject_category_t;
  38. // __BORLANDC__ == 0x0561 isn't happy with BOOST_STATIC_CONSTANT
  39. enum { value = (nodes<subject_category_t>
  40. ::template count<subject_t, LeafCountT>::value + 1) };
  41. };
  42. };
  43. template <>
  44. struct nodes<action_parser_category> {
  45. template <typename ParserT, typename LeafCountT>
  46. struct count {
  47. typedef typename ParserT::subject_t subject_t;
  48. typedef typename subject_t::parser_category_t subject_category_t;
  49. // __BORLANDC__ == 0x0561 isn't happy with BOOST_STATIC_CONSTANT
  50. enum { value = (nodes<subject_category_t>
  51. ::template count<subject_t, LeafCountT>::value + 1) };
  52. };
  53. };
  54. template <>
  55. struct nodes<binary_parser_category> {
  56. template <typename ParserT, typename LeafCountT>
  57. struct count {
  58. typedef typename ParserT::left_t left_t;
  59. typedef typename ParserT::right_t right_t;
  60. typedef typename left_t::parser_category_t left_category_t;
  61. typedef typename right_t::parser_category_t right_category_t;
  62. typedef count self_t;
  63. // __BORLANDC__ == 0x0561 isn't happy with BOOST_STATIC_CONSTANT
  64. enum {
  65. leftcount = (nodes<left_category_t>
  66. ::template count<left_t, LeafCountT>::value),
  67. rightcount = (nodes<right_category_t>
  68. ::template count<right_t, LeafCountT>::value),
  69. value = ((self_t::leftcount) + (self_t::rightcount) + 1)
  70. };
  71. };
  72. };
  73. ///////////////////////////////////////////////////////////////////////////
  74. //
  75. // Helper template for counting the number of leaf nodes contained in a
  76. // given parser type.
  77. // Only plain_parser_category type parsers are counted as leaf nodes.
  78. //
  79. ///////////////////////////////////////////////////////////////////////////
  80. template <typename CategoryT>
  81. struct leafs;
  82. template <>
  83. struct leafs<plain_parser_category> {
  84. template <typename ParserT, typename LeafCountT>
  85. struct count {
  86. // __BORLANDC__ == 0x0561 isn't happy with BOOST_STATIC_CONSTANT
  87. enum { value = (LeafCountT::value + 1) };
  88. };
  89. };
  90. template <>
  91. struct leafs<unary_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 = (leafs<subject_category_t>
  98. ::template count<subject_t, LeafCountT>::value) };
  99. };
  100. };
  101. template <>
  102. struct leafs<action_parser_category> {
  103. template <typename ParserT, typename LeafCountT>
  104. struct count {
  105. typedef typename ParserT::subject_t subject_t;
  106. typedef typename subject_t::parser_category_t subject_category_t;
  107. // __BORLANDC__ == 0x0561 isn't happy with BOOST_STATIC_CONSTANT
  108. enum { value = (leafs<subject_category_t>
  109. ::template count<subject_t, LeafCountT>::value) };
  110. };
  111. };
  112. template <>
  113. struct leafs<binary_parser_category> {
  114. template <typename ParserT, typename LeafCountT>
  115. struct count {
  116. typedef typename ParserT::left_t left_t;
  117. typedef typename ParserT::right_t right_t;
  118. typedef typename left_t::parser_category_t left_category_t;
  119. typedef typename right_t::parser_category_t right_category_t;
  120. typedef count self_t;
  121. // __BORLANDC__ == 0x0561 isn't happy with BOOST_STATIC_CONSTANT
  122. enum {
  123. leftcount = (leafs<left_category_t>
  124. ::template count<left_t, LeafCountT>::value),
  125. rightcount = (leafs<right_category_t>
  126. ::template count<right_t, LeafCountT>::value),
  127. value = (self_t::leftcount + self_t::rightcount)
  128. };
  129. };
  130. };
  131. } // namespace impl
  132. ///////////////////////////////////////////////////////////////////////////////
  133. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  134. }} // namespace boost::spirit
  135. #endif // !defined(BOOST_SPIRIT_FUNDAMENTAL_IPP)