main.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #include <boost/metaparse/repeated.hpp>
  6. #include <boost/metaparse/sequence.hpp>
  7. #include <boost/metaparse/lit_c.hpp>
  8. #include <boost/metaparse/last_of.hpp>
  9. #include <boost/metaparse/first_of.hpp>
  10. #include <boost/metaparse/space.hpp>
  11. #include <boost/metaparse/int_.hpp>
  12. #include <boost/metaparse/foldl_reject_incomplete_start_with_parser.hpp>
  13. #include <boost/metaparse/one_of.hpp>
  14. #include <boost/metaparse/get_result.hpp>
  15. #include <boost/metaparse/token.hpp>
  16. #include <boost/metaparse/entire_input.hpp>
  17. #include <boost/metaparse/string.hpp>
  18. #include <boost/metaparse/build_parser.hpp>
  19. #include <boost/mpl/apply_wrap.hpp>
  20. #include <boost/mpl/fold.hpp>
  21. #include <boost/mpl/front.hpp>
  22. #include <boost/mpl/back.hpp>
  23. #include <boost/mpl/plus.hpp>
  24. #include <boost/mpl/minus.hpp>
  25. #include <boost/mpl/times.hpp>
  26. #include <boost/mpl/divides.hpp>
  27. #include <boost/mpl/bool.hpp>
  28. #include <boost/mpl/equal_to.hpp>
  29. #include <boost/mpl/eval_if.hpp>
  30. #include <boost/mpl/bool.hpp>
  31. using boost::metaparse::sequence;
  32. using boost::metaparse::lit_c;
  33. using boost::metaparse::last_of;
  34. using boost::metaparse::first_of;
  35. using boost::metaparse::space;
  36. using boost::metaparse::repeated;
  37. using boost::metaparse::build_parser;
  38. using boost::metaparse::int_;
  39. using boost::metaparse::foldl_reject_incomplete_start_with_parser;
  40. using boost::metaparse::get_result;
  41. using boost::metaparse::one_of;
  42. using boost::metaparse::token;
  43. using boost::metaparse::entire_input;
  44. using boost::mpl::apply_wrap1;
  45. using boost::mpl::fold;
  46. using boost::mpl::front;
  47. using boost::mpl::back;
  48. using boost::mpl::plus;
  49. using boost::mpl::minus;
  50. using boost::mpl::times;
  51. using boost::mpl::divides;
  52. using boost::mpl::eval_if;
  53. using boost::mpl::bool_;
  54. using boost::mpl::equal_to;
  55. using boost::mpl::bool_;
  56. /*
  57. * The grammar
  58. *
  59. * expression ::= plus_exp
  60. * plus_exp ::= prod_exp ((plus_token | minus_token) prod_exp)*
  61. * prod_exp ::= int_token ((mult_token | div_token) int_token)*
  62. */
  63. typedef token<lit_c<'+'> > plus_token;
  64. typedef token<lit_c<'-'> > minus_token;
  65. typedef token<lit_c<'*'> > mult_token;
  66. typedef token<lit_c<'/'> > div_token;
  67. typedef token<int_> int_token;
  68. template <class T, char C>
  69. struct is_c : bool_<T::type::value == C> {};
  70. struct eval_plus
  71. {
  72. template <class State, class C>
  73. struct apply :
  74. eval_if<
  75. is_c<front<C>, '+'>,
  76. plus<typename State::type, typename back<C>::type>,
  77. minus<typename State::type, typename back<C>::type>
  78. >
  79. {};
  80. };
  81. struct eval_mult
  82. {
  83. template <class State, class C>
  84. struct apply :
  85. eval_if<
  86. is_c<front<C>, '*'>,
  87. times<typename State::type, typename back<C>::type>,
  88. divides<typename State::type, typename back<C>::type>
  89. >
  90. {};
  91. };
  92. typedef
  93. foldl_reject_incomplete_start_with_parser<
  94. sequence<one_of<mult_token, div_token>, int_token>,
  95. int_token,
  96. eval_mult
  97. >
  98. prod_exp;
  99. typedef
  100. foldl_reject_incomplete_start_with_parser<
  101. sequence<one_of<plus_token, minus_token>, prod_exp>,
  102. prod_exp,
  103. eval_plus
  104. >
  105. plus_exp;
  106. typedef last_of<repeated<space>, plus_exp> expression;
  107. typedef build_parser<entire_input<expression> > calculator_parser;
  108. #ifdef _STR
  109. # error _STR already defined
  110. #endif
  111. #define _STR BOOST_METAPARSE_STRING
  112. #if BOOST_METAPARSE_STD < 2011
  113. int main()
  114. {
  115. using std::cout;
  116. using std::endl;
  117. using boost::metaparse::string;
  118. cout
  119. << apply_wrap1<calculator_parser, string<'1','3'> >::type::value << endl
  120. <<
  121. apply_wrap1<
  122. calculator_parser, string<' ','1','+',' ','2','*','4','-','6','/','2'>
  123. >::type::value
  124. << endl
  125. ;
  126. }
  127. #else
  128. int main()
  129. {
  130. using std::cout;
  131. using std::endl;
  132. cout
  133. << apply_wrap1<calculator_parser, _STR("13")>::type::value << endl
  134. << apply_wrap1<calculator_parser, _STR(" 1+ 2*4-6/2")>::type::value << endl
  135. ;
  136. }
  137. #endif