plus.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 <string>
  7. #include <vector>
  8. #include <boost/detail/lightweight_test.hpp>
  9. #include <boost/spirit/home/x3.hpp>
  10. #include <boost/fusion/include/vector.hpp>
  11. #include <string>
  12. #include <iostream>
  13. #include "test.hpp"
  14. #include "utils.hpp"
  15. struct x_attr
  16. {
  17. };
  18. namespace boost { namespace spirit { namespace x3 { namespace traits
  19. {
  20. template <>
  21. struct container_value<x_attr>
  22. {
  23. typedef char type; // value type of container
  24. };
  25. template <>
  26. struct push_back_container<x_attr>
  27. {
  28. static bool call(x_attr& /*c*/, char /*val*/)
  29. {
  30. // push back value type into container
  31. return true;
  32. }
  33. };
  34. }}}}
  35. int
  36. main()
  37. {
  38. using spirit_test::test;
  39. using spirit_test::test_attr;
  40. using boost::spirit::x3::char_;
  41. using boost::spirit::x3::alpha;
  42. using boost::spirit::x3::upper;
  43. using boost::spirit::x3::space;
  44. using boost::spirit::x3::digit;
  45. //~ using boost::spirit::x3::no_case;
  46. using boost::spirit::x3::int_;
  47. using boost::spirit::x3::omit;
  48. using boost::spirit::x3::lit;
  49. //~ using boost::spirit::x3::_1;
  50. using boost::spirit::x3::lexeme;
  51. {
  52. BOOST_TEST(test("aaaaaaaa", +char_));
  53. BOOST_TEST(test("a", +char_));
  54. BOOST_TEST(!test("", +char_));
  55. BOOST_TEST(test("aaaaaaaa", +alpha));
  56. BOOST_TEST(!test("aaaaaaaa", +upper));
  57. }
  58. {
  59. BOOST_TEST(test(" a a aaa aa", +char_, space));
  60. BOOST_TEST(test("12345 678 9 ", +digit, space));
  61. }
  62. //~ {
  63. //~ BOOST_TEST(test("aBcdeFGH", no_case[+char_]));
  64. //~ BOOST_TEST(test("a B cde FGH ", no_case[+char_], space));
  65. //~ }
  66. {
  67. std::vector<int> v;
  68. BOOST_TEST(test_attr("123 456 789 10", +int_, v, space) && 4 == v.size() &&
  69. v[0] == 123 && v[1] == 456 && v[2] == 789 && v[3] == 10);
  70. }
  71. {
  72. std::vector<std::string> v;
  73. BOOST_TEST(test_attr("a b c d", +lexeme[+alpha], v, space) && 4 == v.size() &&
  74. v[0] == "a" && v[1] == "b" && v[2] == "c" && v[3] == "d");
  75. }
  76. {
  77. BOOST_TEST(test("Kim Kim Kim", +lit("Kim"), space));
  78. }
  79. // $$$ Fixme $$$
  80. /*{
  81. // The following 2 tests show that omit does not inhibit explicit attributes
  82. std::string s;
  83. BOOST_TEST(test_attr("bbbb", omit[+char_('b')], s) && s == "bbbb");
  84. s.clear();
  85. BOOST_TEST(test_attr("b b b b ", omit[+char_('b')], s, space) && s == "bbbb");
  86. }*/
  87. { // actions
  88. std::string v;
  89. auto f = [&](auto& ctx){ v = _attr(ctx); };
  90. BOOST_TEST(test("bbbb", (+char_)[f]) && 4 == v.size() &&
  91. v[0] == 'b' && v[1] == 'b' && v[2] == 'b' && v[3] == 'b');
  92. }
  93. { // more actions
  94. std::vector<int> v;
  95. auto f = [&](auto& ctx){ v = _attr(ctx); };
  96. BOOST_TEST(test("1 2 3", (+int_)[f], space) && 3 == v.size() &&
  97. v[0] == 1 && v[1] == 2 && v[2] == 3);
  98. }
  99. { // attribute customization
  100. x_attr x;
  101. test_attr("abcde", +char_, x);
  102. }
  103. // single-element fusion vector tests
  104. {
  105. boost::fusion::vector<std::string> fs;
  106. BOOST_TEST((test_attr("12345", +char_, fs))); // ok
  107. BOOST_TEST(boost::fusion::at_c<0>(fs) == "12345");
  108. }
  109. { // test move only types
  110. std::vector<move_only> v;
  111. BOOST_TEST(test_attr("sss", +synth_move_only, v));
  112. BOOST_TEST_EQ(v.size(), 3);
  113. }
  114. return boost::report_errors();
  115. }