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

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

    Definition of the unput queue iterator
    
    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(UNPUT_QUEUE_ITERATOR_HPP_76DA23D0_4893_4AD5_ABCC_6CED7CFB89BC_INCLUDED)
#define UNPUT_QUEUE_ITERATOR_HPP_76DA23D0_4893_4AD5_ABCC_6CED7CFB89BC_INCLUDED

#include <list>
#include <boost/iterator_adaptors.hpp>

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

template <typename TokenT, typename ContainerT>
class unput_queue_policies : public boost::default_iterator_policies
{
public:
    unput_queue_policies(ContainerT &unput_queue_)
    :   unput_queue(unput_queue_)
    {}
    
    unput_queue_policies &operator= (unput_queue_policies const &rhs)
    { 
        unput_queue = rhs.unput_queue; 
        return *this; 
    }
    
    template <typename BaseT>
    void initialize(BaseT &)
    {}

    template <typename IteratorAdaptorT>
    typename IteratorAdaptorT::reference 
    dereference(const IteratorAdaptorT &x) const
    { 
        if (x.policies().unput_queue.size() > 0)
            return x.policies().unput_queue.front();
        return *x.base(); 
    }

    template <typename IteratorAdaptorT>
    void 
    increment(IteratorAdaptorT &x)
    { 
        if (x.policies().unput_queue.size() > 0) {
        // there exist pending tokens in the unput queue
            x.policies().unput_queue.pop_front();
        }
        else {
        // the unput_queue is empty, so advance the base iterator
            ++x.base(); 
        }
    }

    template <typename IteratorAdaptorT1, typename IteratorAdaptorT2>
    bool 
    equal(const IteratorAdaptorT1 &x, const IteratorAdaptorT2 &y) const
    { 
    // two iterators are equal, if both begin() iterators of the queue objects
    // are equal and the base iterators too
        return 
           (x.policies().unput_queue.begin() == y.policies().unput_queue.begin() ||
            (0 == x.policies().queuesize() && 0 == y.policies().queuesize())) &&
            x.base() == y.base(); 
    }
    
    typename ContainerT::size_type queuesize() const 
    { return unput_queue.size(); }

    ContainerT &get_unput_queue() { return unput_queue; }
    
private:
    ContainerT &unput_queue;
};

///////////////////////////////////////////////////////////////////////////////
//
//  unput_queue_iterator
//
//      The unput_queue_iterator templates encapsulates an unput_queue together
//      with the direct input to be read after the unput queue is emptied
//
///////////////////////////////////////////////////////////////////////////////
template <typename IteratorT, typename TokenT, typename ContainerT>
class unput_queue_iterator 
:   public boost::iterator_adaptor<
        IteratorT, unput_queue_policies<TokenT, ContainerT>, TokenT, 
        TokenT const &, TokenT const *>
{
    typedef 
        boost::iterator_adaptor<
            IteratorT, unput_queue_policies<TokenT, ContainerT>, TokenT, 
            TokenT const &, TokenT const *
        >
        base_t;
        
public:
    typedef ContainerT  container_t;
    typedef IteratorT   iterator_t;
    
    unput_queue_iterator(IteratorT const &it, 
            unput_queue_policies<TokenT, ContainerT> policies)
    :   base_t(it, policies)
    {}
    
    ContainerT &get_unput_queue() { return policies().get_unput_queue(); }
    IteratorT &get_base_iterator() { return base(); }
};

namespace impl {

    ///////////////////////////////////////////////////////////////////////////
    template <typename IteratorT, typename TokenT, typename ContainerT>
    struct gen_unput_queue_iterator {
    
        typedef unput_queue_iterator<IteratorT, TokenT, ContainerT> return_t;
        typedef unput_queue_policies<TokenT, ContainerT> policies_t;
        
        static return_t
        generate(ContainerT &queue, IteratorT const &it)
        {
            return return_t(it, policies_t(queue));
        }
    };
    
    template <typename IteratorT, typename TokenT, typename ContainerT>
    struct gen_unput_queue_iterator<
        unput_queue_iterator<IteratorT, TokenT, ContainerT>, 
            TokenT, ContainerT> 
    {
    
        typedef unput_queue_iterator<IteratorT, TokenT, ContainerT> return_t;
        typedef unput_queue_policies<TokenT, ContainerT> policies_t;
        
        static return_t 
        generate(ContainerT &queue, return_t const &it)
        {
            return return_t(it.base(), policies_t(queue));
        }
    };
    
    ///////////////////////////////////////////////////////////////////////////
    template <typename IteratorT>
    struct assign_iterator {
    
        static void 
        do_ (IteratorT &dest, IteratorT const &src)
        {
            dest = src;
        }
    };
    
