]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Copyright (c) 2001-2011 Hartmut Kaiser |
2 | // Copyright (c) 2001-2011 Joel de Guzman | |
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 | ||
7 | #if !defined(BOOST_SPIRIT_KARMA_LAZY_MARCH_27_2007_1231PM) | |
8 | #define BOOST_SPIRIT_KARMA_LAZY_MARCH_27_2007_1231PM | |
9 | ||
10 | #if defined(_MSC_VER) | |
11 | #pragma once | |
12 | #endif | |
13 | ||
14 | #include <boost/spirit/home/karma/domain.hpp> | |
15 | #include <boost/spirit/home/karma/delimit_out.hpp> | |
16 | #include <boost/spirit/home/karma/meta_compiler.hpp> | |
17 | #include <boost/spirit/home/karma/detail/attributes.hpp> | |
18 | #include <boost/spirit/home/support/unused.hpp> | |
19 | #include <boost/spirit/home/support/info.hpp> | |
20 | #include <boost/spirit/home/support/lazy.hpp> | |
21 | #include <boost/spirit/include/phoenix_core.hpp> | |
22 | #include <boost/fusion/include/at.hpp> | |
23 | #include <boost/utility/result_of.hpp> | |
24 | #include <boost/type_traits/remove_reference.hpp> | |
25 | #include <boost/mpl/not.hpp> | |
26 | ||
27 | namespace boost { namespace spirit | |
28 | { | |
29 | /////////////////////////////////////////////////////////////////////////// | |
30 | // Enablers | |
31 | /////////////////////////////////////////////////////////////////////////// | |
32 | template <typename Eval> | |
33 | struct use_terminal<karma::domain, phoenix::actor<Eval> > // enables phoenix actors | |
34 | : mpl::true_ {}; | |
35 | ||
36 | // forward declaration | |
37 | template <typename Terminal, typename Actor, int Arity> | |
38 | struct lazy_terminal; | |
39 | ||
40 | }} | |
41 | ||
42 | namespace boost { namespace spirit { namespace karma | |
43 | { | |
44 | using spirit::lazy; | |
45 | typedef modify<karma::domain> karma_modify; | |
46 | ||
47 | namespace detail | |
48 | { | |
49 | template <typename Generator, typename OutputIterator, typename Context | |
50 | , typename Delimiter, typename Attribute> | |
51 | bool lazy_generate_impl(Generator const& g, OutputIterator& sink | |
52 | , Context& context, Delimiter const& delim | |
53 | , Attribute const& attr, mpl::false_) | |
54 | { | |
55 | return g.generate(sink, context, delim, attr); | |
56 | } | |
57 | ||
58 | template <typename Generator, typename OutputIterator, typename Context | |
59 | , typename Delimiter, typename Attribute> | |
60 | bool lazy_generate_impl(Generator const& g, OutputIterator& sink | |
61 | , Context& context, Delimiter const& delim | |
62 | , Attribute const& attr, mpl::true_) | |
63 | { | |
64 | // If DeducedAuto is false (semantic actions is present), the | |
65 | // component's attribute is unused. | |
66 | return g.generate(sink, context, delim, unused); | |
67 | } | |
68 | ||
69 | template <typename Generator, typename OutputIterator, typename Context | |
70 | , typename Delimiter, typename Attribute> | |
71 | bool lazy_generate_impl_main(Generator const& g, OutputIterator& sink | |
72 | , Context& context, Delimiter const& delim, Attribute const& attr) | |
73 | { | |
74 | // If DeducedAuto is true (no semantic action), we pass the parser's | |
75 | // attribute on to the component. | |
76 | typedef typename traits::has_semantic_action<Generator>::type auto_rule; | |
77 | return lazy_generate_impl(g, sink, context, delim, attr, auto_rule()); | |
78 | } | |
79 | } | |
80 | ||
81 | template <typename Function, typename Modifiers> | |
82 | struct lazy_generator : generator<lazy_generator<Function, Modifiers> > | |
83 | { | |
84 | typedef mpl::int_<generator_properties::all_properties> properties; | |
85 | ||
86 | template <typename Context, typename Iterator> | |
87 | struct attribute | |
88 | { | |
89 | typedef typename | |
90 | boost::result_of<karma_modify(tag::lazy_eval, Modifiers)>::type | |
91 | modifier; | |
92 | ||
93 | typedef typename | |
94 | remove_reference< | |
95 | typename boost::result_of<Function(unused_type, Context)>::type | |
96 | >::type | |
97 | expr_type; | |
98 | ||
99 | // If you got an error_invalid_expression error message here, | |
100 | // then the expression (expr_type) is not a valid spirit karma | |
101 | // expression. | |
102 | BOOST_SPIRIT_ASSERT_MATCH(karma::domain, expr_type); | |
103 | ||
104 | typedef typename | |
105 | result_of::compile<karma::domain, expr_type, modifier>::type | |
106 | generator_type; | |
107 | ||
108 | typedef typename | |
109 | traits::attribute_of<generator_type, Context, Iterator>::type | |
110 | type; | |
111 | }; | |
112 | ||
113 | lazy_generator(Function const& func, Modifiers const& modifiers) | |
114 | : func(func), modifiers(modifiers) {} | |
115 | ||
116 | template < | |
117 | typename OutputIterator, typename Context, | |
118 | typename Delimiter, typename Attribute | |
119 | > | |
120 | bool generate(OutputIterator& sink, Context& context, | |
121 | Delimiter const& d, Attribute const& attr) const | |
122 | { | |
123 | return detail::lazy_generate_impl_main( | |
124 | compile<karma::domain>(func(unused, context) | |
125 | , karma_modify()(tag::lazy_eval(), modifiers)) | |
126 | , sink, context, d, attr); | |
127 | } | |
128 | ||
129 | template <typename Context> | |
130 | info what(Context& context) const | |
131 | { | |
132 | return info("lazy" | |
133 | , compile<karma::domain>(func(unused, context) | |
134 | , karma_modify()(tag::lazy_eval(), modifiers)) | |
135 | .what(context) | |
136 | ); | |
137 | } | |
138 | ||
139 | Function func; | |
140 | Modifiers modifiers; | |
141 | ||
142 | private: | |
143 | // silence MSVC warning C4512: assignment operator could not be generated | |
144 | lazy_generator& operator= (lazy_generator const&); | |
145 | }; | |
146 | ||
147 | /////////////////////////////////////////////////////////////////////////// | |
148 | template <typename Function, typename Subject, typename Modifiers> | |
149 | struct lazy_directive | |
150 | : unary_generator<lazy_directive<Function, Subject, Modifiers> > | |
151 | { | |
152 | typedef mpl::int_<generator_properties::all_properties> properties; | |
153 | ||
154 | typedef Subject subject_type; | |
155 | ||
156 | template <typename Context, typename Iterator> | |
157 | struct attribute | |
158 | { | |
159 | typedef typename | |
160 | boost::result_of<karma_modify(tag::lazy_eval, Modifiers)>::type | |
161 | modifier; | |
162 | ||
163 | typedef typename | |
164 | remove_reference< | |
165 | typename boost::result_of<Function(unused_type, Context)>::type | |
166 | >::type | |
167 | directive_expr_type; | |
168 | ||
169 | typedef typename | |
170 | proto::result_of::make_expr< | |
171 | proto::tag::subscript | |
172 | , directive_expr_type | |
173 | , Subject | |
174 | >::type | |
175 | expr_type; | |
176 | ||
177 | // If you got an error_invalid_expression error message here, | |
178 | // then the expression (expr_type) is not a valid spirit karma | |
179 | // expression. | |
180 | BOOST_SPIRIT_ASSERT_MATCH(karma::domain, expr_type); | |
181 | ||
182 | typedef typename | |
183 | result_of::compile<karma::domain, expr_type, modifier>::type | |
184 | generator_type; | |
185 | ||
186 | typedef typename | |
187 | traits::attribute_of<generator_type, Context, Iterator>::type | |
188 | type; | |
189 | }; | |
190 | ||
191 | lazy_directive(Function const& function, Subject const& subject | |
192 | , Modifiers const& modifiers) | |
193 | : function(function), subject(subject), modifiers(modifiers) {} | |
194 | ||
195 | template <typename OutputIterator, typename Context, typename Delimiter | |
196 | , typename Attribute> | |
197 | bool generate(OutputIterator& sink, Context& ctx, Delimiter const& d | |
198 | , Attribute const& attr) const | |
199 | { | |
200 | return detail::lazy_generate_impl_main(compile<karma::domain>( | |
201 | proto::make_expr<proto::tag::subscript>( | |
202 | function(unused, ctx), subject) | |
203 | , karma_modify()(tag::lazy_eval(), modifiers)) | |
204 | , sink, ctx, d, attr); | |
205 | } | |
206 | ||
207 | template <typename Context> | |
208 | info what(Context& ctx) const | |
209 | { | |
210 | return info("lazy-directive" | |
211 | , compile<karma::domain>( | |
212 | proto::make_expr<proto::tag::subscript>( | |
213 | function(unused, ctx), subject) | |
214 | , karma_modify()(tag::lazy_eval(), modifiers)) | |
215 | .what(ctx) | |
216 | ); | |
217 | } | |
218 | ||
219 | Function function; | |
220 | Subject subject; | |
221 | Modifiers modifiers; | |
222 | }; | |
223 | ||
224 | /////////////////////////////////////////////////////////////////////////// | |
225 | // Generator generators: make_xxx function (objects) | |
226 | /////////////////////////////////////////////////////////////////////////// | |
227 | template <typename Eval, typename Modifiers> | |
228 | struct make_primitive<phoenix::actor<Eval>, Modifiers> | |
229 | { | |
230 | typedef lazy_generator<phoenix::actor<Eval>, Modifiers> result_type; | |
231 | result_type operator()(phoenix::actor<Eval> const& f | |
232 | , Modifiers const& modifiers) const | |
233 | { | |
234 | return result_type(f, modifiers); | |
235 | } | |
236 | }; | |
237 | ||
238 | template <typename Terminal, typename Actor, int Arity, typename Modifiers> | |
239 | struct make_primitive<lazy_terminal<Terminal, Actor, Arity>, Modifiers> | |
240 | { | |
241 | typedef lazy_generator<Actor, Modifiers> result_type; | |
242 | result_type operator()( | |
243 | lazy_terminal<Terminal, Actor, Arity> const& lt | |
244 | , Modifiers const& modifiers) const | |
245 | { | |
246 | return result_type(lt.actor, modifiers); | |
247 | } | |
248 | }; | |
249 | ||
250 | template < | |
251 | typename Terminal, typename Actor, int Arity, typename Subject | |
252 | , typename Modifiers> | |
253 | struct make_directive<lazy_terminal<Terminal, Actor, Arity> | |
254 | , Subject, Modifiers> | |
255 | { | |
256 | typedef lazy_directive<Actor, Subject, Modifiers> result_type; | |
257 | result_type operator()( | |
258 | lazy_terminal<Terminal, Actor, Arity> const& lt | |
259 | , Subject const& subject, Modifiers const& modifiers) const | |
260 | { | |
261 | return result_type(lt.actor, subject, modifiers); | |
262 | } | |
263 | }; | |
264 | ||
265 | }}} | |
266 | ||
267 | #endif |