TokenType enum

dev-time-step
krzys-h 2015-12-23 14:05:29 +01:00
parent c9e0249008
commit 9b4a6e0131
6 changed files with 62 additions and 54 deletions

View File

@ -65,11 +65,12 @@ enum CBotGet
}; };
/** /**
* \enum EID * \enum TokenId
* \brief This enum contains possible token types * \brief This enum contains possible token types
*/ */
enum EID enum TokenId
{ {
TokenKeyWord = 2000, //!< keywords
ID_IF = 2000, ID_IF = 2000,
ID_ELSE, ID_ELSE,
ID_WHILE, ID_WHILE,
@ -104,11 +105,13 @@ enum EID
ID_VOID, ID_VOID,
ID_BOOL, ID_BOOL,
TokenKeyVal = 2200, //!< keywords that represent values (true, false, null, nan)
ID_TRUE = 2200, ID_TRUE = 2200,
ID_FALSE, ID_FALSE,
ID_NULL, ID_NULL,
ID_NAN, ID_NAN,
TokenKeyOp = 2300, //!< operators
ID_OPENPAR = 2300, ID_OPENPAR = 2300,
ID_CLOSEPAR, ID_CLOSEPAR,
ID_OPBLK, ID_OPBLK,
@ -157,10 +160,24 @@ enum EID
ID_MODULO, ID_MODULO,
ID_POWER, ID_POWER,
ID_ASSMODULO, ID_ASSMODULO,
TX_UNDEF = 4000, TX_UNDEF = 4000,
TX_NAN TX_NAN
}; };
/**
* \enum TokenType
* \brief Types of tokens
*/
enum TokenType {
TokenTypNone = 0,
TokenTypKeyWord = 1, //!< keyword of the language (see TokenKeyWord)
TokenTypNum = 2, //!< number
TokenTypString = 3, //!< string
TokenTypVar = 4, //!< a variable name
TokenTypDef = 5 //!< value according DefineNum
};
/** /**
* \enum CBotError * \enum CBotError
* \brief This enum contains possible CBot error values. Values in range 5000-5999 are compile errors, 6000-6999 are runtime errors * \brief This enum contains possible CBot error values. Values in range 5000-5999 are compile errors, 6000-6999 are runtime errors

View File

@ -4,7 +4,7 @@
//! \brief Keeps the string corresponding to keyword ID //! \brief Keeps the string corresponding to keyword ID
// Map is filled with id-string pars that are needed for CBot language parsing // Map is filled with id-string pars that are needed for CBot language parsing
static const std::map<EID, const std::string> s_keywordString = { static const std::map<TokenId, const std::string> s_keywordString = {
{ID_IF, "if"}, {ID_IF, "if"},
{ID_ELSE, "else"}, {ID_ELSE, "else"},
{ID_WHILE, "while"}, {ID_WHILE, "while"},
@ -95,7 +95,7 @@ static const std::map<EID, const std::string> s_keywordString = {
}; };
static const std::string emptyString = ""; static const std::string emptyString = "";
const std::string& LoadString(EID id) const std::string& LoadString(TokenId id)
{ {
if (s_keywordString.find(id) != s_keywordString.end()) if (s_keywordString.find(id) != s_keywordString.end())
{ {

View File

@ -9,4 +9,4 @@
* \param id Provided identifier. * \param id Provided identifier.
* \return String if found, else NullString. * \return String if found, else NullString.
*/ */
const std::string& LoadString(EID id); const std::string& LoadString(TokenId id);

View File

@ -50,7 +50,7 @@ CBotToken::CBotToken(const CBotToken* pSrc)
m_Text.clear(); m_Text.clear();
m_Sep.clear(); m_Sep.clear();
m_type = 0; m_type = TokenTypNone;
m_IdKeyWord = 0; m_IdKeyWord = 0;
m_start = 0; m_start = 0;
@ -350,7 +350,7 @@ bis:
if (CharInList( mot[0], num )) token->m_type = TokenTypNum; if (CharInList( mot[0], num )) token->m_type = TokenTypNum;
if (mot[0] == '\"') token->m_type = TokenTypString; if (mot[0] == '\"') token->m_type = TokenTypString;
if (first) token->m_type = 0; if (first) token->m_type = TokenTypNone;
token->m_IdKeyWord = GetKeyWords(mot); token->m_IdKeyWord = GetKeyWords(mot);
if (token->m_IdKeyWord > 0) token->m_type = TokenTypKeyWord; if (token->m_IdKeyWord > 0) token->m_type = TokenTypKeyWord;
@ -400,7 +400,7 @@ CBotToken* CBotToken::CompileTokens(const std::string& program, int& error)
// adds a token as a terminator // adds a token as a terminator
// ( useful for the previous ) // ( useful for the previous )
nxt = new CBotToken(); nxt = new CBotToken();
nxt->m_type = 0; nxt->m_type = TokenTypNone;
prv->m_next = nxt; // added after prv->m_next = nxt; // added after
nxt->m_prev = prv; nxt->m_prev = prv;
@ -459,14 +459,7 @@ void CBotToken::LoadKeyWords()
int i, n = 0; int i, n = 0;
i = TokenKeyWord; //start with keywords of the language i = TokenKeyWord; //start with keywords of the language
while (!(s = LoadString(static_cast<EID>(i))).empty()) while (!(s = LoadString(static_cast<TokenId>(i))).empty())
{
m_ListKeyWords.push_back(s);
m_ListIdKeyWords[n++] = i++;
}
i = TokenKeyDeclare; //keywords of declarations
while (!(s = LoadString(static_cast<EID>(i))).empty())
{ {
m_ListKeyWords.push_back(s); m_ListKeyWords.push_back(s);
m_ListIdKeyWords[n++] = i++; m_ListIdKeyWords[n++] = i++;
@ -474,14 +467,14 @@ void CBotToken::LoadKeyWords()
i = TokenKeyVal; //keywords of values i = TokenKeyVal; //keywords of values
while (!(s = LoadString(static_cast<EID>(i))).empty()) while (!(s = LoadString(static_cast<TokenId>(i))).empty())
{ {
m_ListKeyWords.push_back(s); m_ListKeyWords.push_back(s);
m_ListIdKeyWords[n++] = i++; m_ListIdKeyWords[n++] = i++;
} }
i = TokenKeyOp; //operators i = TokenKeyOp; //operators
while (!(s = LoadString(static_cast<EID>(i))).empty()) while (!(s = LoadString(static_cast<TokenId>(i))).empty())
{ {
m_ListKeyWords.push_back(s); m_ListKeyWords.push_back(s);
m_ListIdKeyWords[n++] = i++; m_ListIdKeyWords[n++] = i++;
@ -542,4 +535,3 @@ bool IsOfTypeList(CBotToken* &p, int type1, ...)
} }
} }
} }

View File

@ -22,40 +22,43 @@
#include <vector> #include <vector>
#include <string> #include <string>
///////////////////////////////////////////////////////////////////////////////////// #include "CBot/CBotEnums.h"
// Token management (tokens)
#define TokenTypKeyWord 1 // a keyword of the language (see TokenKeyWord)
#define TokenTypNum 2 // number
#define TokenTypString 3 // string
#define TokenTypVar 4 // a variable name
#define TokenTypDef 5 // value according DefineNum
#define TokenKeyWord 2000 // keywords of the language
#define TokenKeyDeclare 2100 // keywords of declarations (int, float,..)
#define TokenKeyVal 2200 // keywords representing the value (true, false, null, nan)
#define TokenKeyOp 2300 // operators
#define MAXDEFNUM 1000 // limited number of DefineNum #define MAXDEFNUM 1000 // limited number of DefineNum
/** /**
* \class CBotToken * \brief Class representing one token of a program.
* Responsible for token management. A CBot program is a text string. This string *
* is first transformed into a list of token. It will only treat the case as an * A CBot program starts as a text string. This string is first transformed into a list of tokens.
* error where there is an illegal character in a string. *
* For example : * \section Example Example
* int var = 3 * ( pos.y + x ) * This code:
* is decomposed into (each line is a token) * \code
* int * int var = 3 * ( pos.y + x );
* var * string test = "Hello world";
* = * \endcode
* 3 *
* * * Is decomposed into (each line is a token, separate CBotToken instance):
* ( * \code
* pos.y * int
* + * var
* x * =
* ) * 3
* *
* (
* pos
* .
* y
* +
* x
* )
* ;
* string
* test
* =
* "Hello world"
* ;
* \endcode
*/ */
class CBotToken class CBotToken
@ -204,7 +207,7 @@ private:
//! The previous token in the linked list //! The previous token in the linked list
CBotToken* m_prev; CBotToken* m_prev;
//! The token type //! The token type
int m_type; // type of Token TokenType m_type; // type of Token
//! The id of the keyword //! The id of the keyword
long m_IdKeyWord; long m_IdKeyWord;

View File

@ -667,10 +667,6 @@ void CScript::ColorizeScript(Ui::CEdit* edit, int rangeStart, int rangeEnd)
{ {
color = Gfx::FONT_HIGHLIGHT_KEYWORD; color = Gfx::FONT_HIGHLIGHT_KEYWORD;
} }
else if (type >= TokenKeyDeclare && type < TokenKeyDeclare+100) // TODO: no idea :P seems to never happen
{
color = Gfx::FONT_HIGHLIGHT_TYPE;
}
else if (type >= TokenKeyVal && type < TokenKeyVal+100) // true, false, null, nan else if (type >= TokenKeyVal && type < TokenKeyVal+100) // true, false, null, nan
{ {
color = Gfx::FONT_HIGHLIGHT_CONST; color = Gfx::FONT_HIGHLIGHT_CONST;