kleene.cpp 3.3 KB

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