1 /*=============================================================================
2 Copyright (c) 2001-2011 Joel de Guzman
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(SPIRIT_ACTION_JANUARY_07_2007_1128AM)
8 #define SPIRIT_ACTION_JANUARY_07_2007_1128AM
14 #include <boost/spirit/home/qi/meta_compiler.hpp>
15 #include <boost/spirit/home/qi/parser.hpp>
16 #include <boost/spirit/home/qi/detail/attributes.hpp>
17 #include <boost/spirit/home/support/argument.hpp>
18 #include <boost/spirit/home/support/context.hpp>
19 #include <boost/spirit/home/support/unused.hpp>
20 #include <boost/spirit/home/support/info.hpp>
21 #include <boost/spirit/home/support/action_dispatch.hpp>
22 #include <boost/spirit/home/support/handles_container.hpp>
24 #include <boost/mpl/bool.hpp>
25 #include <boost/mpl/if.hpp>
26 #include <boost/type_traits/remove_const.hpp>
27 #include <boost/type_traits/is_same.hpp>
29 namespace boost { namespace spirit { namespace qi
31 BOOST_PP_REPEAT(SPIRIT_ARGUMENTS_LIMIT, SPIRIT_USING_ARGUMENT, _)
33 template <typename Subject, typename Action>
34 struct action : unary_parser<action<Subject, Action> >
36 typedef Subject subject_type;
37 typedef Action action_type;
39 template <typename Context, typename Iterator>
41 : traits::attribute_of<Subject, Context, Iterator>
44 action(Subject const& subject_, Action f_)
45 : subject(subject_), f(f_) {}
47 #ifndef BOOST_SPIRIT_ACTIONS_ALLOW_ATTR_COMPAT
48 template <typename Iterator, typename Context
49 , typename Skipper, typename Attribute>
50 bool parse(Iterator& first, Iterator const& last
51 , Context& context, Skipper const& skipper
52 , Attribute& attr_) const
54 typedef typename attribute<Context, Iterator>::type attr_type;
55 typedef traits::make_attribute<attr_type, Attribute> make_attribute;
57 // create an attribute if one is not supplied
58 typedef traits::transform_attribute<
59 typename make_attribute::type, attr_type, domain> transform;
61 typename make_attribute::type made_attr = make_attribute::call(attr_);
62 typename transform::type attr = transform::pre(made_attr);
64 Iterator save = first;
65 if (subject.parse(first, last, context, skipper, attr))
67 // call the function, passing the attribute, the context.
68 // The client can return false to fail parsing.
69 if (traits::action_dispatch<Subject>()(f, attr, context))
71 // Do up-stream transformation, this integrates the results
72 // back into the original attribute value, if appropriate.
73 traits::post_transform(attr_, attr);
77 // reset iterators if semantic action failed the match
84 template <typename Iterator, typename Context
85 , typename Skipper, typename Attribute>
86 bool parse(Iterator& first, Iterator const& last
87 , Context& context, Skipper const& skipper
88 , Attribute& attr) const
90 Iterator save = first;
91 if (subject.parse(first, last, context, skipper, attr)) // Use the attribute as-is
93 // call the function, passing the attribute, the context.
94 // The client can return false to fail parsing.
95 if (traits::action_dispatch<Subject>()(f, attr, context))
98 // reset iterators if semantic action failed the match
105 template <typename Iterator, typename Context
107 bool parse(Iterator& first, Iterator const& last
108 , Context& context, Skipper const& skipper
111 typedef typename attribute<Context, Iterator>::type attr_type;
112 typedef traits::make_attribute<attr_type, unused_type> make_attribute;
114 // synthesize the attribute since one is not supplied
115 typedef traits::transform_attribute<
116 typename make_attribute::type, attr_type, domain> transform;
118 typename make_attribute::type made_attr = make_attribute::call(unused_type());
119 typename transform::type attr = transform::pre(made_attr);
121 Iterator save = first;
122 if (subject.parse(first, last, context, skipper, attr))
124 // call the function, passing the attribute, the context.
125 // The client can return false to fail parsing.
126 if (traits::action_dispatch<Subject>()(f, attr, context))
129 // reset iterators if semantic action failed the match
137 template <typename Context>
138 info what(Context& context) const
140 // the action is transparent (does not add any info)
141 return subject.what(context);
148 // silence MSVC warning C4512: assignment operator could not be generated
149 action& operator= (action const&);
153 namespace boost { namespace spirit
155 // Qi action meta-compiler
157 struct make_component<qi::domain, tag::action>
159 template <typename Sig>
162 template <typename This, typename Elements, typename Modifiers>
163 struct result<This(Elements, Modifiers)>
166 remove_const<typename Elements::car_type>::type
170 remove_const<typename Elements::cdr_type::car_type>::type
173 typedef qi::action<subject_type, action_type> type;
176 template <typename Elements>
177 typename result<make_component(Elements, unused_type)>::type
178 operator()(Elements const& elements, unused_type) const
180 typename result<make_component(Elements, unused_type)>::type
181 result(elements.car, elements.cdr.car);
187 namespace boost { namespace spirit { namespace traits
189 ///////////////////////////////////////////////////////////////////////////
190 template <typename Subject, typename Action>
191 struct has_semantic_action<qi::action<Subject, Action> >
194 ///////////////////////////////////////////////////////////////////////////
195 template <typename Subject, typename Action, typename Attribute
196 , typename Context, typename Iterator>
197 struct handles_container<qi::action<Subject, Action>, Attribute
199 : unary_handles_container<Subject, Attribute, Context, Iterator> {};