1 /*=============================================================================
2     Boost.Wave: A Standard compliant C++ preprocessor library
3 
4     http://www.boost.org/
5 
6     Copyright (c) 2001-2012 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 <fstream>
13 #include <string>
14 #include <vector>
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 // main entry point
main(int argc,char * argv[])27 int main(int argc, char *argv[])
28 {
29     if (2 != argc) {
30         std::cerr << "Usage: quick_start infile" << std::endl;
31         return -1;
32     }
33 
34 // current file position is saved for exception handling
35 boost::wave::util::file_position_type current_position;
36 
37     try {
38 //[quick_start_main
39     //  The following preprocesses the input file given by argv[1].
40     //  Open and read in the specified input file.
41         std::ifstream instream(argv[1]);
42         std::string instring;
43 
44         if (!instream.is_open()) {
45             std::cerr << "Could not open input file: " << argv[1] << std::endl;
46             return -2;
47         }
48         instream.unsetf(std::ios::skipws);
49         instring = std::string(std::istreambuf_iterator<char>(instream.rdbuf()),
50                                 std::istreambuf_iterator<char>());
51 
52     //  This token type is one of the central types used throughout the library.
53     //  It is a template parameter to some of the public classes and instances
54     //  of this type are returned from the iterators.
55         typedef boost::wave::cpplexer::lex_token<> token_type;
56 
57     //  The template boost::wave::cpplexer::lex_iterator<> is the lexer type to
58     //  to use as the token source for the preprocessing engine. It is
59     //  parametrized with the token type.
60         typedef boost::wave::cpplexer::lex_iterator<token_type> lex_iterator_type;
61 
62     //  This is the resulting context type. The first template parameter should
63     //  match the iterator type used during construction of the context
64     //  instance (see below). It is the type of the underlying input stream.
65         typedef boost::wave::context<std::string::iterator, lex_iterator_type>
66             context_type;
67 
68     //  The preprocessor iterator shouldn't be constructed directly. It is
69     //  generated through a wave::context<> object. This wave:context<> object
70     //  is additionally used to initialize and define different parameters of
71     //  the actual preprocessing (not done here).
72     //
73     //  The preprocessing of the input stream is done on the fly behind the
74     //  scenes during iteration over the range of context_type::iterator_type
75     //  instances.
76         context_type ctx (instring.begin(), instring.end(), argv[1]);
77 
78     //  Get the preprocessor iterators and use them to generate the token
79     //  sequence.
80         context_type::iterator_type first = ctx.begin();
81         context_type::iterator_type last = ctx.end();
82 
83     //  The input stream is preprocessed for you while iterating over the range
84     //  [first, last). The dereferenced iterator returns tokens holding
85     //  information about the preprocessed input stream, such as token type,
86     //  token value, and position.
87         while (first != last) {
88             current_position = (*first).get_position();
89             std::cout << (*first).get_value();
90             ++first;
91         }
92 //]
93     }
94     catch (boost::wave::cpp_exception const& e) {
95     // some preprocessing error
96         std::cerr
97             << e.file_name() << "(" << e.line_no() << "): "
98             << e.description() << std::endl;
99         return 2;
100     }
101     catch (std::exception const& e) {
102     // use last recognized token to retrieve the error position
103         std::cerr
104             << current_position.get_file()
105             << "(" << current_position.get_line() << "): "
106             << "exception caught: " << e.what()
107             << std::endl;
108         return 3;
109     }
110     catch (...) {
111     // use last recognized token to retrieve the error position
112         std::cerr
113             << current_position.get_file()
114             << "(" << current_position.get_line() << "): "
115             << "unexpected exception caught." << std::endl;
116         return 4;
117     }
118     return 0;
119 }
120