Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Wave: a Standard conformant C++ preprocessor library

, 10 Jan 2004
Describes a free and fully Standard conformant C++ preprocessor library
wave_preprocessor_demo.zip
wave.exe
wave_preprocessor_demo1.zip
wave.exe
wave_preprocessor_src.zip
wave
doc
theme
bkd.gif
bkd2.gif
bullet.gif
l_arr.gif
l_arr_disabled.gif
r_arr.gif
r_arr_disabled.gif
uc.gif
u_arr.gif
wave.gif
test
boost-build.jam
cpp_tokens
Jamfile.v2
list_includes
project-root.jam
wave
Jamfile
Jamfile.v2
runtests.sh
test_files
wave
cpplexer
re2clex
cpp.re
test
lextest.in
lextest.output
run_tests.sh
test_lexer.in
test_lexer.output
slex
test
lextest.in
lextest.re2c.output
lextest.slex.output
run_tests.sh
grammars
idllexer
re2clex
idl.re
util
wave_preprocessor_src1.zip
bkd.gif
bkd2.gif
bullet.gif
l_arr.gif
l_arr_disabled.gif
r_arr.gif
r_arr_disabled.gif
u_arr.gif
uc.gif
wave.gif
boost-build.jam
Jamfile.v2
project-root.jam
Jamfile
Jamfile.v2
runtests.sh
cpp.re
lextest.in
lextest.output
run_tests.sh
test_lexer.in
test_lexer.output
lextest.in
lextest.re2c.output
lextest.slex.output
run_tests.sh
/*=============================================================================
    A Standard compliant C++ preprocessor

    Test the Re2C based C++ lexer
    
    Copyright (c) 2001-2003 Hartmut Kaiser
    http://spirit.sourceforge.net/

    Permission to copy, use, modify, sell and distribute this software
    is granted provided this copyright notice appears in all copies.
    This software is provided "as is" without express or implied
    warranty, and with no claim as to its suitability for any purpose.

    See Copyright.txt for full copyright notices and acknowledgments.
=============================================================================*/

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

///////////////////////////////////////////////////////////////////////////////
//  include required boost libraries
#include <boost/assert.hpp>

#include "test_re2c_lexer.hpp"      // config data

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

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

using namespace boost::spirit;
using namespace wave::cpplexer;

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

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

//  As you can see, the slex::lex_functor object, which is hidden behind the 
//  lex_iterator isn't explicit mentioned here. This allows to separate the 
//  compilation of the slex::lex_functor and reduces compilation time.
    using namespace wave::cpplexer::re2clex;

char const *begin = teststr.c_str();
char const *end = begin + teststr.size();
lex_iterator<wave::cpplexer::lex_token<> > first (begin, end, argv[1]);
lex_iterator<wave::cpplexer::lex_token<> > last;
wave::cpplexer::lex_token<> current_token;

    try {
        while (first != last) {
            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)) << "): " 
                << ">" << current_token.get_value() << "<"
                << endl;
            ++first;
        }
    }
    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 1;
    }
    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 2;
    }
    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

Share

About the Author

Hartmut Kaiser

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141220.1 | Last Updated 11 Jan 2004
Article Copyright 2003 by Hartmut Kaiser
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid