argument_phoenix.hpp 7.2 KB

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