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

    Definition of the preprocessor context
    
    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(CPP_CONTEXT_HPP_907485E2_6649_4A87_911B_7F7225F3E5B8_INCLUDED)
#define CPP_CONTEXT_HPP_907485E2_6649_4A87_911B_7F7225F3E5B8_INCLUDED

#include <string>
#include <vector>
#include <stack>

#include <boost/concept_check.hpp>

#include "wave/cpplexer/cpp_token_ids.hpp"

#include "wave/util/unput_queue_iterator.hpp"
#include "wave/util/cpp_ifblock.hpp"
#include "wave/util/cpp_include_pathes.hpp"
#include "wave/util/iteration_context.hpp"
#include "wave/util/cpp_iterator.hpp"
#include "wave/util/cpp_macromap.hpp"

#include "wave/macro_trace_policies.hpp"
#include "wave/cpp_iteration_context.hpp"
#include "wave/language_support.hpp"

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

///////////////////////////////////////////////////////////////////////////////
// 
//  The C preprocessor context template class
//
//      The wave::context template is the main interface class to 
//      control the behaviour of the preprocessing engine.
//
//      The following template parameters has to be supplied:
//
//      IteratorT       The iterator type of the underlying input stream
//      TokenT          The token type to return from the preprocessing
//      InputPolicyT    The input policy type to use for loading the files
//                      to be included. This template parameter is optional and 
//                      defaults to the 
//                          iteration_context_policies::load_file_to_string
//                      type
//
///////////////////////////////////////////////////////////////////////////////

template <
    typename IteratorT,
    typename TokenT, 
    typename InputPolicyT = iteration_context_policies::load_file_to_string,
    typename TraceT = macro_trace_policies::no_tracing
>
class context {

public:

// concept checks
// the given iterator shall be at least a forward iterator type
    BOOST_CLASS_REQUIRE(IteratorT, boost, ForwardIteratorConcept);
    
// public typedefs
    typedef context<IteratorT, TokenT, InputPolicyT, TraceT> self_t;
    
    typedef IteratorT                               target_iterator_t;
    typedef wave::cpplexer::lex_iterator<TokenT>    lex_t;
    typedef pp_iterator<self_t>                     iterator_t;
    typedef TokenT                                  token_t;

    typedef InputPolicyT                            input_policy_t;
    typedef typename TokenT::position_t             position_t;
        
private:
// stack of shared_ptr's to the pending iteration contexts 
    typedef boost::shared_ptr<base_iteration_context<lex_t> > iteration_ptr_t;
    typedef wave::util::iteration_context_stack<iteration_ptr_t> 
            iteration_context_stack_t;
    typedef typename iteration_context_stack_t::size_type iter_size_t;

public:
    context(target_iterator_t const &first_, target_iterator_t const &last_, 
            char const *fname = "<Unknown>", TraceT const &trace = TraceT())
    :   first(first_), last(last_), filename(fname), macros(*this, trace),
        language(wave::support_cpp)
    {
        macros.init_predefined_macros();
        includes.init_initial_path();
        includes.set_current_directory(filename.c_str());
    }

// iterator interface
    iterator_t begin() 
    { 
        return iterator_t(*this, first, last, position_t(filename.c_str()),
            get_language()); 
    }
    iterator_t end() const 
        { return iterator_t(); }

// maintain include paths
    bool add_include_path(char const *path_)
        { return includes.add_include_path(path_, false);}
    bool add_sysinclude_path(char const *path_)
        { return includes.add_include_path(path_, true);}
    void set_sysinclude_delimiter() { includes.set_sys_include_delimiter(); }
    typename iteration_context_stack_t::size_type get_iteration_depth() const 
        { return iter_ctxs.size(); }

// maintain defined macros
#if defined(WAVE_ENABLE_COMMANDLINE_MACROS)
    bool add_macro_definition(std::string macrostring, 
            bool is_predefined = false)
        { return wave::util::add_macro_definition(*this, macrostring, 
            is_predefined, get_language()); }
#endif // defined(WAVE_ENABLE_COMMANDLINE_MACROS)
    bool add_macro_definition(TokenT const &name, bool has_params,
            std::vector<TokenT> &parameters, std::list<TokenT> &definition,
            bool is_predefined = false)
        { return macros.add_macro(name, has_params, parameters, definition, 
            is_predefined); }
    template <typename IteratorT2>
    bool is_defined_macro(IteratorT2 const &begin, IteratorT2 const &end) 
        { return macros.is_defined(begin, end); }
    bool remove_macro_definition(typename TokenT::string_t const &name, 
            bool even_predefined = false)
        { return macros.remove_macro(name, even_predefined); }
    void reset_macro_definitions() 
        { macros.reset_macromap(); macros.init_predefined_macros(); }

// get the pp-iterator version information 
    static std::string get_version()  
        { return wave::util::predefined_macros::get_fullversion(false); }
    static std::string get_version_string()  
        { return wave::util::predefined_macros::get_versionstr(false); }

