before_12.qbk 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. [#before_12]
  2. ['Definitions before section 12.]
  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. >;
  108. #include <boost/metaparse/foldl.hpp>
  109. using exp_parser10 =
  110. build_parser<
  111. transform<
  112. sequence<
  113. int_token,
  114. foldl<
  115. sequence<plus_token, int_token>,
  116. boost::mpl::int_<0>,
  117. boost::mpl::quote2<sum_items>
  118. >
  119. >,
  120. boost::mpl::quote1<sum_vector>>
  121. >;
  122. #include <boost/metaparse/foldl_start_with_parser.hpp>
  123. using exp_parser11 =
  124. build_parser<
  125. foldl_start_with_parser<
  126. sequence<plus_token, int_token>, /* apply this parser repeatedly */
  127. int_token, /* use this parser to get the initial value */
  128. boost::mpl::quote2<sum_items> /* use this function to add a new value to the summary */
  129. >
  130. >;
  131. using minus_token = token<lit_c<'-'>>;
  132. #include <boost/metaparse/one_of.hpp>
  133. using exp_parser12 =
  134. build_parser<
  135. foldl_start_with_parser<
  136. sequence<one_of<plus_token, minus_token>, int_token>,
  137. int_token,
  138. boost::mpl::quote2<sum_items>
  139. >
  140. >;
  141. #include <boost/mpl/minus.hpp>
  142. template <class L, char Op, class R> struct eval_binary_op;
  143. template <class L, class R> struct eval_binary_op<L, '+', R> : boost::mpl::plus<L, R>::type {};
  144. template <class L, class R> struct eval_binary_op<L, '-', R> : boost::mpl::minus<L, R>::type {};
  145. template <class S, class Item>
  146. struct binary_op :
  147. eval_binary_op<
  148. S,
  149. boost::mpl::at_c<Item, 0>::type::value,
  150. typename boost::mpl::at_c<Item, 1>::type
  151. >
  152. {};
  153. using exp_parser13 =
  154. build_parser<
  155. foldl_start_with_parser<
  156. sequence<one_of<plus_token, minus_token>, int_token>,
  157. int_token,
  158. boost::mpl::quote2<binary_op>
  159. >
  160. >;
  161. #include <boost/mpl/times.hpp>
  162. template <class L, class R> struct eval_binary_op<L, '*', R> : boost::mpl::times<L, R>::type {};
  163. using times_token = token<lit_c<'*'>>;
  164. using exp_parser14 =
  165. build_parser<
  166. foldl_start_with_parser<
  167. sequence<one_of<plus_token, minus_token, times_token>, int_token>,
  168. int_token,
  169. boost::mpl::quote2<binary_op>
  170. >
  171. >;
  172. using mult_exp1 = foldl_start_with_parser<sequence<times_token, int_token>, int_token, boost::mpl::quote2<binary_op>>;
  173. using exp_parser15 =
  174. build_parser<
  175. foldl_start_with_parser<
  176. sequence<one_of<plus_token, minus_token>, mult_exp1>,
  177. mult_exp1,
  178. boost::mpl::quote2<binary_op>
  179. >
  180. >;
  181. #include <boost/mpl/divides.hpp>
  182. template <class L, class R> struct eval_binary_op<L, '/', R> : boost::mpl::divides<L, R>::type {};
  183. using divides_token = token<lit_c<'/'>>;
  184. using mult_exp2 =
  185. foldl_start_with_parser<
  186. sequence<one_of<times_token, divides_token>, int_token>,
  187. int_token,
  188. boost::mpl::quote2<binary_op>
  189. >;
  190. using exp_parser16 =
  191. build_parser<
  192. foldl_start_with_parser<
  193. sequence<one_of<plus_token, minus_token>, mult_exp2>,
  194. mult_exp2,
  195. boost::mpl::quote2<binary_op>
  196. >
  197. >;
  198. template <class S, class Item>
  199. struct reverse_binary_op :
  200. eval_binary_op<
  201. typename boost::mpl::at_c<Item, 0>::type,
  202. boost::mpl::at_c<Item, 1>::type::value,
  203. S
  204. >
  205. {};
  206. #include <boost/metaparse/foldr_start_with_parser.hpp>
  207. using mult_exp3 =
  208. foldr_start_with_parser<
  209. sequence<int_token, one_of<times_token, divides_token>>, /* The parser applied repeatedly */
  210. int_token, /* The parser parsing the last number */
  211. boost::mpl::quote2<reverse_binary_op> /* The function called for every result */
  212. /* of applying the above parser */
  213. >;
  214. using exp_parser17 =
  215. build_parser<
  216. foldl_start_with_parser<
  217. sequence<one_of<plus_token, minus_token>, mult_exp3>,
  218. mult_exp3,
  219. boost::mpl::quote2<binary_op>
  220. >
  221. >;
  222. #include <boost/mpl/negate.hpp>
  223. using unary_exp1 =
  224. foldr_start_with_parser<
  225. minus_token,
  226. int_token,
  227. boost::mpl::lambda<boost::mpl::negate<boost::mpl::_1>>::type
  228. >;
  229. using mult_exp4 =
  230. foldl_start_with_parser<
  231. sequence<one_of<times_token, divides_token>, unary_exp1>,
  232. unary_exp1,
  233. boost::mpl::quote2<binary_op>
  234. >;
  235. using exp_parser18 =
  236. build_parser<
  237. foldl_start_with_parser<
  238. sequence<one_of<plus_token, minus_token>, mult_exp4>,
  239. mult_exp4,
  240. boost::mpl::quote2<binary_op>
  241. >
  242. >;
  243. using lparen_token = token<lit_c<'('>>;
  244. using rparen_token = token<lit_c<')'>>;
  245. using plus_exp1 =
  246. foldl_start_with_parser<
  247. sequence<one_of<plus_token, minus_token>, mult_exp4>,
  248. mult_exp4,
  249. boost::mpl::quote2<binary_op>
  250. >;
  251. using paren_exp1 = sequence<lparen_token, plus_exp1, rparen_token>;
  252. #include <boost/metaparse/middle_of.hpp>
  253. using paren_exp2 = middle_of<lparen_token, plus_exp1, rparen_token>;
  254. using primary_exp1 = one_of<int_token, paren_exp2>;
  255. struct plus_exp2;
  256. using paren_exp3 = middle_of<lparen_token, plus_exp2, rparen_token>;
  257. using primary_exp2 = one_of<int_token, paren_exp2>;
  258. using unary_exp2 =
  259. foldr_start_with_parser<
  260. minus_token,
  261. primary_exp2,
  262. boost::mpl::lambda<boost::mpl::negate<boost::mpl::_1>>::type
  263. >;
  264. using mult_exp5 =
  265. foldl_start_with_parser<
  266. sequence<one_of<times_token, divides_token>, unary_exp2>,
  267. unary_exp2,
  268. boost::mpl::quote2<binary_op>
  269. >;
  270. struct plus_exp2 :
  271. foldl_start_with_parser<
  272. sequence<one_of<plus_token, minus_token>, mult_exp5>,
  273. mult_exp5,
  274. boost::mpl::quote2<binary_op>
  275. > {};
  276. using exp_parser19 = build_parser<plus_exp2>;
  277. #include <boost/metaparse/define_error.hpp>
  278. BOOST_METAPARSE_DEFINE_ERROR(missing_primary_expression, "Missing primary expression");
  279. struct plus_exp3;
  280. using paren_exp4 = middle_of<lparen_token, plus_exp3, rparen_token>;
  281. #include <boost/metaparse/fail.hpp>
  282. using primary_exp3 = one_of<int_token, paren_exp4, fail<missing_primary_expression>>;
  283. using unary_exp3 =
  284. foldr_start_with_parser<
  285. minus_token,
  286. primary_exp3,
  287. boost::mpl::lambda<boost::mpl::negate<boost::mpl::_1>>::type
  288. >;
  289. using mult_exp6 =
  290. foldl_start_with_parser<
  291. sequence<one_of<times_token, divides_token>, unary_exp3>,
  292. unary_exp3,
  293. boost::mpl::quote2<binary_op>
  294. >;
  295. struct plus_exp3 :
  296. foldl_start_with_parser<
  297. sequence<one_of<plus_token, minus_token>, mult_exp6>,
  298. mult_exp6,
  299. boost::mpl::quote2<binary_op>
  300. > {};
  301. using exp_parser20 = build_parser<plus_exp3>;
  302. #include <boost/metaparse/fail_at_first_char_expected.hpp>
  303. #include <boost/metaparse/first_of.hpp>
  304. struct plus_exp4 :
  305. first_of<
  306. foldl_start_with_parser<
  307. sequence<one_of<plus_token, minus_token>, mult_exp6>,
  308. mult_exp6,
  309. boost::mpl::quote2<binary_op>
  310. >,
  311. fail_at_first_char_expected<
  312. sequence<one_of<plus_token, minus_token>, mult_exp6>
  313. >
  314. > {};
  315. using exp_parser21 = build_parser<plus_exp4>;
  316. #include <boost/metaparse/foldl_reject_incomplete_start_with_parser.hpp>
  317. struct plus_exp5 :
  318. foldl_reject_incomplete_start_with_parser<
  319. sequence<one_of<plus_token, minus_token>, mult_exp6>,
  320. mult_exp6,
  321. boost::mpl::quote2<binary_op>
  322. > {};
  323. using exp_parser22 = build_parser<plus_exp5>;
  324. struct plus_exp6;
  325. using paren_exp5 = middle_of<lparen_token, plus_exp6, rparen_token>;
  326. using primary_exp4 = one_of<int_token, paren_exp5, fail<missing_primary_expression>>;
  327. using unary_exp4 =
  328. foldr_start_with_parser<
  329. minus_token,
  330. primary_exp4,
  331. boost::mpl::lambda<boost::mpl::negate<boost::mpl::_1>>::type
  332. >;
  333. using mult_exp7 =
  334. foldl_reject_incomplete_start_with_parser<
  335. sequence<one_of<times_token, divides_token>, unary_exp4>,
  336. unary_exp4,
  337. boost::mpl::quote2<binary_op>
  338. >;
  339. struct plus_exp6 :
  340. foldl_reject_incomplete_start_with_parser<
  341. sequence<one_of<plus_token, minus_token>, mult_exp7>,
  342. mult_exp7,
  343. boost::mpl::quote2<binary_op>
  344. > {};
  345. using exp_parser23 = build_parser<plus_exp6>;