Chris@102
|
1 /*=============================================================================
|
Chris@102
|
2 Copyright (c) 2001-2014 Joel de Guzman
|
Chris@102
|
3
|
Chris@102
|
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@102
|
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@102
|
6 =============================================================================*/
|
Chris@102
|
7 #if !defined(BOOST_SPIRIT_X3_GUARD_FERBRUARY_02_2013_0649PM)
|
Chris@102
|
8 #define BOOST_SPIRIT_X3_GUARD_FERBRUARY_02_2013_0649PM
|
Chris@102
|
9
|
Chris@102
|
10 #if defined(_MSC_VER)
|
Chris@102
|
11 #pragma once
|
Chris@102
|
12 #endif
|
Chris@102
|
13
|
Chris@102
|
14 #include <boost/spirit/home/x3/support/context.hpp>
|
Chris@102
|
15 #include <boost/spirit/home/x3/directive/expect.hpp>
|
Chris@102
|
16
|
Chris@102
|
17 namespace boost { namespace spirit { namespace x3
|
Chris@102
|
18 {
|
Chris@102
|
19 enum class error_handler_result
|
Chris@102
|
20 {
|
Chris@102
|
21 fail
|
Chris@102
|
22 , retry
|
Chris@102
|
23 , accept
|
Chris@102
|
24 , rethrow
|
Chris@102
|
25 };
|
Chris@102
|
26
|
Chris@102
|
27 template <typename Subject, typename Handler>
|
Chris@102
|
28 struct guard : unary_parser<Subject, guard<Subject, Handler>>
|
Chris@102
|
29 {
|
Chris@102
|
30 typedef unary_parser<Subject, guard<Subject, Handler>> base_type;
|
Chris@102
|
31 static bool const is_pass_through_unary = true;
|
Chris@102
|
32
|
Chris@102
|
33 guard(Subject const& subject, Handler handler)
|
Chris@102
|
34 : base_type(subject), handler(handler) {}
|
Chris@102
|
35
|
Chris@102
|
36 template <typename Iterator, typename Context
|
Chris@102
|
37 , typename RuleContext, typename Attribute>
|
Chris@102
|
38 bool parse(Iterator& first, Iterator const& last
|
Chris@102
|
39 , Context const& context, RuleContext& rcontext, Attribute& attr) const
|
Chris@102
|
40 {
|
Chris@102
|
41 for (;;)
|
Chris@102
|
42 {
|
Chris@102
|
43 try
|
Chris@102
|
44 {
|
Chris@102
|
45 Iterator i = first;
|
Chris@102
|
46 bool r = this->subject.parse(i, last, context, rcontext, attr);
|
Chris@102
|
47 if (r)
|
Chris@102
|
48 first = i;
|
Chris@102
|
49 return r;
|
Chris@102
|
50 }
|
Chris@102
|
51 catch (expectation_failure<Iterator> const& x)
|
Chris@102
|
52 {
|
Chris@102
|
53 switch (handler(first, last, x, context))
|
Chris@102
|
54 {
|
Chris@102
|
55 case error_handler_result::fail:
|
Chris@102
|
56 return false;
|
Chris@102
|
57 case error_handler_result::retry:
|
Chris@102
|
58 continue;
|
Chris@102
|
59 case error_handler_result::accept:
|
Chris@102
|
60 return true;
|
Chris@102
|
61 case error_handler_result::rethrow:
|
Chris@102
|
62 throw;
|
Chris@102
|
63 }
|
Chris@102
|
64 }
|
Chris@102
|
65 }
|
Chris@102
|
66 return false;
|
Chris@102
|
67 }
|
Chris@102
|
68
|
Chris@102
|
69 Handler handler;
|
Chris@102
|
70 };
|
Chris@102
|
71 }}}
|
Chris@102
|
72
|
Chris@102
|
73 #endif
|