1 /*=============================================================================
2 Boost.Wave: A Standard compliant C++ preprocessor library
3 Sample demonstrating the usage of advanced preprocessor hooks.
4
5 http://www.boost.org/
6
7 Copyright (c) 2001-2010 Hartmut Kaiser. Distributed under the Boost
8 Software License, Version 1.0. (See accompanying file
9 LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
10 =============================================================================*/
11
12 #include <iostream>
13 #include <fstream>
14 #include <string>
15 #include <vector>
16
17 ///////////////////////////////////////////////////////////////////////////////
18 // Include Wave itself
19 #include <boost/wave.hpp>
20
21 ///////////////////////////////////////////////////////////////////////////////
22 // Include the lexer stuff
23 #include <boost/wave/cpplexer/cpp_lex_token.hpp> // token class
24 #include <boost/wave/cpplexer/cpp_lex_iterator.hpp> // lexer class
25
26 #include "advanced_hooks.hpp"
27
28 ///////////////////////////////////////////////////////////////////////////////
29 // Main entry point
30 //
31 // This sample shows how to use the advanced hooks to output not only the
32 // preprocessed tokens but also the conditional directives found in the input
33 // file (these are commented out, tough) and the tokens from inside the
34 // conditional block which were not evaluated because the corresponding
35 // condition was false. These tokens are commented out as well.
36 //
main(int argc,char * argv[])37 int main(int argc, char *argv[])
38 {
39 if (2 != argc) {
40 std::cerr << "Usage: advanced_hooks infile" << std::endl;
41 return -1;
42 }
43
44 // current file position is saved for exception handling
45 boost::wave::util::file_position_type current_position;
46
47 try {
48 // Open and read in the specified input file.
49 std::ifstream instream(argv[1]);
50 std::string instring;
51
52 if (!instream.is_open()) {
53 std::cerr << "Could not open input file: " << argv[1] << std::endl;
54 return -2;
55 }
56 instream.unsetf(std::ios::skipws);
57 instring = std::string(std::istreambuf_iterator<char>(instream.rdbuf()),
58 std::istreambuf_iterator<char>());
59
60 // The template boost::wave::cpplexer::lex_token<> is the token type to be
61 // used by the Wave library.
62 typedef boost::wave::cpplexer::lex_token<> token_type;
63
64 // The template boost::wave::cpplexer::lex_iterator<> is the lexer type to
65 // be used by the Wave library.
66 typedef boost::wave::cpplexer::lex_iterator<token_type> lex_iterator_type;
67
68 // This is the resulting context type to use. The first template parameter
69 // should match the iterator type to be used during construction of the
70 // corresponding context object (see below).
71 typedef boost::wave::context<std::string::iterator, lex_iterator_type,
72 boost::wave::iteration_context_policies::load_file_to_string,
73 advanced_preprocessing_hooks
74 > context_type;
75
76 // The preprocessor iterator shouldn't be constructed directly. It is
77 // to be generated through a wave::context<> object. This wave:context<>
78 // object additionally may be used to initialize and define different
79 // parameters of the actual preprocessing (not done here).
80 //
81 // The preprocessing of the input stream is done on the fly behind the
82 // scenes during iteration over the context_type::iterator_type stream.
83 context_type ctx (instring.begin(), instring.end(), argv[1]);
84
85 ctx.set_language(boost::wave::enable_long_long(ctx.get_language()));
86 ctx.set_language(boost::wave::enable_preserve_comments(ctx.get_language()));
87 ctx.set_language(boost::wave::enable_prefer_pp_numbers(ctx.get_language()));
88
89 // analyze the input file, print out the preprocessed tokens
90 context_type::iterator_type first = ctx.begin();
91 context_type::iterator_type last = ctx.end();
92
93 while (first != last) {
94 current_position = (*first).get_position();
95 std::cout << (*first).get_value();
96 ++first;
97 }
98 }
99 catch (boost::wave::cpp_exception const& e) {
100 // some preprocessing error
101 std::cerr
102 << e.file_name() << "(" << e.line_no() << "): "
103 << e.description() << std::endl;
104 return 2;
105 }
106 catch (std::exception const& e) {
107 // use last recognized token to retrieve the error position
108 std::cerr
109 << current_position.get_file()
110 << "(" << current_position.get_line() << "): "
111 << "exception caught: " << e.what()
112 << std::endl;
113 return 3;
114 }
115 catch (...) {
116 // use last recognized token to retrieve the error position
117 std::cerr
118 << current_position.get_file()
119 << "(" << current_position.get_line() << "): "
120 << "unexpected exception caught." << std::endl;
121 return 4;
122 }
123 return 0;
124 }
125