meta_grammar.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*=============================================================================
  2. Copyright (c) 2005-2010 Joel de Guzman
  3. Copyright (c) 2010 Eric Niebler
  4. Copyright (c) 2010 Thomas Heller
  5. Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. ==============================================================================*/
  8. #ifndef BOOST_PHOENIX_CORE_META_GRAMMAR_HPP
  9. #define BOOST_PHOENIX_CORE_META_GRAMMAR_HPP
  10. #include <boost/phoenix/core/limits.hpp>
  11. #include <boost/mpl/deref.hpp>
  12. #include <boost/phoenix/core/environment.hpp>
  13. #include <boost/proto/matches.hpp>
  14. #include <boost/proto/transform/call.hpp>
  15. #include <boost/proto/transform/default.hpp>
  16. namespace boost { namespace phoenix
  17. {
  18. /////////////////////////////////////////////////////////////////////////////
  19. // The grammar defining valid phoenix expressions
  20. struct meta_grammar
  21. : proto::switch_<meta_grammar>
  22. {
  23. template <typename Tag, typename Dummy = void>
  24. struct case_
  25. : proto::not_<proto::_>
  26. {};
  27. };
  28. struct evaluator
  29. {
  30. BOOST_PROTO_TRANSFORM(evaluator)
  31. template <typename Expr, typename State, typename Data>
  32. struct impl
  33. : proto::transform_impl<Expr, State, Data>
  34. {
  35. typedef meta_grammar::impl<Expr, State, Data> what;
  36. typedef typename what::result_type result_type;
  37. result_type operator()(
  38. typename impl::expr_param e
  39. , typename impl::state_param s
  40. , typename impl::data_param d
  41. ) const
  42. {
  43. return what()(e, s, d);
  44. }
  45. };
  46. template <typename Expr, typename State>
  47. struct impl<Expr, State, proto::empty_env>
  48. : proto::transform_impl<Expr, State, proto::empty_env>
  49. {
  50. typedef
  51. meta_grammar::impl<
  52. Expr
  53. , typename result_of::env<State>::type
  54. , typename result_of::actions<State>::type
  55. >
  56. what;
  57. typedef typename what::result_type result_type;
  58. result_type operator()(
  59. typename impl::expr_param e
  60. , typename impl::state_param s
  61. , typename impl::data_param
  62. ) const
  63. {
  64. return what()(e, phoenix::env(s), actions(s));
  65. }
  66. };
  67. template <typename Expr, typename State>
  68. struct impl<Expr, State, unused>
  69. : proto::transform_impl<Expr, State, unused>
  70. {
  71. typedef
  72. meta_grammar::impl<
  73. Expr
  74. , typename result_of::env<State>::type
  75. , typename result_of::actions<State>::type
  76. >
  77. what;
  78. typedef typename what::result_type result_type;
  79. result_type operator()(
  80. typename impl::expr_param e
  81. , typename impl::state_param s
  82. , typename impl::data_param
  83. ) const
  84. {
  85. return what()(e, phoenix::env(s), actions(s));
  86. }
  87. };
  88. };
  89. /////////////////////////////////////////////////////////////////////////////
  90. // Set of default actions. Extend this whenever you add a new phoenix
  91. // construct
  92. struct default_actions
  93. {
  94. template <typename Rule, typename Dummy = void>
  95. struct when
  96. : proto::_default<meta_grammar>
  97. {};
  98. };
  99. template <typename Rule, typename Dummy = void>
  100. struct enable_rule
  101. : proto::when<Rule, proto::external_transform>
  102. {};
  103. namespace result_of
  104. {
  105. template <typename Expr, typename Context>
  106. struct eval
  107. : boost::result_of< ::boost::phoenix::evaluator(Expr, Context)>
  108. {};
  109. }
  110. /////////////////////////////////////////////////////////////////////////////
  111. // A function we can call to evaluate our expression
  112. template <typename Expr, typename Context>
  113. inline
  114. typename meta_grammar::template impl<
  115. Expr const&
  116. , typename result_of::env<Context const&>::type
  117. , typename result_of::actions<Context const&>::type
  118. >::result_type
  119. eval(Expr const& expr, Context const & ctx)
  120. {
  121. static evaluator const e = {};
  122. return e(expr, ctx);
  123. }
  124. template <typename Expr, typename Context>
  125. inline
  126. typename meta_grammar::template impl<
  127. Expr &
  128. , typename result_of::env<Context const&>::type
  129. , typename result_of::actions<Context const&>::type
  130. >::result_type
  131. eval(Expr & expr, Context const & ctx)
  132. {
  133. static evaluator const e = {};
  134. return e(expr, ctx);
  135. }
  136. }}
  137. #endif