]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*============================================================================= |
2 | Boost.Wave: A Standard compliant C++ preprocessor library | |
3 | Example demonstrating how to preprocess the token stream generated by a | |
4 | #pragma directive | |
5 | ||
6 | http://www.boost.org/ | |
7 | ||
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 | =============================================================================*/ | |
12 | ||
13 | #include <iostream> | |
14 | #include <fstream> | |
15 | #include <string> | |
16 | #include <vector> | |
17 | ||
18 | /////////////////////////////////////////////////////////////////////////////// | |
19 | // Include Wave itself | |
20 | #include <boost/wave.hpp> | |
21 | ||
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 | |
26 | ||
27 | /////////////////////////////////////////////////////////////////////////////// | |
28 | // Include special preprocessing hooks | |
29 | #include "preprocess_pragma_output.hpp" | |
30 | ||
31 | /////////////////////////////////////////////////////////////////////////////// | |
32 | // main entry point | |
33 | int main(int argc, char *argv[]) | |
34 | { | |
35 | if (2 != argc) { | |
36 | std::cerr << "Usage: preprocess_pragma_output infile" << std::endl; | |
37 | return -1; | |
38 | } | |
39 | ||
40 | // current file position is saved for exception handling | |
41 | boost::wave::util::file_position_type current_position; | |
42 | ||
43 | try { | |
44 | // Open and read in the specified input file. | |
45 | std::ifstream instream(argv[1]); | |
46 | std::string instring; | |
47 | ||
48 | if (!instream.is_open()) { | |
49 | std::cerr << "Could not open input file: " << argv[1] << std::endl; | |
50 | return -2; | |
51 | } | |
52 | instream.unsetf(std::ios::skipws); | |
53 | instring = std::string(std::istreambuf_iterator<char>(instream.rdbuf()), | |
54 | std::istreambuf_iterator<char>()); | |
55 | ||
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; | |
59 | ||
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; | |
63 | ||
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> | |
69 | context_type; | |
70 | ||
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). | |
75 | // | |
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]); | |
79 | ||
80 | // analyze the input file | |
81 | context_type::iterator_type first = ctx.begin(); | |
82 | context_type::iterator_type last = ctx.end(); | |
83 | ||
84 | while (first != last) { | |
85 | current_position = (*first).get_position(); | |
86 | std::cout << (*first).get_value(); | |
87 | ++first; | |
88 | } | |
89 | } | |
90 | catch (boost::wave::cpp_exception const& e) { | |
91 | // some preprocessing error | |
92 | std::cerr | |
93 | << e.file_name() << "(" << e.line_no() << "): " | |
94 | << e.description() << std::endl; | |
95 | return 2; | |
96 | } | |
97 | catch (std::exception const& e) { | |
98 | // use last recognized token to retrieve the error position | |
99 | std::cerr | |
100 | << current_position.get_file() | |
101 | << "(" << current_position.get_line() << "): " | |
102 | << "exception caught: " << e.what() | |
103 | << std::endl; | |
104 | return 3; | |
105 | } | |
106 | catch (...) { | |
107 | // use last recognized token to retrieve the error position | |
108 | std::cerr | |
109 | << current_position.get_file() | |
110 | << "(" << current_position.get_line() << "): " | |
111 | << "unexpected exception caught." << std::endl; | |
112 | return 4; | |
113 | } | |
114 | return 0; | |
115 | } |