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(BOOST_SPIRIT_X3_ACTION_JANUARY_07_2007_1128AM)
8 #define BOOST_SPIRIT_X3_ACTION_JANUARY_07_2007_1128AM
10 #include <boost/spirit/home/x3/support/context.hpp>
11 #include <boost/spirit/home/x3/support/traits/attribute_of.hpp>
12 #include <boost/spirit/home/x3/support/traits/make_attribute.hpp>
13 #include <boost/spirit/home/x3/core/call.hpp>
14 #include <boost/spirit/home/x3/nonterminal/detail/transform_attribute.hpp>
15 #include <boost/range/iterator_range.hpp>
17 namespace boost { namespace spirit { namespace x3
19 struct raw_attribute_type;
20 struct parse_pass_context_tag;
22 template <typename Context>
23 inline bool& _pass(Context const& context)
25 return x3::get<parse_pass_context_tag>(context);
28 template <typename Subject, typename Action>
29 struct action : unary_parser<Subject, action<Subject, Action>>
31 typedef unary_parser<Subject, action<Subject, Action>> base_type;
32 static bool const is_pass_through_unary = true;
33 static bool const has_action = true;
35 action(Subject const& subject, Action f)
36 : base_type(subject), f(f) {}
38 template <typename Iterator, typename Context, typename RuleContext, typename Attribute>
40 Iterator& first, Iterator const& last
41 , Context const& context, RuleContext& rcontext, Attribute& attr) const
44 auto action_context = make_context<parse_pass_context_tag>(pass, context);
45 call(f, first, last, action_context, rcontext, attr);
49 template <typename Iterator, typename Context
50 , typename RuleContext, typename Attribute>
51 bool parse_main(Iterator& first, Iterator const& last
52 , Context const& context, RuleContext& rcontext, Attribute& attr) const
54 Iterator save = first;
55 if (this->subject.parse(first, last, context, rcontext, attr))
57 if (call_action(first, last, context, rcontext, attr))
60 // reset iterators if semantic action failed the match
67 // attr==raw_attribute_type, action wants iterator_range (see raw.hpp)
68 template <typename Iterator, typename Context, typename RuleContext>
69 bool parse_main(Iterator& first, Iterator const& last
70 , Context const& context, RuleContext& rcontext, raw_attribute_type&) const
72 boost::iterator_range<Iterator> rng;
73 // synthesize the attribute since one is not supplied
74 return parse_main(first, last, context, rcontext, rng);
77 // attr==unused, action wants attribute
78 template <typename Iterator, typename Context, typename RuleContext>
79 bool parse(Iterator& first, Iterator const& last
80 , Context const& context, RuleContext& rcontext, unused_type) const
83 traits::attribute_of<action<Subject, Action>, Context>::type
85 typedef traits::make_attribute<attribute_type, unused_type> make_attribute;
86 typedef traits::transform_attribute<
87 typename make_attribute::type, attribute_type, parser_id>
90 // synthesize the attribute since one is not supplied
91 typename make_attribute::type made_attr = make_attribute::call(unused_type());
92 typename transform::type attr = transform::pre(made_attr);
93 return parse_main(first, last, context, rcontext, attr);
96 // main parse function
97 template <typename Iterator, typename Context
98 , typename RuleContext, typename Attribute>
99 bool parse(Iterator& first, Iterator const& last
100 , Context const& context, RuleContext& rcontext, Attribute& attr) const
102 return parse_main(first, last, context, rcontext, attr);
108 template <typename P, typename Action>
109 inline action<typename extension::as_parser<P>::value_type, Action>
110 operator/(P const& p, Action f)
112 return { as_parser(p), f };