]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/wave/samples/real_positions/real_positions.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / wave / samples / real_positions / real_positions.cpp
1 /*=============================================================================
2 Boost.Wave: A Standard compliant C++ preprocessor library
3
4 Sample showing how to correct the positions inside the returned tokens in
5 a way that these appear to be consecutive (ignoring positions from macro
6 definitions).
7
8 http://www.boost.org/
9
10 Copyright (c) 2001-2010 Hartmut Kaiser. Distributed under the Boost
11 Software License, Version 1.0. (See accompanying file
12 LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
13 =============================================================================*/
14
15 #include <iostream>
16 #include <fstream>
17 #include <iomanip>
18 #include <string>
19 #include <vector>
20
21 ///////////////////////////////////////////////////////////////////////////////
22 // Include Wave itself
23 #include <boost/wave.hpp>
24
25 ///////////////////////////////////////////////////////////////////////////////
26 // Include the lexer stuff
27 #include "real_position_token.hpp" // token class
28 #include <boost/wave/cpplexer/cpp_lex_iterator.hpp> // lexer type
29
30 #include "correct_token_positions.hpp"
31
32 ///////////////////////////////////////////////////////////////////////////////
33 //
34 // Special output operator for a lex_token.
35 //
36 // Note: this doesn't compile if BOOST_SPIRIT_DEBUG is defined.
37 //
38 ///////////////////////////////////////////////////////////////////////////////
39 template <typename PositionT>
40 inline std::ostream &
41 operator<< (std::ostream &stream, lex_token<PositionT> const &t)
42 {
43 using namespace std;
44 using namespace boost::wave;
45
46 token_id id = token_id(t);
47 stream << setw(16)
48 << left << boost::wave::get_token_name(id) << " ("
49 << "#" << setw(3) << BASEID_FROM_TOKEN(id);
50
51 if (ExtTokenTypeMask & id) {
52 // this is an extended token id
53 if (AltTokenType == (id & ExtTokenOnlyMask)) {
54 stream << ", AltTokenType";
55 }
56 else if (TriGraphTokenType == (id & ExtTokenOnlyMask)) {
57 stream << ", TriGraphTokenType";
58 }
59 else if (AltExtTokenType == (id & ExtTokenOnlyMask)){
60 stream << ", AltExtTokenType";
61 }
62 }
63
64 stream << "): >";
65
66 typedef typename lex_token<PositionT>::string_type string_type;
67
68 string_type const& value = t.get_value();
69 for (std::size_t i = 0; i < value.size(); ++i) {
70 switch (value[i]) {
71 case '\r': stream << "\\r"; break;
72 case '\n': stream << "\\n"; break;
73 case '\t': stream << "\\t"; break;
74 default:
75 stream << value[i];
76 break;
77 }
78 }
79 stream << "<" << std::endl;
80 stream << " at: " << t.get_position().get_file() << " ("
81 << setw(3) << right << t.get_position().get_line() << "/"
82 << setw(2) << right << t.get_position().get_column()
83 << ")" << std::endl;
84 stream << " and: " << t.get_corrected_position().get_file() << " ("
85 << setw(3) << right << t.get_corrected_position().get_line() << "/"
86 << setw(2) << right << t.get_corrected_position().get_column()
87 << ")";
88
89 return stream;
90 }
91
92 ///////////////////////////////////////////////////////////////////////////////
93 // main entry point
94 int main(int argc, char *argv[])
95 {
96 if (2 != argc) {
97 std::cerr << "Usage: real_positions infile" << std::endl;
98 return -1;
99 }
100
101 // current file position is saved for exception handling
102 boost::wave::util::file_position_type current_position;
103
104 try {
105 // Open and read in the specified input file.
106 std::ifstream instream(argv[1]);
107 std::string instring;
108
109 if (!instream.is_open()) {
110 std::cerr << "Could not open input file: " << argv[1] << std::endl;
111 return -2;
112 }
113 instream.unsetf(std::ios::skipws);
114 instring = std::string(std::istreambuf_iterator<char>(instream.rdbuf()),
115 std::istreambuf_iterator<char>());
116
117 // The template real_positions::lex_token<> is the token type to be
118 // used by the Wave library.
119 typedef lex_token<> token_type;
120
121 // The template boost::wave::cpplexer::lex_iterator<> is the lexer type to
122 // be used by the Wave library.
123 typedef boost::wave::cpplexer::lex_iterator<token_type>
124 lex_iterator_type;
125
126 // This is the resulting context type to use. The first template parameter
127 // should match the iterator type to be used during construction of the
128 // corresponding context object (see below).
129 typedef boost::wave::context<
130 std::string::iterator, lex_iterator_type,
131 boost::wave::iteration_context_policies::load_file_to_string,
132 correct_token_position<token_type> >
133 context_type;
134
135 // This preprocessor hooks are used to correct the file positions inside
136 // the tokens returned from the library
137 correct_token_position<token_type> hooks(argv[1]);
138
139 // The preprocessor iterator shouldn't be constructed directly. It is
140 // to be generated through a wave::context<> object. This wave:context<>
141 // object is to be used additionally to initialize and define different
142 // parameters of the actual preprocessing (not done here).
143 //
144 // The preprocessing of the input stream is done on the fly behind the
145 // scenes during iteration over the context_type::iterator_type stream.
146 context_type ctx (instring.begin(), instring.end(), argv[1], hooks);
147
148 // analyze the input file
149 context_type::iterator_type first = ctx.begin();
150 context_type::iterator_type last = ctx.end();
151
152 while (first != last) {
153 current_position = (*first).get_position();
154 std::cout << *first << std::endl;
155 ++first;
156 }
157 }
158 catch (boost::wave::cpp_exception const& e) {
159 // some preprocessing error
160 std::cerr
161 << e.file_name() << "(" << e.line_no() << "): "
162 << e.description() << std::endl;
163 return 2;
164 }
165 catch (std::exception const& e) {
166 // use last recognized token to retrieve the error position
167 std::cerr
168 << current_position.get_file()
169 << "(" << current_position.get_line() << "): "
170 << "exception caught: " << e.what()
171 << std::endl;
172 return 3;
173 }
174 catch (...) {
175 // use last recognized token to retrieve the error position
176 std::cerr
177 << current_position.get_file()
178 << "(" << current_position.get_line() << "): "
179 << "unexpected exception caught." << std::endl;
180 return 4;
181 }
182 return 0;
183 }