as_alternate.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // as_alternate.hpp
  3. //
  4. // Copyright 2008 Eric Niebler. Distributed under the Boost
  5. // Software License, Version 1.0. (See accompanying file
  6. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_XPRESSIVE_DETAIL_STATIC_TRANSFORMS_AS_ALTERNATE_HPP_EAN_04_01_2007
  8. #define BOOST_XPRESSIVE_DETAIL_STATIC_TRANSFORMS_AS_ALTERNATE_HPP_EAN_04_01_2007
  9. // MS compatible compilers support #pragma once
  10. #if defined(_MSC_VER)
  11. # pragma once
  12. #endif
  13. #include <boost/config.hpp>
  14. #include <boost/mpl/assert.hpp>
  15. #include <boost/type_traits/is_same.hpp>
  16. #include <boost/proto/core.hpp>
  17. #include <boost/xpressive/detail/detail_fwd.hpp>
  18. #include <boost/xpressive/detail/static/static.hpp>
  19. #include <boost/xpressive/detail/core/matcher/alternate_matcher.hpp>
  20. #include <boost/xpressive/detail/utility/cons.hpp>
  21. namespace boost { namespace xpressive
  22. {
  23. namespace detail
  24. {
  25. ///////////////////////////////////////////////////////////////////////////////
  26. // alternates_list
  27. // a fusion-compatible sequence of alternate expressions, that also keeps
  28. // track of the list's width and purity.
  29. template<typename Head, typename Tail>
  30. struct alternates_list
  31. : fusion::cons<Head, Tail>
  32. {
  33. BOOST_STATIC_CONSTANT(std::size_t, width = Head::width == Tail::width ? Head::width : detail::unknown_width::value);
  34. BOOST_STATIC_CONSTANT(bool, pure = Head::pure && Tail::pure);
  35. alternates_list(Head const &head, Tail const &tail)
  36. : fusion::cons<Head, Tail>(head, tail)
  37. {
  38. }
  39. };
  40. template<typename Head>
  41. struct alternates_list<Head, fusion::nil>
  42. : fusion::cons<Head, fusion::nil>
  43. {
  44. BOOST_STATIC_CONSTANT(std::size_t, width = Head::width);
  45. BOOST_STATIC_CONSTANT(bool, pure = Head::pure);
  46. alternates_list(Head const &head, fusion::nil const &tail)
  47. : fusion::cons<Head, fusion::nil>(head, tail)
  48. {
  49. }
  50. };
  51. }
  52. namespace grammar_detail
  53. {
  54. ///////////////////////////////////////////////////////////////////////////////
  55. // in_alternate_list
  56. template<typename Grammar, typename Callable = proto::callable>
  57. struct in_alternate_list : proto::transform<in_alternate_list<Grammar, Callable> >
  58. {
  59. template<typename Expr, typename State, typename Data>
  60. struct impl : proto::transform_impl<Expr, State, Data>
  61. {
  62. typedef
  63. detail::alternates_list<
  64. typename Grammar::template impl<
  65. Expr
  66. , detail::alternate_end_xpression
  67. , Data
  68. >::result_type
  69. , State
  70. >
  71. result_type;
  72. result_type operator ()(
  73. typename impl::expr_param expr
  74. , typename impl::state_param state
  75. , typename impl::data_param data
  76. ) const
  77. {
  78. return result_type(
  79. typename Grammar::template impl<Expr, detail::alternate_end_xpression, Data>()(
  80. expr
  81. , detail::alternate_end_xpression()
  82. , data
  83. )
  84. , state
  85. );
  86. }
  87. };
  88. };
  89. ///////////////////////////////////////////////////////////////////////////////
  90. // as_alternate_matcher
  91. template<typename Grammar, typename Callable = proto::callable>
  92. struct as_alternate_matcher : proto::transform<as_alternate_matcher<Grammar, Callable> >
  93. {
  94. template<typename Expr, typename State, typename Data>
  95. struct impl : proto::transform_impl<Expr, State, Data>
  96. {
  97. typedef typename impl::data data_type;
  98. typedef
  99. detail::alternate_matcher<
  100. typename Grammar::template impl<Expr, State, Data>::result_type
  101. , typename data_type::traits_type
  102. >
  103. result_type;
  104. result_type operator ()(
  105. typename impl::expr_param expr
  106. , typename impl::state_param state
  107. , typename impl::data_param data
  108. ) const
  109. {
  110. return result_type(
  111. typename Grammar::template impl<Expr, State, Data>()(expr, state, data)
  112. );
  113. }
  114. };
  115. };
  116. }
  117. }}
  118. #endif