Click here to Skip to main content
15,860,972 members
Articles / Programming Languages / C++

Wave: a Standard conformant C++ preprocessor library

Rate me:
Please Sign up or sign in to vote.
4.96/5 (58 votes)
10 Jan 200413 min read 392.1K   4.4K   81  
Describes a free and fully Standard conformant C++ preprocessor library
/*=============================================================================
    Wave: A Standard compliant C++ preprocessor
    
    Sample: prints out the preprocessed tokens returned by the pp iterator

    Copyright (c) 2001-2004 Hartmut Kaiser
    http://spirit.sourceforge.net/

    Use, modification and distribution is subject to 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)

    See Copyright.txt for full acknowledgements.
=============================================================================*/

#include <iostream>
#include <fstream>
#include <string>

#include "cpp_tokens_config.hpp"                  // global configuration

#include "wave/cpplexer/cpp_token_ids.hpp"
#include "wave/cpplexer/cpp_lex_iterator.hpp"
#include "wave/cpplexer/cpp_lex_token.hpp"

#include "wave/cpp_context.hpp"

///////////////////////////////////////////////////////////////////////////////
//  include lexer specifics, import lexer names
#if defined(WAVE_USE_SLEX_CPP_LEXER)
// use the slex based C++ lexer

#if !defined(WAVE_SEPARATE_LEXER_INSTANTIATION)
#include "wave/cpplexer/slex/cpp_slex_lexer.hpp"
#endif // !defined(WAVE_SEPARATE_LEXER_INSTANTIATION)

#elif defined(WAVE_USE_RE2C_CPP_LEXER)
// use the re2c based C++ lexer

#if !defined(WAVE_SEPARATE_LEXER_INSTANTIATION)
#include "wave/cpplexer/re2clex/cpp_re2c_lexer.hpp"
#endif // !defined(WAVE_SEPARATE_LEXER_INSTANTIATION)

#endif

///////////////////////////////////////////////////////////////////////////////
//  import required names
using namespace boost::spirit;

using std::string;
using std::getline;
using std::ifstream;
using std::cout;
using std::cerr;
using std::endl;
using std::ostream;

///////////////////////////////////////////////////////////////////////////////
//  main program
int
main(int argc, char *argv[])
{
    if (2 != argc) {
        cout << "Usage: <cpp_tokens file>" << endl;
        return 1;
    }
    
    ifstream infile(argv[1]);
    string teststr;
    if (infile.is_open()) {
        infile.unsetf(ios::skipws);
        string line;
        for (getline(infile, line); infile.good(); getline(infile, line)) {
            teststr += line;
            teststr += '\n';
        }
    }
    else {
        teststr = argv[1];
    }

// The template lex_token<> is the token type to be used by the Wave library.
    typedef wave::context<std::string::iterator, wave::cpplexer::lex_token<> > 
        context_t;

// The C++ preprocessor iterator shouldn't be constructed directly. It is to be
// generated through a wave::context<> object. This wave:context<> object is 
// additionally to be used to initialize and define different parameters of 
// the actual preprocessing.
// The preprocessing of the input stream is done on the fly behind the scenes
// during iteration over the context_t::iterator_t stream.
context_t ctx (teststr.begin(), teststr.end(), argv[1]);
context_t::iterator_t first = ctx.begin();
context_t::iterator_t last = ctx.end();
context_t::token_t current_token;

    try {
        while (first != last) {
            using namespace wave::cpplexer;
            
        // retrieve next token
            current_token = *first;
        
        // find token name
        string tokenname (get_token_name(token_id(current_token)));

        // output token info
            cout << "matched token " 
                << tokenname 
                << "(#" << token_id(ID_FROM_TOKEN(current_token)) 
                << ") at " << current_token.get_position().file() << " (" 
                    << current_token.get_position().line() << "/" 
                    << current_token.get_position().column() 
                << "): >" << current_token.get_value() << "<"
                << endl;
            ++first;
        }
    }
    catch (wave::cpp_exception &e) {
    // some preprocessing error
        cerr 
            << e.file_name() << "(" << e.line_no() << "): "
            << e.description() << endl;
        return 2;
    }
    catch (std::exception &e) {
    // use last recognized token to retrieve the error position
        cerr 
            << current_token.get_position().file() 
            << "(" << current_token.get_position().line() << "): "
            << "unexpected exception: " << e.what()
            << endl;
        return 3;
    }
    catch (...) {
    // use last recognized token to retrieve the error position
        cerr 
            << current_token.get_position().file() 
            << "(" << current_token.get_position().line() << "): "
            << "unexpected exception." << endl;
        return 4;
    }
    return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
Actively involved in Boost and the development of the Spirit parser construction framework.

Comments and Discussions