// -*- mode: c++; indent-tabs-mode: nil; c-basic-offset: 4 -*- // $Header: /srv/cvs/sources/mpak/mpak/include/mpak/spec/Attic/lexer.hh,v 1.1 2003/07/28 18:20:26 pbgavin Exp $ // mpak - the advanced package manager // ecstacy - extended c++ portable operating system interface library // Copyright (C) 2003 Peter Gavin // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #ifndef __MPAK__LEXER_HH__ #define __MPAK__LEXER_HH__ #include #include #include #include #include #include #include #include namespace mpak { namespace spec { template > class basic_lexer { public: class failure : public std::runtime_error { public: inline failure (const std::string &what) : runtime_error (what) { } }; typedef basic_lexer this_type; typedef char_type_ char_type; typedef typename traits_type_::int_type int_type; typedef traits_type_ traits_type; typedef std::basic_ios ios_type; typedef std::basic_streambuf streambuf_type; typedef std::ctype ctype_type; static const ecstacy::size_type tab_size; class token { public: enum type_t { type_none, type_identifier, type_string, type_block_start, type_block_end, type_equals, type_endl, type_eof }; type_t type; std::basic_string value; ecstacy::size_type line; ecstacy::size_type column; inline token (void) : type (), value (), line (), column () { } inline token (type_t type, const std::basic_string &value, ecstacy::size_type line, ecstacy::size_type column) : type (type), value (value), line (line), column (column) { } }; private: std::string filename_; ecstacy::size_type line_, column_; // pointer to read buffer. If 0, open this->filename_. bool own_buf_; streambuf_type *buf_; private: inline int_type next_char (void) ECSTACY_THROW_SPEC (()) { int_type c = this->buf_->sgetc (); const ctype_type &ct = std::use_facet (this->buf_->getloc ()); this->buf_->snextc (); if (traits_type::eq (c, ct.widen ('\n'))) { ++this->line_; this->column_ = 0; } else { ++this->column_; } return c; } inline void putback_char (char_type c) ECSTACY_THROW_SPEC (()) { const ctype_type &ct = std::use_facet (this->buf_->getloc ()); this->buf_->sputbackc (c); if (traits_type::eq (c, ct.widen ('\n'))) { --this->line_; this->column = 0; } else { --this->column_; } } public: inline basic_lexer (std::string filename) : filename_ (filename), line_ (1), column_ (0), own_buf_ (true), buf_ (0) { std::basic_filebuf *buf = new std::basic_filebuf (); buf->open (this->filename_.c_str (), std::ios_base::in); this->buf_ = buf; } inline basic_lexer (std::string filename, streambuf_type *buf) : filename_ (filename), line_ (1), column_ (0), own_buf_ (false), buf_ (buf) { } inline ~basic_lexer (void) { if (this->own_buf_) { delete this->buf_; } } token next_token (void); }; typedef basic_lexer lexer; typedef basic_lexer wlexer; extern template class basic_lexer; extern template class basic_lexer; } } #include #endif // ifndef __MPAK__LEXER_H__