1 /*=============================================================================
2 Copyright (c) 2009 Francois Barel
3 Copyright (c) 2001-2011 Joel de Guzman
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_REPOSITORY_QI_SUBRULE_AUGUST_06_2009_0239AM)
9 #define BOOST_SPIRIT_REPOSITORY_QI_SUBRULE_AUGUST_06_2009_0239AM
15 #include <boost/spirit/home/qi/domain.hpp>
16 #include <boost/spirit/home/qi/meta_compiler.hpp>
17 #include <boost/spirit/home/qi/parser.hpp>
18 #include <boost/spirit/home/qi/reference.hpp>
19 #include <boost/spirit/home/qi/nonterminal/detail/parameterized.hpp>
20 #include <boost/spirit/home/qi/nonterminal/detail/parser_binder.hpp>
21 #include <boost/spirit/home/support/argument.hpp>
22 #include <boost/spirit/home/support/assert_msg.hpp>
23 #include <boost/spirit/home/qi/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 qi
56 ///////////////////////////////////////////////////////////////////////////
58 // - parser 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::qi::reference<subrule_group<Defs> const>
71 , spirit::qi::parser<subrule_group<Defs> >
73 // Fusion associative sequence, associating each subrule ID in this
74 // group (as an MPL integral constant) with its definition
75 typedef Defs defs_type;
77 typedef subrule_group<Defs> this_type;
78 typedef spirit::qi::reference<this_type const> reference_;
79 typedef typename proto::terminal<reference_>::type terminal;
80 typedef proto::extends<terminal, this_type> base_type;
82 static size_t const params_size =
83 // Forward to first subrule.
85 typename fusion::result_of::front<Defs>::type
86 >::type::second_type::params_size;
88 subrule_group(subrule_group const& rhs)
89 : base_type(terminal::make(reference_(*this)))
94 explicit subrule_group(Defs const& defs)
95 : base_type(terminal::make(reference_(*this)))
100 // from a subrule ID, get the type of a reference to its definition
104 typedef mpl::int_<ID> id_type;
106 // If you are seeing a compilation error here, you are trying
107 // to use a subrule which was not defined in this group.
108 BOOST_SPIRIT_ASSERT_MSG(
109 (fusion::result_of::has_key<
110 defs_type const, id_type>::type::value)
111 , subrule_used_without_being_defined, (mpl::int_<ID>));
114 fusion::result_of::at_key<defs_type const, id_type>::type
118 // from a subrule ID, get a reference to its definition
120 typename def_type<ID>::type def() const
122 return fusion::at_key<mpl::int_<ID> >(defs);
125 template <typename Context, typename Iterator>
127 // Forward to first subrule.
129 typename remove_reference<
130 typename fusion::result_of::front<Defs>::type
131 >::type::second_type::attr_type> {};
133 template <typename Iterator, typename Context
134 , typename Skipper, typename Attribute>
135 bool parse(Iterator& first, Iterator const& last
136 , Context& context, Skipper const& skipper
137 , Attribute& attr) const
139 // Forward to first subrule.
140 return parse_subrule(fusion::front(defs).second
141 , first, last, context, skipper, attr);
144 template <typename Iterator, typename Context
145 , typename Skipper, typename Attribute, typename Params>
146 bool parse(Iterator& first, Iterator const& last
147 , Context& context, Skipper const& skipper
148 , Attribute& attr, Params const& params) const
150 // Forward to first subrule.
151 return parse_subrule(fusion::front(defs).second
152 , first, last, context, skipper, attr, params);
155 template <int ID, typename Iterator, typename Context
156 , typename Skipper, typename Attribute>
157 bool parse_subrule_id(Iterator& first, Iterator const& last
158 , Context& context, Skipper const& skipper
159 , Attribute& attr) const
161 return parse_subrule(def<ID>()
162 , first, last, context, skipper, attr);
165 template <int ID, typename Iterator, typename Context
166 , typename Skipper, typename Attribute, typename Params>
167 bool parse_subrule_id(Iterator& first, Iterator const& last
168 , Context& context, Skipper const& skipper
169 , Attribute& attr, Params const& params) const
171 return parse_subrule(def<ID>()
172 , first, last, context, skipper, attr, params);
175 template <typename Def
176 , typename Iterator, typename Context
177 , typename Skipper, typename Attribute>
178 bool parse_subrule(Def const& def
179 , Iterator& first, Iterator const& last
180 , Context& /*caller_context*/, Skipper const& skipper
181 , Attribute& attr) const
183 // compute context type for this subrule
184 typedef typename Def::locals_type subrule_locals_type;
185 typedef typename Def::attr_type subrule_attr_type;
186 typedef typename Def::attr_reference_type subrule_attr_reference_type;
187 typedef typename Def::parameter_types subrule_parameter_types;
193 subrule_attr_reference_type, subrule_parameter_types>
194 , subrule_locals_type
199 typedef traits::make_attribute<
200 subrule_attr_type, Attribute> make_attribute;
202 // do down-stream transformation, provides attribute for
204 typedef traits::transform_attribute<
205 typename make_attribute::type, subrule_attr_type, spirit::qi::domain>
208 typename make_attribute::type made_attr = make_attribute::call(attr);
209 typename transform::type attr_ = transform::pre(made_attr);
211 // If you are seeing a compilation error here, you are probably
212 // trying to use a subrule which has inherited attributes,
213 // without passing values for them.
214 context_type context(*this, attr_);
216 if (def.binder(first, last, context, skipper))
218 // do up-stream transformation, this integrates the results
219 // back into the original attribute value, if appropriate
220 traits::post_transform(attr, attr_);
224 // inform attribute transformation of failed rhs
225 traits::fail_transform(attr, attr_);
229 template <typename Def
230 , typename Iterator, typename Context
231 , typename Skipper, typename Attribute, typename Params>
232 bool parse_subrule(Def const& def
233 , Iterator& first, Iterator const& last
234 , Context& caller_context, Skipper const& skipper
235 , Attribute& attr, Params const& params) const
237 // compute context type for this subrule
238 typedef typename Def::locals_type subrule_locals_type;
239 typedef typename Def::attr_type subrule_attr_type;
240 typedef typename Def::attr_reference_type subrule_attr_reference_type;
241 typedef typename Def::parameter_types subrule_parameter_types;
247 subrule_attr_reference_type, subrule_parameter_types>
248 , subrule_locals_type
253 typedef traits::make_attribute<
254 subrule_attr_type, Attribute> make_attribute;
256 // do down-stream transformation, provides attribute for
258 typedef traits::transform_attribute<
259 typename make_attribute::type, subrule_attr_type, spirit::qi::domain>
262 typename make_attribute::type made_attr = make_attribute::call(attr);
263 typename transform::type attr_ = transform::pre(made_attr);
265 // If you are seeing a compilation error here, you are probably
266 // trying to use a subrule which has inherited attributes,
267 // passing values of incompatible types for them.
268 context_type context(*this, attr_, params, caller_context);
270 if (def.binder(first, last, context, skipper))
272 // do up-stream transformation, this integrates the results
273 // back into the original attribute value, if appropriate
274 traits::post_transform(attr, attr_);
278 // inform attribute transformation of failed rhs
279 traits::fail_transform(attr, attr_);
283 template <typename Context>
284 info what(Context& context) const
286 // Forward to first subrule.
287 return fusion::front(defs).second.binder.p.what(context);
290 template <typename Defs2>
292 typename fusion::result_of::as_map<
293 typename fusion::result_of::join<
294 Defs const, Defs2 const>::type>::type>
295 operator,(subrule_group<Defs2> const& other) const
297 typedef subrule_group<
298 typename fusion::result_of::as_map<
299 typename fusion::result_of::join<
300 Defs const, Defs2 const>::type>::type> result_type;
301 return result_type(fusion::as_map(fusion::join(defs, other.defs)));
304 // bring in the operator() overloads
305 this_type const& get_parameterized_subject() const { return *this; }
306 typedef this_type parameterized_subject_type;
307 #include <boost/spirit/home/qi/nonterminal/detail/fcall.hpp>
312 ///////////////////////////////////////////////////////////////////////////
313 // subrule_definition: holds one particular definition of a subrule
314 ///////////////////////////////////////////////////////////////////////////
320 , typename Parameters
325 struct subrule_definition
327 typedef mpl::int_<ID_> id_type;
328 BOOST_STATIC_CONSTANT(int, ID = ID_);
330 typedef Locals locals_type;
331 typedef Attr attr_type;
332 typedef AttrRef attr_reference_type;
333 typedef Parameters parameter_types;
334 static size_t const params_size = ParamsSize;
336 typedef Subject subject_type;
337 typedef mpl::bool_<Auto_> auto_type;
338 BOOST_STATIC_CONSTANT(bool, Auto = Auto_);
340 typedef spirit::qi::detail::parser_binder<
341 Subject, auto_type> binder_type;
343 subrule_definition(Subject const& subject, std::string const& name)
344 : binder(subject), name(name)
348 binder_type const binder;
349 std::string const name;
352 ///////////////////////////////////////////////////////////////////////////
353 // subrule placeholder:
354 // - on subrule definition: helper for creation of subrule_group,
355 // - on subrule invocation: Proto terminal and parser.
356 ///////////////////////////////////////////////////////////////////////////
359 , typename T1 = unused_type
360 , typename T2 = unused_type
364 typename proto::terminal<
365 spirit::qi::reference<subrule<ID_, T1, T2> const>
367 , subrule<ID_, T1, T2>
369 , spirit::qi::parser<subrule<ID_, T1, T2> >
371 typedef mpl::int_<ID_> id_type;
372 BOOST_STATIC_CONSTANT(int, ID = ID_);
374 typedef subrule<ID_, T1, T2> this_type;
375 typedef spirit::qi::reference<this_type const> reference_;
376 typedef typename proto::terminal<reference_>::type terminal;
377 typedef proto::extends<terminal, this_type> base_type;
379 typedef mpl::vector<T1, T2> template_params;
381 // locals_type is a sequence of types to be used as local variables
383 spirit::detail::extract_locals<template_params>::type
387 spirit::detail::extract_sig<template_params>::type
390 // This is the subrule's attribute type
392 spirit::detail::attr_from_sig<sig_type>::type
394 typedef typename add_reference<attr_type>::type attr_reference_type;
396 // parameter_types is a sequence of types passed as parameters to the subrule
398 spirit::detail::params_from_sig<sig_type>::type
401 static size_t const params_size =
402 fusion::result_of::size<parameter_types>::type::value;
404 explicit subrule(std::string const& name_ = "unnamed-subrule")
405 : base_type(terminal::make(reference_(*this)))
410 // compute type of this subrule's definition for expr type Expr
411 template <typename Expr, bool Auto>
412 struct def_type_helper
414 // Report invalid expression error as early as possible.
415 // If you got an error_invalid_expression error message here,
416 // then the expression (Expr) is not a valid spirit qi expression.
417 BOOST_SPIRIT_ASSERT_MATCH(spirit::qi::domain, Expr);
419 typedef typename result_of::compile<
420 spirit::qi::domain, Expr>::type subject_type;
422 typedef subrule_definition<
426 , attr_reference_type
434 // compute type of subrule group containing only this
435 // subrule's definition for expr type Expr
436 template <typename Expr, bool Auto>
437 struct group_type_helper
439 typedef typename def_type_helper<Expr, Auto>::type def_type;
441 // create Defs map with only one entry: (ID -> def)
443 #ifndef BOOST_FUSION_HAS_VARIADIC_MAP
444 fusion::result_of::make_map<id_type, def_type>::type
446 fusion::result_of::make_map<id_type>::template apply<def_type>::type
450 typedef subrule_group<defs_type> type;
453 template <typename Expr>
454 typename group_type_helper<Expr, false>::type
455 operator=(Expr const& expr) const
457 typedef group_type_helper<Expr, false> helper;
458 typedef typename helper::def_type def_type;
459 typedef typename helper::type result_type;
460 return result_type(fusion::make_map<id_type>(
461 def_type(compile<spirit::qi::domain>(expr), name_)));
464 template <typename Expr>
465 friend typename group_type_helper<Expr, true>::type
466 operator%=(subrule const& sr, Expr const& expr)
468 typedef group_type_helper<Expr, true> helper;
469 typedef typename helper::def_type def_type;
470 typedef typename helper::type result_type;
471 return result_type(fusion::make_map<id_type>(
472 def_type(compile<spirit::qi::domain>(expr), sr.name_)));
475 // non-const versions needed to suppress proto's %= kicking in
476 template <typename Expr>
477 friend typename group_type_helper<Expr, true>::type
478 operator%=(subrule const& sr, Expr& expr)
482 , static_cast<Expr const&>(expr));
484 template <typename Expr>
485 friend typename group_type_helper<Expr, true>::type
486 operator%=(subrule& sr, Expr const& expr)
489 static_cast<subrule const&>(sr)
492 template <typename Expr>
493 friend typename group_type_helper<Expr, true>::type
494 operator%=(subrule& sr, Expr& expr)
497 static_cast<subrule const&>(sr)
498 , static_cast<Expr const&>(expr));
501 std::string const& name() const
506 void name(std::string const& str)
511 template <typename Context, typename Iterator>
514 typedef attr_type type;
517 template <typename Iterator, typename Group
518 , typename Attributes, typename Locals
519 , typename Skipper, typename Attribute>
520 bool parse(Iterator& first, Iterator const& last
521 , subrule_context<Group, Attributes, Locals>& context
522 , Skipper const& skipper, Attribute& attr) const
524 return context.group.template parse_subrule_id<ID_>(
525 first, last, context, skipper, attr);
528 template <typename Iterator, typename Context
529 , typename Skipper, typename Attribute>
530 bool parse(Iterator& /*first*/, Iterator const& /*last*/
531 , Context& /*context*/
532 , Skipper const& /*skipper*/, Attribute& /*attr*/) const
534 // If you are seeing a compilation error here, you are trying
535 // to use a subrule as a parser outside of a subrule group.
536 BOOST_SPIRIT_ASSERT_FAIL(Iterator
537 , subrule_used_outside_subrule_group, (id_type));
542 template <typename Iterator, typename Group
543 , typename Attributes, typename Locals
544 , typename Skipper, typename Attribute
546 bool parse(Iterator& first, Iterator const& last
547 , subrule_context<Group, Attributes, Locals>& context
548 , Skipper const& skipper, Attribute& attr
549 , Params const& params) const
551 return context.group.template parse_subrule_id<ID_>(
552 first, last, context, skipper, attr, params);
555 template <typename Iterator, typename Context
556 , typename Skipper, typename Attribute
558 bool parse(Iterator& /*first*/, Iterator const& /*last*/
559 , Context& /*context*/
560 , Skipper const& /*skipper*/, Attribute& /*attr*/
561 , Params const& /*params*/) const
563 // If you are seeing a compilation error here, you are trying
564 // to use a subrule as a parser outside of a subrule group.
565 BOOST_SPIRIT_ASSERT_FAIL(Iterator
566 , subrule_used_outside_subrule_group, (id_type));
571 template <typename Context>
572 info what(Context& /*context*/) const
577 // bring in the operator() overloads
578 this_type const& get_parameterized_subject() const { return *this; }
579 typedef this_type parameterized_subject_type;
580 #include <boost/spirit/home/qi/nonterminal/detail/fcall.hpp>
586 #if defined(BOOST_MSVC)
587 # pragma warning(pop)