1 /*=============================================================================
2 Copyright (c) 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_DETAIL_RULE_JAN_08_2012_0326PM)
8 #define BOOST_SPIRIT_X3_DETAIL_RULE_JAN_08_2012_0326PM
10 #include <boost/core/ignore_unused.hpp>
11 #include <boost/spirit/home/x3/auxiliary/guard.hpp>
12 #include <boost/spirit/home/x3/core/parser.hpp>
13 #include <boost/spirit/home/x3/core/skip_over.hpp>
14 #include <boost/spirit/home/x3/directive/expect.hpp>
15 #include <boost/spirit/home/x3/support/traits/make_attribute.hpp>
16 #include <boost/spirit/home/x3/support/utility/sfinae.hpp>
17 #include <boost/spirit/home/x3/nonterminal/detail/transform_attribute.hpp>
18 #include <boost/utility/addressof.hpp>
20 #if defined(BOOST_SPIRIT_X3_DEBUG)
21 #include <boost/spirit/home/x3/nonterminal/simple_trace.hpp>
24 namespace boost { namespace spirit { namespace x3
26 template <typename ID>
29 template <typename ID, typename Attribute = unused_type, bool force_attribute = false>
32 struct parse_pass_context_tag;
36 // we use this so we can detect if the default parse_rule
37 // is the being called.
38 struct default_parse_rule_result
40 default_parse_rule_result(bool r)
42 operator bool() const { return r; }
47 // default parse_rule implementation
48 template <typename ID, typename Attribute, typename Iterator
49 , typename Context, typename ActualAttribute>
50 inline detail::default_parse_rule_result
52 rule<ID, Attribute> rule_
53 , Iterator& first, Iterator const& last
54 , Context const& context, ActualAttribute& attr);
57 namespace boost { namespace spirit { namespace x3 { namespace detail
59 #if defined(BOOST_SPIRIT_X3_DEBUG)
60 template <typename Iterator, typename Attribute>
65 , Iterator const& first, Iterator const& last
66 , Attribute const& attr
67 , bool const& ok_parse //was parse successful?
69 : ok_parse(ok_parse), rule_name(rule_name)
70 , first(first), last(last)
72 , f(detail::get_simple_trace())
74 f(first, last, attr, pre_parse, rule_name);
79 auto status = ok_parse ? successful_parse : failed_parse ;
80 f(first, last, attr, status, rule_name);
84 char const* rule_name;
85 Iterator const& first;
87 Attribute const& attr;
88 detail::simple_trace_type& f;
92 template <typename ID, typename Iterator, typename Context, typename Enable = void>
93 struct has_on_error : mpl::false_ {};
95 template <typename ID, typename Iterator, typename Context>
96 struct has_on_error<ID, Iterator, Context,
97 typename disable_if_substitution_failure<
99 std::declval<ID>().on_error(
100 std::declval<Iterator&>()
101 , std::declval<Iterator>()
102 , std::declval<expectation_failure<Iterator>>()
103 , std::declval<Context>()
110 template <typename ID, typename Iterator, typename Attribute, typename Context, typename Enable = void>
111 struct has_on_success : mpl::false_ {};
113 template <typename ID, typename Iterator, typename Attribute, typename Context>
114 struct has_on_success<ID, Iterator, Context, Attribute,
115 typename disable_if_substitution_failure<
117 std::declval<ID>().on_success(
118 std::declval<Iterator&>()
119 , std::declval<Iterator>()
120 , std::declval<Attribute&>()
121 , std::declval<Context>()
128 template <typename ID>
131 typedef identity<ID> type;
134 template <typename ID>
135 struct make_id<identity<ID>>
137 typedef identity<ID> type;
140 template <typename ID, typename RHS, typename Context>
142 make_rule_context(RHS const& /* rhs */, Context const& context
143 , mpl::false_ /* is_default_parse_rule */)
148 template <typename ID, typename RHS, typename Context>
149 auto make_rule_context(RHS const& rhs, Context const& context
150 , mpl::true_ /* is_default_parse_rule */ )
152 return make_unique_context<ID>(rhs, context);
155 template <typename Attribute, typename ID>
158 template <typename Iterator, typename Context, typename ActualAttribute>
159 static bool call_on_success(
160 Iterator& /* first */, Iterator const& /* last */
161 , Context const& /* context */, ActualAttribute& /* attr */
162 , mpl::false_ /* No on_success handler */ )
167 template <typename Iterator, typename Context, typename ActualAttribute>
168 static bool call_on_success(
169 Iterator& first, Iterator const& last
170 , Context const& context, ActualAttribute& attr
171 , mpl::true_ /* Has on_success handler */)
178 , make_context<parse_pass_context_tag>(pass, context)
183 template <typename RHS, typename Iterator, typename Context
184 , typename RContext, typename ActualAttribute>
185 static bool parse_rhs_main(
187 , Iterator& first, Iterator const& last
188 , Context const& context, RContext& rcontext, ActualAttribute& attr
191 // see if the user has a BOOST_SPIRIT_DEFINE for this rule
194 rule<ID, Attribute>(), first, last
195 , make_unique_context<ID>(rhs, context), attr))
198 // If there is no BOOST_SPIRIT_DEFINE for this rule,
199 // we'll make a context for this rule tagged by its ID
200 // so we can extract the rule later on in the default
201 // (generic) parse_rule function.
203 is_same<parse_rule_result, default_parse_rule_result>
204 is_default_parse_rule;
210 , make_rule_context<ID>(rhs, context, is_default_parse_rule())
218 x3::skip_over(first_, last, context);
219 r = call_on_success(first_, i, context, attr
220 , has_on_success<ID, Iterator, Context, ActualAttribute>());
228 template <typename RHS, typename Iterator, typename Context
229 , typename RContext, typename ActualAttribute>
230 static bool parse_rhs_main(
232 , Iterator& first, Iterator const& last
233 , Context const& context, RContext& rcontext, ActualAttribute& attr
234 , mpl::true_ /* on_error is found */)
240 return parse_rhs_main(
241 rhs, first, last, context, rcontext, attr, mpl::false_());
243 catch (expectation_failure<Iterator> const& x)
245 switch (ID().on_error(first, last, x, context))
247 case error_handler_result::fail:
249 case error_handler_result::retry:
251 case error_handler_result::accept:
253 case error_handler_result::rethrow:
260 template <typename RHS, typename Iterator
261 , typename Context, typename RContext, typename ActualAttribute>
262 static bool parse_rhs_main(
264 , Iterator& first, Iterator const& last
265 , Context const& context, RContext& rcontext, ActualAttribute& attr)
267 return parse_rhs_main(
268 rhs, first, last, context, rcontext, attr
269 , has_on_error<ID, Iterator, Context>()
273 template <typename RHS, typename Iterator
274 , typename Context, typename RContext, typename ActualAttribute>
275 static bool parse_rhs(
277 , Iterator& first, Iterator const& last
278 , Context const& context, RContext& rcontext, ActualAttribute& attr
281 return parse_rhs_main(rhs, first, last, context, rcontext, attr);
284 template <typename RHS, typename Iterator
285 , typename Context, typename RContext, typename ActualAttribute>
286 static bool parse_rhs(
288 , Iterator& first, Iterator const& last
289 , Context const& context, RContext& rcontext, ActualAttribute& /* attr */
292 return parse_rhs_main(rhs, first, last, context, rcontext, unused);
295 template <typename RHS, typename Iterator, typename Context
296 , typename ActualAttribute, typename ExplicitAttrPropagation>
297 static bool call_rule_definition(
299 , char const* rule_name
300 , Iterator& first, Iterator const& last
301 , Context const& context, ActualAttribute& attr
302 , ExplicitAttrPropagation)
304 boost::ignore_unused(rule_name);
306 typedef traits::make_attribute<Attribute, ActualAttribute> make_attribute;
308 // do down-stream transformation, provides attribute for
310 typedef traits::transform_attribute<
311 typename make_attribute::type, Attribute, parser_id>
314 typedef typename make_attribute::value_type value_type;
315 typedef typename transform::type transform_attr;
316 value_type made_attr = make_attribute::call(attr);
317 transform_attr attr_ = transform::pre(made_attr);
320 //Creates a place to hold the result of parse_rhs
321 //called inside the following scope.
324 // Create a scope to cause the dbg variable below (within
325 // the #if...#endif) to call it's DTOR before any
326 // modifications are made to the attribute, attr_ passed
327 // to parse_rhs (such as might be done in
328 // traits::post_transform when, for example,
329 // ActualAttribute is a recursive variant).
330 #if defined(BOOST_SPIRIT_X3_DEBUG)
331 context_debug<Iterator, transform_attr>
332 dbg(rule_name, first, last, attr_, ok_parse);
334 ok_parse = parse_rhs(rhs, first, last, context, attr_, attr_
337 && !ExplicitAttrPropagation::value
344 // do up-stream transformation, this integrates the results
345 // back into the original attribute value, if appropriate
346 traits::post_transform(attr, std::forward<transform_attr>(attr_));