main.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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)
  5. #include <boost/metaparse/string.hpp>
  6. #include <boost/metaparse/sequence_apply.hpp>
  7. #include <boost/metaparse/last_of.hpp>
  8. #include <boost/metaparse/int_.hpp>
  9. #include <boost/metaparse/token.hpp>
  10. #include <boost/metaparse/lit_c.hpp>
  11. #include <boost/metaparse/one_of.hpp>
  12. #include <boost/metaparse/empty.hpp>
  13. #include <boost/metaparse/entire_input.hpp>
  14. #include <boost/metaparse/build_parser.hpp>
  15. #include <boost/rational.hpp>
  16. #include <boost/config.hpp>
  17. #include <boost/mpl/int.hpp>
  18. #include <iostream>
  19. using boost::metaparse::sequence_apply2;
  20. using boost::metaparse::last_of;
  21. using boost::metaparse::int_;
  22. using boost::metaparse::token;
  23. using boost::metaparse::lit_c;
  24. using boost::metaparse::one_of;
  25. using boost::metaparse::empty;
  26. using boost::metaparse::entire_input;
  27. using boost::metaparse::build_parser;
  28. template <class Num, class Denom>
  29. struct rational
  30. {
  31. typedef rational type;
  32. static boost::rational<int> run()
  33. {
  34. return boost::rational<int>(Num::type::value, Denom::type::value);
  35. }
  36. };
  37. typedef
  38. sequence_apply2<
  39. rational,
  40. token<int_>,
  41. one_of<
  42. last_of<lit_c<'/'>, token<int_> >,
  43. empty<boost::mpl::int_<1> >
  44. >
  45. >
  46. rational_grammar;
  47. typedef build_parser<entire_input<rational_grammar> > rational_parser;
  48. #ifdef RATIONAL
  49. # error RATIONAL already defined
  50. #endif
  51. #define RATIONAL(s) \
  52. (::rational_parser::apply<BOOST_METAPARSE_STRING(s)>::type::run())
  53. #if BOOST_METAPARSE_STD < 2011
  54. int main()
  55. {
  56. std::cout << "Please use a compiler that supports constexpr" << std::endl;
  57. }
  58. #else
  59. int main()
  60. {
  61. const boost::rational<int> r1 = RATIONAL("1/3");
  62. const boost::rational<int> r2 = RATIONAL("4/4");
  63. const boost::rational<int> r3 = RATIONAL("1");
  64. const boost::rational<int> r4 = RATIONAL("13/11");
  65. // Uncommenting the following line generates a compilation error. On a
  66. // number of platforms the error report contains the following (or something
  67. // similar):
  68. // x__________________PARSING_FAILED__________________x<1, 3, digit_expected>
  69. // where 1, 3 is the location of the error inside the string literal.
  70. // const boost::rational<int> r5 = RATIONAL("7/");
  71. std::cout
  72. << r1 << std::endl
  73. << r2 << std::endl
  74. << r3 << std::endl
  75. << r4 << std::endl;
  76. }
  77. #endif