1 /*=============================================================================
2 Copyright (arg) 2001-2014 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/x3/support/context.hpp>
15 #include <boost/spirit/home/x3/support/traits/attribute_of.hpp>
16 #include <boost/spirit/home/x3/support/traits/make_attribute.hpp>
17 #include <boost/spirit/home/x3/core/call.hpp>
18 #include <boost/spirit/home/x3/nonterminal/detail/transform_attribute.hpp>
19 #include <boost/range/iterator_range.hpp>
21 namespace boost { namespace spirit { namespace x3
23 struct raw_attribute_type;
24 struct parse_pass_context_tag;
26 template <typename Context>
27 inline bool& _pass(Context const& context)
29 return x3::get<parse_pass_context_tag>(context);
32 template <typename Subject, typename Action>
33 struct action : unary_parser<Subject, action<Subject, Action>>
35 typedef unary_parser<Subject, action<Subject, Action>> base_type;
36 static bool const is_pass_through_unary = true;
37 static bool const has_action = true;
39 action(Subject const& subject, Action f)
40 : base_type(subject), f(f) {}
42 template <typename Iterator, typename Context, typename RuleContext, typename Attribute>
44 Iterator& first, Iterator const& last
45 , Context const& context, RuleContext& rcontext, Attribute& attr) const
48 auto action_context = make_context<parse_pass_context_tag>(pass, context);
49 call(f, first, last, action_context, rcontext, attr);
53 template <typename Iterator, typename Context
54 , typename RuleContext, typename Attribute>
55 bool parse_main(Iterator& first, Iterator const& last
56 , Context const& context, RuleContext& rcontext, Attribute& attr) const
58 Iterator save = first;
59 if (this->subject.parse(first, last, context, rcontext, attr))
61 if (call_action(first, last, context, rcontext, attr))
64 // reset iterators if semantic action failed the match
71 // attr==raw_attribute_type, action wants iterator_range (see raw.hpp)
72 template <typename Iterator, typename Context, typename RuleContext>
73 bool parse_main(Iterator& first, Iterator const& last
74 , Context const& context, RuleContext& rcontext, raw_attribute_type&) const
76 boost::iterator_range<Iterator> rng;
77 // synthesize the attribute since one is not supplied
78 return parse_main(first, last, context, rcontext, rng);
81 // attr==unused, action wants attribute
82 template <typename Iterator, typename Context, typename RuleContext>
83 bool parse(Iterator& first, Iterator const& last
84 , Context const& context, RuleContext& rcontext, unused_type) const
87 traits::attribute_of<action<Subject, Action>, Context>::type
89 typedef traits::make_attribute<attribute_type, unused_type> make_attribute;
90 typedef traits::transform_attribute<
91 typename make_attribute::type, attribute_type, parser_id>
94 // synthesize the attribute since one is not supplied
95 typename make_attribute::type made_attr = make_attribute::call(unused_type());
96 typename transform::type attr = transform::pre(made_attr);
97 return parse_main(first, last, context, rcontext, attr);
100 // main parse function
101 template <typename Iterator, typename Context
102 , typename RuleContext, typename Attribute>
103 bool parse(Iterator& first, Iterator const& last
104 , Context const& context, RuleContext& rcontext, Attribute& attr) const
106 return parse_main(first, last, context, rcontext, attr);
112 template <typename P, typename Action>
113 inline action<typename extension::as_parser<P>::value_type, Action>
114 operator/(P const& p, Action f)
116 return {as_parser(p), f};