Cafu Engine
TextParserT Class Reference

This is a class for parsing text. More...

#include "TextParser.hpp"

Classes

class  ParseError
 Error when parsing a text/file. More...
 

Public Member Functions

 TextParserT (const char *Input, std::string Delims="", bool IsFileName=true, const char CommentChar='\0')
 The constructor. More...
 
std::string GetNextToken ()
 Returns the next token. More...
 
int GetNextTokenAsInt ()
 Returns the next token as an int. More...
 
float GetNextTokenAsFloat ()
 Returns the next token as a float. More...
 
void PutBack (const std::string &Token)
 Puts back the string Token, such that the next call to GetNextToken() returns Token. More...
 
std::string PeekNextToken ()
 Returns a peek at the next token without reading over it. More...
 
void AssertAndSkipToken (const std::string &Token)
 Makes sure that the next token is equal to Token. More...
 
bool WasLastTokenQuoted () const
 Returns whether the last read "real" token was a "quoted" token. More...
 
std::string SkipLine ()
 Skips tokens until the end of the current line of text. More...
 
std::string SkipBlock (const std::string &OpeningToken, const std::string &ClosingToken, bool CallerAlreadyReadOpeningToken)
 Skips a whole "block" of tokens, e.g. More...
 
unsigned long GetReadPosByte () const
 Returns the current read position in the input file in byte. More...
 
float GetReadPosPercent () const
 Returns the current read position in the input file in percent. More...
 
bool IsAtEOF () const
 Returns whether the parser has reached the EOF or not. More...
 

Detailed Description

This is a class for parsing text.

It has the following features: a) C++ style comments ("// ...") are recognized. b) Quoted tokens are recognized (ie. "1 2 3" is returned as ONE token).

Constructor & Destructor Documentation

TextParserT::TextParserT ( const char *  Input,
std::string  Delims = "",
bool  IsFileName = true,
const char  CommentChar = '\0' 
)

The constructor.

If IsFileName=true, Input specifies the name of the input file. If IsFileName=false, Input is interpreted as the input string itself. If the constructor experiences any problems (e.g. the input file cannot be read), it creates a text parser for the text of length 0 (the empty text). Delims specifies token delimiters that are returned as separate tokens even if they are not bordered by white-space. CommentChar specifies the character that starts a comment. The default 0 makes "//" being recognized as comment start. If CommentChar has another value, that character is considered as initiating a comment.

Parameters
InputAn input string or the filename of an input file depending on IsFileName.
DelimsSpecifies token delimiters that are returned as separate tokens even if they are not bordered by white-space.
IsFileNameWhether the input string is a real string to parse or the name of a file to parse.
CommentCharChar that initiates a comment line (default is '//').

Member Function Documentation

void TextParserT::AssertAndSkipToken ( const std::string &  Token)

Makes sure that the next token is equal to Token.

Otherwise, a ParseError is thrown. This is short for: "if (TP.GetNextToken()!=Token) throw TextParserT::ParseError();".

Parameters
TokenThe string asserted as the next token.
Exceptions
ParseError
std::string TextParserT::GetNextToken ( )

Returns the next token.

ParseError is thrown when an error is encountered (e.g. EOF), after which the parsing cannot be continued.

Exceptions
ParseError
float TextParserT::GetNextTokenAsFloat ( )

Returns the next token as a float.

ParseError is thrown when an error is encountered (e.g. EOF), after which the parsing cannot be continued.

Exceptions
ParseError
int TextParserT::GetNextTokenAsInt ( )

Returns the next token as an int.

ParseError is thrown when an error is encountered (e.g. EOF), after which the parsing cannot be continued.

Exceptions
ParseError
unsigned long TextParserT::GetReadPosByte ( ) const

Returns the current read position in the input file in byte.

Tokens that have been put back are not taken into account.

float TextParserT::GetReadPosPercent ( ) const

Returns the current read position in the input file in percent.

Tokens that have been put back are not taken into account.

bool TextParserT::IsAtEOF ( ) const

Returns whether the parser has reached the EOF or not.

std::string TextParserT::PeekNextToken ( )

Returns a peek at the next token without reading over it.

This is equivalent to: "std::string T=GetNextToken(); PutBack(T); return T;".

Exceptions
ParseError
void TextParserT::PutBack ( const std::string &  Token)

Puts back the string Token, such that the next call to GetNextToken() returns Token.

This function can be called multiple times in sequence, GetNextToken() will return all put back tokens in a stack-like manner. No checks are performed if Token was actually returned by a previous call by GetNextToken().

Parameters
TokenThe string to put back in the string parsed by TextParserT.
std::string TextParserT::SkipBlock ( const std::string &  OpeningToken,
const std::string &  ClosingToken,
bool  CallerAlreadyReadOpeningToken 
)

Skips a whole "block" of tokens, e.g.

a { ... } or ( ... ) block. The block can contain nested blocks. It can (and must) be stated if the caller has already read the opening token.

Parameters
OpeningTokenOpening token of the block.
ClosingTokenClosing token of the block.
CallerAlreadyReadOpeningTokenThe opening token has already been read by the caller.
Returns
the (possibly multi-line) content of the skipped block.
std::string TextParserT::SkipLine ( )

Skips tokens until the end of the current line of text.

This method always succeeds (it never throws).

Returns
the rest (skipped portion) of the line, preceeded by previously put back or peeked tokens. (Note that previously put back or peeked tokens are returned as well. The whitespace that is used to separate these tokens is however not guaranteed to be identical with the original input text.)
bool TextParserT::WasLastTokenQuoted ( ) const
inline

Returns whether the last read "real" token was a "quoted" token.

"Real" token means that this method doesn't take into account tokens that have been put back with the PutBack() method. That is, when more than one token has been put back at a time (i.e. PutBack() has been called two times in a row without an intermediate call to one of the Get...() methods), the result of this method is not reliable and should not be used.

Returns
whether the last read "real" token was a "quoted" token.

The documentation for this class was generated from the following files: