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/spirit/home/x3/auxiliary/guard.hpp>
11 #include <boost/spirit/home/x3/core/parser.hpp>
12 #include <boost/spirit/home/x3/core/skip_over.hpp>
13 #include <boost/spirit/home/x3/directive/expect.hpp>
14 #include <boost/spirit/home/x3/support/traits/make_attribute.hpp>
15 #include <boost/spirit/home/x3/support/utility/sfinae.hpp>
16 #include <boost/spirit/home/x3/nonterminal/detail/transform_attribute.hpp>
17 #include <boost/utility/addressof.hpp>
19 #if defined(BOOST_SPIRIT_X3_DEBUG)
20 #include <boost/spirit/home/x3/nonterminal/simple_trace.hpp>
23 namespace boost { namespace spirit { namespace x3
25 template <typename ID>
28 template <typename ID, typename Attribute = unused_type, bool force_attribute = false>
31 struct parse_pass_context_tag;
35 // we use this so we can detect if the default parse_rule
36 // is the being called.
37 struct default_parse_rule_result
39 default_parse_rule_result(bool r)
41 operator bool() const { return r; }
46 // default parse_rule implementation
47 template <typename ID, typename Attribute, typename Iterator
48 , typename Context, typename ActualAttribute>
49 inline detail::default_parse_rule_result
51 rule<ID, Attribute> rule_
52 , Iterator& first, Iterator const& last
53 , Context const& context, ActualAttribute& attr);
56 namespace boost { namespace spirit { namespace x3 { namespace detail
58 #if defined(BOOST_SPIRIT_X3_DEBUG)
59 template <typename Iterator, typename Attribute>
64 , Iterator const& first, Iterator const& last
65 , Attribute const& attr
66 , bool const& ok_parse //was parse successful?
68 : ok_parse(ok_parse), rule_name(rule_name)
69 , first(first), last(last)
71 , f(detail::get_simple_trace())
73 f(first, last, attr, pre_parse, rule_name);
78 auto status = ok_parse ? successful_parse : failed_parse ;
79 f(first, last, attr, status, rule_name);
83 char const* rule_name;
84 Iterator const& first;
86 Attribute const& attr;
87 detail::simple_trace_type& f;
91 template <typename ID, typename Iterator, typename Context, typename Enable = void>
92 struct has_on_error : mpl::false_ {};
94 template <typename ID, typename Iterator, typename Context>
95 struct has_on_error<ID, Iterator, Context,
96 typename disable_if_substitution_failure<
98 std::declval<ID>().on_error(
99 std::declval<Iterator&>()
100 , std::declval<Iterator>()
101 , std::declval<expectation_failure<Iterator>>()
102 , std::declval<Context>()
109 template <typename ID, typename Iterator, typename Attribute, typename Context, typename Enable = void>
110 struct has_on_success : mpl::false_ {};
112 template <typename ID, typename Iterator, typename Attribute, typename Context>
113 struct has_on_success<ID, Iterator, Context, Attribute,
114 typename disable_if_substitution_failure<
116 std::declval<ID>().on_success(
117 std::declval<Iterator&>()
118 , std::declval<Iterator>()
119 , std::declval<Attribute&>()
120 , std::declval<Context>()
127 template <typename ID>
130 typedef identity<ID> type;
133 template <typename ID>
134 struct make_id<identity<ID>>
136 typedef identity<ID> type;
139 template <typename ID, typename RHS, typename Context>
141 make_rule_context(RHS const& rhs, Context const& context
142 , mpl::false_ /* is_default_parse_rule */)
147 template <typename ID, typename RHS, typename Context>
148 auto make_rule_context(RHS const& rhs, Context const& context
149 , mpl::true_ /* is_default_parse_rule */ )
151 return make_unique_context<ID>(rhs, context);
154 template <typename Attribute, typename ID>
157 template <typename Iterator, typename Context, typename ActualAttribute>
158 static bool call_on_success(
159 Iterator& first, Iterator const& last
160 , Context const& context, ActualAttribute& attr
161 , mpl::false_ /* No on_success handler */ )
166 template <typename Iterator, typename Context, typename ActualAttribute>
167 static bool call_on_success(
168 Iterator& first, Iterator const& last
169 , Context const& context, ActualAttribute& attr
170 , mpl::true_ /* Has on_success handler */)
177 , make_context<parse_pass_context_tag>(pass, context)
182 template <typename RHS, typename Iterator, typename Context
183 , typename RContext, typename ActualAttribute>
184 static bool parse_rhs_main(
186 , Iterator& first, Iterator const& last
187 , Context const& context, RContext& rcontext, ActualAttribute& attr
190 // see if the user has a BOOST_SPIRIT_DEFINE for this rule
193 rule<ID, Attribute>(), first, last
194 , make_unique_context<ID>(rhs, context), attr))
197 // If there is no BOOST_SPIRIT_DEFINE for this rule,
198 // we'll make a context for this rule tagged by its ID
199 // so we can extract the rule later on in the default
200 // (generic) parse_rule function.
202 is_same<parse_rule_result, default_parse_rule_result>
203 is_default_parse_rule;
209 , make_rule_context<ID>(rhs, context, is_default_parse_rule())
217 x3::skip_over(first_, last, context);
218 r = call_on_success(first_, i, context, attr
219 , has_on_success<ID, Iterator, Context, ActualAttribute>());
227 template <typename RHS, typename Iterator, typename Context
228 , typename RContext, typename ActualAttribute>
229 static bool parse_rhs_main(
231 , Iterator& first, Iterator const& last
232 , Context const& context, RContext& rcontext, ActualAttribute& attr
233 , mpl::true_ /* on_error is found */)
239 return parse_rhs_main(
240 rhs, first, last, context, rcontext, attr, mpl::false_());
242 catch (expectation_failure<Iterator> const& x)
244 switch (ID().on_error(first, last, x, context))
246 case error_handler_result::fail:
248 case error_handler_result::retry:
250 case error_handler_result::accept:
252 case error_handler_result::rethrow:
259 template <typename RHS, typename Iterator
260 , typename Context, typename RContext, typename ActualAttribute>
261 static bool parse_rhs_main(
263 , Iterator& first, Iterator const& last
264 , Context const& context, RContext& rcontext, ActualAttribute& attr)
266 return parse_rhs_main(
267 rhs, first, last, context, rcontext, attr
268 , has_on_error<ID, Iterator, Context>()
272 template <typename RHS, typename Iterator
273 , typename Context, typename RContext, typename ActualAttribute>
274 static bool parse_rhs(
276 , Iterator& first, Iterator const& last
277 , Context const& context, RContext& rcontext, ActualAttribute& attr
280 return parse_rhs_main(rhs, first, last, context, rcontext, attr);
283 template <typename RHS, typename Iterator
284 , typename Context, typename RContext, typename ActualAttribute>
285 static bool parse_rhs(
287 , Iterator& first, Iterator const& last
288 , Context const& context, RContext& rcontext, ActualAttribute& attr
291 return parse_rhs_main(rhs, first, last, context, rcontext, unused);
294 template <typename RHS, typename Iterator, typename Context
295 , typename ActualAttribute, typename ExplicitAttrPropagation>
296 static bool call_rule_definition(
298 , char const* rule_name
299 , Iterator& first, Iterator const& last
300 , Context const& context, ActualAttribute& attr
301 , ExplicitAttrPropagation)
303 typedef traits::make_attribute<Attribute, ActualAttribute> make_attribute;
305 // do down-stream transformation, provides attribute for
307 typedef traits::transform_attribute<
308 typename make_attribute::type, Attribute, parser_id>
311 typedef typename make_attribute::value_type value_type;
312 typedef typename transform::type transform_attr;
313 value_type made_attr = make_attribute::call(attr);
314 transform_attr attr_ = transform::pre(made_attr);
317 //Creates a place to hold the result of parse_rhs
318 //called inside the following scope.
321 // Create a scope to cause the dbg variable below (within
322 // the #if...#endif) to call it's DTOR before any
323 // modifications are made to the attribute, attr_ passed
324 // to parse_rhs (such as might be done in
325 // traits::post_transform when, for example,
326 // ActualAttribute is a recursive variant).
327 #if defined(BOOST_SPIRIT_X3_DEBUG)
328 typedef typename make_attribute::type dbg_attribute_type;
329 context_debug<Iterator, dbg_attribute_type>
330 dbg(rule_name, first, last, dbg_attribute_type(attr_), ok_parse);
332 ok_parse = parse_rhs(rhs, first, last, context, attr_, attr_
335 && !ExplicitAttrPropagation::value
342 // do up-stream transformation, this integrates the results
343 // back into the original attribute value, if appropriate
344 traits::post_transform(attr, std::forward<transform_attr>(attr_));