plus.cpp 3.5 KB

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