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