]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/metaparse/example/binary_number/main.cpp
1 // Copyright Abel Sinkovics (abel@sinkovics.hu) 2011.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
6 #include <boost/metaparse/foldl1.hpp>
7 #include <boost/metaparse/build_parser.hpp>
8 #include <boost/metaparse/transform.hpp>
9 #include <boost/metaparse/one_of_c.hpp>
10 #include <boost/metaparse/entire_input.hpp>
11 #include <boost/metaparse/string.hpp>
13 #include <boost/metaparse/util/digit_to_int.hpp>
15 #include <boost/mpl/int.hpp>
16 #include <boost/mpl/plus.hpp>
17 #include <boost/mpl/times.hpp>
21 using boost::metaparse::foldl1
;
22 using boost::metaparse::build_parser
;
23 using boost::metaparse::transform
;
24 using boost::metaparse::one_of_c
;
25 using boost::metaparse::entire_input
;
27 using boost::metaparse::util::digit_to_int
;
29 using boost::mpl::int_
;
30 using boost::mpl::plus
;
31 using boost::mpl::times
;
36 * expression ::= ('1' | '0')*
41 template <class Acc
, class B
>
42 struct apply
: plus
<times
<Acc
, int_
<2> >, B
> {};
46 foldl1
<transform
<one_of_c
<'0', '1'>, digit_to_int
<> >, int_
<0>, next_element
>
49 typedef build_parser
<entire_input
<S
> > binary_parser
;
52 struct binary
: binary_parser::apply
<S
>::type
{};
55 # error _STR already defined
57 #define _STR BOOST_METAPARSE_STRING
59 #if BOOST_METAPARSE_STD < 2011
65 using boost::metaparse::string
;
68 << binary
<string
<'1','0','0'> >::value
<< endl
69 << binary
<string
<'1','0','1','1'> >::value
<< endl
70 << binary
<string
<'1'> >::value
<< endl
;
79 << binary
<_STR("100")>::value
<< endl
80 << binary
<_STR("1011")>::value
<< endl
81 << binary
<_STR("1")>::value
<< endl
;