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_preprocessor_demo1.zip
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
/*=============================================================================
    Wave: A Standard compliant C++ preprocessor

    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 acknowledgements.
=============================================================================*/

#if !defined(MACRO_DEFINITION_HPP_D68A639E_2DA5_4E9C_8ACD_CFE6B903831E_INCLUDED)
#define MACRO_DEFINITION_HPP_D68A639E_2DA5_4E9C_8ACD_CFE6B903831E_INCLUDED

#include <vector>
#include <list>

#include "wave/cpplexer/cpp_token_ids.hpp"

///////////////////////////////////////////////////////////////////////////////
namespace wave {
namespace util {

///////////////////////////////////////////////////////////////////////////////
//
//  macro_definition
//
//      This class containes all infos for a defined macro. 
//
///////////////////////////////////////////////////////////////////////////////
template <typename TokenT>
struct macro_definition {

    typedef std::vector<TokenT> parameter_container_t;
    typedef 
        typename parameter_container_t::const_iterator 
        const_parameter_iterator_t;
    typedef std::list<TokenT> definition_container_t;
    typedef 
        typename definition_container_t::const_iterator 
        const_definition_iterator_t;

    macro_definition()
    :   is_functionlike(false), 
        replaced_parameters(false), 
        is_available_for_replacement(false),
        is_predefined(false)
#if defined(WAVE_SUPPORT_VARIADICS_PLACEMARKERS)
        , has_ellipsis(false)
#endif // defined(WAVE_SUPPORT_VARIADICS_PLACEMARKERS)
    {}
    
    macro_definition(TokenT const &token_, bool has_parameters, 
            bool is_predefined_ = false)
    :   macroname(token_), is_functionlike(has_parameters), 
        replaced_parameters(false), is_available_for_replacement(true),
        is_predefined(is_predefined_)
#if defined(WAVE_SUPPORT_VARIADICS_PLACEMARKERS)
        , has_ellipsis(false)
#endif // defined(WAVE_SUPPORT_VARIADICS_PLACEMARKERS)
    {
    }
    // generated copy constructor
    // generated destructor
    // generated assignment operator

    // Replace all occurences of the parameters throughout the macrodefinition
    // with special parameter tokens to simplify later macro replacement.
    // Additionally mark all occurences of the macro name itself throughout
    // the macro definition
    void replace_parameters()
    {
        using namespace wave::cpplexer;
        
        if (!replaced_parameters) {
        typename definition_container_t::iterator end = macrodefinition.end();
        typename definition_container_t::iterator it = macrodefinition.begin(); 

            for (/**/; it != end; ++it) {
                if (T_IDENTIFIER == token_id(*it) || 
                    IS_CATEGORY(token_id(*it), KeywordTokenType)) 
                {
                // may be a parameter to replace
                    const_parameter_iterator_t cend = macroparameters.end();
                    const_parameter_iterator_t cit = macroparameters.begin();
                    for (typename parameter_container_t::size_type i = 0; 
                        cit != cend; ++cit, ++i) 
                    {
                        if ((*it).get_value() == (*cit).get_value()) {
                            (*it).set_token_id(token_id(T_PARAMETERBASE+i));
                            break;
                        }
#if defined(WAVE_SUPPORT_VARIADICS_PLACEMARKERS)
                        else if (T_ELLIPSIS == token_id(*cit) && 
                            "__VA_ARGS__" == (*it).get_value()) 
                        {
                        // __VA_ARGS__ requires special handling
                            (*it).set_token_id(token_id(T_EXTPARAMETERBASE+i));
                            break;
                        }
#endif // defined(WAVE_SUPPORT_VARIADICS_PLACEMARKERS)
                    }
                }
            }
            
#if defined(WAVE_SUPPORT_VARIADICS_PLACEMARKERS)
        // we need to know, if the last of the formal arguments is an ellipsis
            if (macroparameters.size() > 0 &&
                T_ELLIPSIS == token_id(macroparameters.back())) 
            {
                has_ellipsis = true;
            }
#endif // defined(WAVE_SUPPORT_VARIADICS_PLACEMARKERS)
            replaced_parameters = true;     // do it only once
        }
    }

    TokenT macroname;
    parameter_container_t macroparameters;
    definition_container_t macrodefinition;
    bool is_functionlike;
    bool replaced_parameters;
    bool is_available_for_replacement;
    bool is_predefined;
#if defined(WAVE_SUPPORT_VARIADICS_PLACEMARKERS)
    bool has_ellipsis;
#endif // defined(WAVE_SUPPORT_VARIADICS_PLACEMARKERS)
};

///////////////////////////////////////////////////////////////////////////////
}   // namespace util
}   // namespace wave

#endif // !defined(MACRO_DEFINITION_HPP_D68A639E_2DA5_4E9C_8ACD_CFE6B903831E_INCLUDED)

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 | Mobile
Web01 | 2.8.140827.1 | Last Updated 11 Jan 2004
Article Copyright 2003 by Hartmut Kaiser
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid