directives_tests.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*=============================================================================
  2. Copyright (c) 2001-2003 Joel de Guzman
  3. http://spirit.sourceforge.net/
  4. Use, modification and distribution is subject to the Boost Software
  5. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. #include <iostream>
  9. #include <boost/detail/lightweight_test.hpp>
  10. #include <string>
  11. #include "impl/string_length.hpp"
  12. #include <boost/spirit/include/classic_core.hpp>
  13. #include <boost/spirit/include/classic_assign_actor.hpp>
  14. using namespace BOOST_SPIRIT_CLASSIC_NS;
  15. ///////////////////////////////////////////////////////////////////////////////
  16. //
  17. // Directives tests
  18. //
  19. ///////////////////////////////////////////////////////////////////////////////
  20. void
  21. directives_test1()
  22. {
  23. char const* cpx = "H e l l o";
  24. char const* cpx_first = cpx;
  25. char const* cpx_last = cpx + test_impl::string_length(cpx);
  26. match<> hit;
  27. typedef skipper_iteration_policy<iteration_policy> iter_policy;
  28. scanner<char const*, scanner_policies<iter_policy> >
  29. scanx(cpx_first, cpx_last);
  30. hit = str_p("Hello").parse(scanx);
  31. BOOST_TEST(!hit);
  32. scanx.first = cpx;
  33. hit = chseq_p("Hello").parse(scanx);
  34. BOOST_TEST(!!hit);
  35. scanx.first = cpx;
  36. char const* cp = "Hello \n\tWorld";
  37. char const* cp_first = cp;
  38. char const* cp_last = cp + test_impl::string_length(cp);
  39. scanner<char const*, scanner_policies<iter_policy> >
  40. scan(cp_first, cp_last);
  41. hit = (+(alpha_p | punct_p)).parse(scan);
  42. BOOST_TEST(!!hit);
  43. BOOST_TEST(scan.first == scan.last);
  44. scan.first = cp;
  45. hit = (+(lexeme_d[+(alpha_p | '\'')])).parse(scan);
  46. BOOST_TEST(!!hit);
  47. BOOST_TEST(scan.first == scan.last);
  48. scan.first = cp;
  49. hit = (+(lexeme_d[lexeme_d[+anychar_p]])).parse(scan);
  50. BOOST_TEST(!!hit);
  51. BOOST_TEST(scan.first == scan.last);
  52. scan.first = cp;
  53. hit = (str_p("Hello") >> "World").parse(scan);
  54. BOOST_TEST(!!hit);
  55. BOOST_TEST(scan.first == scan.last);
  56. scan.first = cp;
  57. hit = as_lower_d[str_p("hello") >> "world"].parse(scan);
  58. BOOST_TEST(!!hit);
  59. BOOST_TEST(scan.first == scan.last);
  60. scan.first = cp;
  61. hit = (+(as_lower_d[as_lower_d[+lower_p | '\'']])).parse(scan);
  62. BOOST_TEST(!!hit);
  63. BOOST_TEST(scan.first == scan.last);
  64. scan.first = cp;
  65. char const* cpy = "123.456";
  66. char const* cpy_first = cpy;
  67. char const* cpy_last = cpy + test_impl::string_length(cpy);
  68. scanner<> scany(cpy_first, cpy_last);
  69. hit = longest_d[(+digit_p >> '.' >> +digit_p) | (+digit_p)].parse(scany);
  70. BOOST_TEST(!!hit);
  71. BOOST_TEST(scany.first == scany.last);
  72. scany.first = cpy;
  73. hit = shortest_d[(+digit_p >> '.' >> +digit_p) | (+digit_p)].parse(scany);
  74. BOOST_TEST(!!hit);
  75. BOOST_TEST(scany.first != scany.last);
  76. scany.first = cpy;
  77. char const* cpz = "razamanaz";
  78. char const* cpz_first = cpz;
  79. char const* cpz_last = cpz + test_impl::string_length(cpz);
  80. scanner<> scanz(cpz_first, cpz_last);
  81. hit = longest_d[str_p("raza") | "razaman" | "razamanaz"].parse(scanz);
  82. BOOST_TEST(!!hit);
  83. BOOST_TEST(scanz.first == scanz.last);
  84. scanz.first = cpz;
  85. hit = shortest_d[str_p("raza") | "razaman" | "razamanaz"].parse(scanz);
  86. BOOST_TEST(!!hit);
  87. BOOST_TEST(scanz.first == cpz+4);
  88. scanz.first = cpz;
  89. // bounds_d
  90. parse_info<> pr = parse("123", limit_d(0, 60)[int_p]);
  91. BOOST_TEST(!pr.hit);
  92. pr = parse("-2", limit_d(0, 60)[int_p]);
  93. BOOST_TEST(!pr.hit);
  94. pr = parse("60", limit_d(0, 60)[int_p]);
  95. BOOST_TEST(pr.hit);
  96. pr = parse("0", limit_d(0, 60)[int_p]);
  97. BOOST_TEST(pr.hit);
  98. pr = parse("-2", min_limit_d(0)[int_p]);
  99. BOOST_TEST(!pr.hit);
  100. pr = parse("-2", min_limit_d(-5)[int_p]);
  101. BOOST_TEST(pr.hit);
  102. pr = parse("101", max_limit_d(100)[int_p]);
  103. BOOST_TEST(!pr.hit);
  104. pr = parse("100", max_limit_d(100)[int_p]);
  105. BOOST_TEST(pr.hit);
  106. }
  107. struct identifier : public grammar<identifier>
  108. {
  109. template <typename ScannerT>
  110. struct definition
  111. {
  112. definition(identifier const& /*self*/)
  113. {
  114. rr = +(alpha_p | '_');
  115. r = lexeme_d[rr];
  116. }
  117. rule<typename lexeme_scanner<ScannerT>::type> rr;
  118. rule<ScannerT> r;
  119. rule<ScannerT> const&
  120. start() const { return r; }
  121. };
  122. };
  123. void
  124. directives_test2()
  125. {
  126. // Test that lexeme_d does not skip trailing spaces
  127. std::string str1, str2;
  128. identifier ident;
  129. parse("rock_n_roll never_dies ",
  130. ident[assign_a(str1)] >> ident[assign_a(str2)], space_p
  131. );
  132. std::cout << '*' << str1 << ',' << str2 << '*' << std::endl;
  133. BOOST_TEST(str1 == "rock_n_roll");
  134. BOOST_TEST(str2 == "never_dies");
  135. }
  136. ///////////////////////////////////////////////////////////////////////////////
  137. //
  138. // Main
  139. //
  140. ///////////////////////////////////////////////////////////////////////////////
  141. int
  142. main()
  143. {
  144. directives_test1();
  145. directives_test2();
  146. return boost::report_errors();
  147. }