1 /*=============================================================================
2 Copyright (c) 2009 Hartmut Kaiser
3 Copyright (c) 2014 Joel de Guzman
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 ==============================================================================*/
8 #if !defined(SPIRIT_X3_BOOL_SEP_29_2009_0709AM)
9 #define SPIRIT_X3_BOOL_SEP_29_2009_0709AM
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/numeric/bool_policies.hpp>
15 namespace boost { namespace spirit { namespace x3
17 template <typename T, typename Encoding, typename BoolPolicies = bool_policies<T>>
18 struct bool_parser : parser<bool_parser<T, Encoding, BoolPolicies>>
20 typedef Encoding encoding;
21 typedef T attribute_type;
22 static bool const has_attribute = true;
27 bool_parser(BoolPolicies const& policies)
28 : policies(policies) {}
30 template <typename Iterator, typename Context>
31 bool parse(Iterator& first, Iterator const& last
32 , Context const& context, unused_type, T& attr) const
34 x3::skip_over(first, last, context);
35 return policies.parse_true(first, last, attr, get_case_compare<encoding>(context))
36 || policies.parse_false(first, last, attr, get_case_compare<encoding>(context));
39 template <typename Iterator, typename Context, typename Attribute>
40 bool parse(Iterator& first, Iterator const& last
41 , Context const& context, unused_type, Attribute& attr_param) const
43 // this case is called when Attribute is not T
45 if (parse(first, last, context, unused, attr_))
47 traits::move_to(attr_, attr_param);
53 BoolPolicies policies;
56 template <typename T, typename Encoding, typename BoolPolicies = bool_policies<T>>
57 struct literal_bool_parser : parser<bool_parser<T, Encoding, BoolPolicies>>
59 typedef Encoding encoding;
60 typedef T attribute_type;
61 static bool const has_attribute = true;
63 template <typename Value>
64 literal_bool_parser(Value const& n)
65 : policies(), n_(n) {}
67 template <typename Value>
68 literal_bool_parser(Value const& n, BoolPolicies const& policies)
69 : policies(policies), n_(n) {}
71 template <typename Iterator, typename Context>
72 bool parse_main(Iterator& first, Iterator const& last
73 , Context& context, T& attr) const
75 x3::skip_over(first, last, context);
76 return (n_ && policies.parse_true(first, last, attr, get_case_compare<encoding>(context)))
77 || (!n_ && policies.parse_false(first, last, attr, get_case_compare<encoding>(context)));
80 template <typename Iterator, typename Context>
81 bool parse(Iterator& first, Iterator const& last
82 , Context& context, unused_type, T& attr) const
84 return parse_main(first, last, context, attr);
87 template <typename Iterator, typename Context, typename Attribute>
88 bool parse(Iterator& first, Iterator const& last
89 , Context const& context, unused_type, Attribute& attr_param) const
91 // this case is called when Attribute is not T
93 if (parse_main(first, last, context, attr_))
95 traits::move_to(attr_, attr_param);
101 BoolPolicies policies;
107 typedef bool_parser<bool, char_encoding::standard> bool_type;
108 bool_type const bool_ = {};
110 typedef literal_bool_parser<bool, char_encoding::standard> true_type;
111 true_type const true_ = { true };
113 typedef literal_bool_parser<bool, char_encoding::standard> false_type;
114 false_type const false_ = { false };
117 namespace standard_wide
119 typedef bool_parser<bool, char_encoding::standard_wide> bool_type;
120 bool_type const bool_ = {};
122 typedef literal_bool_parser<bool, char_encoding::standard_wide> true_type;
123 true_type const true_ = { true };
125 typedef literal_bool_parser<bool, char_encoding::standard_wide> false_type;
126 false_type const false_ = { false };
131 typedef bool_parser<bool, char_encoding::ascii> bool_type;
132 bool_type const bool_ = {};
134 typedef literal_bool_parser<bool, char_encoding::ascii> true_type;
135 true_type const true_ = { true };
137 typedef literal_bool_parser<bool, char_encoding::ascii> false_type;
138 false_type const false_ = { false };
143 typedef bool_parser<bool, char_encoding::iso8859_1> bool_type;
144 bool_type const bool_ = {};
146 typedef literal_bool_parser<bool, char_encoding::iso8859_1> true_type;
147 true_type const true_ = { true };
149 typedef literal_bool_parser<bool, char_encoding::iso8859_1> false_type;
150 false_type const false_ = { false };
153 using standard::bool_;
154 using standard::true_;
155 using standard::false_;