main.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright Abel Sinkovics (abel@sinkovics.hu) 2014.
  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/middle_of.hpp>
  11. #include <boost/metaparse/space.hpp>
  12. #include <boost/metaparse/int_.hpp>
  13. #include <boost/metaparse/foldl_reject_incomplete_start_with_parser.hpp>
  14. #include <boost/metaparse/foldr_start_with_parser.hpp>
  15. #include <boost/metaparse/one_of.hpp>
  16. #include <boost/metaparse/get_result.hpp>
  17. #include <boost/metaparse/token.hpp>
  18. #include <boost/metaparse/entire_input.hpp>
  19. #include <boost/metaparse/string.hpp>
  20. #include <boost/metaparse/build_parser.hpp>
  21. #include <boost/mpl/apply_wrap.hpp>
  22. #include <boost/mpl/fold.hpp>
  23. #include <boost/mpl/front.hpp>
  24. #include <boost/mpl/back.hpp>
  25. #include <boost/mpl/plus.hpp>
  26. #include <boost/mpl/minus.hpp>
  27. #include <boost/mpl/times.hpp>
  28. #include <boost/mpl/divides.hpp>
  29. #include <boost/mpl/bool.hpp>
  30. #include <boost/mpl/equal_to.hpp>
  31. #include <boost/mpl/eval_if.hpp>
  32. #include <boost/mpl/bool.hpp>
  33. #include <boost/mpl/negate.hpp>
  34. #include <boost/mpl/char.hpp>
  35. using boost::metaparse::sequence;
  36. using boost::metaparse::lit_c;
  37. using boost::metaparse::last_of;
  38. using boost::metaparse::first_of;
  39. using boost::metaparse::middle_of;
  40. using boost::metaparse::space;
  41. using boost::metaparse::repeated;
  42. using boost::metaparse::build_parser;
  43. using boost::metaparse::int_;
  44. using boost::metaparse::foldl_reject_incomplete_start_with_parser;
  45. using boost::metaparse::foldr_start_with_parser;
  46. using boost::metaparse::get_result;
  47. using boost::metaparse::one_of;
  48. using boost::metaparse::token;
  49. using boost::metaparse::entire_input;
  50. using boost::mpl::apply_wrap1;
  51. using boost::mpl::fold;
  52. using boost::mpl::front;
  53. using boost::mpl::back;
  54. using boost::mpl::plus;
  55. using boost::mpl::minus;
  56. using boost::mpl::times;
  57. using boost::mpl::divides;
  58. using boost::mpl::eval_if;
  59. using boost::mpl::bool_;
  60. using boost::mpl::equal_to;
  61. using boost::mpl::bool_;
  62. using boost::mpl::negate;
  63. using boost::mpl::char_;
  64. /*
  65. * The grammar
  66. *
  67. * expression ::= plus_exp
  68. * plus_exp ::= prod_exp ((plus_token | minus_token) prod_exp)*
  69. * prod_exp ::= int_token ((mult_token | div_token) simple_exp)*
  70. * simple_exp ::= (plus_token | minus_token)* (int_token | paren_exp)
  71. * paren_exp ::= open_paren_token expression close_paren_token
  72. */
  73. typedef token<lit_c<'+'> > plus_token;
  74. typedef token<lit_c<'-'> > minus_token;
  75. typedef token<lit_c<'*'> > mult_token;
  76. typedef token<lit_c<'/'> > div_token;
  77. typedef token<lit_c<'('> > open_paren_token;
  78. typedef token<lit_c<')'> > close_paren_token;
  79. typedef token<int_> int_token;
  80. template <class T, char C>
  81. struct is_c : bool_<T::type::value == C> {};
  82. struct eval_plus
  83. {
  84. template <class State, class C>
  85. struct apply :
  86. eval_if<
  87. is_c<front<C>, '+'>,
  88. plus<typename State::type, typename back<C>::type>,
  89. minus<typename State::type, typename back<C>::type>
  90. >
  91. {};
  92. };
  93. struct eval_mult
  94. {
  95. template <class State, class C>
  96. struct apply :
  97. eval_if<
  98. is_c<front<C>, '*'>,
  99. times<typename State::type, typename back<C>::type>,
  100. divides<typename State::type, typename back<C>::type>
  101. >
  102. {};
  103. };
  104. struct eval_unary_plus
  105. {
  106. template <class State, class C>
  107. struct apply :
  108. eval_if<
  109. typename equal_to<char_<'+'>, typename C::type>::type,
  110. State, // +State is State
  111. negate<typename State::type>
  112. >
  113. {};
  114. };
  115. struct plus_exp;
  116. typedef middle_of<open_paren_token, plus_exp, close_paren_token> paren_exp;
  117. typedef
  118. foldr_start_with_parser<
  119. one_of<plus_token, minus_token>,
  120. one_of<int_token, paren_exp>,
  121. eval_unary_plus
  122. >
  123. simple_exp;
  124. typedef
  125. foldl_reject_incomplete_start_with_parser<
  126. sequence<one_of<mult_token, div_token>, simple_exp>,
  127. simple_exp,
  128. eval_mult
  129. >
  130. prod_exp;
  131. struct plus_exp :
  132. foldl_reject_incomplete_start_with_parser<
  133. sequence<one_of<plus_token, minus_token>, prod_exp>,
  134. prod_exp,
  135. eval_plus
  136. >
  137. {};
  138. typedef last_of<repeated<space>, plus_exp> expression;
  139. typedef build_parser<entire_input<expression> > calculator_parser;
  140. #ifdef _STR
  141. # error _STR already defined
  142. #endif
  143. #define _STR BOOST_METAPARSE_STRING
  144. #if BOOST_METAPARSE_STD < 2011
  145. int main()
  146. {
  147. std::cout << "Please use a compiler that support constexpr" << std::endl;
  148. }
  149. #else
  150. int main()
  151. {
  152. using std::cout;
  153. using std::endl;
  154. cout
  155. << apply_wrap1<calculator_parser, _STR("13")>::type::value << endl
  156. << apply_wrap1<calculator_parser, _STR(" 1+ 2*4-6/2")>::type::value << endl
  157. << apply_wrap1<calculator_parser, _STR("1+2*3+4")>::type::value << endl
  158. << apply_wrap1<calculator_parser, _STR("(1+2)*(3+4)")>::type::value << endl
  159. << apply_wrap1<calculator_parser, _STR("(2*3)+(4*5)")>::type::value << endl
  160. << apply_wrap1<calculator_parser, _STR("2 * 3 + 4")>::type::value << endl
  161. << apply_wrap1<calculator_parser, _STR("2 + (3 * 4)")>::type::value << endl
  162. << apply_wrap1<calculator_parser, _STR("+3")>::type::value << endl
  163. << apply_wrap1<calculator_parser, _STR("-21")>::type::value << endl
  164. << apply_wrap1<calculator_parser, _STR("24 + - 21")>::type::value << endl
  165. << apply_wrap1<calculator_parser, _STR("- - 21")>::type::value << endl
  166. << apply_wrap1<calculator_parser, _STR("-(3 * 4)")>::type::value << endl
  167. ;
  168. }
  169. #endif