fundamental.ipp 5.6 KB

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