Moved CharStream class out of Parser.{h,cpp} and into its own file

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@1874 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Tyler Dauwalder
2002-11-08 00:38:35 +00:00
parent e5f9569dcb
commit 89ec8a81c2
4 changed files with 192 additions and 108 deletions
@@ -0,0 +1,56 @@
//----------------------------------------------------------------------
// This software is part of the OpenBeOS distribution and is covered
// by the OpenBeOS license.
//---------------------------------------------------------------------
/*!
\file sniffer/CharStream.h
Character stream class
*/
#ifndef _SNIFFER_CHAR_STREAM_H
#define _SNIFFER_CHAR_STREAM_H
#include <SupportDefs.h>
#include <string>
namespace BPrivate {
namespace Storage {
namespace Sniffer {
//! Manages a stream of characters
/*! CharStream is used by the scanner portion of the parser, which is implemented
in TokenStream::SetTo().
It's also used by BPrivate::TRoster while parsing through the the
roster's RosterSettings file.
*/
class CharStream {
public:
CharStream(const std::string &string);
CharStream();
virtual ~CharStream();
status_t SetTo(const std::string &string);
void Unset();
status_t InitCheck() const;
bool IsEmpty() const;
size_t Pos() const;
const std::string& String() const;
char Get();
void Unget();
private:
std::string fString;
size_t fPos;
status_t fCStatus;
CharStream(const CharStream &ref);
CharStream& operator=(const CharStream &ref);
};
}; // namespace Sniffer
}; // namespace Storage
}; // namespace BPrivate
#endif // _SNIFFER_CHAR_STREAM_H
+1 -29
View File
@@ -10,6 +10,7 @@
#define _SNIFFER_PARSER_H
#include <SupportDefs.h>
#include <sniffer/CharStream.h>
#include <sniffer/Err.h>
#include <sniffer/Range.h>
#include <sniffer/Rule.h>
@@ -39,35 +40,6 @@ status_t parse(const char *rule, Rule *result, BString *parseError = NULL);
// Classes used internally by the parser
//------------------------------------------------------------------------------
//! Manages a stream of characters
/*! CharStream is used by the scanner portion of the parser, which is implemented
in TokenStream::SetTo().
*/
class CharStream {
public:
CharStream(const std::string &string);
CharStream();
~CharStream();
status_t SetTo(const std::string &string);
void Unset();
status_t InitCheck() const;
bool IsEmpty() const;
size_t Pos() const;
const std::string& String() const;
char Get();
void Unget();
private:
std::string fString;
size_t fPos;
status_t fCStatus;
CharStream(const CharStream &ref);
CharStream& operator=(const CharStream &ref);
};
//! Types of tokens
typedef enum TokenType {
EmptyToken,
+135
View File
@@ -0,0 +1,135 @@
#include "sniffer/CharStream.h"
#include <sniffer/Err.h>
using namespace BPrivate::Storage::Sniffer;
//------------------------------------------------------------------------------
// CharStream
//------------------------------------------------------------------------------
/*! \brief Creates a new, initialized character stream
\param string The character string to be streamed
*/
CharStream::CharStream(const std::string &string)
: fString(string)
, fPos(0)
, fCStatus(B_OK)
{
}
/*! \brief Creates a new, unitialized character stream
Call SetTo() to initialize the stream.
*/
CharStream::CharStream()
: fString("")
, fPos(0)
, fCStatus(B_NO_INIT)
{
}
/*! \brief Destroys the character stream
*/
CharStream::~CharStream() {
Unset();
}
/*! \brief Reinitializes the character stream to the given string
The stream position is reset to the beginning of the stream.
\param string The new character string to be streamed
\return Returns \c B_OK
*/
status_t
CharStream::SetTo(const std::string &string) {
fString = string;
fPos = 0;
fCStatus = B_OK;
return fCStatus;
}
/*! \brief Unitializes the stream
*/
void
CharStream::Unset() {
fString = "";
fPos = 0;
fCStatus = B_NO_INIT;
}
/*! \brief Returns the current status of the stream
\return
- \c B_OK: Ready and initialized
- \c B_NO_INIT: Unitialized
*/
status_t
CharStream::InitCheck() const {
return fCStatus;
}
/*! \brief Returns \c true if there are no more characters in the stream.
If the stream is unitialized, \c true is also returned.
*/
bool
CharStream::IsEmpty() const {
return fPos >= fString.length();
}
/*! \brief Returns the current offset of the stream into the original string.
If the stream is unitialized, zero is returned.
*/
size_t
CharStream::Pos() const {
return fPos;
}
/*! \brief Returns the entire string being streamed.
*/
const std::string&
CharStream::String() const {
return fString;
}
/*! Returns the next character in the stream.
Also increments the position in the stream. Call Unget() to
undo this operation.
Throws a BPrivate::Storage::Sniffer::Err exception if the stream is
unitialized. If the end of the stream has been reached, the position
marker is still incremented, but an end of text char (\c 0x03) is
returned.
*/
char
CharStream::Get() {
if (fCStatus != B_OK)
throw new Err("Sniffer parser error: CharStream::Get() called on uninitialized CharStream object", -1);
if (fPos < fString.length())
return fString[fPos++];
else {
fPos++; // Increment fPos to keep Unget()s consistent
return 0x3; // Return End-Of-Text char
}
}
/*! Shifts the stream position back one character.
Throws a BPrivate::Storage::Sniffer::Err exception if the stream is
unitialized or there are no more characters to unget.
*/
void
CharStream::Unget() {
if (fCStatus != B_OK)
throw new Err("Sniffer parser error: CharStream::Unget() called on uninitialized CharStream object", -1);
if (fPos > 0)
fPos--;
else
throw new Err("Sniffer parser error: CharStream::Unget() called at beginning of character stream", -1);
}
-79
View File
@@ -60,85 +60,6 @@ BPrivate::Storage::Sniffer::parse(const char *rule, Rule *result, BString *parse
return parser.Parse(rule, result, parseError);
}
//------------------------------------------------------------------------------
// CharStream
//------------------------------------------------------------------------------
CharStream::CharStream(const std::string &string)
: fString(string)
, fPos(0)
, fCStatus(B_OK)
{
}
CharStream::CharStream()
: fString("")
, fPos(0)
, fCStatus(B_NO_INIT)
{
}
CharStream::~CharStream() {
Unset();
}
status_t
CharStream::SetTo(const std::string &string) {
fString = string;
fPos = 0;
fCStatus = B_OK;
return fCStatus;
}
void
CharStream::Unset() {
fString = "";
fPos = 0;
fCStatus = B_NO_INIT;
}
status_t
CharStream::InitCheck() const {
return fCStatus;
}
bool
CharStream::IsEmpty() const {
return fPos >= fString.length();
}
size_t
CharStream::Pos() const {
return fPos;
}
const std::string&
CharStream::String() const {
return fString;
}
char
CharStream::Get() {
if (fCStatus != B_OK)
throw new Err("Sniffer parser error: CharStream::Get() called on uninitialized CharStream object", -1);
if (fPos < fString.length())
return fString[fPos++];
else {
fPos++; // Increment fPos to keep Unget()s consistent
return 0x3; // Return End-Of-Text char
}
}
void
CharStream::Unget() {
if (fCStatus != B_OK)
throw new Err("Sniffer parser error: CharStream::Unget() called on uninitialized CharStream object", -1);
if (fPos > 0)
fPos--;
else
throw new Err("Sniffer parser error: CharStream::Unget() called at beginning of character stream", -1);
}
//------------------------------------------------------------------------------
// Token
//------------------------------------------------------------------------------