regression_file_iterator3.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #define BOOST_SPIRIT_DEBUG 1 // required for token streaming
  7. // #define BOOST_SPIRIT_LEXERTL_DEBUG 1
  8. #include <boost/config/warning_disable.hpp>
  9. #include <boost/detail/lightweight_test.hpp>
  10. #include <boost/spirit/include/support_multi_pass.hpp>
  11. #include <boost/spirit/include/classic_position_iterator.hpp>
  12. #include <boost/spirit/include/lex_lexertl.hpp>
  13. #include <boost/spirit/include/phoenix_core.hpp>
  14. #include <boost/spirit/include/phoenix_operator.hpp>
  15. #include <boost/spirit/include/phoenix_statement.hpp>
  16. #include <sstream>
  17. namespace spirit = boost::spirit;
  18. namespace lex = spirit::lex;
  19. namespace phoenix = boost::phoenix;
  20. typedef spirit::classic::position_iterator2<
  21. spirit::multi_pass<std::istreambuf_iterator<char> >
  22. > file_iterator;
  23. typedef boost::iterator_range<file_iterator> file_range;
  24. inline file_iterator
  25. make_file_iterator(std::istream& input, const std::string& filename)
  26. {
  27. return file_iterator(
  28. spirit::make_default_multi_pass(
  29. std::istreambuf_iterator<char>(input)),
  30. spirit::multi_pass<std::istreambuf_iterator<char> >(),
  31. filename);
  32. }
  33. struct string_literal
  34. {
  35. string_literal(file_iterator, file_iterator)
  36. {
  37. }
  38. };
  39. typedef lex::lexertl::token<
  40. file_iterator, boost::mpl::vector<string_literal>
  41. > token_type;
  42. struct lexer
  43. : lex::lexer<lex::lexertl::actor_lexer<token_type> >
  44. {
  45. lexer() : st("'[^'\\n]*'", 1)
  46. {
  47. lex::token_def<> string_lookahead('\'');
  48. self("LA") = string_lookahead;
  49. // make sure lookahead is implicitly evaluated using the lexer state
  50. // the token_def has been associated with
  51. self = st [
  52. phoenix::if_(lex::lookahead(string_lookahead)) [ lex::more() ]
  53. ]
  54. ;
  55. }
  56. lex::token_def<string_literal> st;
  57. };
  58. typedef lexer::iterator_type token_iterator;
  59. int main()
  60. {
  61. std::stringstream ss;
  62. ss << "'foo''bar'";
  63. file_iterator begin = make_file_iterator(ss, "SS");
  64. file_iterator end;
  65. lexer l;
  66. token_iterator begin2 = l.begin(begin, end);
  67. token_iterator end2 = l.end();
  68. char const* test_data[] = { "1,'foo'", "1,'foo''bar'" };
  69. std::size_t const test_data_size = sizeof(test_data)/sizeof(test_data[0]);
  70. token_iterator it = begin2;
  71. std::size_t i = 0;
  72. for (/**/; it != end2 && i < test_data_size; ++it, ++i)
  73. {
  74. std::stringstream ss;
  75. ss << it->id() << "," << *it;
  76. BOOST_TEST(ss.str() == test_data[i]);
  77. }
  78. BOOST_TEST(it == end2);
  79. BOOST_TEST(i == test_data_size);
  80. return boost::report_errors();
  81. }