lexed_tokens.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*=============================================================================
  2. Boost.Wave: A Standard compliant C++ preprocessor library
  3. http://www.boost.org/
  4. Copyright (c) 2001-2010 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. #include <iostream>
  9. #include <iomanip>
  10. #include <fstream>
  11. #include <string>
  12. #include <vector>
  13. ///////////////////////////////////////////////////////////////////////////////
  14. // Include Wave itself
  15. #include <boost/wave.hpp>
  16. ///////////////////////////////////////////////////////////////////////////////
  17. // Include the lexer stuff
  18. #include <boost/wave/cpplexer/cpp_lex_token.hpp> // token class
  19. #include <boost/wave/cpplexer/cpp_lex_iterator.hpp> // lexer class
  20. ///////////////////////////////////////////////////////////////////////////////
  21. //
  22. // Special output operator for a lex_token.
  23. //
  24. // Note: this doesn't compile if BOOST_SPIRIT_DEBUG is defined.
  25. //
  26. ///////////////////////////////////////////////////////////////////////////////
  27. template <typename PositionT>
  28. inline std::ostream &
  29. operator<< (std::ostream &stream,
  30. boost::wave::cpplexer::lex_token<PositionT> const &t)
  31. {
  32. using namespace std;
  33. using namespace boost::wave;
  34. token_id id = token_id(t);
  35. stream << setw(16)
  36. << left << boost::wave::get_token_name(id) << " ("
  37. << "#" << setw(3) << BASEID_FROM_TOKEN(id);
  38. if (ExtTokenTypeMask & id) {
  39. // this is an extended token id
  40. if (AltTokenType == (id & ExtTokenOnlyMask)) {
  41. stream << ", AltTokenType";
  42. }
  43. else if (TriGraphTokenType == (id & ExtTokenOnlyMask)) {
  44. stream << ", TriGraphTokenType";
  45. }
  46. else if (AltExtTokenType == (id & ExtTokenOnlyMask)){
  47. stream << ", AltExtTokenType";
  48. }
  49. }
  50. stream
  51. << ") at " << t.get_position().get_file() << " ("
  52. << setw(3) << right << t.get_position().get_line() << "/"
  53. << setw(2) << right << t.get_position().get_column()
  54. << "): >";
  55. typedef typename boost::wave::cpplexer::lex_token<PositionT>::string_type
  56. string_type;
  57. string_type const& value = t.get_value();
  58. for (std::size_t i = 0; i < value.size(); ++i) {
  59. switch (value[i]) {
  60. case '\r': stream << "\\r"; break;
  61. case '\n': stream << "\\n"; break;
  62. case '\t': stream << "\\t"; break;
  63. default:
  64. stream << value[i];
  65. break;
  66. }
  67. }
  68. stream << "<";
  69. return stream;
  70. }
  71. ///////////////////////////////////////////////////////////////////////////////
  72. // main entry point
  73. int main(int argc, char *argv[])
  74. {
  75. if (2 != argc) {
  76. std::cerr << "Usage: lexed_tokens infile" << std::endl;
  77. return -1;
  78. }
  79. // current file position is saved for exception handling
  80. boost::wave::util::file_position_type current_position;
  81. try {
  82. // Open and read in the specified input file.
  83. std::ifstream instream(argv[1]);
  84. std::string instr;
  85. if (!instream.is_open()) {
  86. std::cerr << "Could not open input file: " << argv[1] << std::endl;
  87. return -2;
  88. }
  89. instream.unsetf(std::ios::skipws);
  90. instr = std::string(std::istreambuf_iterator<char>(instream.rdbuf()),
  91. std::istreambuf_iterator<char>());
  92. // tokenize the input data into C++ tokens using the C++ lexer
  93. typedef boost::wave::cpplexer::lex_token<> token_type;
  94. typedef boost::wave::cpplexer::lex_iterator<token_type> lexer_type;
  95. typedef token_type::position_type position_type;
  96. position_type pos(argv[1]);
  97. lexer_type it = lexer_type(instr.begin(), instr.end(), pos,
  98. boost::wave::language_support(
  99. boost::wave::support_cpp|boost::wave::support_option_long_long));
  100. lexer_type end = lexer_type();
  101. while (it != end) {
  102. current_position = (*it).get_position(); // for error reporting
  103. std::cout << *it << std::endl; // dump the tokenf info
  104. ++it;
  105. }
  106. }
  107. catch (boost::wave::cpplexer::lexing_exception const& e) {
  108. // some lexing error
  109. std::cerr
  110. << e.file_name() << "(" << e.line_no() << "): "
  111. << e.description() << std::endl;
  112. return 2;
  113. }
  114. catch (std::exception const& e) {
  115. // use last recognized token to retrieve the error position
  116. std::cerr
  117. << current_position.get_file()
  118. << "(" << current_position.get_line() << "): "
  119. << "exception caught: " << e.what()
  120. << std::endl;
  121. return 3;
  122. }
  123. catch (...) {
  124. // use last recognized token to retrieve the error position
  125. std::cerr
  126. << current_position.get_file()
  127. << "(" << current_position.get_line() << "): "
  128. << "unexpected exception caught." << std::endl;
  129. return 4;
  130. }
  131. return 0;
  132. }