1 /*=============================================================================
2 Copyright (c) 2001-2011 Joel de Guzman
3 Copyright (c) 2001-2011 Hartmut Kaiser
4 Copyright (c) 2011 Thomas Heller
6 Distributed under the Boost Software License, Version 1.0. (See accompanying
7 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 ==============================================================================*/
9 #if !defined(BOOST_SPIRIT_CONTEXT_OCTOBER_31_2008_0654PM)
10 #define BOOST_SPIRIT_CONTEXT_OCTOBER_31_2008_0654PM
16 #include <boost/preprocessor/repetition/repeat_from_to.hpp>
17 #include <boost/spirit/include/phoenix_core.hpp>
18 #include <boost/spirit/home/support/nonterminal/expand_arg.hpp>
19 #include <boost/spirit/home/support/assert_msg.hpp>
20 #include <boost/spirit/home/support/argument.hpp>
21 #include <boost/spirit/home/support/limits.hpp>
22 #include <boost/fusion/include/at.hpp>
23 #include <boost/fusion/include/size.hpp>
24 #include <boost/fusion/include/as_list.hpp>
25 #include <boost/fusion/include/transform.hpp>
26 #include <boost/mpl/size.hpp>
27 #include <boost/mpl/at.hpp>
29 ///////////////////////////////////////////////////////////////////////////////
30 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
32 #define SPIRIT_DECLARE_ATTRIBUTE(z, n, data) \
33 typedef phoenix::actor<attribute<n> > \
34 BOOST_PP_CAT(BOOST_PP_CAT(_r, n), _type); \
35 phoenix::actor<attribute<n> > const \
36 BOOST_PP_CAT(_r, n) = BOOST_PP_CAT(BOOST_PP_CAT(_r, n), _type)();
38 #define SPIRIT_USING_ATTRIBUTE(z, n, data) \
39 using spirit::BOOST_PP_CAT(BOOST_PP_CAT(_r, n), _type); \
40 using spirit::BOOST_PP_CAT(_r, n); \
45 #define SPIRIT_DECLARE_ATTRIBUTE(z, n, data) \
46 typedef phoenix::actor<attribute<n> > \
47 BOOST_PP_CAT(BOOST_PP_CAT(_r, n), _type); \
49 #define SPIRIT_USING_ATTRIBUTE(z, n, data) \
50 using spirit::BOOST_PP_CAT(BOOST_PP_CAT(_r, n), _type); \
55 namespace boost { namespace spirit
61 struct local_variable;
64 BOOST_PHOENIX_DEFINE_CUSTOM_TERMINAL(
66 , boost::spirit::attribute<N>
67 , mpl::false_ // is not nullary
70 boost::spirit::attribute<N>()
73 functional::env(proto::_state)
78 BOOST_PHOENIX_DEFINE_CUSTOM_TERMINAL(
80 , boost::spirit::local_variable<N>
81 , mpl::false_ // is not nullary
84 boost::spirit::local_variable<N>()
87 functional::env(proto::_state)
92 namespace boost { namespace spirit
94 template <typename Attributes, typename Locals>
97 typedef Attributes attributes_type;
98 typedef Locals locals_type;
100 context(typename Attributes::car_type attribute)
101 : attributes(attribute, fusion::nil_()), locals() {}
103 template <typename Args, typename Context>
105 typename Attributes::car_type attribute
107 , Context& caller_context
113 , detail::expand_arg<Context>(caller_context)
119 context(Attributes const& attributes_)
120 : attributes(attributes_), locals() {}
122 Attributes attributes; // The attributes
123 Locals locals; // Local variables
126 template <typename Context>
129 typedef typename Context::attributes_type type;
132 template <typename Context>
133 struct attributes_of<Context const>
135 typedef typename Context::attributes_type const type;
138 template <typename Context>
139 struct attributes_of<Context &>
140 : attributes_of<Context>
143 template <typename Context>
146 typedef typename Context::locals_type type;
149 template <typename Context>
150 struct locals_of<Context const>
152 typedef typename Context::locals_type const type;
155 template <typename Context>
156 struct locals_of<Context &>
158 typedef typename Context::locals_type type;
164 typedef mpl::true_ no_nullary;
166 template <typename Env>
170 attributes_of<typename
171 mpl::at_c<typename Env::args_type, 1>::type
176 fusion::result_of::size<attributes_type>::type
179 // report invalid argument not found (N is out of bounds)
180 BOOST_SPIRIT_ASSERT_MSG(
181 (N < attributes_size::value),
182 index_is_out_of_bounds, ());
185 fusion::result_of::at_c<attributes_type, N>::type
189 template <typename Env>
190 typename result<Env>::type
191 eval(Env const& env) const
193 return fusion::at_c<N>((fusion::at_c<1>(env.args())).attributes);
198 struct local_variable
200 typedef mpl::true_ no_nullary;
202 template <typename Env>
207 mpl::at_c<typename Env::args_type, 1>::type
212 fusion::result_of::size<locals_type>::type
215 // report invalid argument not found (N is out of bounds)
216 BOOST_SPIRIT_ASSERT_MSG(
217 (N < locals_size::value),
218 index_is_out_of_bounds, ());
221 fusion::result_of::at_c<locals_type, N>::type
225 template <typename Env>
226 typename result<Env>::type
227 eval(Env const& env) const
229 return get_arg<N>((fusion::at_c<1>(env.args())).locals);
233 typedef phoenix::actor<attribute<0> > _val_type;
234 typedef phoenix::actor<attribute<0> > _r0_type;
235 typedef phoenix::actor<attribute<1> > _r1_type;
236 typedef phoenix::actor<attribute<2> > _r2_type;
238 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
239 // _val refers to the 'return' value of a rule (same as _r0)
240 // _r1, _r2, ... refer to the rule arguments
241 _val_type const _val = _val_type();
242 _r0_type const _r0 = _r0_type();
243 _r1_type const _r1 = _r1_type();
244 _r2_type const _r2 = _r2_type();
247 // Bring in the rest of the attributes (_r4 .. _rN+1), using PP
248 BOOST_PP_REPEAT_FROM_TO(
249 3, SPIRIT_ATTRIBUTES_LIMIT, SPIRIT_DECLARE_ATTRIBUTE, _)
251 typedef phoenix::actor<local_variable<0> > _a_type;
252 typedef phoenix::actor<local_variable<1> > _b_type;
253 typedef phoenix::actor<local_variable<2> > _c_type;
254 typedef phoenix::actor<local_variable<3> > _d_type;
255 typedef phoenix::actor<local_variable<4> > _e_type;
256 typedef phoenix::actor<local_variable<5> > _f_type;
257 typedef phoenix::actor<local_variable<6> > _g_type;
258 typedef phoenix::actor<local_variable<7> > _h_type;
259 typedef phoenix::actor<local_variable<8> > _i_type;
260 typedef phoenix::actor<local_variable<9> > _j_type;
262 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
263 // _a, _b, ... refer to the local variables of a rule
264 _a_type const _a = _a_type();
265 _b_type const _b = _b_type();
266 _c_type const _c = _c_type();
267 _d_type const _d = _d_type();
268 _e_type const _e = _e_type();
269 _f_type const _f = _f_type();
270 _g_type const _g = _g_type();
271 _h_type const _h = _h_type();
272 _i_type const _i = _i_type();
273 _j_type const _j = _j_type();
276 // You can bring these in with the using directive
277 // without worrying about bringing in too much.
280 BOOST_PP_REPEAT(SPIRIT_ARGUMENTS_LIMIT, SPIRIT_USING_ARGUMENT, _)
281 BOOST_PP_REPEAT(SPIRIT_ATTRIBUTES_LIMIT, SPIRIT_USING_ATTRIBUTE, _)
283 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS