kleene.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 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/include/qi_operator.hpp>
  10. #include <boost/spirit/include/qi_char.hpp>
  11. #include <boost/spirit/include/qi_string.hpp>
  12. #include <boost/spirit/include/qi_numeric.hpp>
  13. #include <boost/spirit/include/qi_directive.hpp>
  14. #include <boost/spirit/include/qi_action.hpp>
  15. #include <boost/spirit/include/support_argument.hpp>
  16. #include <boost/spirit/include/phoenix_core.hpp>
  17. #include <boost/spirit/include/phoenix_operator.hpp>
  18. #include <string>
  19. #include <iostream>
  20. #include "test.hpp"
  21. struct x_attr
  22. {
  23. };
  24. namespace boost { namespace spirit { namespace traits
  25. {
  26. template <>
  27. struct container_value<x_attr>
  28. {
  29. typedef char type; // value type of container
  30. };
  31. template <>
  32. struct push_back_container<x_attr, char>
  33. {
  34. static bool call(x_attr& /*c*/, char /*val*/)
  35. {
  36. // push back value type into container
  37. return true;
  38. }
  39. };
  40. }}}
  41. int
  42. main()
  43. {
  44. using spirit_test::test;
  45. using spirit_test::test_attr;
  46. using namespace boost::spirit::ascii;
  47. using boost::spirit::qi::omit;
  48. using boost::spirit::qi::uint_;
  49. using boost::spirit::qi::int_;
  50. using boost::spirit::qi::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. BOOST_TEST(test("12345 678 955 987", *uint_, space));
  68. BOOST_TEST(test("12345, 678, 955, 987", uint_ >> *(',' >> uint_), space));
  69. }
  70. {
  71. std::string s;
  72. BOOST_TEST(test_attr("bbbb", *char_, s) && 4 == s.size() && s == "bbbb");
  73. s.clear();
  74. BOOST_TEST(test_attr("b b b b ", *char_, s, space) && s == "bbbb");
  75. // The following 2 tests show that omit does not inhibit explicit attributes
  76. s.clear();
  77. BOOST_TEST(test_attr("bbbb", omit[*char_('b')], s) && s == "bbbb");
  78. s.clear();
  79. BOOST_TEST(test_attr("b b b b", omit[*char_('b')], s, space) && s == "bbbb");
  80. }
  81. {
  82. std::vector<int> v;
  83. BOOST_TEST(test_attr("123 456 789 10", *int_, v, space) && 4 == v.size() &&
  84. v[0] == 123 && v[1] == 456 && v[2] == 789 && v[3] == 10);
  85. }
  86. {
  87. std::vector<std::string> v;
  88. BOOST_TEST(test_attr("a b c d", *lexeme[+alpha], v, space) && 4 == v.size() &&
  89. v[0] == "a" && v[1] == "b" && v[2] == "c" && v[3] == "d");
  90. }
  91. {
  92. std::vector<int> v;
  93. BOOST_TEST(test_attr("123 456 789", *int_, v, space) && 3 == v.size() &&
  94. v[0] == 123 && v[1] == 456 && v[2] == 789);
  95. }
  96. { // actions
  97. namespace phx = boost::phoenix;
  98. using boost::spirit::_1;
  99. std::vector<char> v;
  100. BOOST_TEST(test("bbbb", (*char_)[phx::ref(v) = _1]) && 4 == v.size() &&
  101. v[0] == 'b' && v[1] == 'b' && v[2] == 'b' && v[3] == 'b');
  102. }
  103. { // more actions
  104. namespace phx = boost::phoenix;
  105. using boost::spirit::_1;
  106. std::vector<int> v;
  107. BOOST_TEST(test("123 456 789", (*int_)[phx::ref(v) = _1], space) && 3 == v.size() &&
  108. v[0] == 123 && v[1] == 456 && v[2] == 789);
  109. }
  110. { // attribute customization
  111. x_attr x;
  112. test_attr("abcde", *char_, x);
  113. }
  114. return boost::report_errors();
  115. }