grammar.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. // Copyright Abel Sinkovics (abel@sinkovics.hu) 2012.
  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/grammar.hpp>
  6. #include <boost/metaparse/lit_c.hpp>
  7. #include <boost/metaparse/get_result.hpp>
  8. #include <boost/metaparse/start.hpp>
  9. #include <boost/metaparse/is_error.hpp>
  10. #include <boost/metaparse/string.hpp>
  11. #include "test_case.hpp"
  12. #include <boost/mpl/apply_wrap.hpp>
  13. #include <boost/mpl/equal_to.hpp>
  14. #include <boost/mpl/equal.hpp>
  15. #include <boost/mpl/vector.hpp>
  16. #include <boost/mpl/char.hpp>
  17. #include <boost/mpl/assert.hpp>
  18. using boost::mpl::char_;
  19. namespace
  20. {
  21. struct next_char
  22. {
  23. typedef next_char type;
  24. template <class C>
  25. struct apply
  26. {
  27. typedef char_<C::type::value + 1> type;
  28. };
  29. };
  30. }
  31. BOOST_METAPARSE_TEST_CASE(grammar)
  32. {
  33. using boost::metaparse::grammar;
  34. using boost::metaparse::lit_c;
  35. using boost::metaparse::get_result;
  36. using boost::metaparse::start;
  37. using boost::metaparse::is_error;
  38. using boost::metaparse::string;
  39. using boost::mpl::apply_wrap2;
  40. using boost::mpl::equal_to;
  41. using boost::mpl::equal;
  42. using boost::mpl::vector;
  43. // import
  44. BOOST_MPL_ASSERT((
  45. equal_to<
  46. char_<'x'>,
  47. get_result<
  48. apply_wrap2<
  49. grammar<>
  50. ::import<string<'S'>, lit_c<'x'> >::type,
  51. string<'x'>,
  52. start
  53. >
  54. >::type
  55. >
  56. ));
  57. // rename_import
  58. BOOST_MPL_ASSERT((
  59. equal_to<
  60. char_<'x'>,
  61. get_result<
  62. apply_wrap2<
  63. grammar<>
  64. ::import<string<'I'>, lit_c<'x'> >::type
  65. ::rule<string<'S',' ',':',':','=',' ','I'> >::type,
  66. string<'x'>,
  67. start
  68. >
  69. >::type
  70. >
  71. ));
  72. // char
  73. BOOST_MPL_ASSERT((
  74. equal_to<
  75. char_<'x'>,
  76. get_result<
  77. apply_wrap2<
  78. grammar<>
  79. ::rule<string<'S',' ',':',':','=',' ','\'','x','\''> >::type,
  80. string<'x'>,
  81. start
  82. >
  83. >::type
  84. >
  85. ));
  86. // char_failure
  87. BOOST_MPL_ASSERT((
  88. is_error<
  89. apply_wrap2<
  90. grammar<>
  91. ::rule<string<'S',' ',':',':','=',' ','\'','x','\''> >::type,
  92. string<'y'>,
  93. start
  94. >
  95. >
  96. ));
  97. // char_n
  98. BOOST_MPL_ASSERT((
  99. equal_to<
  100. char_<'\n'>,
  101. get_result<
  102. apply_wrap2<
  103. grammar<>
  104. ::rule<string<'S',' ',':',':','=',' ','\'','\\','n','\''> >::type,
  105. string<'\n'>,
  106. start
  107. >
  108. >::type
  109. >
  110. ));
  111. // char_r
  112. BOOST_MPL_ASSERT((
  113. equal_to<
  114. char_<'\r'>,
  115. get_result<
  116. apply_wrap2<
  117. grammar<>
  118. ::rule<string<'S',' ',':',':','=',' ','\'','\\','r','\''> >::type,
  119. string<'\r'>,
  120. start
  121. >
  122. >::type
  123. >
  124. ));
  125. // char_t
  126. BOOST_MPL_ASSERT((
  127. equal_to<
  128. char_<'\t'>,
  129. get_result<
  130. apply_wrap2<
  131. grammar<>
  132. ::rule<string<'S',' ',':',':','=',' ','\'','\\','t','\''> >::type,
  133. string<'\t'>,
  134. start
  135. >
  136. >::type
  137. >
  138. ));
  139. // backslash
  140. BOOST_MPL_ASSERT((
  141. equal_to<
  142. char_<'\\'>,
  143. get_result<
  144. apply_wrap2<
  145. grammar<>
  146. ::rule<string<'S',' ',':',':','=',' ','\'','\\','\\','\''> >::type,
  147. string<'\\'>,
  148. start
  149. >
  150. >::type
  151. >
  152. ));
  153. // char_\'
  154. BOOST_MPL_ASSERT((
  155. equal_to<
  156. char_<'\''>,
  157. get_result<
  158. apply_wrap2<
  159. grammar<>
  160. ::rule<string<'S',' ',':',':','=',' ','\'','\\','\'','\''> >::type,
  161. string<'\''>,
  162. start
  163. >
  164. >::type
  165. >
  166. ));
  167. // rename_rule
  168. BOOST_MPL_ASSERT((
  169. equal_to<
  170. char_<'x'>,
  171. get_result<
  172. apply_wrap2<
  173. grammar<>
  174. ::rule<string<'R',' ',':',':','=',' ','\'','x','\''> >::type
  175. ::rule<string<'S',' ',':',':','=',' ','R'> >::type,
  176. string<'x'>,
  177. start
  178. >
  179. >::type
  180. >
  181. ));
  182. // sequence
  183. BOOST_MPL_ASSERT((
  184. equal<
  185. vector<char_<'x'>, char_<'x'> >,
  186. get_result<
  187. apply_wrap2<
  188. grammar<>
  189. ::rule<string<'R',' ',':',':','=',' ','\'','x','\''> >::type
  190. ::rule<string<'S',' ',':',':','=',' ','R',' ','R'> >::type,
  191. string<'x','x'>,
  192. start
  193. >
  194. >::type
  195. >
  196. ));
  197. // sequence_first_fail
  198. BOOST_MPL_ASSERT((
  199. is_error<
  200. apply_wrap2<
  201. grammar<>
  202. ::rule<string<'R',' ',':',':','=',' ','\'','x','\''> >::type
  203. ::rule<string<'S',' ',':',':','=',' ','R',' ','R'> >::type,
  204. string<'y','x'>,
  205. start
  206. >
  207. >
  208. ));
  209. // sequence_second_fail
  210. BOOST_MPL_ASSERT((
  211. is_error<
  212. apply_wrap2<
  213. grammar<>
  214. ::rule<string<'R',' ',':',':','=',' ','\'','x','\''> >::type
  215. ::rule<string<'S',' ',':',':','=',' ','R',' ','R'> >::type,
  216. string<'x','y'>,
  217. start
  218. >
  219. >
  220. ));
  221. // selection 1
  222. BOOST_MPL_ASSERT((
  223. equal_to<
  224. char_<'x'>,
  225. get_result<
  226. apply_wrap2<
  227. grammar<>
  228. ::rule<string<'Y',' ',':',':','=',' ','\'','y','\''> >::type
  229. ::rule<string<'X',' ',':',':','=',' ','\'','x','\''> >::type
  230. ::rule<string<'S',' ',':',':','=',' ','X','|','Y'> >::type,
  231. string<'x'>,
  232. start
  233. >
  234. >::type
  235. >
  236. ));
  237. // selection 2
  238. BOOST_MPL_ASSERT((
  239. equal_to<
  240. char_<'y'>,
  241. get_result<
  242. apply_wrap2<
  243. grammar<>
  244. ::rule<string<'Y',' ',':',':','=',' ','\'','y','\''> >::type
  245. ::rule<string<'X',' ',':',':','=',' ','\'','x','\''> >::type
  246. ::rule<string<'S',' ',':',':','=',' ','X','|','Y'> >::type,
  247. string<'y'>,
  248. start
  249. >
  250. >::type
  251. >
  252. ));
  253. // selection_fail
  254. BOOST_MPL_ASSERT((
  255. is_error<
  256. apply_wrap2<
  257. grammar<>
  258. ::rule<string<'Y',' ',':',':','=',' ','\'','y','\''> >::type
  259. ::rule<string<'X',' ',':',':','=',' ','\'','x','\''> >::type
  260. ::rule<string<'S',' ',':',':','=',' ','X','|','Y'> >::type,
  261. string<'z'>,
  262. start
  263. >
  264. >
  265. ));
  266. // repeated_0
  267. BOOST_MPL_ASSERT((
  268. equal<
  269. vector<>,
  270. get_result<
  271. apply_wrap2<
  272. grammar<>
  273. ::rule<string<'X',' ',':',':','=',' ','\'','x','\''> >::type
  274. ::rule<string<'S',' ',':',':','=',' ','X','*'> >::type,
  275. string<'y'>,
  276. start
  277. >
  278. >::type
  279. >
  280. ));
  281. // repeated_1
  282. BOOST_MPL_ASSERT((
  283. equal<
  284. vector<char_<'x'> >,
  285. get_result<
  286. apply_wrap2<
  287. grammar<>
  288. ::rule<string<'X',' ',':',':','=',' ','\'','x','\''> >::type
  289. ::rule<string<'S',' ',':',':','=',' ','X','*'> >::type,
  290. string<'x','y'>,
  291. start
  292. >
  293. >::type
  294. >
  295. ));
  296. // repeated_2
  297. BOOST_MPL_ASSERT((
  298. equal<
  299. vector<char_<'x'>, char_<'x'> >,
  300. get_result<
  301. apply_wrap2<
  302. grammar<>
  303. ::rule<string<'X',' ',':',':','=',' ','\'','x','\''> >::type
  304. ::rule<string<'S',' ',':',':','=',' ','X','*'> >::type,
  305. string<'x','x','y'>,
  306. start
  307. >
  308. >::type
  309. >
  310. ));
  311. // bracket
  312. BOOST_MPL_ASSERT((
  313. equal_to<
  314. char_<'x'>,
  315. get_result<
  316. apply_wrap2<
  317. grammar<>
  318. ::rule<
  319. string<'S',' ',':',':','=',' ','(','\'','x','\'',')'>
  320. >::type,
  321. string<'x'>,
  322. start
  323. >
  324. >::type
  325. >
  326. ));
  327. // semantic_action
  328. BOOST_MPL_ASSERT((
  329. equal_to<
  330. char_<'y'>,
  331. get_result<
  332. apply_wrap2<
  333. grammar<>
  334. ::rule<
  335. string<'S',' ',':',':','=',' ','\'','x','\''>,
  336. next_char
  337. >::type,
  338. string<'x'>,
  339. start
  340. >
  341. >::type
  342. >
  343. ));
  344. // repeated+_0
  345. BOOST_MPL_ASSERT((
  346. is_error<
  347. apply_wrap2<
  348. grammar<>
  349. ::rule<string<'X',' ',':',':','=',' ','\'','x','\''> >::type
  350. ::rule<string<'S',' ',':',':','=',' ','X','+'> >::type,
  351. string<'y'>,
  352. start
  353. >
  354. >
  355. ));
  356. // repeated+_1
  357. BOOST_MPL_ASSERT((
  358. equal<
  359. vector<char_<'x'> >,
  360. get_result<
  361. apply_wrap2<
  362. grammar<>
  363. ::rule<string<'X',' ',':',':','=',' ','\'','x','\''> >::type
  364. ::rule<string<'S',' ',':',':','=',' ','X','+'> >::type,
  365. string<'x','y'>,
  366. start
  367. >
  368. >::type
  369. >
  370. ));
  371. // repeated+_2
  372. BOOST_MPL_ASSERT((
  373. equal<
  374. vector<char_<'x'>, char_<'x'> >,
  375. get_result<
  376. apply_wrap2<
  377. grammar<>
  378. ::rule<string<'X',' ',':',':','=',' ','\'','x','\''> >::type
  379. ::rule<string<'S',' ',':',':','=',' ','X','+'> >::type,
  380. string<'x','x','y'>,
  381. start
  382. >
  383. >::type
  384. >
  385. ));
  386. }