quick_start.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #include <iostream>
  9. #include <fstream>
  10. #include <string>
  11. #include <vector>
  12. ///////////////////////////////////////////////////////////////////////////////
  13. // Include Wave itself
  14. #include <boost/wave.hpp>
  15. ///////////////////////////////////////////////////////////////////////////////
  16. // Include the lexer stuff
  17. #include <boost/wave/cpplexer/cpp_lex_token.hpp> // token class
  18. #include <boost/wave/cpplexer/cpp_lex_iterator.hpp> // lexer class
  19. ///////////////////////////////////////////////////////////////////////////////
  20. // main entry point
  21. int main(int argc, char *argv[])
  22. {
  23. if (2 != argc) {
  24. std::cerr << "Usage: quick_start infile" << std::endl;
  25. return -1;
  26. }
  27. // current file position is saved for exception handling
  28. boost::wave::util::file_position_type current_position;
  29. try {
  30. //[quick_start_main
  31. // The following preprocesses the input file given by argv[1].
  32. // Open and read in the specified input file.
  33. std::ifstream instream(argv[1]);
  34. std::string instring;
  35. if (!instream.is_open()) {
  36. std::cerr << "Could not open input file: " << argv[1] << std::endl;
  37. return -2;
  38. }
  39. instream.unsetf(std::ios::skipws);
  40. instring = std::string(std::istreambuf_iterator<char>(instream.rdbuf()),
  41. std::istreambuf_iterator<char>());
  42. // This token type is one of the central types used throughout the library.
  43. // It is a template parameter to some of the public classes and instances
  44. // of this type are returned from the iterators.
  45. typedef boost::wave::cpplexer::lex_token<> token_type;
  46. // The template boost::wave::cpplexer::lex_iterator<> is the lexer type to
  47. // to use as the token source for the preprocessing engine. It is
  48. // parametrized with the token type.
  49. typedef boost::wave::cpplexer::lex_iterator<token_type> lex_iterator_type;
  50. // This is the resulting context type. The first template parameter should
  51. // match the iterator type used during construction of the context
  52. // instance (see below). It is the type of the underlying input stream.
  53. typedef boost::wave::context<std::string::iterator, lex_iterator_type>
  54. context_type;
  55. // The preprocessor iterator shouldn't be constructed directly. It is
  56. // generated through a wave::context<> object. This wave:context<> object
  57. // is additionally used to initialize and define different parameters of
  58. // the actual preprocessing (not done here).
  59. //
  60. // The preprocessing of the input stream is done on the fly behind the
  61. // scenes during iteration over the range of context_type::iterator_type
  62. // instances.
  63. context_type ctx (instring.begin(), instring.end(), argv[1]);
  64. // Get the preprocessor iterators and use them to generate the token
  65. // sequence.
  66. context_type::iterator_type first = ctx.begin();
  67. context_type::iterator_type last = ctx.end();
  68. // The input stream is preprocessed for you while iterating over the range
  69. // [first, last). The dereferenced iterator returns tokens holding
  70. // information about the preprocessed input stream, such as token type,
  71. // token value, and position.
  72. while (first != last) {
  73. current_position = (*first).get_position();
  74. std::cout << (*first).get_value();
  75. ++first;
  76. }
  77. //]
  78. }
  79. catch (boost::wave::cpp_exception const& e) {
  80. // some preprocessing error
  81. std::cerr
  82. << e.file_name() << "(" << e.line_no() << "): "
  83. << e.description() << std::endl;
  84. return 2;
  85. }
  86. catch (std::exception const& e) {
  87. // use last recognized token to retrieve the error position
  88. std::cerr
  89. << current_position.get_file()
  90. << "(" << current_position.get_line() << "): "
  91. << "exception caught: " << e.what()
  92. << std::endl;
  93. return 3;
  94. }
  95. catch (...) {
  96. // use last recognized token to retrieve the error position
  97. std::cerr
  98. << current_position.get_file()
  99. << "(" << current_position.get_line() << "): "
  100. << "unexpected exception caught." << std::endl;
  101. return 4;
  102. }
  103. return 0;
  104. }