| 30 #include "lexer.h" |
30 #include "lexer.h" |
| 31 |
31 |
| 32 static StringList FileNameStack; |
32 static StringList FileNameStack; |
| 33 static Lexer* MainLexer = null; |
33 static Lexer* MainLexer = null; |
| 34 |
34 |
| 35 // ============================================================================= |
35 // _________________________________________________________________________________________________ |
| 36 // |
36 // |
| 37 Lexer::Lexer() |
37 Lexer::Lexer() |
| 38 { |
38 { |
| 39 ASSERT_EQ (MainLexer, null); |
39 ASSERT_EQ (MainLexer, null); |
| 40 MainLexer = this; |
40 MainLexer = this; |
| 41 } |
41 } |
| 42 |
42 |
| 43 // ============================================================================= |
43 // _________________________________________________________________________________________________ |
| 44 // |
44 // |
| 45 Lexer::~Lexer() |
45 Lexer::~Lexer() |
| 46 { |
46 { |
| 47 MainLexer = null; |
47 MainLexer = null; |
| 48 } |
48 } |
| 49 |
49 |
| 50 // ============================================================================= |
50 // _________________________________________________________________________________________________ |
| 51 // |
51 // |
| 52 void Lexer::processFile (String fileName) |
52 void Lexer::processFile (String fileName) |
| 53 { |
53 { |
| 54 FileNameStack << fileName; |
54 FileNameStack << fileName; |
| 55 FILE* fp = fopen (fileName, "r"); |
55 FILE* fp = fopen (fileName, "r"); |
| 141 { |
141 { |
| 142 if (!isValidHeader (sc.readLine())) |
142 if (!isValidHeader (sc.readLine())) |
| 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::next (Token req) |
148 bool Lexer::next (Token req) |
| 149 { |
149 { |
| 150 Iterator pos = m_tokenPosition; |
150 Iterator pos = m_tokenPosition; |
| 151 |
151 |
| 161 } |
161 } |
| 162 |
162 |
| 163 return true; |
163 return true; |
| 164 } |
164 } |
| 165 |
165 |
| 166 // ============================================================================= |
166 // _________________________________________________________________________________________________ |
| 167 // |
167 // |
| 168 void Lexer::mustGetNext (Token tok) |
168 void Lexer::mustGetNext (Token tok) |
| 169 { |
169 { |
| 170 if (!next()) |
170 if (!next()) |
| 171 error ("unexpected EOF"); |
171 error ("unexpected EOF"); |
| 172 |
172 |
| 173 if (tok !=Token::Any) |
173 if (tok !=Token::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, Token tt) |
180 void Lexer::mustGetFromScanner (LexerScanner& sc, Token tt) |
| 181 { |
181 { |
| 182 if (sc.getNextToken() == false) |
182 if (sc.getNextToken() == false) |
| 195 DescribeTokenType (tt), |
195 DescribeTokenType (tt), |
| 196 DescribeToken (&tok)); |
196 DescribeToken (&tok)); |
| 197 } |
197 } |
| 198 } |
198 } |
| 199 |
199 |
| 200 // ============================================================================= |
200 // _________________________________________________________________________________________________ |
| 201 // |
201 // |
| 202 void Lexer::mustGetAnyOf (const List<Token>& toks) |
202 void Lexer::mustGetAnyOf (const List<Token>& toks) |
| 203 { |
203 { |
| 204 if (!next()) |
204 if (!next()) |
| 205 error ("unexpected EOF"); |
205 error ("unexpected EOF"); |
| 221 } |
221 } |
| 222 |
222 |
| 223 error ("expected %1, got %2", toknames, DescribeToken (token())); |
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 (!next()) |
230 if (!next()) |
| 231 error ("unexpected EOF"); |
231 error ("unexpected EOF"); |
| 241 |
241 |
| 242 error ("expected one of %1, got %2", syms, DescribeToken (token())); |
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 (Token tok) |
248 void Lexer::tokenMustBe (Token tok) |
| 249 { |
249 { |
| 250 if (tokenType() != tok) |
250 if (tokenType() != tok) |
| 251 error ("expected %1, got %2", DescribeTokenType (tok), |
251 error ("expected %1, got %2", DescribeTokenType (tok), |
| 252 DescribeToken (token())); |
252 DescribeToken (token())); |
| 253 } |
253 } |
| 254 |
254 |
| 255 // ============================================================================= |
255 // _________________________________________________________________________________________________ |
| 256 // |
256 // |
| 257 String Lexer::DescribeTokenPrivate (Token tokType, Lexer::TokenInfo* tok) |
257 String Lexer::DescribeTokenPrivate (Token tokType, Lexer::TokenInfo* tok) |
| 258 { |
258 { |
| 259 if (tokType < LastNamedToken) |
259 if (tokType < LastNamedToken) |
| 260 return "\"" + LexerScanner::GetTokenString (tokType) + "\""; |
260 return "\"" + LexerScanner::GetTokenString (tokType) + "\""; |
| 297 |
297 |
| 298 m_tokenPosition = pos; |
298 m_tokenPosition = pos; |
| 299 return result; |
299 return result; |
| 300 } |
300 } |
| 301 |
301 |
| 302 // ============================================================================= |
302 // _________________________________________________________________________________________________ |
| 303 // |
303 // |
| 304 Lexer* Lexer::GetCurrentLexer() |
304 Lexer* Lexer::GetCurrentLexer() |
| 305 { |
305 { |
| 306 return MainLexer; |
306 return MainLexer; |
| 307 } |
307 } |
| 308 |
308 |
| 309 // ============================================================================= |
309 // _________________________________________________________________________________________________ |
| 310 // |
310 // |
| 311 String Lexer::peekNextString (int a) |
311 String Lexer::peekNextString (int a) |
| 312 { |
312 { |
| 313 if (m_tokenPosition + a >= m_tokens.end()) |
313 if (m_tokenPosition + a >= m_tokens.end()) |
| 314 return ""; |
314 return ""; |
| 318 String result = token()->text; |
318 String result = token()->text; |
| 319 m_tokenPosition = oldpos; |
319 m_tokenPosition = 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 token()->file + ":" + token()->line; |
327 return token()->file + ":" + token()->line; |
| 328 } |
328 } |
| 329 |
329 |
| 330 // ============================================================================= |
330 // _________________________________________________________________________________________________ |
| 331 // |
331 // |
| 332 String Lexer::describeTokenPosition() |
332 String Lexer::describeTokenPosition() |
| 333 { |
333 { |
| 334 return format ("%1 / %2", m_tokenPosition - m_tokens.begin(), m_tokens.size()); |
334 return format ("%1 / %2", m_tokenPosition - m_tokens.begin(), m_tokens.size()); |
| 335 } |
335 } |
| 336 |
336 |
| 337 // ============================================================================= |
337 // _________________________________________________________________________________________________ |
| 338 // |
338 // |
| 339 void Lexer::mustGetSymbol (const String& a) |
339 void Lexer::mustGetSymbol (const String& a) |
| 340 { |
340 { |
| 341 mustGetNext (Token::Any); |
341 mustGetNext (Token::Any); |
| 342 |
342 |