1 /*=============================================================================
2 Boost.Wave: A Standard compliant C++ preprocessor library
3
4 http://www.boost.org/
5
6 Copyright (c) 2001-2010 Hartmut Kaiser. Distributed under the Boost
7 Software License, Version 1.0. (See accompanying file
8 LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 =============================================================================*/
10
11 #include <iostream>
12 #include <iomanip>
13 #include <fstream>
14 #include <string>
15
16 ///////////////////////////////////////////////////////////////////////////////
17 // Include Wave itself
18 #include <boost/wave.hpp>
19
20 ///////////////////////////////////////////////////////////////////////////////
21 // Include the lexer stuff
22 #include <boost/wave/cpplexer/cpp_lex_token.hpp> // token class
23 #include <boost/wave/cpplexer/cpp_lex_iterator.hpp> // lexer class
24
25 ///////////////////////////////////////////////////////////////////////////////
26 //
27 // Special output operator for a lex_token.
28 //
29 // Note: this doesn't compile if BOOST_SPIRIT_DEBUG is defined.
30 //
31 ///////////////////////////////////////////////////////////////////////////////
32 template <typename PositionT>
33 inline std::ostream &
operator <<(std::ostream & stream,boost::wave::cpplexer::lex_token<PositionT> const & t)34 operator<< (std::ostream &stream,
35 boost::wave::cpplexer::lex_token<PositionT> const &t)
36 {
37 using namespace std;
38 using namespace boost::wave;
39
40 token_id id = token_id(t);
41 stream << setw(16)
42 << left << boost::wave::get_token_name(id) << " ("
43 << "#" << setw(3) << BASEID_FROM_TOKEN(id);
44
45 if (ExtTokenTypeMask & id) {
46 // this is an extended token id
47 if (AltTokenType == (id & ExtTokenOnlyMask)) {
48 stream << ", AltTokenType";
49 }
50 else if (TriGraphTokenType == (id & ExtTokenOnlyMask)) {
51 stream << ", TriGraphTokenType";
52 }
53 else if (AltExtTokenType == (id & ExtTokenOnlyMask)){
54 stream << ", AltExtTokenType";
55 }
56 }
57
58 stream
59 << ") at " << t.get_position().get_file() << " ("
60 << setw(3) << right << t.get_position().get_line() << "/"
61 << setw(2) << right << t.get_position().get_column()
62 << "): >";
63
64 typedef typename boost::wave::cpplexer::lex_token<PositionT>::string_type
65 string_type;
66
67 string_type const& value = t.get_value();
68 for (std::size_t i = 0; i < value.size(); ++i) {
69 switch (value[i]) {
70 case '\r': stream << "\\r"; break;
71 case '\n': stream << "\\n"; break;
72 case '\t': stream << "\\t"; break;
73 default:
74 stream << value[i];
75 break;
76 }
77 }
78 stream << "<";
79
80 return stream;
81 }
82
83 ///////////////////////////////////////////////////////////////////////////////
84 // main entry point
main(int argc,char * argv[])85 int main(int argc, char *argv[])
86 {
87 if (2 != argc) {
88 std::cerr << "Usage: lexed_tokens infile" << std::endl;
89 return -1;
90 }
91
92 // current file position is saved for exception handling
93 boost::wave::util::file_position_type current_position;
94
95 try {
96 // Open and read in the specified input file.
97 std::ifstream instream(argv[1]);
98 std::string instr;
99
100 if (!instream.is_open()) {
101 std::cerr << "Could not open input file: " << argv[1] << std::endl;
102 return -2;
103 }
104 instream.unsetf(std::ios::skipws);
105 instr = std::string(std::istreambuf_iterator<char>(instream.rdbuf()),
106 std::istreambuf_iterator<char>());
107
108 // tokenize the input data into C++ tokens using the C++ lexer
109 typedef boost::wave::cpplexer::lex_token<> token_type;
110 typedef boost::wave::cpplexer::lex_iterator<token_type> lexer_type;
111 typedef token_type::position_type position_type;
112
113 position_type pos(argv[1]);
114 lexer_type it = lexer_type(instr.begin(), instr.end(), pos,
115 boost::wave::language_support(
116 boost::wave::support_cpp|boost::wave::support_option_long_long));
117 lexer_type end = lexer_type();
118
119 while (it != end) {
120 current_position = (*it).get_position(); // for error reporting
121 std::cout << *it << std::endl; // dump the tokenf info
122 ++it;
123 }
124 }
125 catch (boost::wave::cpplexer::lexing_exception const& e) {
126 // some lexing error
127 std::cerr
128 << e.file_name() << "(" << e.line_no() << "): "
129 << e.description() << std::endl;
130 return 2;
131 }
132 catch (std::exception const& e) {
133 // use last recognized token to retrieve the error position
134 std::cerr
135 << current_position.get_file()
136 << "(" << current_position.get_line() << "): "
137 << "exception caught: " << e.what()
138 << std::endl;
139 return 3;
140 }
141 catch (...) {
142 // use last recognized token to retrieve the error position
143 std::cerr
144 << current_position.get_file()
145 << "(" << current_position.get_line() << "): "
146 << "unexpected exception caught." << std::endl;
147 return 4;
148 }
149 return 0;
150 }
151