pattern4.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright (c) 2001-2010 Hartmut Kaiser
  2. //
  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. #include <boost/config/warning_disable.hpp>
  6. #include <boost/detail/lightweight_test.hpp>
  7. #include <boost/spirit/include/karma_operator.hpp>
  8. #include <boost/spirit/include/karma_char.hpp>
  9. #include <boost/spirit/include/karma_auxiliary.hpp>
  10. #include <boost/spirit/include/karma_string.hpp>
  11. #include <boost/spirit/include/karma_numeric.hpp>
  12. #include <boost/spirit/include/karma_nonterminal.hpp>
  13. #include <boost/spirit/include/karma_action.hpp>
  14. #include <boost/spirit/include/karma_directive.hpp>
  15. #include <boost/spirit/include/phoenix_core.hpp>
  16. #include <boost/spirit/include/phoenix_operator.hpp>
  17. #include "test.hpp"
  18. using namespace spirit_test;
  19. ///////////////////////////////////////////////////////////////////////////////
  20. int main()
  21. {
  22. using namespace boost;
  23. using namespace boost::spirit;
  24. using namespace boost::spirit::ascii;
  25. typedef spirit_test::output_iterator<char>::type outiter_type;
  26. {
  27. karma::rule<outiter_type, void(char, int, double)> start;
  28. fusion::vector<char, int, double> vec('a', 10, 12.4);
  29. start = char_[_1 = _r1] << int_[_1 = _r2] << double_[_1 = _r3];
  30. BOOST_TEST(test("a1012.4", start('a', 10, 12.4)));
  31. start = (char_ << int_ << double_)[_1 = _r1, _2 = _r2, _3 = _r3];
  32. BOOST_TEST(test("a1012.4", start('a', 10, 12.4)));
  33. karma::rule<outiter_type, void(char)> a;
  34. karma::rule<outiter_type, void(int)> b;
  35. karma::rule<outiter_type, void(double)> c;
  36. a = char_[_1 = _r1];
  37. b = int_[_1 = _r1];
  38. c = double_[_1 = _r1];
  39. start = a(_r1) << b(_r2) << c(_r3);
  40. BOOST_TEST(test("a1012.4", start('a', 10, 12.4)));
  41. }
  42. {
  43. karma::rule<outiter_type, space_type, void(char, int, double)> start;
  44. fusion::vector<char, int, double> vec('a', 10, 12.4);
  45. start = char_[_1 = _r1] << int_[_1 = _r2] << double_[_1 = _r3];
  46. BOOST_TEST(test_delimited("a 10 12.4 ", start('a', 10, 12.4), space));
  47. start = (char_ << int_ << double_)[_1 = _r1, _2 = _r2, _3 = _r3];
  48. BOOST_TEST(test_delimited("a 10 12.4 ", start('a', 10, 12.4), space));
  49. karma::rule<outiter_type, space_type, void(char)> a;
  50. karma::rule<outiter_type, space_type, void(int)> b;
  51. karma::rule<outiter_type, space_type, void(double)> c;
  52. a = char_[_1 = _r1];
  53. b = int_[_1 = _r1];
  54. c = double_[_1 = _r1];
  55. start = a(_r1) << b(_r2) << c(_r3);
  56. BOOST_TEST(test_delimited("a 10 12.4 ", start('a', 10, 12.4), space));
  57. }
  58. // copy tests
  59. {
  60. karma::rule<outiter_type> a, b, c, start;
  61. a = 'a';
  62. b = int_(10);
  63. c = double_(12.4);
  64. // The FF is the dynamic equivalent of start = a << b << c;
  65. start = a;
  66. start = start.copy() << b;
  67. start = start.copy() << c;
  68. start = start.copy();
  69. BOOST_TEST(test("a1012.4", start));
  70. }
  71. {
  72. karma::rule<outiter_type, space_type> a, b, c, start;
  73. a = 'a';
  74. b = int_(10);
  75. c = double_(12.4);
  76. // The FF is the dynamic equivalent of start = a << b << c;
  77. start = a;
  78. start = start.copy() << b;
  79. start = start.copy() << c;
  80. start = start.copy();
  81. BOOST_TEST(test_delimited("a 10 12.4 ", start, space));
  82. }
  83. { // specifying the encoding
  84. using karma::lower;
  85. using karma::upper;
  86. using karma::string;
  87. typedef boost::spirit::char_encoding::iso8859_1 iso8859_1;
  88. karma::rule<outiter_type, iso8859_1> r;
  89. r = lower['\xE1'];
  90. BOOST_TEST(test("\xE1", r));
  91. r = lower[char_('\xC1')];
  92. BOOST_TEST(test("\xE1", r));
  93. r = upper['\xE1'];
  94. BOOST_TEST(test("\xC1", r));
  95. r = upper[char_('\xC1')];
  96. BOOST_TEST(test("\xC1", r));
  97. r = lower["\xE1\xC1"];
  98. BOOST_TEST(test("\xE1\xE1", r));
  99. r = lower[lit("\xE1\xC1")];
  100. BOOST_TEST(test("\xE1\xE1", r));
  101. r = lower[string("\xE1\xC1")];
  102. BOOST_TEST(test("\xE1\xE1", r));
  103. r = upper["\xE1\xC1"];
  104. BOOST_TEST(test("\xC1\xC1", r));
  105. r = upper[lit("\xE1\xC1")];
  106. BOOST_TEST(test("\xC1\xC1", r));
  107. r = upper[string("\xE1\xC1")];
  108. BOOST_TEST(test("\xC1\xC1", r));
  109. }
  110. return boost::report_errors();
  111. }