before_5_2_3.qbk 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. [#before_5_2_3]
  2. ['Definitions before section 5.2.3.]
  3. #include <boost/metaparse/string.hpp>
  4. #include <boost/metaparse/int_.hpp>
  5. #include <boost/metaparse/build_parser.hpp>
  6. using namespace boost::metaparse;
  7. using exp_parser1 = build_parser<int_>;
  8. #include <boost/metaparse/entire_input.hpp>
  9. using exp_parser2 = build_parser<entire_input<int_>>;
  10. #include <boost/metaparse/token.hpp>
  11. using exp_parser3 = build_parser<entire_input<token<int_>>>;
  12. #include <boost/metaparse/lit_c.hpp>
  13. #include <boost/metaparse/sequence.hpp>
  14. using exp_parser4 = build_parser<sequence<token<int_>, token<lit_c<'+'>>, token<int_>>>;
  15. #include <metashell/formatter.hpp>
  16. using int_token = token<int_>;
  17. using plus_token = token<lit_c<'+'>>;
  18. using exp_parser5 = build_parser<sequence<int_token, plus_token, int_token>>;
  19. #include <boost/metaparse/transform.hpp>
  20. #include <boost/mpl/plus.hpp>
  21. #include <boost/mpl/at.hpp>
  22. template <class Vector>
  23. struct eval_plus :
  24. boost::mpl::plus<
  25. typename boost::mpl::at_c<Vector, 0>::type,
  26. typename boost::mpl::at_c<Vector, 2>::type
  27. > {};
  28. #include <boost/mpl/quote.hpp>
  29. using exp_parser6 =
  30. build_parser<
  31. transform<
  32. sequence<int_token, plus_token, int_token>,
  33. boost::mpl::quote1<eval_plus>
  34. >
  35. >;
  36. #include <boost/metaparse/any.hpp>
  37. using exp_parser7 =
  38. build_parser<
  39. sequence<
  40. int_token, /* The first <number> */
  41. repeated<sequence<plus_token, int_token>> /* The "+ <number>" elements */
  42. >
  43. >;
  44. using temp_result = exp_parser7::apply<BOOST_METAPARSE_STRING("1 + 2 + 3 + 4")>::type;
  45. #include <boost/mpl/fold.hpp>
  46. using vector_of_numbers =
  47. boost::mpl::vector<
  48. boost::mpl::int_<2>,
  49. boost::mpl::int_<5>,
  50. boost::mpl::int_<6>
  51. >;
  52. template <class Vector>
  53. struct sum_vector :
  54. boost::mpl::fold<
  55. Vector,
  56. boost::mpl::int_<0>,
  57. boost::mpl::lambda<
  58. boost::mpl::plus<boost::mpl::_1, boost::mpl::_2>
  59. >::type
  60. >
  61. {};
  62. template <class Sum, class Item>
  63. struct sum_items :
  64. boost::mpl::plus<
  65. Sum,
  66. typename boost::mpl::at_c<Item, 1>::type
  67. >
  68. {};
  69. using exp_parser8 =
  70. build_parser<
  71. sequence<
  72. int_token, /* parse the first <number> */
  73. transform<
  74. repeated<sequence<plus_token, int_token>>, /* parse the "+ <number>" elements */
  75. /* lambda expression summarising the "+ <number>" elements using fold */
  76. boost::mpl::lambda<
  77. /* The folding expression we have just created */
  78. boost::mpl::fold<
  79. boost::mpl::_1, /* the argument of the lambda expression, the result */
  80. /* of the repeated<...> parser */
  81. boost::mpl::int_<0>,
  82. boost::mpl::quote2<sum_items>
  83. >
  84. >::type
  85. >
  86. >
  87. >;
  88. using exp_parser9 =
  89. build_parser<
  90. transform<
  91. /* What we had so far */
  92. sequence<
  93. int_token,
  94. transform<
  95. repeated<sequence<plus_token, int_token>>,
  96. boost::mpl::lambda<
  97. boost::mpl::fold<
  98. boost::mpl::_1,
  99. boost::mpl::int_<0>,
  100. boost::mpl::quote2<sum_items>
  101. >
  102. >::type
  103. >
  104. >,
  105. boost::mpl::quote1<sum_vector> /* summarise the vector of numbers */
  106. >
  107. >;