Colobot
Public Member Functions | Static Public Member Functions | List of all members
CBot::CBotToken Class Reference

Class representing one token of a program. More...

#include <CBotToken.h>

Inheritance diagram for CBot::CBotToken:
Inheritance graph
[legend]

Public Member Functions

 CBotToken ()
 Default constructor. More...
 
 CBotToken (const CBotToken &pSrc)
 Copy constructor. More...
 
 CBotToken (const std::string &text, const std::string &sep="", int start=0, int end=0)
 Constructor. More...
 
 ~CBotToken ()
 Destructor. More...
 
int GetType ()
 Return the token type or the keyword id. More...
 
std::string GetString ()
 Return the token string. More...
 
void SetString (const std::string &name)
 Set the token string. More...
 
int GetStart ()
 Return the beginning location of this token in the original program string. More...
 
int GetEnd ()
 Return the ending location of this token in the original program string. More...
 
void SetPos (int start, int end)
 SetPos Set the token position in the CBot program. More...
 
long GetKeywordId ()
 Get the keyword id. More...
 
const CBotTokenoperator= (const CBotToken &src)
 Copy operator. More...
 
- Public Member Functions inherited from CBot::CBotDoublyLinkedList< CBotToken >
virtual ~CBotDoublyLinkedList ()
 Destructor. Be careful, destroys the whole linked list! More...
 
CBotTokenGetNext ()
 Returns the next variable in the linked list. More...
 
CBotTokenGetPrev ()
 Returns the previous variable in the linked list. More...
 
void AddNext (CBotToken *elem)
 Appends a new element at the end of the linked list. More...
 

Static Public Member Functions

static std::unique_ptr< CBotTokenCompileTokens (const std::string &prog)
 Transforms a CBot program from a string to a list of tokens. More...
 
static bool DefineNum (const std::string &name, long val)
 Define a new constant. More...
 
static void ClearDefineNum ()
 Clear the list of defined constants. More...
 

Additional Inherited Members

- Protected Attributes inherited from CBot::CBotDoublyLinkedList< CBotToken >
CBotTokenm_next
 
CBotTokenm_prev
 

Detailed Description

Class representing one token of a program.

A CBot program starts as a text string. This string is first transformed into a list of tokens.

Example tokens

This code:

int var = 3 * ( pos.y + x );
string test = "Hello world";

Is decomposed into (each line is a token, separate CBotToken instance):

int
var
=
3
*
(
pos
.
y
+
x
)
;
string
test
=
"Hello world"
;

Example usage

std::unique_ptr<CBotToken> tokens = CBotToken::CompileTokens(program);
CBotToken* token = tokens.get();
while(token != nullptr)
{
printf("%s\n", token->GetString());
token = token->GetNext();
}

Constructor & Destructor Documentation

CBot::CBotToken::CBotToken ( )

Default constructor.

CBot::CBotToken::CBotToken ( const CBotToken pSrc)

Copy constructor.

CBot::CBotToken::CBotToken ( const std::string &  text,
const std::string &  sep = "",
int  start = 0,
int  end = 0 
)

Constructor.

Parameters
textThe string this token represents
sepAll separators that appeared after this token
startBeginning location in the source code of this token
endEnding location in the source code of this token
CBot::CBotToken::~CBotToken ( )

Destructor.

Be careful! This destroys the whole linked list of tokens

Never call in the middle of the sequence - always on the first token in the list

Member Function Documentation

int CBot::CBotToken::GetType ( )

Return the token type or the keyword id.

Returns
A value from TokenType. For TokenTypKeyWord, returns the keyword ID instead.
std::string CBot::CBotToken::GetString ( )

Return the token string.

Returns
The string associated with this token
void CBot::CBotToken::SetString ( const std::string &  name)

Set the token string.

Parameters
nameThe new string to set
int CBot::CBotToken::GetStart ( )

Return the beginning location of this token in the original program string.

int CBot::CBotToken::GetEnd ( )

Return the ending location of this token in the original program string.

void CBot::CBotToken::SetPos ( int  start,
int  end 
)

SetPos Set the token position in the CBot program.

Parameters
startThe start position of the token
endThe end position of the token
long CBot::CBotToken::GetKeywordId ( )

Get the keyword id.

Returns
The keyword id, see TokenId
const CBotToken & CBot::CBotToken::operator= ( const CBotToken src)

Copy operator.

std::unique_ptr< CBotToken > CBot::CBotToken::CompileTokens ( const std::string &  prog)
static

Transforms a CBot program from a string to a list of tokens.

Parameters
progThe program string
Returns
The first token in the linked list
bool CBot::CBotToken::DefineNum ( const std::string &  name,
long  val 
)
static

Define a new constant.

Parameters
nameName of the constant
valValue of the constant
Returns
true on success, false if already defined
void CBot::CBotToken::ClearDefineNum ( )
static

Clear the list of defined constants.

See also
DefineNum()

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