regression_file_iterator1.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright (c) 2001-2011 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. #include <boost/config/warning_disable.hpp>
  7. #include <boost/detail/lightweight_test.hpp>
  8. #include <boost/spirit/include/support_multi_pass.hpp>
  9. #include <boost/spirit/include/classic_position_iterator.hpp>
  10. #include <boost/spirit/include/lex_lexertl.hpp>
  11. namespace spirit = boost::spirit;
  12. namespace lex = spirit::lex;
  13. typedef spirit::classic::position_iterator2<
  14. spirit::multi_pass<std::istreambuf_iterator<char> >
  15. > file_iterator;
  16. typedef boost::iterator_range<file_iterator> file_range;
  17. inline file_iterator
  18. make_file_iterator(std::istream& input, const std::string& filename)
  19. {
  20. return file_iterator(
  21. spirit::make_default_multi_pass(
  22. std::istreambuf_iterator<char>(input)),
  23. spirit::multi_pass<std::istreambuf_iterator<char> >(),
  24. filename);
  25. }
  26. struct identifier
  27. {
  28. identifier(file_iterator, file_iterator)
  29. {
  30. }
  31. };
  32. struct string_literal
  33. {
  34. string_literal(file_iterator, file_iterator)
  35. {
  36. }
  37. };
  38. typedef lex::lexertl::token<
  39. file_iterator, boost::mpl::vector<identifier, string_literal>
  40. > token_type;
  41. struct lexer
  42. : lex::lexer<lex::lexertl::actor_lexer<token_type> >
  43. {
  44. lexer()
  45. : id("[a-zA-Z0-9]+", 1)
  46. , st("'[^'\\n]*'", 2)
  47. {
  48. self("ST") =
  49. st [ lex::_state = "INITIAL" ]
  50. ;
  51. self("*") =
  52. id [ lex::_state = "ST" ]
  53. | lex::token_def<>(".", 3) [ lex::_state = "ST" ]
  54. ;
  55. }
  56. lex::token_def<identifier> id;
  57. lex::token_def<string_literal> st;
  58. };
  59. typedef lexer::iterator_type token_iterator;
  60. int main()
  61. {
  62. std::stringstream ss;
  63. ss << "foo 'bar'";
  64. file_iterator begin = make_file_iterator(ss, "SS");
  65. file_iterator end;
  66. lexer l;
  67. token_iterator begin2 = l.begin(begin, end, "ST");
  68. token_iterator end2 = l.end();
  69. std::size_t test_data[] = { 1, 3, 2 };
  70. std::size_t const test_data_size = sizeof(test_data)/sizeof(test_data[0]);
  71. token_iterator it = begin2;
  72. std::size_t i = 0;
  73. for (/**/; it != end2 && i < test_data_size; ++it, ++i)
  74. {
  75. BOOST_TEST(it->id() == test_data[i]);
  76. }
  77. BOOST_TEST(it == end2);
  78. BOOST_TEST(i == test_data_size);
  79. return boost::report_errors();
  80. }