test_lexertl_lexer.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. // system headers
  9. #include <string>
  10. #include <iostream>
  11. #include <limits>
  12. #include <boost/wave/wave_config.hpp>
  13. #undef BOOST_WAVE_SEPARATE_LEXER_INSTANTIATION
  14. #include <boost/detail/lightweight_test.hpp>
  15. #if defined(TESTLEXERS_TIMING)
  16. #include "high_resolution_timer.hpp"
  17. #endif
  18. // include the lexertl lexer related stuff
  19. #include <boost/wave/cpplexer/cpp_lex_token.hpp> // token type
  20. #include <libs/wave/samples/list_includes/lexertl/lexertl_lexer.hpp> // lexer type
  21. typedef boost::wave::cpplexer::lex_token<> token_type;
  22. typedef boost::wave::cpplexer::lexertl::lex_iterator<token_type> lexer_type;
  23. ///////////////////////////////////////////////////////////////////////////////
  24. // include test data
  25. #include "cpp_tokens.hpp"
  26. ///////////////////////////////////////////////////////////////////////////////
  27. int
  28. main(int argc, char *argv[])
  29. {
  30. try {
  31. token_type::position_type pos("<testdata>");
  32. #if defined(TESTLEXERS_TIMING)
  33. boost::high_resolution_timer tim;
  34. for (int i = 0; i < 1000; ++i) {
  35. #endif
  36. for (lexem const* data = lexems; NULL != data->token; ++data) {
  37. // feed the token to the lexer
  38. token_type::string_type instr(data->token);
  39. lexer_type it = lexer_type(instr.begin(), instr.end(), pos,
  40. boost::wave::support_option_long_long);
  41. lexer_type end = lexer_type();
  42. // verify the correct outcome of the tokenization
  43. #if defined(TESTLEXERS_VERBOSE)
  44. std::cerr << boost::wave::get_token_name(data->id) << std::endl;
  45. #endif
  46. if (data->id != boost::wave::token_id(*it)) {
  47. BOOST_TEST(data->id == boost::wave::token_id(*it));
  48. std::cerr << data->token << ": expected: "
  49. << boost::wave::get_token_name(data->id);
  50. std::cerr << ", found: "
  51. << boost::wave::get_token_name(boost::wave::token_id(*it))
  52. << std::endl;
  53. }
  54. BOOST_TEST(++it != end);
  55. if (boost::wave::T_EOF != boost::wave::token_id(*it)) {
  56. BOOST_TEST(boost::wave::T_EOF == boost::wave::token_id(*it));
  57. std::cerr << data->token << ": not fully matched, "
  58. << "first non-matched token was: " << (*it).get_value()
  59. << std::endl;
  60. }
  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_lexertl_lexer: "
  71. << e.description() << std::endl;
  72. return (std::numeric_limits<int>::max)() - 1;
  73. }
  74. return boost::report_errors();
  75. }