    void set_language(wave::language_support language_) 
    { 
        language = language_; 
        reset_macro_definitions();
    }
    wave::language_support get_language() const { return language; }
        
// change and ask for maximal possible include nesting depth
    void set_max_include_nesting_depth(iter_size_t new_depth)
        { iter_ctxs.set_max_include_nesting_depth(new_depth); }
    iter_size_t get_max_include_nesting_depth() const
        { return iter_ctxs.get_max_include_nesting_depth(); }

// enable/disable tracing
    void enable_tracing(bool enable) { macros.enable_tracing(enable); }

#if !defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS)
protected:
    typedef typename iterator_t::input_policy_t pp_iterator_policy_t;
    friend pp_iterator_policy_t;
#endif
    
// maintain include pathes (helper functions)
    bool find_include_file (std::string &s, bool is_system) const
        { return includes.find_include_file(s, is_system); }
    void set_current_directory(char const *path_) 
        { includes.set_current_directory(path_); }

// conditional compilation contexts
    bool get_if_block_status() const { return ifblocks.get_status(); }
    void enter_if_block(bool new_status) 
        { ifblocks.enter_if_block(new_status); }
    bool enter_elif_block(bool new_status) 
        { return ifblocks.enter_elif_block(new_status); }
    bool enter_else_block() { return ifblocks.enter_else_block(); }
    bool exit_if_block() { return ifblocks.exit_if_block(); }
    typename wave::util::if_block_stack::size_type get_if_block_depth() const 
        { return ifblocks.get_if_block_depth(); }

// stack of iteration contexts
    iteration_ptr_t pop_iteration_context()
        { iteration_ptr_t top = iter_ctxs.top(); iter_ctxs.pop(); return top; }
    void push_iteration_context(position_t const &act_pos, iteration_ptr_t iter_ctx)
        { iter_ctxs.push(act_pos, iter_ctx); }

    position_t &get_main_pos() { return macros.get_main_pos(); }
    
///////////////////////////////////////////////////////////////////////////////
//
//  expand_tokensequence(): 
//      expands all macros contained in a given token sequence, handles '##' 
//      and '#' pp operators and re-scans the resulting sequence 
//      (essentially preprocesses the token sequence).
//
//      The expand_undefined parameter is true during macro expansion inside
//      a C++ expression given for a #if or #elif statement. 
//
///////////////////////////////////////////////////////////////////////////////
    template <typename IteratorT2>
    TokenT expand_tokensequence(IteratorT2 &first, IteratorT2 const &last, 
        std::list<token_t> &pending, std::list<token_t> &expanded, 
        bool expand_undefined = false)
    {
        return macros.expand_tokensequence(first, last, pending, expanded, 
            expand_undefined);
    }

    template <typename IteratorT2>
    void expand_whole_tokensequence(IteratorT2 &first, IteratorT2 const &last, 
        std::list<token_t> &expanded, bool expand_undefined = true)
    {
        macros.expand_whole_tokensequence(expanded, first, last, 
            expand_undefined);

    // remove an placeholder, if it is the last token
        using namespace cpplexer;
        if (expanded.size() > 0 && T_PLACEHOLDER == token_id(expanded.back()))
        {
        typename std::list<token_t>::reverse_iterator rit = expanded.rbegin();

            expanded.erase((++rit).base());
        }
    }

#if defined(WAVE_ENABLE_CPP0X_EXTENSIONS)
// experimental: macro scoping support
    template <typename IteratorT2>
    void import_name(IteratorT2 const &begin, IteratorT2 const &end) 
        { macros.import_name(begin, end); }
    template <typename IteratorT2>
    void begin_scope(IteratorT2 const &begin, IteratorT2 const &end) 
        { macros.begin_scope(begin, end); }
    void begin_unnamed_scope() { macros.begin_unnamed_scope(); }
    void end_scope() { macros.end_scope(); }
#endif // defined(WAVE_ENABLE_CPP0X_EXTENSIONS)
    
private:
// the main input stream
    target_iterator_t const &first; // underlying input stream
    target_iterator_t const &last;
    std::string filename;           // associated main filename

    wave::util::if_block_stack ifblocks;  // conditional compilation contexts
    wave::util::include_pathes includes;  // lists of include directories to search
    iteration_context_stack_t iter_ctxs;  // iteration contexts
    wave::util::macromap<self_t, TraceT> macros;    // map of defined macros
    wave::language_support language;      // supported language/extensions
};

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

#endif // !defined(CPP_CONTEXT_HPP_907485E2_6649_4A87_911B_7F7225F3E5B8_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

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