|
/*=============================================================================
Wave: A Standard compliant C++ preprocessor
Whitespace eater
Copyright (c) 2003 Paul Mensonides
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(EAT_WHITESPACE_HPP_4CE9AD17_F82D_4AB2_A117_555DF0DCC801_INCLUDED)
#define EAT_WHITESPACE_HPP_4CE9AD17_F82D_4AB2_A117_555DF0DCC801_INCLUDED
#include "wave/cpplexer/cpp_token_ids.hpp"
///////////////////////////////////////////////////////////////////////////////
namespace wave {
namespace util {
template <typename TokenT>
class eat_whitespace {
public:
eat_whitespace();
bool may_skip (TokenT &token, bool &skipped_newline);
private:
typedef bool state_t(TokenT &token, bool &skipped_newline);
state_t eat_whitespace::* state;
state_t general, newline, newline_2nd, whitespace;
};
template <typename TokenT>
inline
eat_whitespace<TokenT>::eat_whitespace()
: state(&eat_whitespace::newline)
{
}
template <typename TokenT>
inline bool
eat_whitespace<TokenT>::may_skip(TokenT &token, bool &skipped_newline)
{
return (this->*state)(token, skipped_newline);
}
template <typename TokenT>
inline bool
eat_whitespace<TokenT>::general(TokenT &token, bool &skipped_newline)
{
using wave::cpplexer::token_id;
token_id id = token_id(token);
if (T_NEWLINE == id || T_CPPCOMMENT == id) {
state = &eat_whitespace::newline;
}
else if (T_SPACE == id || T_CCOMMENT == id) {
state = &eat_whitespace::whitespace;
if (T_CCOMMENT == id) {
if (typename TokenT::string_t::npos !=
token.get_value().find_first_of("\n"))
{
skipped_newline = true;
}
}
if (token.get_value().size() > 1)
token.set_value(" "); // replace with a single space
}
else {
state = &eat_whitespace::general;
}
return false;
}
template <typename TokenT>
inline bool
eat_whitespace<TokenT>::newline(TokenT &token, bool &skipped_newline)
{
using wave::cpplexer::token_id;
token_id id = token_id(token);
if (T_NEWLINE == id || T_CPPCOMMENT == id) {
// skipped_newline = true;
state = &eat_whitespace::newline_2nd;
return false;
}
else if (T_SPACE != id && T_CCOMMENT != id) {
return general(token, skipped_newline);
}
if (T_CCOMMENT == id) {
if (typename TokenT::string_t::npos !=
token.get_value().find_first_of("\n"))
{
skipped_newline = true;
}
return true;
}
return true;
}
template <typename TokenT>
inline bool
eat_whitespace<TokenT>::newline_2nd(TokenT &token, bool &skipped_newline)
{
using wave::cpplexer::token_id;
token_id id = token_id(token);
if (T_SPACE == id)
return true;
if (T_CCOMMENT == id) {
if (typename TokenT::string_t::npos !=
token.get_value().find_first_of("\n"))
{
skipped_newline = true;
}
return true;
}
if (T_NEWLINE != id && T_CPPCOMMENT != id)
return general(token, skipped_newline);
skipped_newline = true;
return true;
}
template <typename TokenT>
inline bool
eat_whitespace<TokenT>::whitespace(TokenT &token, bool &skipped_newline)
{
using wave::cpplexer::token_id;
token_id id = token_id(token);
if (T_SPACE != id && T_CCOMMENT != id)
return general(token, skipped_newline);
if (T_CCOMMENT == id) {
if (typename TokenT::string_t::npos !=
token.get_value().find_first_of("\n"))
{
skipped_newline = true;
}
return true;
}
return true;
}
///////////////////////////////////////////////////////////////////////////////
} // namespace util
} // namespace wave
#endif // !defined(EAT_WHITESPACE_HPP_4CE9AD17_F82D_4AB2_A117_555DF0DCC801_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.
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
Actively involved in Boost and the development of the Spirit parser construction framework.