context.hpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Copyright (c) 2001-2011 Hartmut Kaiser
  4. Copyright (c) 2011 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. #if !defined(BOOST_SPIRIT_CONTEXT_OCTOBER_31_2008_0654PM)
  9. #define BOOST_SPIRIT_CONTEXT_OCTOBER_31_2008_0654PM
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. #include <boost/preprocessor/repetition/repeat_from_to.hpp>
  14. #include <boost/spirit/home/support/nonterminal/expand_arg.hpp>
  15. #include <boost/spirit/home/support/assert_msg.hpp>
  16. #include <boost/spirit/home/support/argument.hpp>
  17. #include <boost/spirit/home/support/limits.hpp>
  18. #include <boost/fusion/include/at.hpp>
  19. #include <boost/fusion/include/size.hpp>
  20. #include <boost/fusion/include/as_list.hpp>
  21. #include <boost/fusion/include/transform.hpp>
  22. #include <boost/mpl/size.hpp>
  23. #include <boost/mpl/at.hpp>
  24. #include <boost/phoenix/core/actor.hpp>
  25. #include <boost/phoenix/core/terminal.hpp>
  26. #include <boost/phoenix/core/v2_eval.hpp>
  27. #include <boost/proto/proto_fwd.hpp> // for transform placeholders
  28. ///////////////////////////////////////////////////////////////////////////////
  29. #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  30. #define SPIRIT_DECLARE_ATTRIBUTE(z, n, data) \
  31. typedef phoenix::actor<attribute<n> > \
  32. BOOST_PP_CAT(BOOST_PP_CAT(_r, n), _type); \
  33. phoenix::actor<attribute<n> > const \
  34. BOOST_PP_CAT(_r, n) = BOOST_PP_CAT(BOOST_PP_CAT(_r, n), _type)();
  35. /***/
  36. #define SPIRIT_USING_ATTRIBUTE(z, n, data) \
  37. using spirit::BOOST_PP_CAT(BOOST_PP_CAT(_r, n), _type); \
  38. using spirit::BOOST_PP_CAT(_r, n); \
  39. /***/
  40. #else
  41. #define SPIRIT_DECLARE_ATTRIBUTE(z, n, data) \
  42. typedef phoenix::actor<attribute<n> > \
  43. BOOST_PP_CAT(BOOST_PP_CAT(_r, n), _type); \
  44. /***/
  45. #define SPIRIT_USING_ATTRIBUTE(z, n, data) \
  46. using spirit::BOOST_PP_CAT(BOOST_PP_CAT(_r, n), _type); \
  47. /***/
  48. #endif
  49. namespace boost { namespace spirit
  50. {
  51. template <int>
  52. struct attribute;
  53. template <int>
  54. struct local_variable;
  55. }}
  56. BOOST_PHOENIX_DEFINE_CUSTOM_TERMINAL(
  57. template <int N>
  58. , boost::spirit::attribute<N>
  59. , mpl::false_ // is not nullary
  60. , v2_eval(
  61. proto::make<
  62. boost::spirit::attribute<N>()
  63. >
  64. , proto::call<
  65. functional::env(proto::_state)
  66. >
  67. )
  68. )
  69. BOOST_PHOENIX_DEFINE_CUSTOM_TERMINAL(
  70. template <int N>
  71. , boost::spirit::local_variable<N>
  72. , mpl::false_ // is not nullary
  73. , v2_eval(
  74. proto::make<
  75. boost::spirit::local_variable<N>()
  76. >
  77. , proto::call<
  78. functional::env(proto::_state)
  79. >
  80. )
  81. )
  82. namespace boost { namespace spirit
  83. {
  84. template <typename Attributes, typename Locals>
  85. struct context
  86. {
  87. typedef Attributes attributes_type;
  88. typedef Locals locals_type;
  89. context(typename Attributes::car_type attribute)
  90. : attributes(attribute, fusion::nil_()), locals() {}
  91. template <typename Args, typename Context>
  92. context(
  93. typename Attributes::car_type attribute
  94. , Args const& args
  95. , Context& caller_context
  96. ) : attributes(
  97. attribute
  98. , fusion::as_list(
  99. fusion::transform(
  100. args
  101. , detail::expand_arg<Context>(caller_context)
  102. )
  103. )
  104. )
  105. , locals() {}
  106. context(Attributes const& attributes_)
  107. : attributes(attributes_), locals() {}
  108. Attributes attributes; // The attributes
  109. Locals locals; // Local variables
  110. };
  111. template <typename Context>
  112. struct attributes_of
  113. {
  114. typedef typename Context::attributes_type type;
  115. };
  116. template <typename Context>
  117. struct attributes_of<Context const>
  118. {
  119. typedef typename Context::attributes_type const type;
  120. };
  121. template <typename Context>
  122. struct attributes_of<Context &>
  123. : attributes_of<Context>
  124. {};
  125. template <typename Context>
  126. struct locals_of
  127. {
  128. typedef typename Context::locals_type type;
  129. };
  130. template <typename Context>
  131. struct locals_of<Context const>
  132. {
  133. typedef typename Context::locals_type const type;
  134. };
  135. template <typename Context>
  136. struct locals_of<Context &>
  137. {
  138. typedef typename Context::locals_type type;
  139. };
  140. template <int N>
  141. struct attribute
  142. {
  143. typedef mpl::true_ no_nullary;
  144. template <typename Env>
  145. struct result
  146. {
  147. typedef typename
  148. attributes_of<typename
  149. mpl::at_c<typename Env::args_type, 1>::type
  150. >::type
  151. attributes_type;
  152. typedef typename
  153. fusion::result_of::size<attributes_type>::type
  154. attributes_size;
  155. // report invalid argument not found (N is out of bounds)
  156. BOOST_SPIRIT_ASSERT_MSG(
  157. (N < attributes_size::value),
  158. index_is_out_of_bounds, ());
  159. typedef typename
  160. fusion::result_of::at_c<attributes_type, N>::type
  161. type;
  162. };
  163. template <typename Env>
  164. typename result<Env>::type
  165. eval(Env const& env) const
  166. {
  167. return fusion::at_c<N>((fusion::at_c<1>(env.args())).attributes);
  168. }
  169. };
  170. template <int N>
  171. struct local_variable
  172. {
  173. typedef mpl::true_ no_nullary;
  174. template <typename Env>
  175. struct result
  176. {
  177. typedef typename
  178. locals_of<typename
  179. mpl::at_c<typename Env::args_type, 1>::type
  180. >::type
  181. locals_type;
  182. typedef typename
  183. fusion::result_of::size<locals_type>::type
  184. locals_size;
  185. // report invalid argument not found (N is out of bounds)
  186. BOOST_SPIRIT_ASSERT_MSG(
  187. (N < locals_size::value),
  188. index_is_out_of_bounds, ());
  189. typedef typename
  190. fusion::result_of::at_c<locals_type, N>::type
  191. type;
  192. };
  193. template <typename Env>
  194. typename result<Env>::type
  195. eval(Env const& env) const
  196. {
  197. return get_arg<N>((fusion::at_c<1>(env.args())).locals);
  198. }
  199. };
  200. typedef phoenix::actor<attribute<0> > _val_type;
  201. typedef phoenix::actor<attribute<0> > _r0_type;
  202. typedef phoenix::actor<attribute<1> > _r1_type;
  203. typedef phoenix::actor<attribute<2> > _r2_type;
  204. #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  205. // _val refers to the 'return' value of a rule (same as _r0)
  206. // _r1, _r2, ... refer to the rule arguments
  207. _val_type const _val = _val_type();
  208. _r0_type const _r0 = _r0_type();
  209. _r1_type const _r1 = _r1_type();
  210. _r2_type const _r2 = _r2_type();
  211. #endif
  212. // Bring in the rest of the attributes (_r4 .. _rN+1), using PP
  213. BOOST_PP_REPEAT_FROM_TO(
  214. 3, SPIRIT_ATTRIBUTES_LIMIT, SPIRIT_DECLARE_ATTRIBUTE, _)
  215. typedef phoenix::actor<local_variable<0> > _a_type;
  216. typedef phoenix::actor<local_variable<1> > _b_type;
  217. typedef phoenix::actor<local_variable<2> > _c_type;
  218. typedef phoenix::actor<local_variable<3> > _d_type;
  219. typedef phoenix::actor<local_variable<4> > _e_type;
  220. typedef phoenix::actor<local_variable<5> > _f_type;
  221. typedef phoenix::actor<local_variable<6> > _g_type;
  222. typedef phoenix::actor<local_variable<7> > _h_type;
  223. typedef phoenix::actor<local_variable<8> > _i_type;
  224. typedef phoenix::actor<local_variable<9> > _j_type;
  225. #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  226. // _a, _b, ... refer to the local variables of a rule
  227. _a_type const _a = _a_type();
  228. _b_type const _b = _b_type();
  229. _c_type const _c = _c_type();
  230. _d_type const _d = _d_type();
  231. _e_type const _e = _e_type();
  232. _f_type const _f = _f_type();
  233. _g_type const _g = _g_type();
  234. _h_type const _h = _h_type();
  235. _i_type const _i = _i_type();
  236. _j_type const _j = _j_type();
  237. #endif
  238. // You can bring these in with the using directive
  239. // without worrying about bringing in too much.
  240. namespace labels
  241. {
  242. BOOST_PP_REPEAT(SPIRIT_ARGUMENTS_LIMIT, SPIRIT_USING_ARGUMENT, _)
  243. BOOST_PP_REPEAT(SPIRIT_ATTRIBUTES_LIMIT, SPIRIT_USING_ATTRIBUTE, _)
  244. #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
  245. using spirit::_val;
  246. using spirit::_a;
  247. using spirit::_b;
  248. using spirit::_c;
  249. using spirit::_d;
  250. using spirit::_e;
  251. using spirit::_f;
  252. using spirit::_g;
  253. using spirit::_h;
  254. using spirit::_i;
  255. using spirit::_j;
  256. #endif
  257. }
  258. }}
  259. #endif