auto_switch_lexerstate.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. // Additionally this test makes sure the syntax 'self("state", "targetstate")'
  10. // works properly.
  11. #include <boost/config/warning_disable.hpp>
  12. #include <boost/detail/lightweight_test.hpp>
  13. #include <boost/spirit/include/support_multi_pass.hpp>
  14. #include <boost/spirit/include/classic_position_iterator.hpp>
  15. #include <boost/spirit/include/lex_lexertl.hpp>
  16. namespace spirit = boost::spirit;
  17. namespace lex = spirit::lex;
  18. typedef spirit::classic::position_iterator2<
  19. spirit::multi_pass<std::istreambuf_iterator<char> >
  20. > file_iterator;
  21. inline file_iterator
  22. make_file_iterator(std::istream& input, const std::string& filename)
  23. {
  24. return file_iterator(
  25. spirit::make_default_multi_pass(
  26. std::istreambuf_iterator<char>(input)),
  27. spirit::multi_pass<std::istreambuf_iterator<char> >(),
  28. filename);
  29. }
  30. typedef lex::lexertl::token<file_iterator> token_type;
  31. struct lexer
  32. : lex::lexer<lex::lexertl::actor_lexer<token_type> >
  33. {
  34. lexer() : word("^[a-zA-Z0-9]+$", 1)
  35. {
  36. self("INITIAL", "O") =
  37. word
  38. | lex::string("!.*$") [
  39. lex::_pass = lex::pass_flags::pass_ignore
  40. ]
  41. | lex::token_def<>('\n', 2)
  42. ;
  43. self("O", "INITIAL") =
  44. lex::string(".") [
  45. lex::_pass = lex::pass_flags::pass_fail
  46. ]
  47. ;
  48. }
  49. lex::token_def<> word;
  50. };
  51. typedef lexer::iterator_type token_iterator;
  52. int main()
  53. {
  54. std::stringstream ss;
  55. ss << "!foo\nbar\n!baz";
  56. file_iterator begin = make_file_iterator(ss, "SS");
  57. file_iterator end;
  58. lexer l;
  59. token_iterator begin2 = l.begin(begin, end);
  60. token_iterator end2 = l.end();
  61. std::size_t test_data[] = { 2, 1, 2 };
  62. std::size_t const test_data_size = sizeof(test_data)/sizeof(test_data[0]);
  63. token_iterator it = begin2;
  64. std::size_t i = 0;
  65. for (/**/; it != end2 && i < test_data_size; ++it, ++i)
  66. {
  67. BOOST_TEST(it->id() == test_data[i]);
  68. }
  69. BOOST_TEST(it == end2);
  70. BOOST_TEST(i == test_data_size);
  71. return boost::report_errors();
  72. }