• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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)
5 
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>
12 
13 #include <boost/metaparse/util/digit_to_int.hpp>
14 
15 #include <boost/mpl/int.hpp>
16 #include <boost/mpl/plus.hpp>
17 #include <boost/mpl/times.hpp>
18 
19 #include <iostream>
20 
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;
26 
27 using boost::metaparse::util::digit_to_int;
28 
29 using boost::mpl::int_;
30 using boost::mpl::plus;
31 using boost::mpl::times;
32 
33 /*
34  * The grammar
35  *
36  * expression ::= ('1' | '0')*
37  */
38 
39 struct next_element
40 {
41   template <class Acc, class B>
42   struct apply : plus<times<Acc, int_<2> >, B> {};
43 };
44 
45 typedef
46   foldl1<transform<one_of_c<'0', '1'>, digit_to_int<> >, int_<0>, next_element>
47   S;
48 
49 typedef build_parser<entire_input<S> > binary_parser;
50 
51 template <class S>
52 struct binary : binary_parser::apply<S>::type {};
53 
54 #ifdef _STR
55 #  error _STR already defined
56 #endif
57 #define _STR BOOST_METAPARSE_STRING
58 
59 #if BOOST_METAPARSE_STD < 2011
60 
main()61 int main()
62 {
63   using std::cout;
64   using std::endl;
65   using boost::metaparse::string;
66 
67   cout
68     << binary<string<'1','0','0'> >::value << endl
69     << binary<string<'1','0','1','1'> >::value << endl
70     << binary<string<'1'> >::value << endl;
71 }
72 #else
main()73 int main()
74 {
75   using std::cout;
76   using std::endl;
77 
78   cout
79     << binary<_STR("100")>::value << endl
80     << binary<_STR("1011")>::value << endl
81     << binary<_STR("1")>::value << endl;
82 }
83 #endif
84 
85 
86