143 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>'"); |
144 } |
144 } |
145 |
145 |
146 // ============================================================================= |
146 // ============================================================================= |
147 // |
147 // |
148 bool Lexer::GetNext (TokenType req) |
148 bool Lexer::Next (ETokenType req) |
149 { |
149 { |
150 Iterator pos = mTokenPosition; |
150 Iterator pos = mTokenPosition; |
151 |
151 |
152 if (mTokens.IsEmpty()) |
152 if (mTokens.IsEmpty()) |
153 return false; |
153 return false; |
154 |
154 |
155 mTokenPosition++; |
155 mTokenPosition++; |
156 |
156 |
157 if (IsAtEnd() || (req !=TK_Any && GetTokenType() != req)) |
157 if (IsAtEnd() || (req !=TK_Any && TokenType() != req)) |
158 { |
158 { |
159 mTokenPosition = pos; |
159 mTokenPosition = pos; |
160 return false; |
160 return false; |
161 } |
161 } |
162 |
162 |
163 return true; |
163 return true; |
164 } |
164 } |
165 |
165 |
166 // ============================================================================= |
166 // ============================================================================= |
167 // |
167 // |
168 void Lexer::MustGetNext (TokenType tok) |
168 void Lexer::MustGetNext (ETokenType tok) |
169 { |
169 { |
170 if (!GetNext()) |
170 if (!Next()) |
171 Error ("unexpected EOF"); |
171 Error ("unexpected EOF"); |
172 |
172 |
173 if (tok !=TK_Any) |
173 if (tok !=TK_Any) |
174 TokenMustBe (tok); |
174 TokenMustBe (tok); |
175 } |
175 } |
176 |
176 |
177 // ============================================================================= |
177 // ============================================================================= |
178 // eugh.. |
178 // eugh.. |
179 // |
179 // |
180 void Lexer::MustGetFromScanner (LexerScanner& sc, TokenType tt) |
180 void Lexer::MustGetFromScanner (LexerScanner& sc, ETokenType tt) |
181 { |
181 { |
182 if (!sc.GetNextToken()) |
182 if (!sc.GetNextToken()) |
183 Error ("unexpected EOF"); |
183 Error ("unexpected EOF"); |
184 |
184 |
185 if (tt !=TK_Any && sc.GetTokenType() != tt) |
185 if (tt !=TK_Any && sc.GetTokenType() != tt) |
186 { |
186 { |
187 // TODO |
187 // TODO |
188 Token tok; |
188 TokenInfo tok; |
189 tok.type = sc.GetTokenType(); |
189 tok.type = sc.GetTokenType(); |
190 tok.text = sc.GetTokenText(); |
190 tok.text = sc.GetTokenText(); |
191 |
191 |
192 Error ("at %1:%2: expected %3, got %4", |
192 Error ("at %1:%2: expected %3, got %4", |
193 gFileNameStack.Last(), |
193 gFileNameStack.Last(), |
197 } |
197 } |
198 } |
198 } |
199 |
199 |
200 // ============================================================================= |
200 // ============================================================================= |
201 // |
201 // |
202 void Lexer::MustGetAnyOf (const List<TokenType>& toks) |
202 void Lexer::MustGetAnyOf (const List<ETokenType>& toks) |
203 { |
203 { |
204 if (!GetNext()) |
204 if (!Next()) |
205 Error ("unexpected EOF"); |
205 Error ("unexpected EOF"); |
206 |
206 |
207 for (TokenType tok : toks) |
207 for (ETokenType tok : toks) |
208 if (GetTokenType() == tok) |
208 if (TokenType() == tok) |
209 return; |
209 return; |
210 |
210 |
211 String toknames; |
211 String toknames; |
212 |
212 |
213 for (const TokenType& tokType : toks) |
213 for (const ETokenType& tokType : toks) |
214 { |
214 { |
215 if (&tokType == &toks.Last()) |
215 if (&tokType == &toks.Last()) |
216 toknames += " or "; |
216 toknames += " or "; |
217 elif (toknames.IsEmpty() == false) |
217 elif (toknames.IsEmpty() == false) |
218 toknames += ", "; |
218 toknames += ", "; |
219 |
219 |
220 toknames += DescribeTokenType (tokType); |
220 toknames += DescribeTokenType (tokType); |
221 } |
221 } |
222 |
222 |
223 Error ("expected %1, got %2", toknames, DescribeToken (GetToken())); |
223 Error ("expected %1, got %2", toknames, DescribeToken (Token())); |
224 } |
224 } |
225 |
225 |
226 // ============================================================================= |
226 // ============================================================================= |
227 // |
227 // |
228 int Lexer::GetOneSymbol (const StringList& syms) |
228 int Lexer::GetOneSymbol (const StringList& syms) |
229 { |
229 { |
230 if (!GetNext()) |
230 if (!Next()) |
231 Error ("unexpected EOF"); |
231 Error ("unexpected EOF"); |
232 |
232 |
233 if (GetTokenType() ==TK_Symbol) |
233 if (TokenType() ==TK_Symbol) |
234 { |
234 { |
235 for (int i = 0; i < syms.Size(); ++i) |
235 for (int i = 0; i < syms.Size(); ++i) |
236 { |
236 { |
237 if (syms[i] == GetToken()->text) |
237 if (syms[i] == Token()->text) |
238 return i; |
238 return i; |
239 } |
239 } |
240 } |
240 } |
241 |
241 |
242 Error ("expected one of %1, got %2", syms, DescribeToken (GetToken())); |
242 Error ("expected one of %1, got %2", syms, DescribeToken (Token())); |
243 return -1; |
243 return -1; |
244 } |
244 } |
245 |
245 |
246 // ============================================================================= |
246 // ============================================================================= |
247 // |
247 // |
248 void Lexer::TokenMustBe (TokenType tok) |
248 void Lexer::TokenMustBe (ETokenType tok) |
249 { |
249 { |
250 if (GetTokenType() != tok) |
250 if (TokenType() != tok) |
251 Error ("expected %1, got %2", DescribeTokenType (tok), |
251 Error ("expected %1, got %2", DescribeTokenType (tok), |
252 DescribeToken (GetToken())); |
252 DescribeToken (Token())); |
253 } |
253 } |
254 |
254 |
255 // ============================================================================= |
255 // ============================================================================= |
256 // |
256 // |
257 String Lexer::DescribeTokenPrivate (TokenType tokType, Lexer::Token* tok) |
257 String Lexer::DescribeTokenPrivate (ETokenType tokType, Lexer::TokenInfo* tok) |
258 { |
258 { |
259 if (tokType <gLastNamedToken) |
259 if (tokType <gLastNamedToken) |
260 return "\"" + LexerScanner::GetTokenString (tokType) + "\""; |
260 return "\"" + LexerScanner::GetTokenString (tokType) + "\""; |
261 |
261 |
262 switch (tokType) |
262 switch (tokType) |
271 return ""; |
271 return ""; |
272 } |
272 } |
273 |
273 |
274 // ============================================================================= |
274 // ============================================================================= |
275 // |
275 // |
276 bool Lexer::PeekNext (Lexer::Token* tk) |
276 bool Lexer::PeekNext (Lexer::TokenInfo* tk) |
277 { |
277 { |
278 Iterator pos = mTokenPosition; |
278 Iterator pos = mTokenPosition; |
279 bool r = GetNext(); |
279 bool r = Next(); |
280 |
280 |
281 if (r && tk != null) |
281 if (r && tk != null) |
282 *tk = *mTokenPosition; |
282 *tk = *mTokenPosition; |
283 |
283 |
284 mTokenPosition = pos; |
284 mTokenPosition = pos; |
285 return r; |
285 return r; |
286 } |
286 } |
287 |
287 |
288 // ============================================================================= |
288 // ============================================================================= |
289 // |
289 // |
290 bool Lexer::PeekNextType (TokenType req) |
290 bool Lexer::PeekNextType (ETokenType req) |
291 { |
291 { |
292 Iterator pos = mTokenPosition; |
292 Iterator pos = mTokenPosition; |
293 bool result = false; |
293 bool result = false; |
294 |
294 |
295 if (GetNext() && GetTokenType() == req) |
295 if (Next() && TokenType() == req) |
296 result = true; |
296 result = true; |
297 |
297 |
298 mTokenPosition = pos; |
298 mTokenPosition = pos; |
299 return result; |
299 return result; |
300 } |
300 } |
313 if (mTokenPosition + a >= mTokens.end()) |
313 if (mTokenPosition + a >= mTokens.end()) |
314 return ""; |
314 return ""; |
315 |
315 |
316 Iterator oldpos = mTokenPosition; |
316 Iterator oldpos = mTokenPosition; |
317 mTokenPosition += a; |
317 mTokenPosition += a; |
318 String result = GetToken()->text; |
318 String result = Token()->text; |
319 mTokenPosition = oldpos; |
319 mTokenPosition = oldpos; |
320 return result; |
320 return result; |
321 } |
321 } |
322 |
322 |
323 // ============================================================================= |
323 // ============================================================================= |
324 // |
324 // |
325 String Lexer::DescribeCurrentPosition() |
325 String Lexer::DescribeCurrentPosition() |
326 { |
326 { |
327 return GetToken()->file + ":" + GetToken()->line; |
327 return Token()->file + ":" + Token()->line; |
328 } |
328 } |
329 |
329 |
330 // ============================================================================= |
330 // ============================================================================= |
331 // |
331 // |
332 String Lexer::DescribeTokenPosition() |
332 String Lexer::DescribeTokenPosition() |