scriptreader.cxx

changeset 63
0557babc8675
parent 54
8cc91ef94754
child 67
0a202714eea4
equal deleted inserted replaced
62:824ab7b28e3c 63:0557babc8675
43 #include "string.h" 43 #include "string.h"
44 #include "str.h" 44 #include "str.h"
45 #include "common.h" 45 #include "common.h"
46 #include "scriptreader.h" 46 #include "scriptreader.h"
47 47
48 #define STORE_POSITION \
49 bool _atnewline = atnewline; \
50 unsigned int _curline = curline[fc]; \
51 unsigned int _curchar = curchar[fc];
52
53 #define RESTORE_POSITION \
54 atnewline = _atnewline; \
55 curline[fc] = _curline; \
56 curchar[fc] = _curchar;
57
48 // ============================================================================ 58 // ============================================================================
49 ScriptReader::ScriptReader (str path) { 59 ScriptReader::ScriptReader (str path) {
50 token = ""; 60 token = "";
51 prevtoken = ""; 61 prevtoken = "";
52 prevpos = 0; 62 prevpos = 0;
150 // ============================================================================ 160 // ============================================================================
151 // Peeks the next character 161 // Peeks the next character
152 char ScriptReader::PeekChar (int offset) { 162 char ScriptReader::PeekChar (int offset) {
153 // Store current position 163 // Store current position
154 long curpos = ftell (fp[fc]); 164 long curpos = ftell (fp[fc]);
165 STORE_POSITION
155 166
156 // Forward by offset 167 // Forward by offset
157 fseek (fp[fc], offset, SEEK_CUR); 168 fseek (fp[fc], offset, SEEK_CUR);
158 169
159 // Read the character 170 // Read the character
164 return 0; 175 return 0;
165 } 176 }
166 177
167 // Rewind back 178 // Rewind back
168 fseek (fp[fc], curpos, SEEK_SET); 179 fseek (fp[fc], curpos, SEEK_SET);
180 RESTORE_POSITION
169 181
170 return c[0]; 182 return c[0];
171 } 183 }
172 184
173 // ============================================================================ 185 // ============================================================================
265 // Returns the next token without advancing the cursor. 277 // Returns the next token without advancing the cursor.
266 str ScriptReader::PeekNext (int offset) { 278 str ScriptReader::PeekNext (int offset) {
267 // Store current information 279 // Store current information
268 str storedtoken = token; 280 str storedtoken = token;
269 int cpos = ftell (fp[fc]); 281 int cpos = ftell (fp[fc]);
282 STORE_POSITION
270 283
271 // Advance on the token. 284 // Advance on the token.
272 while (offset >= 0) { 285 while (offset >= 0) {
273 if (!Next (true)) 286 if (!Next (true))
274 return ""; 287 return "";
279 292
280 // Restore position 293 // Restore position
281 fseek (fp[fc], cpos, SEEK_SET); 294 fseek (fp[fc], cpos, SEEK_SET);
282 pos[fc]--; 295 pos[fc]--;
283 token = storedtoken; 296 token = storedtoken;
297 RESTORE_POSITION
284 return tmp; 298 return tmp;
285 } 299 }
286 300
287 // ============================================================================ 301 // ============================================================================
288 void ScriptReader::Seek (unsigned int n, int origin) { 302 void ScriptReader::Seek (unsigned int n, int origin) {
335 } 349 }
336 350
337 // ============================================================================ 351 // ============================================================================
338 void ScriptReader::ParserMessage (const char* header, char* message) { 352 void ScriptReader::ParserMessage (const char* header, char* message) {
339 if (fc >= 0 && fc < MAX_FILESTACK) 353 if (fc >= 0 && fc < MAX_FILESTACK)
340 fprintf (stderr, "%sIn file %s, at line %u, col %u: %s\n", 354 fprintf (stderr, "%s%s:%u:%u: %s\n",
341 header, filepath[fc], curline[fc], curchar[fc], message); 355 header, filepath[fc], curline[fc], curchar[fc], message);
342 else 356 else
343 fprintf (stderr, "%s%s\n", header, message); 357 fprintf (stderr, "%s%s\n", header, message);
344 } 358 }
345 359

mercurial