1 /*=============================================================================
2 Copyright (c) 2001-2011 Hartmut Kaiser
3 Copyright (c) 2011 Bryce Lelbach
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_QI_BOOL_SEP_29_2009_0709AM)
9 #define SPIRIT_QI_BOOL_SEP_29_2009_0709AM
15 #include <boost/spirit/home/qi/skip_over.hpp>
16 #include <boost/spirit/home/qi/detail/enable_lit.hpp>
17 #include <boost/spirit/home/qi/meta_compiler.hpp>
18 #include <boost/spirit/home/qi/parser.hpp>
19 #include <boost/spirit/home/qi/numeric/bool_policies.hpp>
20 #include <boost/spirit/home/support/common_terminals.hpp>
21 #include <boost/mpl/assert.hpp>
22 #include <boost/detail/workaround.hpp>
23 #include <boost/type_traits/is_same.hpp>
25 namespace boost { namespace spirit
29 ///////////////////////////////////////////////////////////////////////
30 // forward declaration only
34 ///////////////////////////////////////////////////////////////////////
35 // This is the class that the user can instantiate directly in
36 // order to create a customized bool parser
37 template <typename T, typename BoolPolicies = bool_policies<T> >
39 : spirit::terminal<tag::stateful_tag<BoolPolicies, tag::bool_, T> >
41 typedef tag::stateful_tag<BoolPolicies, tag::bool_, T> tag_type;
44 bool_parser(BoolPolicies const& data)
45 : spirit::terminal<tag_type>(data) {}
49 ///////////////////////////////////////////////////////////////////////////
51 ///////////////////////////////////////////////////////////////////////////
52 template <> // enables bool_
53 struct use_terminal<qi::domain, tag::bool_>
56 template <> // enables true_
57 struct use_terminal<qi::domain, tag::true_>
60 template <> // enables false_
61 struct use_terminal<qi::domain, tag::false_>
64 ///////////////////////////////////////////////////////////////////////////
65 template <typename A0> // enables lit(...)
66 struct use_terminal<qi::domain
67 , terminal_ex<tag::lit, fusion::vector1<A0> >
68 , typename enable_if<is_same<A0, bool> >::type>
71 ///////////////////////////////////////////////////////////////////////////
72 template <typename A0> // enables bool_(...)
73 struct use_terminal<qi::domain
74 , terminal_ex<tag::bool_, fusion::vector1<A0> >
77 template <> // enables *lazy* bool_(...)
78 struct use_lazy_terminal<qi::domain, tag::bool_, 1>
81 ///////////////////////////////////////////////////////////////////////////
82 // enables any custom bool_parser
83 template <typename T, typename BoolPolicies>
84 struct use_terminal<qi::domain
85 , tag::stateful_tag<BoolPolicies, tag::bool_, T> >
88 // enables any custom bool_parser(...)
89 template <typename T, typename BoolPolicies, typename A0>
90 struct use_terminal<qi::domain
91 , terminal_ex<tag::stateful_tag<BoolPolicies, tag::bool_, T>
92 , fusion::vector1<A0> > >
95 // enables *lazy* custom bool_parser(...)
96 template <typename T, typename BoolPolicies>
97 struct use_lazy_terminal<
99 , tag::stateful_tag<BoolPolicies, tag::bool_, T>
104 namespace boost { namespace spirit { namespace qi
106 #ifndef BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
109 using spirit::false_;
110 using spirit::lit; // lit(true) is equivalent to true
112 using spirit::bool_type;
113 using spirit::true_type;
114 using spirit::false_type;
115 using spirit::lit_type;
119 template <typename T, typename BoolPolicies>
122 template <typename Iterator, typename Attribute>
123 static bool parse(Iterator& first, Iterator const& last
124 , Attribute& attr, BoolPolicies const& p, bool allow_true = true
125 , bool disallow_false = false)
130 #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1600))
131 p; // suppresses warning: C4100: 'p' : unreferenced formal parameter
133 return (allow_true && p.parse_true(first, last, attr)) ||
134 (!disallow_false && p.parse_false(first, last, attr));
139 ///////////////////////////////////////////////////////////////////////////
140 // This actual boolean parser
141 ///////////////////////////////////////////////////////////////////////////
142 template <typename T, typename BoolPolicies = bool_policies<T> >
143 struct any_bool_parser
144 : primitive_parser<any_bool_parser<T, BoolPolicies> >
146 template <typename Context, typename Iterator>
152 template <typename Iterator, typename Context
153 , typename Skipper, typename Attribute>
154 bool parse(Iterator& first, Iterator const& last
155 , Context& /*context*/, Skipper const& skipper
156 , Attribute& attr_) const
158 typedef detail::bool_impl<T, BoolPolicies> extract;
159 qi::skip_over(first, last, skipper);
160 return extract::parse(first, last, attr_, BoolPolicies());
163 template <typename Context>
164 info what(Context& /*context*/) const
166 return info("boolean");
170 template <typename T, typename BoolPolicies = bool_policies<T>
171 , bool no_attribute = true>
172 struct literal_bool_parser
173 : primitive_parser<literal_bool_parser<T, BoolPolicies, no_attribute> >
175 template <typename Value>
176 literal_bool_parser(Value const& n) : n_(n) {}
178 template <typename Context, typename Iterator>
180 : mpl::if_c<no_attribute, unused_type, T>
183 template <typename Iterator, typename Context
184 , typename Skipper, typename Attribute>
185 bool parse(Iterator& first, Iterator const& last
186 , Context& /*context*/, Skipper const& skipper
187 , Attribute& attr_) const
189 typedef detail::bool_impl<T, BoolPolicies> extract;
190 qi::skip_over(first, last, skipper);
191 return extract::parse(first, last, attr_, BoolPolicies(), n_, n_);
194 template <typename Context>
195 info what(Context& /*context*/) const
197 return info("boolean");
203 ///////////////////////////////////////////////////////////////////////////
204 // Parser generators: make_xxx function (objects)
205 ///////////////////////////////////////////////////////////////////////////
206 template <typename T, typename Modifiers
207 , typename Policies = bool_policies<T> >
210 typedef has_modifier<Modifiers, tag::char_code_base<tag::no_case> >
213 typedef typename mpl::if_<
216 , is_same<bool_policies<T>, Policies>
218 , any_bool_parser<T, no_case_bool_policies<T> >
219 , any_bool_parser<T, Policies> >::type
222 result_type operator()(unused_type, unused_type) const
224 return result_type();
228 template <typename T, typename Modifiers
229 , typename Policies = bool_policies<T> >
230 struct make_direct_bool
232 typedef has_modifier<Modifiers, tag::char_code_base<tag::no_case> >
235 typedef typename mpl::if_<
238 , is_same<bool_policies<T>, Policies>
240 , literal_bool_parser<T, no_case_bool_policies<T>, false>
241 , literal_bool_parser<T, Policies, false> >::type
244 template <typename Terminal>
245 result_type operator()(Terminal const& term, unused_type) const
247 return result_type(fusion::at_c<0>(term.args));
251 template <typename T, typename Modifiers, bool b
252 , typename Policies = bool_policies<T> >
253 struct make_predefined_direct_bool
255 typedef has_modifier<Modifiers, tag::char_code_base<tag::no_case> >
258 typedef typename mpl::if_<
261 , is_same<bool_policies<T>, Policies>
263 , literal_bool_parser<T, no_case_bool_policies<T>, false>
264 , literal_bool_parser<T, Policies, false> >::type
267 result_type operator()(unused_type, unused_type) const
269 return result_type(b);
273 template <typename T, typename Modifiers
274 , typename Policies = bool_policies<T> >
275 struct make_literal_bool
277 typedef has_modifier<Modifiers, tag::char_code_base<tag::no_case> >
280 typedef typename mpl::if_<
283 , is_same<bool_policies<T>, Policies>
285 , literal_bool_parser<T, no_case_bool_policies<T> >
286 , literal_bool_parser<T, Policies> >::type
289 template <typename Terminal>
290 result_type operator()(Terminal const& term, unused_type) const
292 return result_type(fusion::at_c<0>(term.args));
296 ///////////////////////////////////////////////////////////////////////////
297 template <typename Modifiers, typename A0>
298 struct make_primitive<
299 terminal_ex<tag::lit, fusion::vector1<A0> >
300 , Modifiers, typename enable_if<is_same<A0, bool> >::type>
301 : make_literal_bool<bool, Modifiers> {};
303 ///////////////////////////////////////////////////////////////////////////
304 template <typename Modifiers>
305 struct make_primitive<tag::false_, Modifiers>
306 : make_predefined_direct_bool<bool, Modifiers, false>
309 template <typename Modifiers>
310 struct make_primitive<tag::true_, Modifiers>
311 : make_predefined_direct_bool<bool, Modifiers, true>
314 ///////////////////////////////////////////////////////////////////////////
315 template <typename T, typename Policies, typename Modifiers>
316 struct make_primitive<
317 tag::stateful_tag<Policies, tag::bool_, T>, Modifiers>
318 : make_bool<T, Modifiers, Policies> {};
320 template <typename T, typename Policies, typename A0, typename Modifiers>
321 struct make_primitive<
322 terminal_ex<tag::stateful_tag<Policies, tag::bool_, T>
323 , fusion::vector1<A0> >, Modifiers>
324 : make_direct_bool<T, Modifiers, Policies> {};
326 ///////////////////////////////////////////////////////////////////////////
327 template <typename Modifiers>
328 struct make_primitive<tag::bool_, Modifiers>
329 : make_bool<bool, Modifiers> {};
331 template <typename Modifiers, typename A0>
332 struct make_primitive<
333 terminal_ex<tag::bool_
334 , fusion::vector1<A0> >, Modifiers>
335 : make_direct_bool<bool, Modifiers> {};