regression_file_iterator4.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright (c) 2001-2010 Hartmut Kaiser
  2. // Copyright (c) 2010 Mathias Gaunard
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. // This test makes sure that the BOL state (begin of line) is properly reset
  7. // if a token matched at the beginning of a line is discarded using
  8. // lex::pass_fail.
  9. #include <boost/config/warning_disable.hpp>
  10. #include <boost/detail/lightweight_test.hpp>
  11. #include <boost/spirit/include/support_multi_pass.hpp>
  12. #include <boost/spirit/include/classic_position_iterator.hpp>
  13. #include <boost/spirit/include/lex_lexertl.hpp>
  14. namespace spirit = boost::spirit;
  15. namespace lex = spirit::lex;
  16. typedef spirit::classic::position_iterator2<
  17. spirit::multi_pass<std::istreambuf_iterator<char> >
  18. > file_iterator;
  19. inline file_iterator
  20. make_file_iterator(std::istream& input, const std::string& filename)
  21. {
  22. return file_iterator(
  23. spirit::make_default_multi_pass(
  24. std::istreambuf_iterator<char>(input)),
  25. spirit::multi_pass<std::istreambuf_iterator<char> >(),
  26. filename);
  27. }
  28. typedef lex::lexertl::token<file_iterator> token_type;
  29. struct lexer
  30. : lex::lexer<lex::lexertl::actor_lexer<token_type> >
  31. {
  32. lexer() : word("^[a-zA-Z0-9]+$", 1)
  33. {
  34. self = word [
  35. lex::_state = "O"
  36. ]
  37. | lex::token_def<>("!.*$") [
  38. lex::_state = "O"
  39. , lex::_pass = lex::pass_flags::pass_ignore
  40. ]
  41. | lex::token_def<>('\n', 2) [
  42. lex::_state = "O"
  43. ]
  44. ;
  45. self("O") =
  46. lex::token_def<>(".") [
  47. lex::_state = "INITIAL"
  48. , lex::_pass = lex::pass_flags::pass_fail
  49. ]
  50. ;
  51. }
  52. lex::token_def<> word;
  53. };
  54. typedef lexer::iterator_type token_iterator;
  55. int main()
  56. {
  57. std::stringstream ss;
  58. ss << "!foo\nbar\n!baz";
  59. file_iterator begin = make_file_iterator(ss, "SS");
  60. file_iterator end;
  61. lexer l;
  62. token_iterator begin2 = l.begin(begin, end);
  63. token_iterator end2 = l.end();
  64. std::size_t test_data[] = { 2, 1, 2 };
  65. std::size_t const test_data_size = sizeof(test_data)/sizeof(test_data[0]);
  66. token_iterator it = begin2;
  67. std::size_t i = 0;
  68. for (/**/; it != end2 && i < test_data_size; ++it, ++i)
  69. {
  70. BOOST_TEST(it->id() == test_data[i]);
  71. }
  72. BOOST_TEST(it == end2);
  73. BOOST_TEST(i == test_data_size);
  74. return boost::report_errors();
  75. }