/*============================================================================= Boost.Wave: A Standard compliant C++ preprocessor library Example demonstrating how to preprocess the token stream generated by a #pragma directive http://www.boost.org/ Copyright (c) 2001-2010 Hartmut Kaiser. Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) =============================================================================*/ #include #include #include #include /////////////////////////////////////////////////////////////////////////////// // Include Wave itself #include /////////////////////////////////////////////////////////////////////////////// // Include the lexer stuff #include // token class #include // lexer class /////////////////////////////////////////////////////////////////////////////// // Include special preprocessing hooks #include "preprocess_pragma_output.hpp" /////////////////////////////////////////////////////////////////////////////// // main entry point int main(int argc, char *argv[]) { if (2 != argc) { std::cerr << "Usage: preprocess_pragma_output infile" << std::endl; return -1; } // current file position is saved for exception handling boost::wave::util::file_position_type current_position; try { // Open and read in the specified input file. std::ifstream instream(argv[1]); std::string instring; if (!instream.is_open()) { std::cerr << "Could not open input file: " << argv[1] << std::endl; return -2; } instream.unsetf(std::ios::skipws); instring = std::string(std::istreambuf_iterator(instream.rdbuf()), std::istreambuf_iterator()); // The template boost::wave::cpplexer::lex_token<> is the token type to be // used by the Wave library. typedef boost::wave::cpplexer::lex_token<> token_type; // The template boost::wave::cpplexer::lex_iterator<> is the lexer type to // be used by the Wave library. typedef boost::wave::cpplexer::lex_iterator lex_iterator_type; // This is the resulting context type to use. typedef boost::wave::context< std::string::iterator, lex_iterator_type, boost::wave::iteration_context_policies::load_file_to_string, preprocess_pragma_output_hooks> context_type; // The preprocessor iterator shouldn't be constructed directly. It is // to be generated through a wave::context<> object. This wave:context<> // object is to be used additionally to initialize and define different // parameters of the actual preprocessing (not done here). // // The preprocessing of the input stream is done on the fly behind the // scenes during iteration over the context_type::iterator_type stream. context_type ctx (instring.begin(), instring.end(), argv[1]); // analyze the input file context_type::iterator_type first = ctx.begin(); context_type::iterator_type last = ctx.end(); while (first != last) { current_position = (*first).get_position(); std::cout << (*first).get_value(); ++first; } } catch (boost::wave::cpp_exception const& e) { // some preprocessing error std::cerr << e.file_name() << "(" << e.line_no() << "): " << e.description() << std::endl; return 2; } catch (std::exception const& e) { // use last recognized token to retrieve the error position std::cerr << current_position.get_file() << "(" << current_position.get_line() << "): " << "exception caught: " << e.what() << std::endl; return 3; } catch (...) { // use last recognized token to retrieve the error position std::cerr << current_position.get_file() << "(" << current_position.get_line() << "): " << "unexpected exception caught." << std::endl; return 4; } return 0; }