]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*============================================================================= |
2 | Copyright (arg) 2001-2014 Joel de Guzman | |
3 | ||
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 | |
9 | ||
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> | |
16 | ||
17 | namespace boost { namespace spirit { namespace x3 | |
18 | { | |
19 | struct raw_attribute_type; | |
20 | struct parse_pass_context_tag; | |
21 | ||
22 | template <typename Context> | |
23 | inline bool& _pass(Context const& context) | |
24 | { | |
25 | return x3::get<parse_pass_context_tag>(context); | |
26 | } | |
27 | ||
28 | template <typename Subject, typename Action> | |
29 | struct action : unary_parser<Subject, action<Subject, Action>> | |
30 | { | |
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; | |
34 | ||
35 | action(Subject const& subject, Action f) | |
36 | : base_type(subject), f(f) {} | |
37 | ||
38 | template <typename Iterator, typename Context, typename RuleContext, typename Attribute> | |
39 | bool call_action( | |
40 | Iterator& first, Iterator const& last | |
41 | , Context const& context, RuleContext& rcontext, Attribute& attr) const | |
42 | { | |
43 | bool pass = true; | |
44 | auto action_context = make_context<parse_pass_context_tag>(pass, context); | |
45 | call(f, first, last, action_context, rcontext, attr); | |
46 | return pass; | |
47 | } | |
48 | ||
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 | |
53 | { | |
54 | Iterator save = first; | |
55 | if (this->subject.parse(first, last, context, rcontext, attr)) | |
56 | { | |
57 | if (call_action(first, last, context, rcontext, attr)) | |
58 | return true; | |
59 | ||
60 | // reset iterators if semantic action failed the match | |
61 | // retrospectively | |
62 | first = save; | |
63 | } | |
64 | return false; | |
65 | } | |
66 | ||
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 | |
71 | { | |
72 | boost::iterator_range<Iterator> rng; | |
73 | // synthesize the attribute since one is not supplied | |
74 | return parse_main(first, last, context, rcontext, rng); | |
75 | } | |
76 | ||
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 | |
81 | { | |
82 | typedef typename | |
83 | traits::attribute_of<action<Subject, Action>, Context>::type | |
84 | attribute_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> | |
88 | transform; | |
89 | ||
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); | |
94 | } | |
95 | ||
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 | |
101 | { | |
102 | return parse_main(first, last, context, rcontext, attr); | |
103 | } | |
104 | ||
105 | Action f; | |
106 | }; | |
107 | ||
108 | template <typename P, typename Action> | |
109 | inline action<typename extension::as_parser<P>::value_type, Action> | |
110 | operator/(P const& p, Action f) | |
111 | { | |
112 | return { as_parser(p), f }; | |
113 | } | |
114 | }}} | |
115 | ||
116 | #endif |