    template <typename IteratorT, typename TokenT, typename ContainerT>
    struct assign_iterator<
        unput_queue_iterator<IteratorT, TokenT, ContainerT> > {
    
        typedef unput_queue_iterator<IteratorT, TokenT, ContainerT> iterator_t;
        
        static void 
        do_ (iterator_t &dest, iterator_t const &src)
        {
            dest.base() = src.base();
            dest.policies() = src.policies();
        }
    };

    ///////////////////////////////////////////////////////////////////////////
    //
    // Look for the first non-whitespace token and return this token id.
    // Note though, that the embedded unput_queues are not touched in any way!
    //
    template <typename IteratorT>
    struct next_token {
    
        static wave::cpplexer::token_id  
        peek(IteratorT it, IteratorT end, bool skip_whitespace = true)
        {
            using namespace wave::cpplexer;
            if (skip_whitespace) {
                for (++it; it != end; ++it) {
                    if (!IS_CATEGORY(*it, WhiteSpaceTokenType) &&
                        T_NEWLINE != token_id(*it))
                    {
                        break;  // stop at the first non-whitespace token
                    }
                }
            }
            else {
                ++it;           // we have at least to look ahead
            }
            if (it != end) 
                return token_id(*it);
            return T_EOI;
        }
    };
    
    template <typename IteratorT, typename TokenT, typename ContainerT>
    struct next_token<
        unput_queue_iterator<IteratorT, TokenT, ContainerT> > {
        
        typedef unput_queue_iterator<IteratorT, TokenT, ContainerT> iterator_t;
        
        static wave::cpplexer::token_id 
        peek(iterator_t it, iterator_t end, bool skip_whitespace = true)
        {
            using namespace wave::cpplexer;
            
        typename iterator_t::container_t &queue = it.get_unput_queue();
        
        // first try to find it in the unput_queue
            if (0 != queue.size()) {
            typename iterator_t::container_t::iterator cit = queue.begin();
            typename iterator_t::container_t::iterator cend = queue.end();

                if (skip_whitespace) {            
                    for (++cit; cit != cend; ++cit) {
                        if (!IS_CATEGORY(*cit, WhiteSpaceTokenType) &&
                            T_NEWLINE != token_id(*cit))
                        {
                            break;  // stop at the first non-whitespace token
                        }
                    }
                }
                else {
                    ++cit;          // we have at least to look ahead
                }
                if (cit != cend) 
                    return token_id(*cit);
            }
            
        // second try to move on into the base iterator stream
        typename iterator_t::iterator_t base_it = it.get_base_iterator();
        typename iterator_t::iterator_t base_end = end.get_base_iterator();

            if (0 == queue.size())
                ++base_it;  // advance, if the unput queue is empty

            if (skip_whitespace) {
                for (/**/; base_it != base_end; ++base_it) {
                    if (!IS_CATEGORY(*base_it, WhiteSpaceTokenType) &&
                        T_NEWLINE != token_id(*base_it))
                    {
                        break;  // stop at the first non-whitespace token
                    }
                }
            }
            if (base_it == base_end)
                return T_EOI;

            return token_id(*base_it);
        }
    };

    // Skip all whitespace characters and queue the skipped characters into the
    // given container
    template <typename IteratorT>
    inline wave::cpplexer::token_id 
    skip_whitespace(IteratorT &first, IteratorT const &last)
    {
        using namespace cpplexer;
        
    token_id id = next_token<IteratorT>::peek(first, last, false);

        if (IS_CATEGORY(id, WhiteSpaceTokenType)) {
            do {
                ++first;
                id = next_token<IteratorT>::peek(first, last, false);
            } while (IS_CATEGORY(id, WhiteSpaceTokenType));
        }
        ++first;
        return id;
    }
    
    template <typename IteratorT, typename ContainerT>
    inline wave::cpplexer::token_id 
    skip_whitespace(IteratorT &first, IteratorT const &last, ContainerT &queue)
    {
        using namespace cpplexer;
        queue.push_back (*first);       // queue up the current token
        
    token_id id = next_token<IteratorT>::peek(first, last, false);

        if (IS_CATEGORY(id, WhiteSpaceTokenType)) {
            do {
                queue.push_back(*++first);  // queue up the next whitespace 
                id = next_token<IteratorT>::peek(first, last, false);
            } while (IS_CATEGORY(id, WhiteSpaceTokenType));
        }
        ++first;
        return id;
    }

///////////////////////////////////////////////////////////////////////////////
}   // namespace impl

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

#endif // !defined(UNPUT_QUEUE_ITERATOR_HPP_76DA23D0_4893_4AD5_ABCC_6CED7CFB89BC_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.140905.1 | Last Updated 11 Jan 2004
Article Copyright 2003 by Hartmut Kaiser
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid