argument_phoenix.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // Copyright (c) 2001-2011 Hartmut Kaiser
  2. // Copyright (c) 2011 Thomas Heller
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #if !defined(BOOST_SPIRIT_LEX_ARGUMENT_PHEONIX_MARCH_25_2011_1841PM)
  7. #define BOOST_SPIRIT_LEX_ARGUMENT_PHEONIX_MARCH_25_2011_1841PM
  8. #if defined(_MSC_VER)
  9. #pragma once
  10. #endif
  11. #include <boost/phoenix/core/actor.hpp>
  12. #include <boost/phoenix/core/as_actor.hpp>
  13. #include <boost/phoenix/core/expression.hpp>
  14. #include <boost/phoenix/core/v2_eval.hpp>
  15. #include <boost/phoenix/core/value.hpp> // includes as_actor specialization
  16. #include <boost/proto/traits.hpp>
  17. #include <boost/proto/proto_fwd.hpp> // for transform placeholders
  18. namespace boost { namespace spirit { namespace lex
  19. {
  20. ///////////////////////////////////////////////////////////////////////////
  21. // The value_context is used as a noop Phoenix actor to create the
  22. // placeholder '_val' (see below). It is a noop actor because it is used
  23. // as a placeholder only, while it is being converted either to a
  24. // value_getter (if used as a rvalue) or to a value_setter (if used as a
  25. // lvalue). The conversion is achieved by specializing and overloading a
  26. // couple of the Phoenix templates from the Phoenix expression composition
  27. // engine (see the end of this file).
  28. struct value_context
  29. {
  30. typedef mpl::true_ no_nullary;
  31. typedef unused_type result_type;
  32. template <typename Env>
  33. struct result
  34. {
  35. typedef unused_type type;
  36. };
  37. template <typename Env>
  38. unused_type
  39. eval(Env const&) const
  40. {
  41. return unused;
  42. }
  43. };
  44. // forward declarations
  45. struct value_getter;
  46. template <typename> struct value_setter;
  47. ///////////////////////////////////////////////////////////////////////////
  48. // The state_context is used as a noop Phoenix actor to create the
  49. // placeholder '_state' (see below). It is a noop actor because it is used
  50. // as a placeholder only, while it is being converted either to a
  51. // state_getter (if used as a rvalue) or to a state_setter (if used as a
  52. // lvalue). The conversion is achieved by specializing and overloading a
  53. // couple of the Phoenix templates from the Phoenix expression composition
  54. // engine (see the end of this file).
  55. struct state_context
  56. {
  57. typedef mpl::true_ no_nullary;
  58. typedef unused_type result_type;
  59. template <typename Env>
  60. struct result
  61. {
  62. typedef unused_type type;
  63. };
  64. template <typename Env>
  65. unused_type
  66. eval(Env const&) const
  67. {
  68. return unused;
  69. }
  70. };
  71. // forward declarations
  72. struct state_getter;
  73. template <typename> struct state_setter;
  74. struct eoi_getter;
  75. }}}
  76. ///////////////////////////////////////////////////////////////////////////////
  77. BOOST_PHOENIX_DEFINE_EXPRESSION(
  78. (boost)(spirit)(lex)(value_setter)
  79. , (boost::phoenix::meta_grammar)
  80. )
  81. BOOST_PHOENIX_DEFINE_EXPRESSION(
  82. (boost)(spirit)(lex)(state_setter)
  83. , (boost::phoenix::meta_grammar)
  84. )
  85. namespace boost { namespace phoenix
  86. {
  87. namespace result_of
  88. {
  89. template <>
  90. struct is_nullary<custom_terminal<boost::spirit::lex::value_context> >
  91. : mpl::false_
  92. {};
  93. }
  94. template <typename Dummy>
  95. struct is_custom_terminal<boost::spirit::lex::value_context, Dummy>: mpl::true_ {};
  96. template <typename Dummy>
  97. struct custom_terminal<boost::spirit::lex::value_context, Dummy>
  98. : proto::call<
  99. v2_eval(
  100. proto::make<boost::spirit::lex::value_getter()>
  101. , proto::call<functional::env(proto::_state)>
  102. )
  103. >
  104. {};
  105. template <typename Dummy>
  106. struct is_nullary::when<spirit::lex::rule::value_setter, Dummy>
  107. : proto::make<mpl::false_()>
  108. {};
  109. template <typename Dummy>
  110. struct default_actions::when<spirit::lex::rule::value_setter, Dummy>
  111. : proto::call<
  112. v2_eval(
  113. proto::make<
  114. spirit::lex::value_setter<proto::_child0>(
  115. proto::_child0
  116. )
  117. >
  118. , _env
  119. )
  120. >
  121. {};
  122. template <>
  123. struct actor<spirit::lex::value_context>
  124. : boost::phoenix::actor<proto::terminal<spirit::lex::value_context>::type>
  125. {
  126. typedef boost::phoenix::actor<
  127. proto::terminal<spirit::lex::value_context>::type
  128. > base_type;
  129. actor(base_type const & base = base_type())
  130. : base_type(base)
  131. {}
  132. template <typename Expr>
  133. typename spirit::lex::expression::value_setter<
  134. typename phoenix::as_actor<Expr>::type>::type const
  135. operator=(Expr const & expr) const
  136. {
  137. return
  138. spirit::lex::expression::value_setter<
  139. typename phoenix::as_actor<Expr>::type
  140. >::make(phoenix::as_actor<Expr>::convert(expr));
  141. }
  142. };
  143. namespace result_of
  144. {
  145. template <>
  146. struct is_nullary<custom_terminal<boost::spirit::lex::state_context> >
  147. : mpl::false_
  148. {};
  149. }
  150. template <typename Dummy>
  151. struct is_custom_terminal<boost::spirit::lex::state_context, Dummy>: mpl::true_ {};
  152. template <typename Dummy>
  153. struct custom_terminal<boost::spirit::lex::state_context, Dummy>
  154. : proto::call<
  155. v2_eval(
  156. proto::make<boost::spirit::lex::state_getter()>
  157. , proto::call<functional::env(proto::_state)>
  158. )
  159. >
  160. {};
  161. template <typename Dummy>
  162. struct is_nullary::when<spirit::lex::rule::state_setter, Dummy>
  163. : proto::make<mpl::false_()>
  164. {};
  165. template <typename Dummy>
  166. struct default_actions::when<spirit::lex::rule::state_setter, Dummy>
  167. : proto::call<
  168. v2_eval(
  169. proto::make<
  170. spirit::lex::state_setter<proto::_child0>(
  171. proto::_child0
  172. )
  173. >
  174. , _env
  175. )
  176. >
  177. {};
  178. template <>
  179. struct actor<spirit::lex::state_context>
  180. : boost::phoenix::actor<proto::terminal<spirit::lex::state_context>::type>
  181. {
  182. typedef boost::phoenix::actor<
  183. proto::terminal<spirit::lex::state_context>::type
  184. > base_type;
  185. actor(base_type const & base = base_type())
  186. : base_type(base)
  187. {}
  188. template <typename Expr>
  189. typename spirit::lex::expression::state_setter<
  190. typename phoenix::as_actor<Expr>::type>::type const
  191. operator=(Expr const & expr) const
  192. {
  193. return
  194. spirit::lex::expression::state_setter<
  195. typename phoenix::as_actor<Expr>::type
  196. >::make(phoenix::as_actor<Expr>::convert(expr));
  197. }
  198. };
  199. namespace result_of
  200. {
  201. template <>
  202. struct is_nullary<custom_terminal<boost::spirit::lex::eoi_getter> >
  203. : mpl::false_
  204. {};
  205. }
  206. template <typename Dummy>
  207. struct is_custom_terminal<boost::spirit::lex::eoi_getter, Dummy>: mpl::true_ {};
  208. template <typename Dummy>
  209. struct custom_terminal<boost::spirit::lex::eoi_getter, Dummy>
  210. : proto::call<
  211. v2_eval(
  212. proto::make<boost::spirit::lex::eoi_getter()>
  213. , proto::call<functional::env(proto::_state)>
  214. )
  215. >
  216. {};
  217. }}
  218. #endif