Click here to Skip to main content
15,896,154 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 402.8K   4.4K   81  
Describes a free and fully Standard conformant C++ preprocessor library
/*=============================================================================
    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(CPP_GRAMMAR_GEN_HPP_80CB8A59_5411_4E45_B406_62531A12FB99_INCLUDED)
#define CPP_GRAMMAR_GEN_HPP_80CB8A59_5411_4E45_B406_62531A12FB99_INCLUDED

#include <boost/spirit/tree/parse_tree.hpp>

#include "wave/cpplexer/cpp_lex_iterator.hpp"
#include "wave/language_support.hpp"

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

///////////////////////////////////////////////////////////////////////////////
//  
//  store parser_id's of all rules of the cpp_grammar here for later access
//
///////////////////////////////////////////////////////////////////////////////
struct cpp_grammar_rule_ids {
    long pp_statement_id;
    long include_file_id;       // #include "..."
    long sysinclude_file_id;    // #include <...>
    long macroinclude_file_id;  // #include ...
    long plain_define_id;       // #define
    long macro_parameters_id;
    long macro_definition_id;
    long undefine_id;           // #undef
    long ifdef_id;              // #ifdef
    long ifndef_id;             // #ifndef
    long if_id;                 // #if
    long elif_id;               // #elif
    long else_id;               // #else
    long endif_id;              // #endif
    long line_id;               // #line
    long error_id;              // #error
    long warning_id;            // #warning
    long pragma_id;             // #pragma
    long illformed_id;
    long ppspace_id;
#if defined(WAVE_ENABLE_CPP0X_EXTENSIONS)
    long pp_regionsupport_id;
    long ppregion_id;           // #region
    long ppendregion_id;        // #endregion
    long ppimport_id;           // #import
#endif // defined(WAVE_ENABLE_CPP0X_EXTENSIONS)
    long ppqualifiedname_id;
};

///////////////////////////////////////////////////////////////////////////////
//  
//  cpp_grammar_gen template class
//
//      This template helps separating the compilation of the cpp_grammar 
//      class from the compilation of the main pp_iterator. This is done to
//      safe compilation time.
//
///////////////////////////////////////////////////////////////////////////////

template <typename TokenT>
struct cpp_grammar_gen
{
    typedef wave::cpplexer::lex_iterator<TokenT>    iterator_t;
    typedef typename TokenT::position_t             position_t;
    
//  the parser_id's of all rules of the cpp_grammar are stored here
//  note: these are valid only after the first call to parse_cpp_grammar
    static cpp_grammar_rule_ids rule_ids;

//  the actual position of the last matched T_NEWLINE is stored here into the
//  member 'pos_of_newline'
    static position_t pos_of_newline;

//  the found_eof flag is set to true during the parsing, if the directive 
//  under inspection terminates with a T__EOF token
    static bool found_eof;

//  the found_directive contains the token_id of the recognized pp directive
    static wave::cpplexer::token_id found_directive;
        
//  parse the cpp_grammar and return the resulting parse tree    
    static boost::spirit::tree_parse_info<iterator_t> 
    parse_cpp_grammar (iterator_t const &first, iterator_t const &last,
        bool &found_eof_, position_t const &act_pos);
};

///////////////////////////////////////////////////////////////////////////////
//  definitions of the static members
template <typename TokenT>
cpp_grammar_rule_ids cpp_grammar_gen<TokenT>::rule_ids;

template <typename TokenT>
typename TokenT::position_t cpp_grammar_gen<TokenT>::pos_of_newline;

template <typename TokenT>
bool cpp_grammar_gen<TokenT>::found_eof = false;

template <typename TokenT>
typename wave::cpplexer::token_id cpp_grammar_gen<TokenT>::found_directive = 
    wave::cpplexer::T_EOF;

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

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