|
/*=============================================================================
Wave: A Standard compliant C++ preprocessor
Definition of the position_iterator and file_position templates
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(FILE_POSITION_H_52BDEDF7_DAD3_4F24_802F_E66BB8098F68_INCLUDED)
#define FILE_POSITION_H_52BDEDF7_DAD3_4F24_802F_E66BB8098F68_INCLUDED
#include <string>
#include <ostream>
#include <boost/spirit/iterator/position_iterator.hpp>
///////////////////////////////////////////////////////////////////////////////
namespace wave {
namespace util {
///////////////////////////////////////////////////////////////////////////////
//
// file_position
//
// A structure to hold positional information. This includes the filename,
// line number and column number of a current token position.
//
///////////////////////////////////////////////////////////////////////////////
template <typename StringT = std::string>
struct file_position {
public:
typedef StringT string_t;
file_position()
: file(), line(1), column(1)
{}
explicit file_position(string_t const& file_, int line_ = 1,
int column_ = 1)
: file(file_), line(line_), column(column_)
{}
// accessors
string_t const &get_file() const { return file; }
int get_line() const { return line; }
int get_column() const { return column; }
void set_file(string_t const &file_) { file = file_; }
void set_line(int line_) { line = line_; }
void set_column(int column_) { column = column_; }
private:
string_t file;
int line;
int column;
};
template <typename StringT>
bool operator== (file_position<StringT> const &lhs,
file_position<StringT> const &rhs)
{
return lhs.get_column() == rhs.get_column() &&
lhs.get_line() == rhs.get_line() && lhs.get_file() == rhs.get_file();
}
template <typename StringT>
inline std::ostream &
operator<< (std::ostream &o, file_position<StringT> const &pos)
{
o << pos.get_file() << "(" << pos.get_line() << ")";
return o;
}
#if !defined(WAVE_STRINGTYPE)
#define WAVE_STRINGTYPE std::string
#endif // !defined(WAVE_STRINGTYPE)
typedef file_position<WAVE_STRINGTYPE> file_position_t;
///////////////////////////////////////////////////////////////////////////////
//
// position_iterator
//
// The position_iterator used by Wave is now based on the corresponding Spirit
// type. This type is used with our own file_position though. The needed
// specialization of the boost::spirit::position_policy class is provided
// below.
//
///////////////////////////////////////////////////////////////////////////////
template <typename IteratorT, typename PositionT>
struct position_iterator
: boost::spirit::position_iterator<IteratorT, PositionT>
{
typedef boost::spirit::position_iterator<IteratorT, PositionT> base_t;
position_iterator()
{
}
position_iterator(IteratorT const &begin, IteratorT const &end,
PositionT const &pos)
: base_t(begin, end, pos)
{
}
};
///////////////////////////////////////////////////////////////////////////////
} // namespace util
} // namespace wave
///////////////////////////////////////////////////////////////////////////////
namespace boost {
namespace spirit {
///////////////////////////////////////////////////////////////////////////////
//
// The boost::spirit::position_policy has to be specialized for our
// file_position class
//
///////////////////////////////////////////////////////////////////////////////
template <>
class position_policy<wave::util::file_position_t> {
public:
position_policy()
: m_CharsPerTab(4)
{}
void next_line(wave::util::file_position_t &pos)
{
pos.set_line(pos.get_line() + 1);
pos.set_column(1);
}
void set_tab_chars(unsigned int chars)
{
m_CharsPerTab = chars;
}
void next_char(wave::util::file_position_t &pos)
{
pos.set_column(pos.get_column() + 1);
}
void tabulation(wave::util::file_position_t &pos)
{
pos.set_column(pos.get_column() + m_CharsPerTab -
(pos.get_column() - 1) % m_CharsPerTab);
}
private:
unsigned int m_CharsPerTab;
};
///////////////////////////////////////////////////////////////////////////////
} // namespace spirit
} // namespace boost
#endif // !defined(FILE_POSITION_H_52BDEDF7_DAD3_4F24_802F_E66BB8098F68_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.