KFoundation
v2.1
A Better Foundation Library for C++
|
Represents a token in a stream. More...
#include <kfoundation/ObjectStreamReader.h>
Public Member Functions | |
Token (const CodeRange &cr) | |
Constructor. More... | |
bool | is (const type_t &t) const |
Checks if the type of this token matches the given argument. | |
void | validateType (const type_t &t) const |
Checks of the type of this token matches the given argument, and if not, produces a ParseException explaning the problem. | |
Ref< ObjectToken > | asObject () |
Validates if this token represents an object. More... | |
Ref< EndObjectToken > | asEndObject () |
Validates if this token represents end of an object. More... | |
Ref< AttributeToken > | asAttribute () |
Validates if this token represents an attribute. More... | |
Ref< CollectionToken > | asCollection () |
Validates if this token represents begining of a collection. More... | |
Ref< EndCollectionToken > | asEndCollection () |
Validates if this token represents end of a collection. More... | |
![]() | |
virtual Ref< Token > | next ()=0 throw (ParseException) |
Returns the next token in the stream (not the token after this one). | |
![]() | |
bool | equals (RefConst< KFObject > other) const |
Checks if this object is the same as the one reffered by the given refrence. | |
Static Public Member Functions | |
static RefConst< UString > | toString (const type_t t) |
Returns a string corresponding the given parameter. | |
Public Attributes | |
const CodeRange | codeRange |
CodeRange marking begining and end of this token. | |
Represents a token in a stream.
This is an abstract class. The actual object might be of any of the following types:
Use getType() method to determine the type and cast accordingly. Most often this object is used in predictive parsing manner. For example:
void deserialize(Ref<ObjectToken> headToken) { headToken->validateClass("MyClass"); Ref<Token> token = headToken->next(); token->validateType(Token::ATTRIBUTE); _name = token.AS(AttributeToken)->validateName("name")->getValue(); token->next()->validateType(END_OBJECT); }
Conditional statements can be added if desired:
void deserialize(Ref<ObjectToken> headToken) { headToken->validateClass("MyClass"); Ref<Token> token = headToken->next(); if(token.is(Token::ATTRIBUTE)) { Ref<AttributeToken> attrib = token.AS(Attribute); if(attrib->checkName("attrib1")) { _attrib1 = attrib->getValue(); } else if(attrib->checkName("attrib2") { _attrib2 = attrib->getValue(); } else { attrib->throwInvalidName(); } } else { token->validateType(Token::OBJECT); ... } token->next()->validateType(END_OBJECT); }
validateXXX() methods cause an expection to be thrown if the given argument does not match the current token. The exception message will include a code location that helps the user to understand the problem.
kfoundation::Token::Token | ( | const CodeRange & | cr) |
Constructor.
cr | The range marking the begining and the end of input containing this token. |
Ref< AttributeToken > kfoundation::Token::asAttribute | ( | ) |
Validates if this token represents an attribute.
If so casts itself into AttributeToken, otherwise throws a ParseException.
ParseException | if the type of this token is not AttributeToken. |
Ref< CollectionToken > kfoundation::Token::asCollection | ( | ) |
Validates if this token represents begining of a collection.
If so casts itself into CollectionToken, otherwise throws a ParseException.
ParseException | if the type of this token is not CollectionToken. |
Ref< EndCollectionToken > kfoundation::Token::asEndCollection | ( | ) |
Validates if this token represents end of a collection.
If so casts itself into EndCollectionToken, otherwise throws a ParseException.
ParseException | if the type of this token is not EndCollectionToken. |
Ref< EndObjectToken > kfoundation::Token::asEndObject | ( | ) |
Validates if this token represents end of an object.
If so casts itself into EndObjectToken, otherwise throws a ParseException.
ParseException | if the type of this token is not EndObjectToken. |
Ref< ObjectToken > kfoundation::Token::asObject | ( | ) |
Validates if this token represents an object.
If so casts itself into ObjectToken, otherwise throws a ParseException.
ParseException | if the type of this token is not ObjectToken. |