]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/metaparse/example/rational/main.cpp
1 // Copyright Abel Sinkovics (abel@sinkovics.hu) 2015.
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/string.hpp>
7 #include <boost/metaparse/sequence_apply.hpp>
8 #include <boost/metaparse/last_of.hpp>
9 #include <boost/metaparse/int_.hpp>
10 #include <boost/metaparse/token.hpp>
11 #include <boost/metaparse/lit_c.hpp>
12 #include <boost/metaparse/one_of.hpp>
13 #include <boost/metaparse/empty.hpp>
14 #include <boost/metaparse/entire_input.hpp>
15 #include <boost/metaparse/build_parser.hpp>
17 #include <boost/rational.hpp>
19 #include <boost/config.hpp>
21 #include <boost/mpl/int.hpp>
25 using boost::metaparse::sequence_apply2
;
26 using boost::metaparse::last_of
;
27 using boost::metaparse::int_
;
28 using boost::metaparse::token
;
29 using boost::metaparse::lit_c
;
30 using boost::metaparse::one_of
;
31 using boost::metaparse::empty
;
32 using boost::metaparse::entire_input
;
33 using boost::metaparse::build_parser
;
35 template <class Num
, class Denom
>
38 typedef rational type
;
40 static boost::rational
<int> run()
42 return boost::rational
<int>(Num::type::value
, Denom::type::value
);
52 last_of
<lit_c
<'/'>, token
<int_
> >,
53 empty
<boost::mpl::int_
<1> >
58 typedef build_parser
<entire_input
<rational_grammar
> > rational_parser
;
61 # error RATIONAL already defined
64 (::rational_parser::apply<BOOST_METAPARSE_STRING(s)>::type::run())
66 #if BOOST_METAPARSE_STD < 2011
70 std::cout
<< "Please use a compiler that supports constexpr" << std::endl
;
77 const boost::rational
<int> r1
= RATIONAL("1/3");
78 const boost::rational
<int> r2
= RATIONAL("4/4");
79 const boost::rational
<int> r3
= RATIONAL("1");
80 const boost::rational
<int> r4
= RATIONAL("13/11");
82 // Uncommenting the following line generates a compilation error. On a
83 // number of platforms the error report contains the following (or something
85 // x__________________PARSING_FAILED__________________x<1, 3, digit_expected>
86 // where 1, 3 is the location of the error inside the string literal.
87 // const boost::rational<int> r5 = RATIONAL("7/");