1 /*=============================================================================
2 Boost.Wave: A Standard compliant C++ preprocessor library
3 Example demonstrating how to preprocess the token stream generated by a
8 Copyright (c) 2001-2010 Hartmut Kaiser. Distributed under the Boost
9 Software License, Version 1.0. (See accompanying file
10 LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 =============================================================================*/
18 ///////////////////////////////////////////////////////////////////////////////
19 // Include Wave itself
20 #include <boost/wave.hpp>
22 ///////////////////////////////////////////////////////////////////////////////
23 // Include the lexer stuff
24 #include <boost/wave/cpplexer/cpp_lex_token.hpp> // token class
25 #include <boost/wave/cpplexer/cpp_lex_iterator.hpp> // lexer class
27 ///////////////////////////////////////////////////////////////////////////////
28 // Include special preprocessing hooks
29 #include "preprocess_pragma_output.hpp"
31 ///////////////////////////////////////////////////////////////////////////////
33 int main(int argc
, char *argv
[])
36 std::cerr
<< "Usage: preprocess_pragma_output infile" << std::endl
;
40 // current file position is saved for exception handling
41 boost::wave::util::file_position_type current_position
;
44 // Open and read in the specified input file.
45 std::ifstream
instream(argv
[1]);
48 if (!instream
.is_open()) {
49 std::cerr
<< "Could not open input file: " << argv
[1] << std::endl
;
52 instream
.unsetf(std::ios::skipws
);
53 instring
= std::string(std::istreambuf_iterator
<char>(instream
.rdbuf()),
54 std::istreambuf_iterator
<char>());
56 // The template boost::wave::cpplexer::lex_token<> is the token type to be
57 // used by the Wave library.
58 typedef boost::wave::cpplexer::lex_token
<> token_type
;
60 // The template boost::wave::cpplexer::lex_iterator<> is the lexer type to
61 // be used by the Wave library.
62 typedef boost::wave::cpplexer::lex_iterator
<token_type
> lex_iterator_type
;
64 // This is the resulting context type to use.
65 typedef boost::wave::context
<
66 std::string::iterator
, lex_iterator_type
,
67 boost::wave::iteration_context_policies::load_file_to_string
,
68 preprocess_pragma_output_hooks
>
71 // The preprocessor iterator shouldn't be constructed directly. It is
72 // to be generated through a wave::context<> object. This wave:context<>
73 // object is to be used additionally to initialize and define different
74 // parameters of the actual preprocessing (not done here).
76 // The preprocessing of the input stream is done on the fly behind the
77 // scenes during iteration over the context_type::iterator_type stream.
78 context_type
ctx (instring
.begin(), instring
.end(), argv
[1]);
80 // analyze the input file
81 context_type::iterator_type first
= ctx
.begin();
82 context_type::iterator_type last
= ctx
.end();
84 while (first
!= last
) {
85 current_position
= (*first
).get_position();
86 std::cout
<< (*first
).get_value();
90 catch (boost::wave::cpp_exception
const& e
) {
91 // some preprocessing error
93 << e
.file_name() << "(" << e
.line_no() << "): "
94 << e
.description() << std::endl
;
97 catch (std::exception
const& e
) {
98 // use last recognized token to retrieve the error position
100 << current_position
.get_file()
101 << "(" << current_position
.get_line() << "): "
102 << "exception caught: " << e
.what()
107 // use last recognized token to retrieve the error position
109 << current_position
.get_file()
110 << "(" << current_position
.get_line() << "): "
111 << "unexpected exception caught." << std::endl
;