Colobot
CBotToken.h
1 /*
2  * This file is part of the Colobot: Gold Edition source code
3  * Copyright (C) 2001-2016, Daniel Roux, EPSITEC SA & TerranovaTeam
4  * http://epsitec.ch; http://colobot.info; http://github.com/colobot
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see http://gnu.org/licenses
18  */
19 
20 #pragma once
21 
22 #include "CBot/CBotEnums.h"
23 #include "CBot/CBotUtils.h"
24 
25 #include <vector>
26 #include <string>
27 #include <map>
28 #include <memory>
29 
30 namespace CBot
31 {
32 
80 class CBotToken : public CBotDoublyLinkedList<CBotToken>
81 {
82 public:
86  CBotToken();
90  CBotToken(const CBotToken& pSrc);
99  CBotToken(const std::string& text,
100  const std::string& sep = "",
101  int start = 0,
102  int end = 0);
103 
111  ~CBotToken();
112 
117  int GetType();
118 
123  std::string GetString();
124 
129  void SetString(const std::string& name);
130 
134  int GetStart();
135 
139  int GetEnd();
140 
146  void SetPos(int start, int end);
147 
152  long GetKeywordId();
153 
157  const CBotToken& operator=(const CBotToken& src);
158 
164  static std::unique_ptr<CBotToken> CompileTokens(const std::string& prog);
165 
172  static bool DefineNum(const std::string& name, long val);
173 
178  static void ClearDefineNum();
179 
180 private:
190  static CBotToken* NextToken(const char*& program, bool first);
191 
192 private:
194  TokenType m_type = TokenTypVar;
196  long m_keywordId = -1;
197 
199  std::string m_text = "";
201  std::string m_sep = "";
202 
204  int m_start = 0;
206  int m_end = 0;
207 
209  static std::map<std::string, long> m_defineNum;
210 
216  static int GetKeyWord(const std::string& w);
217 
224  static bool GetDefineNum(const std::string& name, CBotToken* token);
225 };
226 
234 extern bool IsOfType(CBotToken* &p, int type1, int type2 = -1);
235 
242 extern bool IsOfTypeList(CBotToken* &p, int type1, ...);
243 
249 const std::string& LoadString(TokenId id);
250 
251 } // namespace CBot
bool IsOfType(CBotToken *&p, int type1, int type2)
Check if this token is of specified type.
Definition: CBotToken.cpp:482
static std::unique_ptr< CBotToken > CompileTokens(const std::string &prog)
Transforms a CBot program from a string to a list of tokens.
Definition: CBotToken.cpp:414
int GetStart()
Return the beginning location of this token in the original program string.
Definition: CBotToken.cpp:226
static bool DefineNum(const std::string &name, long val)
Define a new constant.
Definition: CBotToken.cpp:468
long GetKeywordId()
Get the keyword id.
Definition: CBotToken.cpp:208
int GetEnd()
Return the ending location of this token in the original program string.
Definition: CBotToken.cpp:233
const std::string & LoadString(TokenId id)
Maps given ID to its string equivalent.
Definition: CBotToken.cpp:124
Definition: CBotUtils.h:138
Some enum values used across the CBot engine.
const CBotToken & operator=(const CBotToken &src)
Copy operator.
Definition: CBotToken.cpp:178
std::string GetString()
Return the token string.
Definition: CBotToken.cpp:214
TokenId
This enum contains possible token types.
Definition: CBotEnums.h:73
static void ClearDefineNum()
Clear the list of defined constants.
Definition: CBotToken.cpp:172
int GetType()
Return the token type or the keyword id.
Definition: CBotToken.cpp:200
TokenType
Types of tokens.
Definition: CBotEnums.h:173
bool IsOfTypeList(CBotToken *&p, int type1,...)
Check if this token is of specified type.
Definition: CBotToken.cpp:494
void SetString(const std::string &name)
Set the token string.
Definition: CBotToken.cpp:220
CBot engine.
Definition: CBotCallMethode.cpp:28
CBotToken()
Default constructor.
Definition: CBotToken.cpp:139
a variable name
Definition: CBotEnums.h:179
~CBotToken()
Destructor.
Definition: CBotToken.cpp:167
Class representing one token of a program.
Definition: CBotToken.h:80
void SetPos(int start, int end)
SetPos Set the token position in the CBot program.
Definition: CBotToken.cpp:240