src/Lexer.cc

changeset 110
7a7a53f1d51b
parent 108
6409ece8297c
child 112
def56932f938
equal deleted inserted replaced
109:6572803cd0ca 110:7a7a53f1d51b
61 CheckFileHeader (sc); 61 CheckFileHeader (sc);
62 62
63 while (sc.GetNextToken()) 63 while (sc.GetNextToken())
64 { 64 {
65 // Preprocessor commands: 65 // Preprocessor commands:
66 if (sc.GetTokenType() == tkHash) 66 if (sc.GetTokenType() ==TK_Hash)
67 { 67 {
68 MustGetFromScanner (sc, tkSymbol); 68 MustGetFromScanner (sc,TK_Symbol);
69 69
70 if (sc.GetTokenText() == "include") 70 if (sc.GetTokenText() == "include")
71 { 71 {
72 MustGetFromScanner (sc, tkString); 72 MustGetFromScanner (sc,TK_String);
73 String fileName = sc.GetTokenText(); 73 String fileName = sc.GetTokenText();
74 74
75 if (gFileNameStack.Contains (fileName)) 75 if (gFileNameStack.Contains (fileName))
76 Error ("attempted to #include %1 recursively", sc.GetTokenText()); 76 Error ("attempted to #include %1 recursively", sc.GetTokenText());
77 77
87 tok.line = sc.GetLine(); 87 tok.line = sc.GetLine();
88 tok.column = sc.GetColumn(); 88 tok.column = sc.GetColumn();
89 tok.type = sc.GetTokenType(); 89 tok.type = sc.GetTokenType();
90 tok.text = sc.GetTokenText(); 90 tok.text = sc.GetTokenText();
91 91
92 // devf ("Token #%1: %2:%3:%4: %5 (%6)\n", mTokens.size(), 92 // devf ("Token #%1: %2:%3:%4: %5 (%6)\n", mTokens.Size(),
93 // tok.file, tok.line, tok.column, DescribeToken (&tok), DescribeTokenType (tok.type)); 93 // tok.file, tok.line, tok.column, DescribeToken (&tok),
94 // GetTokenTypeString (tok.type));
94 95
95 mTokens << tok; 96 mTokens << tok;
96 } 97 }
97 } 98 }
98 99
142 Error ("Not a valid botscript file! File must start with '#!botc <version>'"); 143 Error ("Not a valid botscript file! File must start with '#!botc <version>'");
143 } 144 }
144 145
145 // ============================================================================= 146 // =============================================================================
146 // 147 //
147 bool Lexer::GetNext (EToken req) 148 bool Lexer::GetNext (TokenType req)
148 { 149 {
149 Iterator pos = mTokenPosition; 150 Iterator pos = mTokenPosition;
150 151
151 if (mTokens.IsEmpty()) 152 if (mTokens.IsEmpty())
152 return false; 153 return false;
153 154
154 mTokenPosition++; 155 mTokenPosition++;
155 156
156 if (IsAtEnd() || (req != tkAny && GetTokenType() != req)) 157 if (IsAtEnd() || (req !=TK_Any && GetTokenType() != req))
157 { 158 {
158 mTokenPosition = pos; 159 mTokenPosition = pos;
159 return false; 160 return false;
160 } 161 }
161 162
162 return true; 163 return true;
163 } 164 }
164 165
165 // ============================================================================= 166 // =============================================================================
166 // 167 //
167 void Lexer::MustGetNext (EToken tok) 168 void Lexer::MustGetNext (TokenType tok)
168 { 169 {
169 if (!GetNext()) 170 if (!GetNext())
170 Error ("unexpected EOF"); 171 Error ("unexpected EOF");
171 172
172 if (tok != tkAny) 173 if (tok !=TK_Any)
173 TokenMustBe (tok); 174 TokenMustBe (tok);
174 } 175 }
175 176
176 // ============================================================================= 177 // =============================================================================
177 // eugh.. 178 // eugh..
178 // 179 //
179 void Lexer::MustGetFromScanner (LexerScanner& sc, EToken tt) 180 void Lexer::MustGetFromScanner (LexerScanner& sc, TokenType tt)
180 { 181 {
181 if (!sc.GetNextToken()) 182 if (!sc.GetNextToken())
182 Error ("unexpected EOF"); 183 Error ("unexpected EOF");
183 184
184 if (tt != tkAny && sc.GetTokenType() != tt) 185 if (tt !=TK_Any && sc.GetTokenType() != tt)
185 { 186 {
186 // TODO 187 // TODO
187 Token tok; 188 Token tok;
188 tok.type = sc.GetTokenType(); 189 tok.type = sc.GetTokenType();
189 tok.text = sc.GetTokenText(); 190 tok.text = sc.GetTokenText();
196 } 197 }
197 } 198 }
198 199
199 // ============================================================================= 200 // =============================================================================
200 // 201 //
201 void Lexer::MustGetAnyOf (const List<EToken>& toks) 202 void Lexer::MustGetAnyOf (const List<TokenType>& toks)
202 { 203 {
203 if (!GetNext()) 204 if (!GetNext())
204 Error ("unexpected EOF"); 205 Error ("unexpected EOF");
205 206
206 for (EToken tok : toks) 207 for (TokenType tok : toks)
207 if (GetTokenType() == tok) 208 if (GetTokenType() == tok)
208 return; 209 return;
209 210
210 String toknames; 211 String toknames;
211 212
212 for (const EToken& tokType : toks) 213 for (const TokenType& tokType : toks)
213 { 214 {
214 if (&tokType == &toks.Last()) 215 if (&tokType == &toks.Last())
215 toknames += " or "; 216 toknames += " or ";
216 elif (toknames.IsEmpty() == false) 217 elif (toknames.IsEmpty() == false)
217 toknames += ", "; 218 toknames += ", ";
222 Error ("expected %1, got %2", toknames, DescribeToken (GetToken())); 223 Error ("expected %1, got %2", toknames, DescribeToken (GetToken()));
223 } 224 }
224 225
225 // ============================================================================= 226 // =============================================================================
226 // 227 //
227 int Lexer::GEXPRSYM_tOne (const StringList& syms) 228 int Lexer::GetOneSymbol (const StringList& syms)
228 { 229 {
229 if (!GetNext()) 230 if (!GetNext())
230 Error ("unexpected EOF"); 231 Error ("unexpected EOF");
231 232
232 if (GetTokenType() == tkSymbol) 233 if (GetTokenType() ==TK_Symbol)
233 { 234 {
234 for (int i = 0; i < syms.Size(); ++i) 235 for (int i = 0; i < syms.Size(); ++i)
235 { 236 {
236 if (syms[i] == GetToken()->text) 237 if (syms[i] == GetToken()->text)
237 return i; 238 return i;
242 return -1; 243 return -1;
243 } 244 }
244 245
245 // ============================================================================= 246 // =============================================================================
246 // 247 //
247 void Lexer::TokenMustBe (EToken tok) 248 void Lexer::TokenMustBe (TokenType tok)
248 { 249 {
249 if (GetTokenType() != tok) 250 if (GetTokenType() != tok)
250 Error ("expected %1, got %2", DescribeTokenType (tok), 251 Error ("expected %1, got %2", DescribeTokenType (tok),
251 DescribeToken (GetToken())); 252 DescribeToken (GetToken()));
252 } 253 }
253 254
254 // ============================================================================= 255 // =============================================================================
255 // 256 //
256 String Lexer::DescribeTokenPrivate (EToken tokType, Lexer::Token* tok) 257 String Lexer::DescribeTokenPrivate (TokenType tokType, Lexer::Token* tok)
257 { 258 {
258 if (tokType < tkLastNamedToken) 259 if (tokType <gLastNamedToken)
259 return "\"" + LexerScanner::GetTokenString (tokType) + "\""; 260 return "\"" + LexerScanner::GetTokenString (tokType) + "\"";
260 261
261 switch (tokType) 262 switch (tokType)
262 { 263 {
263 case tkSymbol: return tok ? tok->text : "a symbol"; 264 case TK_Symbol: return tok ? tok->text : "a symbol";
264 case tkNumber: return tok ? tok->text : "a number"; 265 case TK_Number: return tok ? tok->text : "a number";
265 case tkString: return tok ? ("\"" + tok->text + "\"") : "a string"; 266 case TK_String: return tok ? ("\"" + tok->text + "\"") : "a string";
266 case tkAny: return tok ? tok->text : "any token"; 267 case TK_Any: return tok ? tok->text : "any token";
267 default: break; 268 default: break;
268 } 269 }
269 270
270 return ""; 271 return "";
271 } 272 }
284 return r; 285 return r;
285 } 286 }
286 287
287 // ============================================================================= 288 // =============================================================================
288 // 289 //
289 bool Lexer::PeekNextType (EToken req) 290 bool Lexer::PeekNextType (TokenType req)
290 { 291 {
291 Iterator pos = mTokenPosition; 292 Iterator pos = mTokenPosition;
292 bool result = false; 293 bool result = false;
293 294
294 if (GetNext() && GetTokenType() == req) 295 if (GetNext() && GetTokenType() == req)

mercurial