1 // Copyright (c) 2009 Francois Barel
2 // Copyright (c) 2001-2011 Joel de Guzman
3 // Copyright (c) 2001-2012 Hartmut Kaiser
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)
8 #if !defined(BOOST_SPIRIT_REPOSITORY_KARMA_SUBRULE_AUGUST_12_2009_0813PM)
9 #define BOOST_SPIRIT_REPOSITORY_KARMA_SUBRULE_AUGUST_12_2009_0813PM
15 #include <boost/spirit/home/karma/domain.hpp>
16 #include <boost/spirit/home/karma/meta_compiler.hpp>
17 #include <boost/spirit/home/karma/generator.hpp>
18 #include <boost/spirit/home/karma/reference.hpp>
19 #include <boost/spirit/home/karma/nonterminal/detail/generator_binder.hpp>
20 #include <boost/spirit/home/karma/nonterminal/detail/parameterized.hpp>
21 #include <boost/spirit/home/support/argument.hpp>
22 #include <boost/spirit/home/support/assert_msg.hpp>
23 #include <boost/spirit/home/karma/detail/attributes.hpp>
24 #include <boost/spirit/home/support/info.hpp>
25 #include <boost/spirit/home/support/unused.hpp>
26 #include <boost/spirit/home/support/nonterminal/extract_param.hpp>
27 #include <boost/spirit/home/support/nonterminal/locals.hpp>
28 #include <boost/spirit/repository/home/support/subrule_context.hpp>
30 #include <boost/fusion/include/as_map.hpp>
31 #include <boost/fusion/include/at_key.hpp>
32 #include <boost/fusion/include/cons.hpp>
33 #include <boost/fusion/include/front.hpp>
34 #include <boost/fusion/include/has_key.hpp>
35 #include <boost/fusion/include/join.hpp>
36 #include <boost/fusion/include/make_map.hpp>
37 #include <boost/fusion/include/make_vector.hpp>
38 #include <boost/fusion/include/size.hpp>
39 #include <boost/fusion/include/vector.hpp>
40 #include <boost/mpl/bool.hpp>
41 #include <boost/mpl/identity.hpp>
42 #include <boost/mpl/int.hpp>
43 #include <boost/mpl/vector.hpp>
44 #include <boost/type_traits/add_reference.hpp>
45 #include <boost/type_traits/is_same.hpp>
46 #include <boost/type_traits/remove_reference.hpp>
48 #if defined(BOOST_MSVC)
49 # pragma warning(push)
50 # pragma warning(disable: 4355) // 'this' : used in base member initializer list warning
53 ///////////////////////////////////////////////////////////////////////////////
54 namespace boost { namespace spirit { namespace repository { namespace karma
56 ///////////////////////////////////////////////////////////////////////////
58 // - generator representing a group of subrule definitions (one or more),
59 // invokes first subrule on entry,
60 // - also a Proto terminal, so that a group behaves like any Spirit
62 ///////////////////////////////////////////////////////////////////////////
63 template <typename Defs>
66 typename proto::terminal<
67 spirit::karma::reference<subrule_group<Defs> const>
71 , spirit::karma::generator<subrule_group<Defs> >
74 // Forward to first subrule.
76 typename fusion::result_of::front<Defs>::type
77 >::type::second_type::subject_type::properties {};
79 // Fusion associative sequence, associating each subrule ID in this
80 // group (as an MPL integral constant) with its definition
81 typedef Defs defs_type;
83 typedef subrule_group<Defs> this_type;
84 typedef spirit::karma::reference<this_type const> reference_;
85 typedef typename proto::terminal<reference_>::type terminal;
86 typedef proto::extends<terminal, this_type> base_type;
88 static size_t const params_size =
89 // Forward to first subrule.
91 typename fusion::result_of::front<Defs>::type
92 >::type::second_type::params_size;
94 subrule_group(subrule_group const& rhs)
95 : base_type(terminal::make(reference_(*this)))
100 explicit subrule_group(Defs const& defs)
101 : base_type(terminal::make(reference_(*this)))
106 // from a subrule ID, get the type of a reference to its definition
110 typedef mpl::int_<ID> id_type;
112 // If you are seeing a compilation error here, you are trying
113 // to use a subrule which was not defined in this group.
114 BOOST_SPIRIT_ASSERT_MSG(
115 (fusion::result_of::has_key<
116 defs_type const, id_type>::type::value)
117 , subrule_used_without_being_defined, (mpl::int_<ID>));
120 fusion::result_of::at_key<defs_type const, id_type>::type
124 // from a subrule ID, get a reference to its definition
126 typename def_type<ID>::type def() const
128 return fusion::at_key<mpl::int_<ID> >(defs);
131 template <typename Context, typename Iterator>
133 // Forward to first subrule.
135 typename remove_reference<
136 typename fusion::result_of::front<Defs>::type
137 >::type::second_type::attr_type> {};
139 template <typename OutputIterator, typename Context
140 , typename Delimiter, typename Attribute>
141 bool generate(OutputIterator& sink, Context& context
142 , Delimiter const& delimiter, Attribute const& attr) const
144 // Forward to first subrule.
145 return generate_subrule(fusion::front(defs).second
146 , sink, context, delimiter, attr);
149 template <typename OutputIterator, typename Context
150 , typename Delimiter, typename Attribute
152 bool generate(OutputIterator& sink, Context& context
153 , Delimiter const& delimiter, Attribute const& attr
154 , Params const& params) const
156 // Forward to first subrule.
157 return generate_subrule(fusion::front(defs).second
158 , sink, context, delimiter, attr, params);
161 template <int ID, typename OutputIterator, typename Context
162 , typename Delimiter, typename Attribute>
163 bool generate_subrule_id(OutputIterator& sink
164 , Context& context, Delimiter const& delimiter
165 , Attribute const& attr) const
167 return generate_subrule(def<ID>()
168 , sink, context, delimiter, attr);
171 template <int ID, typename OutputIterator, typename Context
172 , typename Delimiter, typename Attribute, typename Params>
173 bool generate_subrule_id(OutputIterator& sink
174 , Context& context, Delimiter const& delimiter
175 , Attribute const& attr, Params const& params) const
177 return generate_subrule(def<ID>()
178 , sink, context, delimiter, attr, params);
181 template <typename Def, typename OutputIterator, typename Context
182 , typename Delimiter, typename Attribute>
183 bool generate_subrule(Def const& def, OutputIterator& sink
184 , Context& /*caller_context*/, Delimiter const& delimiter
185 , Attribute const& attr) const
187 // compute context type for this subrule
188 typedef typename Def::locals_type subrule_locals_type;
189 typedef typename Def::attr_type subrule_attr_type;
190 typedef typename Def::attr_reference_type subrule_attr_reference_type;
191 typedef typename Def::parameter_types subrule_parameter_types;
197 subrule_attr_reference_type, subrule_parameter_types>
198 , subrule_locals_type
202 // Create an attribute if none is supplied.
203 typedef traits::make_attribute<subrule_attr_type, Attribute>
206 // If you are seeing a compilation error here, you are probably
207 // trying to use a subrule which has inherited attributes,
208 // without passing values for them.
209 context_type context(*this
210 , traits::pre_transform<spirit::karma::domain, subrule_attr_type>(
211 make_attribute::call(attr)));
213 return def.binder(sink, context, delimiter);
216 template <typename Def, typename OutputIterator, typename Context
217 , typename Delimiter, typename Attribute, typename Params>
218 bool generate_subrule(Def const& def, OutputIterator& sink
219 , Context& caller_context, Delimiter const& delimiter
220 , Attribute const& attr, Params const& params) const
222 // compute context type for this subrule
223 typedef typename Def::locals_type subrule_locals_type;
224 typedef typename Def::attr_type subrule_attr_type;
225 typedef typename Def::attr_reference_type subrule_attr_reference_type;
226 typedef typename Def::parameter_types subrule_parameter_types;
232 subrule_attr_reference_type, subrule_parameter_types>
233 , subrule_locals_type
237 // Create an attribute if none is supplied.
238 typedef traits::make_attribute<subrule_attr_type, Attribute>
241 // If you are seeing a compilation error here, you are probably
242 // trying to use a subrule which has inherited attributes,
243 // passing values of incompatible types for them.
244 context_type context(*this
245 , traits::pre_transform<spirit::karma::domain, subrule_attr_type>(
246 make_attribute::call(attr)), params, caller_context);
248 return def.binder(sink, context, delimiter);
251 template <typename Context>
252 info what(Context& context) const
254 // Forward to first subrule.
255 return fusion::front(defs).second.binder.g.what(context);
258 template <typename Defs2>
260 typename fusion::result_of::as_map<
261 typename fusion::result_of::join<
262 Defs const, Defs2 const>::type>::type>
263 operator,(subrule_group<Defs2> const& other) const
265 typedef subrule_group<
266 typename fusion::result_of::as_map<
267 typename fusion::result_of::join<
268 Defs const, Defs2 const>::type>::type> result_type;
269 return result_type(fusion::as_map(fusion::join(defs, other.defs)));
272 // bring in the operator() overloads
273 this_type const& get_parameterized_subject() const { return *this; }
274 typedef this_type parameterized_subject_type;
275 #include <boost/spirit/home/karma/nonterminal/detail/fcall.hpp>
280 ///////////////////////////////////////////////////////////////////////////
281 // subrule_definition: holds one particular definition of a subrule
282 ///////////////////////////////////////////////////////////////////////////
288 , typename Parameters
293 struct subrule_definition
295 typedef mpl::int_<ID_> id_type;
296 BOOST_STATIC_CONSTANT(int, ID = ID_);
298 typedef Locals locals_type;
299 typedef Attr attr_type;
300 typedef AttrRef attr_reference_type;
301 typedef Parameters parameter_types;
302 static size_t const params_size = ParamsSize;
304 typedef Subject subject_type;
305 typedef mpl::bool_<Auto_> auto_type;
306 BOOST_STATIC_CONSTANT(bool, Auto = Auto_);
308 typedef spirit::karma::detail::generator_binder<
309 Subject, auto_type> binder_type;
311 subrule_definition(Subject const& subject, std::string const& name)
312 : binder(subject), name(name)
316 binder_type const binder;
317 std::string const name;
320 ///////////////////////////////////////////////////////////////////////////
321 // subrule placeholder:
322 // - on subrule definition: helper for creation of subrule_group,
323 // - on subrule invocation: Proto terminal and generator.
324 ///////////////////////////////////////////////////////////////////////////
327 , typename T1 = unused_type
328 , typename T2 = unused_type
332 typename proto::terminal<
333 spirit::karma::reference<subrule<ID_, T1, T2> const>
335 , subrule<ID_, T1, T2>
337 , spirit::karma::generator<subrule<ID_, T1, T2> >
339 //FIXME should go fetch the real properties of this subrule's definition in the current context, but we don't
340 // have the context here (properties would need to be 'template<typename Context> struct properties' instead)
342 spirit::karma::generator_properties::all_properties> properties;
344 typedef mpl::int_<ID_> id_type;
345 BOOST_STATIC_CONSTANT(int, ID = ID_);
347 typedef subrule<ID_, T1, T2> this_type;
348 typedef spirit::karma::reference<this_type const> reference_;
349 typedef typename proto::terminal<reference_>::type terminal;
350 typedef proto::extends<terminal, this_type> base_type;
352 typedef mpl::vector<T1, T2> template_params;
354 // locals_type is a sequence of types to be used as local variables
356 spirit::detail::extract_locals<template_params>::type
360 spirit::detail::extract_sig<template_params>::type
363 // This is the subrule's attribute type
365 spirit::detail::attr_from_sig<sig_type>::type
367 typedef typename add_reference<
368 typename add_const<attr_type>::type>::type attr_reference_type;
370 // parameter_types is a sequence of types passed as parameters to the subrule
372 spirit::detail::params_from_sig<sig_type>::type
375 static size_t const params_size =
376 fusion::result_of::size<parameter_types>::type::value;
378 explicit subrule(std::string const& name_ = "unnamed-subrule")
379 : base_type(terminal::make(reference_(*this)))
384 // compute type of this subrule's definition for expr type Expr
385 template <typename Expr, bool Auto>
386 struct def_type_helper
388 // Report invalid expression error as early as possible.
389 // If you got an error_invalid_expression error message here,
390 // then the expression (Expr) is not a valid spirit karma expression.
391 BOOST_SPIRIT_ASSERT_MATCH(spirit::karma::domain, Expr);
393 typedef typename result_of::compile<
394 spirit::karma::domain, Expr>::type subject_type;
396 typedef subrule_definition<
400 , attr_reference_type
408 // compute type of subrule group containing only this
409 // subrule's definition for expr type Expr
410 template <typename Expr, bool Auto>
411 struct group_type_helper
413 typedef typename def_type_helper<Expr, Auto>::type def_type;
415 // create Defs map with only one entry: (ID -> def)
417 fusion::result_of::make_map<id_type, def_type>::type
420 typedef subrule_group<defs_type> type;
423 template <typename Expr>
424 typename group_type_helper<Expr, false>::type
425 operator=(Expr const& expr) const
427 typedef group_type_helper<Expr, false> helper;
428 typedef typename helper::def_type def_type;
429 typedef typename helper::type result_type;
430 return result_type(fusion::make_map<id_type>(
431 def_type(compile<spirit::karma::domain>(expr), name_)));
434 template <typename Expr>
435 friend typename group_type_helper<Expr, true>::type
436 operator%=(subrule const& sr, Expr const& expr)
438 typedef group_type_helper<Expr, true> helper;
439 typedef typename helper::def_type def_type;
440 typedef typename helper::type result_type;
441 return result_type(fusion::make_map<id_type>(
442 def_type(compile<spirit::karma::domain>(expr), sr.name_)));
445 // non-const versions needed to suppress proto's %= kicking in
446 template <typename Expr>
447 friend typename group_type_helper<Expr, true>::type
448 operator%=(subrule const& sr, Expr& expr)
452 , static_cast<Expr const&>(expr));
454 template <typename Expr>
455 friend typename group_type_helper<Expr, true>::type
456 operator%=(subrule& sr, Expr const& expr)
459 static_cast<subrule const&>(sr)
462 template <typename Expr>
463 friend typename group_type_helper<Expr, true>::type
464 operator%=(subrule& sr, Expr& expr)
467 static_cast<subrule const&>(sr)
468 , static_cast<Expr const&>(expr));
471 std::string const& name() const
476 void name(std::string const& str)
481 template <typename Context, typename Iterator>
484 typedef attr_type type;
487 template <typename OutputIterator, typename Group
488 , typename Attributes, typename Locals
489 , typename Delimiter, typename Attribute>
490 bool generate(OutputIterator& sink
491 , subrule_context<Group, Attributes, Locals>& context
492 , Delimiter const& delimiter, Attribute const& attr) const
494 return context.group.template generate_subrule_id<ID_>(
495 sink, context, delimiter, attr);
498 template <typename OutputIterator, typename Context
499 , typename Delimiter, typename Attribute>
500 bool generate(OutputIterator& /*sink*/
501 , Context& /*context*/
502 , Delimiter const& /*delimiter*/, Attribute const& /*attr*/) const
504 // If you are seeing a compilation error here, you are trying
505 // to use a subrule as a generator outside of a subrule group.
506 BOOST_SPIRIT_ASSERT_FAIL(OutputIterator
507 , subrule_used_outside_subrule_group, (id_type));
512 template <typename OutputIterator, typename Group
513 , typename Attributes, typename Locals
514 , typename Delimiter, typename Attribute
516 bool generate(OutputIterator& sink
517 , subrule_context<Group, Attributes, Locals>& context
518 , Delimiter const& delimiter, Attribute const& attr
519 , Params const& params) const
521 return context.group.template generate_subrule_id<ID_>(
522 sink, context, delimiter, attr, params);
525 template <typename OutputIterator, typename Context
526 , typename Delimiter, typename Attribute
528 bool generate(OutputIterator& /*sink*/
529 , Context& /*context*/
530 , Delimiter const& /*delimiter*/, Attribute const& /*attr*/
531 , Params const& /*params*/) const
533 // If you are seeing a compilation error here, you are trying
534 // to use a subrule as a generator outside of a subrule group.
535 BOOST_SPIRIT_ASSERT_FAIL(OutputIterator
536 , subrule_used_outside_subrule_group, (id_type));
541 template <typename Context>
542 info what(Context& /*context*/) const
547 // bring in the operator() overloads
548 this_type const& get_parameterized_subject() const { return *this; }
549 typedef this_type parameterized_subject_type;
550 #include <boost/spirit/home/karma/nonterminal/detail/fcall.hpp>
556 #if defined(BOOST_MSVC)
557 # pragma warning(pop)