test_slex_lexer.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*=============================================================================
  2. Boost.Wave: A Standard compliant C++ preprocessor library
  3. http://www.boost.org/
  4. Copyright (c) 2001-2012 Hartmut Kaiser. Distributed under the Boost
  5. Software License, Version 1.0. (See accompanying file
  6. LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. // disable stupid compiler warnings
  9. #include <boost/config/warning_disable.hpp>
  10. // system headers
  11. #include <string>
  12. #include <iostream>
  13. #include <limits>
  14. #include <boost/wave/wave_config.hpp>
  15. #undef BOOST_WAVE_SEPARATE_LEXER_INSTANTIATION
  16. #include <boost/detail/lightweight_test.hpp>
  17. #if defined(TESTLEXERS_TIMING)
  18. #include "high_resolution_timer.hpp"
  19. #endif
  20. ///////////////////////////////////////////////////////////////////////////////
  21. // include the Slex lexer related stuff
  22. #include <libs/wave/samples/cpp_tokens/slex_token.hpp> // token type
  23. #include <libs/wave/samples/cpp_tokens/slex/cpp_slex_lexer.hpp> // lexer type
  24. typedef boost::wave::cpplexer::slex_token<> token_type;
  25. typedef boost::wave::cpplexer::slex::slex_iterator<token_type> lexer_type;
  26. // This instantiates the correct 'new_lexer' function, which generates the
  27. // C++ lexer used in this test.
  28. template struct boost::wave::cpplexer::slex::new_lexer_gen<
  29. std::string::iterator>;
  30. ///////////////////////////////////////////////////////////////////////////////
  31. // include test data
  32. #include "cpp_tokens.hpp"
  33. ///////////////////////////////////////////////////////////////////////////////
  34. int
  35. main(int argc, char *argv[])
  36. {
  37. try {
  38. token_type::position_type pos("<testdata>");
  39. #if defined(TESTLEXERS_TIMING)
  40. boost::high_resolution_timer tim;
  41. for (int i = 0; i < 1000; ++i) {
  42. #endif
  43. for (lexem const* data = lexems; NULL != data->token; ++data) {
  44. // feed the token to the lexer
  45. token_type::string_type instr(data->token);
  46. lexer_type it = lexer_type(instr.begin(), instr.end(), pos,
  47. boost::wave::support_option_long_long);
  48. lexer_type end = lexer_type();
  49. // verify the correct outcome of the tokenization
  50. #if defined(TESTLEXERS_VERBOSE)
  51. std::cerr << boost::wave::get_token_name(data->id) << std::endl;
  52. #endif
  53. if (data->id != boost::wave::token_id(*it)) {
  54. BOOST_TEST(data->id == boost::wave::token_id(*it));
  55. std::cerr << data->token << ": expected: "
  56. << boost::wave::get_token_name(data->id);
  57. std::cerr << ", found: "
  58. << boost::wave::get_token_name(boost::wave::token_id(*it))
  59. << std::endl;
  60. }
  61. BOOST_TEST(++it != end);
  62. if (boost::wave::T_EOF != boost::wave::token_id(*it)) {
  63. BOOST_TEST(boost::wave::T_EOF == boost::wave::token_id(*it));
  64. std::cerr << data->token << ": not fully matched, "
  65. << "first non-matched token was: " << (*it).get_value()
  66. << std::endl;
  67. }
  68. }
  69. #if defined(TESTLEXERS_TIMING)
  70. }
  71. std::cout << tim.elapsed() << " [s]" << std::endl;
  72. #endif
  73. }
  74. catch (boost::wave::cpplexer::lexing_exception &e) {
  75. // some lexing error
  76. std::cerr
  77. << "test_slex_lexer: "
  78. << e.description() << std::endl;
  79. return (std::numeric_limits<int>::max)() - 1;
  80. }
  81. return boost::report_errors();
  82. }