Click here to Skip to main content
15,860,861 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

    A generic C++ lexer token definition
    
    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.
=============================================================================*/

#if !defined(CPP_TOKEN_HPP_53A13BD2_FBAA_444B_9B8B_FCB225C2BBA8_INCLUDED)
#define CPP_TOKEN_HPP_53A13BD2_FBAA_444B_9B8B_FCB225C2BBA8_INCLUDED

#include "wave/util/file_position.hpp"
#include "wave/cpplexer/cpp_token_ids.hpp"  
#include "wave/language_support.hpp"

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

///////////////////////////////////////////////////////////////////////////////
//  forward declaration of the token type
template <typename PositionT = wave::util::file_position_t>
class lex_token;

///////////////////////////////////////////////////////////////////////////////
//  
//  new_lexer_gen: generates a new instance of the required C++ lexer
//
///////////////////////////////////////////////////////////////////////////////
template <typename TokenT> struct lex_input_interface; 

template <typename IteratorT, typename PositionT>
struct new_lexer_gen
{
//  The NewLexer function allows the opaque generation of a new lexer object.
//  It is coupled to the token type to allow to decouple the lexer/token 
//  configurations at compile time.
    static lex_input_interface<lex_token<PositionT> > *
    new_lexer(IteratorT const &first, IteratorT const &last, 
        PositionT const &pos, wave::language_support language);
};

///////////////////////////////////////////////////////////////////////////////
//  
//  lex_token
//
///////////////////////////////////////////////////////////////////////////////

template <typename PositionT>
class lex_token 
{
public:
    typedef WAVE_STRINGTYPE         string_t;
    typedef PositionT               position_t;
    
    lex_token()
    :   id(T_EOI)
    {}
    
    lex_token(token_id id_, string_t const &value_, PositionT const &pos_)
    :   id(id_), value(value_), pos(pos_)
    {}

// accessors
    operator token_id() const { return id; }
    string_t const &get_value() const { return value; }
    position_t const &get_position() const { return pos; }
    void set_token_id (token_id id_) { id = id_; }
    void set_value (string_t const &newval) { value = newval; }
    void set_position (position_t const &pos_) { pos = pos_; }

// debug support    
#if defined(WAVE_DUMP_PARSE_TREE)
// access functions for the tree_to_xml functionality
    static int get_token_id(lex_token const &t) 
        { return ID_FROM_TOKEN(token_id(t)); }
    static string_t get_token_value(lex_token const &t) 
        { return t.get_value(); }
#endif // defined(WAVE_DUMP_PARSE_TREE)
    
#if defined(BOOST_SPIRIT_DEBUG)
// debug support
    void print (std::ostream &stream) const
    {
        stream << WAVE_LEXER_NS::get_token_name(id) << "(";
        for (std::size_t i = 0; i < value.size(); ++i) {
            switch (value[i]) {
            case '\r':  stream << "\\r"; break;
            case '\n':  stream << "\\n"; break;
            default:
                stream << value[i]; 
                break;
            }
        }
        stream << ")";
    }
#endif // defined(BOOST_SPIRIT_DEBUG)

//    bool operator== (lex_token<PositionT> const &rhs) const
//    {
//        return id == rhs.id && value == rhs.value && pos == rhs.pos;
//    }

private:
    token_id id;                // the token id
    string_t value;             // the text, which was parsed into this token
    PositionT pos;              // the original file position
};

#if defined(BOOST_SPIRIT_DEBUG)
template <typename PositionT>
inline std::ostream &
operator<< (std::ostream &stream, lex_token<PositionT> const &object)
{
    object.print(stream);
    return stream;
}
#endif // defined(BOOST_SPIRIT_DEBUG)

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

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


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

Comments and Discussions