/*=============================================================================
Wave: A Standard compliant C++ preprocessor
Definition of the lexer 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(CPP_LEX_ITERATOR_HPP_AF0C37E3_CBD8_4F33_A225_51CF576FA61F_INCLUDED)
#define CPP_LEX_ITERATOR_HPP_AF0C37E3_CBD8_4F33_A225_51CF576FA61F_INCLUDED
#include <string>
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/spirit/iterator/multi_pass.hpp>
#include "wave/util/file_position.hpp"
#include "wave/cpplexer/cpp_lex_interface.hpp"
#include "wave/language_support.hpp"
///////////////////////////////////////////////////////////////////////////////
namespace wave {
namespace cpplexer {
namespace impl {
///////////////////////////////////////////////////////////////////////////////
//
// lex_iterator_functor_shim
//
///////////////////////////////////////////////////////////////////////////////
template <typename TokenT>
class lex_iterator_functor_shim
{
public:
template <typename IteratorT>
lex_iterator_functor_shim(IteratorT const &first, IteratorT const &last,
typename TokenT::position_t const &pos,
wave::language_support language)
: functor_ptr(lex_input_interface<TokenT>
::new_lexer(first, last, pos, language))
{}
// interface to the boost::spirit::multi_pass_policies::functor_input policy
typedef TokenT result_type;
// VC7.1 gives a linker error, if the following is defined static
/*static*/ result_type const eof;
result_type operator()()
{
BOOST_ASSERT(0 != functor_ptr.get());
return functor_ptr->get();
}
void set_position(typename TokenT::position_t const &pos)
{
BOOST_ASSERT(0 != functor_ptr.get());
functor_ptr->set_position(pos);
}
private:
boost::shared_ptr<lex_input_interface<TokenT> > functor_ptr;
};
///////////////////////////////////////////////////////////////////////////////
// eof token
// VC7.1 gives a linker error, if the following is defined static
//template <typename LexT>
//typename lex_iterator_functor_shim<LexT>::result_type const
// lex_iterator_functor_shim<LexT>::eof;
///////////////////////////////////////////////////////////////////////////////
} // namespace impl
///////////////////////////////////////////////////////////////////////////////
//
// lex_iterator
//
// A generic C++ lexer interface class, which allows to plug in different
// lexer implementations (template parameter LexT). The following
// requirement apply:
//
// - the lexer type should have a function implemented, which returnes
// the next lexed token from the input stream:
// typename LexT::token_t get();
// - at the end of the input stream this function should return the
// eof token equivalent
// - the lexer should implement a constructor taking two iterators
// pointing to the beginning and the end of the input stream and
// a third parameter containing the name of the parsed input file
//
///////////////////////////////////////////////////////////////////////////////
template <typename TokenT>
class lex_iterator
: public boost::spirit::multi_pass<
impl::lex_iterator_functor_shim<TokenT>,
boost::spirit::multi_pass_policies::functor_input
>
{
typedef impl::lex_iterator_functor_shim<TokenT> input_policy_t;
typedef
boost::spirit::multi_pass<input_policy_t,
boost::spirit::multi_pass_policies::functor_input>
base_t;
typedef lex_iterator<TokenT> self_t;
public:
typedef TokenT token_t;
lex_iterator()
{}
template <typename IteratorT>
lex_iterator(IteratorT const &first, IteratorT const &last,
typename TokenT::position_t const &pos,
wave::language_support language)
: base_t(input_policy_t(first, last, pos, language))
{}
};
///////////////////////////////////////////////////////////////////////////////
} // namespace cpplexer
} // namespace wave
#endif // !defined(CPP_LEX_ITERATOR_HPP_AF0C37E3_CBD8_4F33_A225_51CF576FA61F_INCLUDED)