test_xlex_lexer.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #include <boost/detail/lightweight_test.hpp>
  16. #if defined(TESTLEXERS_TIMING)
  17. #include "high_resolution_timer.hpp"
  18. #endif
  19. ///////////////////////////////////////////////////////////////////////////////
  20. // include the Xpressive lexer related stuff
  21. #include <boost/wave/cpplexer/cpp_lex_token.hpp> // token type
  22. #include <libs/wave/samples/token_statistics/xlex/xlex_lexer.hpp> // lexer type
  23. typedef boost::wave::cpplexer::lex_token<> token_type;
  24. typedef boost::wave::cpplexer::xlex::xlex_iterator<token_type> lexer_type;
  25. // This instantiates the correct 'new_lexer' function, which generates the
  26. // C++ lexer used in this test.
  27. template struct boost::wave::cpplexer::xlex::new_lexer_gen<std::string::iterator>;
  28. ///////////////////////////////////////////////////////////////////////////////
  29. // include test data
  30. #include "cpp_tokens.hpp"
  31. ///////////////////////////////////////////////////////////////////////////////
  32. int
  33. main(int argc, char *argv[])
  34. {
  35. try {
  36. token_type::position_type pos("<testdata>");
  37. #if defined(TESTLEXERS_TIMING)
  38. boost::high_resolution_timer tim;
  39. for (int i = 0; i < 10; ++i) {
  40. #endif
  41. for (lexem const* data = lexems; NULL != data->token; ++data) {
  42. // feed the token to the lexer
  43. token_type::string_type instr(data->token);
  44. lexer_type it = lexer_type(instr.begin(), instr.end(), pos,
  45. boost::wave::support_option_long_long);
  46. lexer_type end = lexer_type();
  47. // verify the correct outcome of the tokenisation
  48. #if defined(TESTLEXERS_VERBOSE)
  49. std::cerr << boost::wave::get_token_name(data->id) << std::endl;
  50. #endif
  51. if (data->id != boost::wave::token_id(*it)) {
  52. BOOST_TEST(data->id == boost::wave::token_id(*it));
  53. std::cerr << data->token << ": expected: "
  54. << boost::wave::get_token_name(data->id);
  55. std::cerr << ", found: "
  56. << boost::wave::get_token_name(boost::wave::token_id(*it))
  57. << std::endl;
  58. }
  59. BOOST_TEST(++it != end);
  60. BOOST_TEST(boost::wave::T_EOF == boost::wave::token_id(*it));
  61. }
  62. #if defined(TESTLEXERS_TIMING)
  63. }
  64. std::cout << tim.elapsed() << " [s]" << std::endl;
  65. #endif
  66. }
  67. catch (boost::wave::cpplexer::lexing_exception &e) {
  68. // some lexing error
  69. std::cerr
  70. << "test_xlex_lexer: "
  71. << e.description() << std::endl;
  72. return (std::numeric_limits<int>::max)() - 1;
  73. }
  74. return boost::report_errors();
  75. }