sequence.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*=============================================================================
  2. Copyright (c) 2001-2015 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. =============================================================================*/
  6. #include <boost/detail/lightweight_test.hpp>
  7. #include <boost/spirit/home/x3.hpp>
  8. #include <boost/fusion/include/vector.hpp>
  9. #include <boost/fusion/include/deque.hpp>
  10. #include <boost/fusion/include/at.hpp>
  11. #include <boost/fusion/include/comparison.hpp>
  12. #include <string>
  13. #include <iostream>
  14. #include "test.hpp"
  15. #include "utils.hpp"
  16. int
  17. main()
  18. {
  19. using boost::spirit::x3::unused_type;
  20. using boost::spirit::x3::char_;
  21. using boost::spirit::x3::space;
  22. using boost::spirit::x3::string;
  23. using boost::spirit::x3::attr;
  24. using boost::spirit::x3::omit;
  25. using boost::spirit::x3::lit;
  26. using boost::spirit::x3::unused;
  27. using boost::spirit::x3::int_;
  28. using boost::spirit::x3::float_;
  29. using boost::spirit::x3::no_case;
  30. using boost::spirit::x3::rule;
  31. using boost::spirit::x3::alnum;
  32. using boost::spirit::x3::traits::attribute_of;
  33. using boost::fusion::vector;
  34. using boost::fusion::deque;
  35. using boost::fusion::at_c;
  36. using spirit_test::test;
  37. using spirit_test::test_attr;
  38. {
  39. BOOST_TEST((test("aa", char_ >> char_)));
  40. BOOST_TEST((test("aa", char_ >> 'a')));
  41. BOOST_TEST((test("aaa", char_ >> char_ >> char_('a'))));
  42. BOOST_TEST((test("xi", char_('x') >> char_('i'))));
  43. BOOST_TEST((!test("xi", char_('x') >> char_('o'))));
  44. BOOST_TEST((test("xin", char_('x') >> char_('i') >> char_('n'))));
  45. }
  46. #ifdef BOOST_SPIRIT_COMPILE_ERROR_CHECK
  47. {
  48. // Compile check only
  49. struct x {};
  50. char_ >> x(); // this should give a reasonable error message
  51. }
  52. #endif
  53. {
  54. BOOST_TEST((test(" a a", char_ >> char_, space)));
  55. BOOST_TEST((test(" x i", char_('x') >> char_('i'), space)));
  56. BOOST_TEST((!test(" x i", char_('x') >> char_('o'), space)));
  57. }
  58. {
  59. BOOST_TEST((test(" Hello, World", lit("Hello") >> ',' >> "World", space)));
  60. }
  61. {
  62. vector<char, char> attr;
  63. BOOST_TEST((test_attr("ab", char_ >> char_, attr)));
  64. BOOST_TEST((at_c<0>(attr) == 'a'));
  65. BOOST_TEST((at_c<1>(attr) == 'b'));
  66. }
  67. #ifdef BOOST_SPIRIT_COMPILE_ERROR_CHECK
  68. {
  69. // Compile check only
  70. vector<char, char> attr;
  71. // error: attr does not have enough elements
  72. test_attr("abc", char_ >> char_ >> char_, attr);
  73. }
  74. #endif
  75. {
  76. vector<char, char, char> attr;
  77. BOOST_TEST((test_attr(" a\n b\n c", char_ >> char_ >> char_, attr, space)));
  78. BOOST_TEST((at_c<0>(attr) == 'a'));
  79. BOOST_TEST((at_c<1>(attr) == 'b'));
  80. BOOST_TEST((at_c<2>(attr) == 'c'));
  81. }
  82. {
  83. // 'b' has an unused_type. unused attributes are not part of the sequence
  84. vector<char, char> attr;
  85. BOOST_TEST((test_attr("abc", char_ >> 'b' >> char_, attr)));
  86. BOOST_TEST((at_c<0>(attr) == 'a'));
  87. BOOST_TEST((at_c<1>(attr) == 'c'));
  88. }
  89. {
  90. // 'b' has an unused_type. unused attributes are not part of the sequence
  91. vector<char, char> attr;
  92. BOOST_TEST((test_attr("acb", char_ >> char_ >> 'b', attr)));
  93. BOOST_TEST((at_c<0>(attr) == 'a'));
  94. BOOST_TEST((at_c<1>(attr) == 'c'));
  95. }
  96. {
  97. // "hello" has an unused_type. unused attributes are not part of the sequence
  98. vector<char, char> attr;
  99. BOOST_TEST((test_attr("a hello c", char_ >> "hello" >> char_, attr, space)));
  100. BOOST_TEST((at_c<0>(attr) == 'a'));
  101. BOOST_TEST((at_c<1>(attr) == 'c'));
  102. }
  103. {
  104. // a single element
  105. char attr;
  106. BOOST_TEST((test_attr("ab", char_ >> 'b', attr)));
  107. BOOST_TEST((attr == 'a'));
  108. }
  109. {
  110. // a single element fusion sequence
  111. vector<char> attr;
  112. BOOST_TEST((test_attr("ab", char_ >> 'b', attr)));
  113. BOOST_TEST((at_c<0>(attr) == 'a'));
  114. }
  115. {
  116. // make sure single element tuples get passed through if the rhs
  117. // has a single element tuple as its attribute. Edit JDG 2014:
  118. // actually he issue here is that if the rhs in this case a rule
  119. // (r), it should get it (i.e. the sequence parser should not
  120. // unwrap it). It's odd that the RHS (r) does not really have a
  121. // single element tuple (it's a deque<char, int>), so the original
  122. // comment is not accurate.
  123. typedef deque<char, int> attr_type;
  124. attr_type fv;
  125. auto r = rule<class r, attr_type>()
  126. = char_ >> ',' >> int_;
  127. BOOST_TEST((test_attr("test:x,1", "test:" >> r, fv) &&
  128. fv == attr_type('x', 1)));
  129. }
  130. {
  131. // make sure single element tuples get passed through if the rhs
  132. // has a single element tuple as its attribute. This is a correction
  133. // of the test above.
  134. typedef deque<int> attr_type;
  135. attr_type fv;
  136. auto r = rule<class r, attr_type>()
  137. = int_;
  138. BOOST_TEST((test_attr("test:1", "test:" >> r, fv) &&
  139. fv == attr_type(1)));
  140. }
  141. {
  142. // unused means we don't care about the attribute
  143. BOOST_TEST((test_attr("abc", char_ >> 'b' >> char_, unused)));
  144. }
  145. {
  146. BOOST_TEST((test("aA", no_case[char_('a') >> 'a'])));
  147. BOOST_TEST((test("BEGIN END", no_case[lit("begin") >> "end"], space)));
  148. BOOST_TEST((!test("BEGIN END", no_case[lit("begin") >> "nend"], space)));
  149. }
  150. { // check attribute is passed through unary to another sequence
  151. using boost::spirit::x3::eps;
  152. std::string s;
  153. BOOST_TEST(test_attr("ab", eps >> no_case[char_ >> char_], s));
  154. BOOST_TEST("ab" == s);
  155. s.clear();
  156. BOOST_TEST(test_attr("ab", no_case[char_ >> char_] >> eps, s));
  157. BOOST_TEST("ab" == s);
  158. s.clear();
  159. BOOST_TEST(test_attr("abc", char_ >> no_case[char_ >> char_], s));
  160. BOOST_TEST("abc" == s);
  161. s.clear();
  162. BOOST_TEST(test_attr("abc", no_case[char_ >> char_] >> char_, s));
  163. BOOST_TEST("abc" == s);
  164. }
  165. {
  166. #ifdef SPIRIT_NO_COMPILE_CHECK
  167. char_ >> char_ = char_ >> char_; // disallow this!
  168. #endif
  169. }
  170. { // alternative forms of attributes. Allow sequences to take in
  171. // stl containers.
  172. std::vector<char> v;
  173. BOOST_TEST(test_attr("abc", char_ >> char_ >> char_, v));
  174. BOOST_TEST(v.size() == 3);
  175. BOOST_TEST(v[0] == 'a');
  176. BOOST_TEST(v[1] == 'b');
  177. BOOST_TEST(v[2] == 'c');
  178. }
  179. { // alternative forms of attributes. Allow sequences to take in
  180. // stl containers.
  181. std::vector<char> v;
  182. BOOST_TEST(test_attr("a,b,c", char_ >> *(',' >> char_), v));
  183. BOOST_TEST(v.size() == 3);
  184. BOOST_TEST(v[0] == 'a');
  185. BOOST_TEST(v[1] == 'b');
  186. BOOST_TEST(v[2] == 'c');
  187. }
  188. { // alternative forms of attributes. Allow sequences to take in
  189. // stl containers.
  190. std::vector<char> v;
  191. BOOST_TEST(test_attr("abc", char_ >> *char_, v));
  192. BOOST_TEST(v.size() == 3);
  193. BOOST_TEST(v[0] == 'a');
  194. BOOST_TEST(v[1] == 'b');
  195. BOOST_TEST(v[2] == 'c');
  196. }
  197. { // alternative forms of attributes. Allow sequences to take in
  198. // stl containers.
  199. //~ using boost::spirit::x3::hold;
  200. std::vector<char> v;
  201. BOOST_TEST(test_attr("abc", char_ >> *(char_ >> char_), v));
  202. BOOST_TEST(v.size() == 3);
  203. BOOST_TEST(v[0] == 'a');
  204. BOOST_TEST(v[1] == 'b');
  205. BOOST_TEST(v[2] == 'c');
  206. v.clear();
  207. BOOST_TEST(!test_attr("abcd", char_ >> *(char_ >> char_), v));
  208. // $$$ hold not yet implementd $$$
  209. //~ v.clear();
  210. //~ BOOST_TEST(test_attr("abcdef", char_ >> *hold[char_ >> char_] >> char_, v));
  211. //~ BOOST_TEST(v.size() == 6);
  212. //~ BOOST_TEST(v[0] == 'a');
  213. //~ BOOST_TEST(v[1] == 'b');
  214. //~ BOOST_TEST(v[2] == 'c');
  215. //~ BOOST_TEST(v[3] == 'd');
  216. //~ BOOST_TEST(v[4] == 'e');
  217. //~ BOOST_TEST(v[5] == 'f');
  218. v.clear();
  219. BOOST_TEST(test_attr("abc", char_ >> +(char_ >> char_), v));
  220. BOOST_TEST(v.size() == 3);
  221. BOOST_TEST(v[0] == 'a');
  222. BOOST_TEST(v[1] == 'b');
  223. BOOST_TEST(v[2] == 'c');
  224. }
  225. { // alternative forms of attributes. Allow sequences to take in
  226. // stl containers.
  227. std::vector<char> v;
  228. BOOST_TEST(test_attr("abc", char_ >> -(+char_), v));
  229. BOOST_TEST(v.size() == 3);
  230. BOOST_TEST(v[0] == 'a');
  231. BOOST_TEST(v[1] == 'b');
  232. BOOST_TEST(v[2] == 'c');
  233. }
  234. { // alternative forms of attributes. Allow sequences to take in
  235. // stl containers.
  236. std::string s;
  237. BOOST_TEST(test_attr("foobar", string("foo") >> string("bar"), s));
  238. BOOST_TEST(s == "foobar");
  239. s.clear();
  240. // $$$ hold not yet implemented $$$
  241. //~ using boost::spirit::x3::hold;
  242. //~ rule<char const*, std::string()> word = +char_("abc");
  243. //~ BOOST_TEST(test_attr("ab.bc.ca", *hold[word >> string(".")] >> word, s));
  244. //~ BOOST_TEST(s == "ab.bc.ca");
  245. }
  246. // Make sure get_sequence_types works for sequences of sequences.
  247. {
  248. std::vector<char> v;
  249. BOOST_TEST(test_attr(" a b", (' ' >> char_) >> (' ' >> char_), v));
  250. BOOST_TEST(v.size() == 2);
  251. BOOST_TEST(v[0] == 'a');
  252. BOOST_TEST(v[1] == 'b');
  253. }
  254. // alternative forms of attributes. Allow sequences to take in
  255. // stl containers of stl containers.
  256. {
  257. std::vector<std::string> v;
  258. BOOST_TEST(test_attr("abc1,abc2",
  259. *~char_(',') >> *(',' >> *~char_(',')), v));
  260. BOOST_TEST(v.size() == 2 && v[0] == "abc1" && v[1] == "abc2");
  261. }
  262. {
  263. std::vector<std::string> v;
  264. auto e = rule<class e, std::string>()
  265. = *~char_(',');
  266. auto l = rule<class l, std::vector<std::string>>()
  267. = e >> *(',' >> e);
  268. BOOST_TEST(test_attr("abc1,abc2,abc3", l, v));
  269. BOOST_TEST(v.size() == 3);
  270. BOOST_TEST(v[0] == "abc1");
  271. BOOST_TEST(v[1] == "abc2");
  272. BOOST_TEST(v[2] == "abc3");
  273. }
  274. // do the same with a plain string object
  275. {
  276. std::string s;
  277. BOOST_TEST(test_attr("abc1,abc2",
  278. *~char_(',') >> *(',' >> *~char_(',')), s));
  279. BOOST_TEST(s == "abc1abc2");
  280. }
  281. {
  282. std::string s;
  283. auto e = rule<class e, std::string>()
  284. = *~char_(',');
  285. auto l = rule<class l, std::string>()
  286. = e >> *(',' >> e);
  287. BOOST_TEST(test_attr("abc1,abc2,abc3", l, s));
  288. BOOST_TEST(s == "abc1abc2abc3");
  289. }
  290. {
  291. std::vector<char> v;
  292. BOOST_TEST(test_attr("ab", char_ >> -char_, v));
  293. BOOST_TEST(v.size() == 2 && v[0] == 'a' && v[1] == 'b');
  294. v.clear();
  295. BOOST_TEST(test_attr("a", char_ >> -char_, v));
  296. BOOST_TEST(v.size() == 1 && v[0] == 'a');
  297. // $$$ should this be allowed? I don't think so... $$$
  298. //~ v.clear();
  299. //~ BOOST_TEST(test_attr("a", char_, v));
  300. //~ BOOST_TEST(v.size() == 1 && v[0] == 'a');
  301. }
  302. {
  303. std::vector<boost::optional<char>> v;
  304. BOOST_TEST(test_attr("ab", char_ >> -char_, v));
  305. BOOST_TEST(v.size() == 2 && v[0] == 'a' && v[1] == 'b');
  306. v.clear();
  307. BOOST_TEST(test_attr("a", char_ >> -char_, v));
  308. BOOST_TEST(v.size() == 2 && v[0] == 'a' && !v[1]);
  309. // $$$ should this be allowed? I don't think so... $$$
  310. //~ v.clear();
  311. //~ BOOST_TEST(test_attr("a", char_, v));
  312. //~ BOOST_TEST(v.size() == 1 && v[0] == 'a');
  313. }
  314. // test from spirit mailing list
  315. // "Optional operator causes string attribute concatenation"
  316. {
  317. typedef vector<char, char, int> attr_type;
  318. attr_type attr;
  319. auto node = alnum >> -('[' >> alnum >> '=' >> int_ >> ']');
  320. BOOST_TEST(test_attr("x[y=123]", node, attr));
  321. BOOST_TEST(attr == attr_type('x', 'y', 123));
  322. }
  323. // test from spirit mailing list (variation of above)
  324. // "Optional operator causes string attribute concatenation"
  325. {
  326. typedef vector<std::string, std::string, int> attr_type;
  327. attr_type attr;
  328. auto node = +alnum >> -('[' >> +alnum >> '=' >> int_ >> ']');
  329. BOOST_TEST(test_attr("xxx[yyy=123]", node, attr));
  330. BOOST_TEST(attr == attr_type("xxx", "yyy", 123));
  331. }
  332. // test from spirit mailing list
  333. // "Error with container within sequence"
  334. {
  335. typedef vector<std::string> attr_type;
  336. attr_type attr;
  337. auto r = *alnum;
  338. BOOST_TEST(test_attr("abcdef", r, attr));
  339. BOOST_TEST(at_c<0>(attr) == "abcdef");
  340. }
  341. // test from spirit mailing list (variation of above)
  342. // "Error with container within sequence"
  343. {
  344. typedef vector<std::vector<int>> attr_type;
  345. attr_type attr;
  346. auto r = *int_;
  347. BOOST_TEST(test_attr("123 456", r, attr, space));
  348. BOOST_TEST(at_c<0>(attr).size() == 2);
  349. BOOST_TEST(at_c<0>(attr)[0] == 123);
  350. BOOST_TEST(at_c<0>(attr)[1] == 456);
  351. }
  352. {
  353. using Attr = boost::variant<int, float>;
  354. Attr attr;
  355. auto const term = rule<class term, Attr>("term") = int_ | float_;
  356. auto const expr = rule<class expr, Attr>("expr") = term | ('(' > term > ')');
  357. BOOST_TEST((test_attr("(1)", expr, attr, space)));
  358. }
  359. // test that failing sequence leaves attribute consistent
  360. {
  361. std::string attr;
  362. //no need to use omit[], but lit() is buggy ATM
  363. BOOST_TEST(test_attr("A\nB\nC", *(char_ >> omit[lit("\n")]), attr, false));
  364. BOOST_TEST(attr == "AB");
  365. }
  366. // test that sequence with only one parser producing attribute
  367. // makes it unwrapped
  368. {
  369. BOOST_TEST((boost::is_same<
  370. typename attribute_of<decltype(lit("abc") >> attr(long())), unused_type>::type,
  371. long>() ));
  372. }
  373. { // test action
  374. using boost::fusion::at_c;
  375. char c = 0;
  376. int n = 0;
  377. auto f = [&](auto& ctx)
  378. {
  379. c = at_c<0>(_attr(ctx));
  380. n = at_c<1>(_attr(ctx));
  381. };
  382. BOOST_TEST(test("x123\"a string\"", (char_ >> int_ >> "\"a string\"")[f]));
  383. BOOST_TEST(c == 'x');
  384. BOOST_TEST(n == 123);
  385. }
  386. { // test action
  387. char c = 0;
  388. int n = 0;
  389. auto f = [&](auto& ctx)
  390. {
  391. c = at_c<0>(_attr(ctx));
  392. n = at_c<1>(_attr(ctx));
  393. };
  394. BOOST_TEST(test("x 123 \"a string\"", (char_ >> int_ >> "\"a string\"")[f], space));
  395. BOOST_TEST(c == 'x');
  396. BOOST_TEST(n == 123);
  397. }
  398. {
  399. #ifdef SPIRIT_NO_COMPILE_CHECK
  400. char const* const s = "";
  401. int i;
  402. parse(s, s, int_ >> int_, i);
  403. #endif
  404. }
  405. { // test move only types
  406. using boost::spirit::x3::eps;
  407. std::vector<move_only> v;
  408. BOOST_TEST(test_attr("ssszs", *synth_move_only >> 'z' >> synth_move_only, v));
  409. BOOST_TEST_EQ(v.size(), 4);
  410. }
  411. return boost::report_errors();
  412. }