Chris@16
|
1 /*=============================================================================
|
Chris@16
|
2 Copyright (c) 2001-2011 Joel de Guzman
|
Chris@16
|
3 Copyright (c) 2001-2011 Hartmut Kaiser
|
Chris@16
|
4 Copyright (c) 2011 Thomas Heller
|
Chris@16
|
5
|
Chris@16
|
6 Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
7 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
8 ==============================================================================*/
|
Chris@16
|
9 #if !defined(BOOST_SPIRIT_ARGUMENT_FEBRUARY_17_2007_0339PM)
|
Chris@16
|
10 #define BOOST_SPIRIT_ARGUMENT_FEBRUARY_17_2007_0339PM
|
Chris@16
|
11
|
Chris@16
|
12 #if defined(_MSC_VER)
|
Chris@16
|
13 #pragma once
|
Chris@16
|
14 #endif
|
Chris@16
|
15
|
Chris@16
|
16 #include <boost/preprocessor/repetition/repeat_from_to.hpp>
|
Chris@16
|
17 #include <boost/preprocessor/arithmetic/inc.hpp>
|
Chris@16
|
18 #include <boost/spirit/include/phoenix_core.hpp>
|
Chris@16
|
19 #include <boost/spirit/home/support/assert_msg.hpp>
|
Chris@16
|
20 #include <boost/spirit/home/support/limits.hpp>
|
Chris@16
|
21 #include <boost/fusion/include/at.hpp>
|
Chris@16
|
22 #include <boost/fusion/include/size.hpp>
|
Chris@16
|
23 #include <boost/mpl/size.hpp>
|
Chris@16
|
24 #include <boost/mpl/at.hpp>
|
Chris@16
|
25
|
Chris@16
|
26 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
|
Chris@16
|
27
|
Chris@16
|
28 #define SPIRIT_DECLARE_ARG(z, n, data) \
|
Chris@16
|
29 typedef phoenix::actor<argument<n> > \
|
Chris@16
|
30 BOOST_PP_CAT(BOOST_PP_CAT(_, BOOST_PP_INC(n)), _type); \
|
Chris@16
|
31 phoenix::actor<argument<n> > const \
|
Chris@16
|
32 BOOST_PP_CAT(_, BOOST_PP_INC(n)) = \
|
Chris@16
|
33 BOOST_PP_CAT(BOOST_PP_CAT(_, BOOST_PP_INC(n)), _type)(); \
|
Chris@16
|
34 /***/
|
Chris@16
|
35
|
Chris@16
|
36 #define SPIRIT_USING_ARGUMENT(z, n, data) \
|
Chris@16
|
37 using spirit::BOOST_PP_CAT(BOOST_PP_CAT(_, n), _type); \
|
Chris@16
|
38 using spirit::BOOST_PP_CAT(_, n); \
|
Chris@16
|
39 /***/
|
Chris@16
|
40
|
Chris@16
|
41 #else
|
Chris@16
|
42
|
Chris@16
|
43 #define SPIRIT_DECLARE_ARG(z, n, data) \
|
Chris@16
|
44 typedef phoenix::actor<argument<n> > \
|
Chris@16
|
45 BOOST_PP_CAT(BOOST_PP_CAT(_, BOOST_PP_INC(n)), _type); \
|
Chris@16
|
46 /***/
|
Chris@16
|
47
|
Chris@16
|
48 #define SPIRIT_USING_ARGUMENT(z, n, data) \
|
Chris@16
|
49 using spirit::BOOST_PP_CAT(BOOST_PP_CAT(_, n), _type); \
|
Chris@16
|
50 /***/
|
Chris@16
|
51
|
Chris@16
|
52 #endif
|
Chris@16
|
53
|
Chris@16
|
54 namespace boost { namespace spirit
|
Chris@16
|
55 {
|
Chris@16
|
56 template <int N>
|
Chris@16
|
57 struct argument;
|
Chris@16
|
58
|
Chris@16
|
59 template <typename Dummy>
|
Chris@16
|
60 struct attribute_context;
|
Chris@16
|
61 }}
|
Chris@16
|
62
|
Chris@16
|
63 BOOST_PHOENIX_DEFINE_CUSTOM_TERMINAL(
|
Chris@16
|
64 template <int N>
|
Chris@16
|
65 , boost::spirit::argument<N>
|
Chris@16
|
66 , mpl::false_ // is not nullary
|
Chris@16
|
67 , v2_eval(
|
Chris@16
|
68 proto::make<
|
Chris@16
|
69 boost::spirit::argument<N>()
|
Chris@16
|
70 >
|
Chris@16
|
71 , proto::call<
|
Chris@16
|
72 functional::env(proto::_state)
|
Chris@16
|
73 >
|
Chris@16
|
74 )
|
Chris@16
|
75 )
|
Chris@16
|
76
|
Chris@16
|
77 BOOST_PHOENIX_DEFINE_CUSTOM_TERMINAL(
|
Chris@16
|
78 template <typename Dummy>
|
Chris@16
|
79 , boost::spirit::attribute_context<Dummy>
|
Chris@16
|
80 , mpl::false_ // is not nullary
|
Chris@16
|
81 , v2_eval(
|
Chris@16
|
82 proto::make<
|
Chris@16
|
83 boost::spirit::attribute_context<Dummy>()
|
Chris@16
|
84 >
|
Chris@16
|
85 , proto::call<
|
Chris@16
|
86 functional::env(proto::_state)
|
Chris@16
|
87 >
|
Chris@16
|
88 )
|
Chris@16
|
89 )
|
Chris@16
|
90
|
Chris@16
|
91 namespace boost { namespace spirit
|
Chris@16
|
92 {
|
Chris@16
|
93 namespace result_of
|
Chris@16
|
94 {
|
Chris@16
|
95 template <typename Sequence, int N>
|
Chris@16
|
96 struct get_arg
|
Chris@16
|
97 {
|
Chris@16
|
98 typedef typename
|
Chris@16
|
99 fusion::result_of::size<Sequence>::type
|
Chris@16
|
100 sequence_size;
|
Chris@16
|
101
|
Chris@16
|
102 // report invalid argument not found (N is out of bounds)
|
Chris@16
|
103 BOOST_SPIRIT_ASSERT_MSG(
|
Chris@16
|
104 (N < sequence_size::value),
|
Chris@16
|
105 index_is_out_of_bounds, ());
|
Chris@16
|
106
|
Chris@16
|
107 typedef typename
|
Chris@16
|
108 fusion::result_of::at_c<Sequence, N>::type
|
Chris@16
|
109 type;
|
Chris@16
|
110
|
Chris@16
|
111 static type call(Sequence& seq)
|
Chris@16
|
112 {
|
Chris@16
|
113 return fusion::at_c<N>(seq);
|
Chris@16
|
114 }
|
Chris@16
|
115 };
|
Chris@16
|
116
|
Chris@16
|
117 template <typename Sequence, int N>
|
Chris@16
|
118 struct get_arg<Sequence&, N> : get_arg<Sequence, N>
|
Chris@16
|
119 {
|
Chris@16
|
120 };
|
Chris@16
|
121 }
|
Chris@16
|
122
|
Chris@16
|
123 template <int N, typename T>
|
Chris@16
|
124 typename result_of::get_arg<T, N>::type
|
Chris@16
|
125 get_arg(T& val)
|
Chris@16
|
126 {
|
Chris@16
|
127 return result_of::get_arg<T, N>::call(val);
|
Chris@16
|
128 }
|
Chris@16
|
129
|
Chris@16
|
130 template <typename>
|
Chris@16
|
131 struct attribute_context
|
Chris@16
|
132 {
|
Chris@16
|
133 typedef mpl::true_ no_nullary;
|
Chris@16
|
134
|
Chris@16
|
135 template <typename Env>
|
Chris@16
|
136 struct result
|
Chris@16
|
137 {
|
Chris@16
|
138 // FIXME: is this remove_const really necessary?
|
Chris@16
|
139 typedef typename
|
Chris@16
|
140 remove_const<
|
Chris@16
|
141 typename mpl::at_c<typename Env::args_type, 0>::type
|
Chris@16
|
142 >::type
|
Chris@16
|
143 type;
|
Chris@16
|
144 };
|
Chris@16
|
145
|
Chris@16
|
146 template <typename Env>
|
Chris@16
|
147 typename result<Env>::type
|
Chris@16
|
148 eval(Env const& env) const
|
Chris@16
|
149 {
|
Chris@16
|
150 return fusion::at_c<0>(env.args());
|
Chris@16
|
151 }
|
Chris@16
|
152 };
|
Chris@16
|
153
|
Chris@16
|
154 template <int N>
|
Chris@16
|
155 struct argument
|
Chris@16
|
156 {
|
Chris@16
|
157 typedef mpl::true_ no_nullary;
|
Chris@16
|
158
|
Chris@16
|
159 template <typename Env>
|
Chris@16
|
160 struct result
|
Chris@16
|
161 {
|
Chris@16
|
162 typedef typename
|
Chris@16
|
163 mpl::at_c<typename Env::args_type, 0>::type
|
Chris@16
|
164 arg_type;
|
Chris@16
|
165
|
Chris@16
|
166 typedef typename result_of::get_arg<arg_type, N>::type type;
|
Chris@16
|
167 };
|
Chris@16
|
168
|
Chris@16
|
169 template <typename Env>
|
Chris@16
|
170 typename result<Env>::type
|
Chris@16
|
171 eval(Env const& env) const
|
Chris@16
|
172 {
|
Chris@16
|
173 return get_arg<N>(fusion::at_c<0>(env.args()));
|
Chris@16
|
174 }
|
Chris@16
|
175 };
|
Chris@16
|
176
|
Chris@16
|
177 // _0 refers to the whole attribute as generated by the lhs parser
|
Chris@16
|
178 typedef phoenix::actor<attribute_context<void> > _0_type;
|
Chris@16
|
179 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
|
Chris@16
|
180 _0_type const _0 = _0_type();
|
Chris@16
|
181 #endif
|
Chris@16
|
182
|
Chris@16
|
183 // _1, _2, ... refer to the attributes of the single components the lhs
|
Chris@16
|
184 // parser is composed of
|
Chris@16
|
185 typedef phoenix::actor<argument<0> > _1_type;
|
Chris@16
|
186 typedef phoenix::actor<argument<1> > _2_type;
|
Chris@16
|
187 typedef phoenix::actor<argument<2> > _3_type;
|
Chris@16
|
188
|
Chris@16
|
189 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
|
Chris@16
|
190 _1_type const _1 = _1_type();
|
Chris@16
|
191 _2_type const _2 = _2_type();
|
Chris@16
|
192 _3_type const _3 = _3_type();
|
Chris@16
|
193 #endif
|
Chris@16
|
194
|
Chris@16
|
195 // '_pass' may be used to make a match fail in retrospective
|
Chris@16
|
196 typedef phoenix::arg_names::_3_type _pass_type;
|
Chris@16
|
197 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
|
Chris@16
|
198 _pass_type const _pass = _pass_type();
|
Chris@16
|
199 #endif
|
Chris@16
|
200
|
Chris@16
|
201 // Bring in the rest of the arguments and attributes (_4 .. _N+1), using PP
|
Chris@16
|
202 BOOST_PP_REPEAT_FROM_TO(
|
Chris@16
|
203 3, SPIRIT_ARGUMENTS_LIMIT, SPIRIT_DECLARE_ARG, _)
|
Chris@16
|
204
|
Chris@16
|
205 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
|
Chris@16
|
206 // You can bring these in with the using directive
|
Chris@16
|
207 // without worrying about bringing in too much.
|
Chris@16
|
208 namespace labels
|
Chris@16
|
209 {
|
Chris@16
|
210 BOOST_PP_REPEAT(SPIRIT_ARGUMENTS_LIMIT, SPIRIT_USING_ARGUMENT, _)
|
Chris@16
|
211 }
|
Chris@16
|
212 #endif
|
Chris@16
|
213
|
Chris@16
|
214 }}
|
Chris@16
|
215
|
Chris@16
|
216 #undef SPIRIT_DECLARE_ARG
|
Chris@16
|
217 #endif
|