scanner_tests.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*=============================================================================
  2. Copyright (c) 1998-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 <list>
  11. #include <boost/spirit/include/classic_core.hpp>
  12. #include "impl/string_length.hpp"
  13. using namespace BOOST_SPIRIT_CLASSIC_NS;
  14. ///////////////////////////////////////////////////////////////////////////////
  15. //
  16. // Scanner tests
  17. //
  18. ///////////////////////////////////////////////////////////////////////////////
  19. struct to_upper_iter_policy : public iteration_policy {
  20. char filter(char ch) const
  21. { using namespace std; return char(toupper(ch)); }
  22. };
  23. inline bool test_isspace(char c)
  24. {
  25. using namespace std; return isspace(c) != 0;
  26. }
  27. inline bool test_islower(char c)
  28. {
  29. using namespace std; return islower(c) != 0;
  30. }
  31. struct skip_white_iter_policy : public iteration_policy {
  32. template <typename ScannerT>
  33. void
  34. advance(ScannerT const& scan) const
  35. {
  36. do
  37. ++scan.first;
  38. while (!at_end(scan) && test_isspace(get(scan)));
  39. }
  40. };
  41. void
  42. scanner_tests()
  43. {
  44. char const* cp = "The Big Brown Fox Jumped \n\tOver The Lazy Dog's Back";
  45. char const* cp_first = cp;
  46. char const* cp_last = cp + test_impl::string_length(cp);
  47. scanner<char const*>
  48. pp1(cp_first, cp_last);
  49. // compile check only...
  50. scanner<> spp1(pp1); (void)spp1;
  51. scanner<> spp2(pp1); (void)spp2;
  52. // spp1 = spp2;
  53. // compile check only...
  54. while (!pp1.at_end())
  55. {
  56. std::cout << *pp1;
  57. ++pp1;
  58. }
  59. std::cout << '\n';
  60. cp_first = cp;
  61. std::list<char> li(cp_first, cp_last);
  62. std::list<char>::iterator li_first = li.begin();
  63. std::list<char>::iterator li_last = li.end();
  64. scanner<std::list<char>::iterator>
  65. pp2(li_first, li_last);
  66. while (!pp2.at_end())
  67. {
  68. std::cout << *pp2;
  69. ++pp2;
  70. }
  71. std::cout << '\n';
  72. li_first = li.begin();
  73. scanner<char const*, scanner_policies<to_upper_iter_policy> >
  74. pp3(cp_first, cp_last);
  75. while (!pp3.at_end())
  76. {
  77. std::cout << *pp3;
  78. BOOST_TEST(!test_islower(*pp3));
  79. ++pp3;
  80. }
  81. std::cout << '\n';
  82. cp_first = cp;
  83. scanner<char const*, scanner_policies<skip_white_iter_policy> >
  84. pp4(cp_first, cp_last);
  85. // compile check only...
  86. pp1.change_policies(scanner_policies<skip_white_iter_policy>());
  87. // compile check only...
  88. while (!pp4.at_end())
  89. {
  90. std::cout << *pp4;
  91. BOOST_TEST(!test_isspace(*pp4));
  92. ++pp4;
  93. }
  94. std::cout << '\n';
  95. cp_first = cp;
  96. std::cout << "sizeof(scanner<>) == " << sizeof(scanner<>) << '\n';
  97. parse_info<> pi = parse("12abcdefg12345ABCDEFG789", +digit_p, alpha_p);
  98. BOOST_TEST(pi.hit);
  99. BOOST_TEST(pi.full);
  100. pi = parse("abcdefg12345ABCDEFG789", +digit_p, alpha_p);
  101. BOOST_TEST(pi.hit);
  102. BOOST_TEST(pi.full);
  103. }
  104. ///////////////////////////////////////////////////////////////////////////////
  105. //
  106. // Main
  107. //
  108. ///////////////////////////////////////////////////////////////////////////////
  109. int
  110. main()
  111. {
  112. scanner_tests();
  113. return boost::report_errors();
  114. }