test_re2c_lexer.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #if !BOOST_WORKAROUND(__SUNPRO_CC, <= 0x580)
  16. #undef BOOST_WAVE_SEPARATE_LEXER_INSTANTIATION
  17. #endif
  18. #include <boost/detail/lightweight_test.hpp>
  19. #if defined(TESTLEXERS_TIMING)
  20. #include "high_resolution_timer.hpp"
  21. #endif
  22. // include the Re2C lexer related stuff
  23. #include <boost/wave/cpplexer/cpp_lex_token.hpp> // token type
  24. #include <boost/wave/cpplexer/cpp_lex_iterator.hpp>
  25. #include <boost/wave/cpplexer/re2clex/cpp_re2c_lexer.hpp> // lexer type
  26. typedef boost::wave::cpplexer::lex_token<> token_type;
  27. typedef boost::wave::cpplexer::lex_iterator<token_type> lexer_type;
  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 < 1000; ++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 tokenization
  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. if (boost::wave::T_EOF != boost::wave::token_id(*it)) {
  61. BOOST_TEST(boost::wave::T_EOF == boost::wave::token_id(*it));
  62. std::cerr << data->token << ": not fully matched, "
  63. << "first non-matched token was: " << (*it).get_value()
  64. << std::endl;
  65. }
  66. }
  67. #if defined(TESTLEXERS_TIMING)
  68. }
  69. std::cout << tim.elapsed() << " [s]" << std::endl;
  70. #endif
  71. }
  72. catch (boost::wave::cpplexer::lexing_exception &e) {
  73. // some lexing error
  74. std::cerr
  75. << "test_re2c_lexer: "
  76. << e.description() << std::endl;
  77. return (std::numeric_limits<int>::max)() - 1;
  78. }
  79. return boost::report_errors();
  80. }