diff options
Diffstat (limited to 'llex.c')
-rw-r--r-- | llex.c | 64 |
1 files changed, 29 insertions, 35 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: llex.c,v 1.9 1997/12/02 12:43:54 roberto Exp roberto $ | 2 | ** $Id: llex.c,v 1.10 1997/12/09 13:35:19 roberto Exp roberto $ |
3 | ** Lexical Analizer | 3 | ** Lexical Analizer |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -8,6 +8,7 @@ | |||
8 | #include <ctype.h> | 8 | #include <ctype.h> |
9 | #include <string.h> | 9 | #include <string.h> |
10 | 10 | ||
11 | #include "lauxlib.h" | ||
11 | #include "llex.h" | 12 | #include "llex.h" |
12 | #include "lmem.h" | 13 | #include "lmem.h" |
13 | #include "lobject.h" | 14 | #include "lobject.h" |
@@ -66,8 +67,7 @@ void luaX_setinput (ZIO *z) | |||
66 | LS->ifstate[0].elsepart = 1; /* to avoid a free $else */ | 67 | LS->ifstate[0].elsepart = 1; /* to avoid a free $else */ |
67 | LS->lex_z = z; | 68 | LS->lex_z = z; |
68 | firstline(LS); | 69 | firstline(LS); |
69 | LS->textbuff.buffsize = 20; | 70 | luaL_resetbuffer(); |
70 | LS->textbuff.text = luaM_buffer(LS->textbuff.buffsize); | ||
71 | } | 71 | } |
72 | 72 | ||
73 | 73 | ||
@@ -201,31 +201,24 @@ static void inclinenumber (LexState *LS) | |||
201 | 201 | ||
202 | 202 | ||
203 | 203 | ||
204 | static void save (LexState *LS, int c) | 204 | #define save(c) luaL_addchar(c) |
205 | { | 205 | #define save_and_next(LS) (save(LS->current), next(LS)) |
206 | if (LS->textbuff.tokensize >= LS->textbuff.buffsize) | ||
207 | LS->textbuff.text = luaM_buffer(LS->textbuff.buffsize *= 2); | ||
208 | LS->textbuff.text[LS->textbuff.tokensize++] = c; | ||
209 | } | ||
210 | 206 | ||
211 | 207 | ||
212 | char *luaX_lasttoken (void) | 208 | char *luaX_lasttoken (void) |
213 | { | 209 | { |
214 | save(L->lexstate, 0); | 210 | save(0); |
215 | return L->lexstate->textbuff.text; | 211 | return luaL_buffer(); |
216 | } | 212 | } |
217 | 213 | ||
218 | 214 | ||
219 | #define save_and_next(LS) (save(LS, LS->current), next(LS)) | ||
220 | |||
221 | |||
222 | static int read_long_string (LexState *LS, YYSTYPE *l) | 215 | static int read_long_string (LexState *LS, YYSTYPE *l) |
223 | { | 216 | { |
224 | int cont = 0; | 217 | int cont = 0; |
225 | while (1) { | 218 | while (1) { |
226 | switch (LS->current) { | 219 | switch (LS->current) { |
227 | case EOZ: | 220 | case EOZ: |
228 | save(LS, 0); | 221 | save(0); |
229 | return WRONGTOKEN; | 222 | return WRONGTOKEN; |
230 | case '[': | 223 | case '[': |
231 | save_and_next(LS); | 224 | save_and_next(LS); |
@@ -243,7 +236,7 @@ static int read_long_string (LexState *LS, YYSTYPE *l) | |||
243 | } | 236 | } |
244 | continue; | 237 | continue; |
245 | case '\n': | 238 | case '\n': |
246 | save(LS, '\n'); | 239 | save('\n'); |
247 | inclinenumber(LS); | 240 | inclinenumber(LS); |
248 | continue; | 241 | continue; |
249 | default: | 242 | default: |
@@ -251,9 +244,9 @@ static int read_long_string (LexState *LS, YYSTYPE *l) | |||
251 | } | 244 | } |
252 | } endloop: | 245 | } endloop: |
253 | save_and_next(LS); /* pass the second ']' */ | 246 | save_and_next(LS); /* pass the second ']' */ |
254 | LS->textbuff.text[LS->textbuff.tokensize-2] = 0; /* erases ']]' */ | 247 | L->Mbuffer[L->Mbuffnext-2] = 0; /* erases ']]' */ |
255 | l->pTStr = luaS_new(LS->textbuff.text+2); | 248 | l->pTStr = luaS_new(L->Mbuffbase+2); |
256 | LS->textbuff.text[LS->textbuff.tokensize-2] = ']'; /* restores ']]' */ | 249 | L->Mbuffer[L->Mbuffnext-2] = ']'; /* restores ']]' */ |
257 | return STRING; | 250 | return STRING; |
258 | } | 251 | } |
259 | 252 | ||
@@ -267,7 +260,7 @@ int luaY_lex (YYSTYPE *l) | |||
267 | { | 260 | { |
268 | LexState *LS = L->lexstate; | 261 | LexState *LS = L->lexstate; |
269 | double a; | 262 | double a; |
270 | LS->textbuff.tokensize = 0; | 263 | luaL_resetbuffer(); |
271 | if (lua_debug) | 264 | if (lua_debug) |
272 | luaY_codedebugline(LS->linelasttoken); | 265 | luaY_codedebugline(LS->linelasttoken); |
273 | LS->linelasttoken = LS->linenumber; | 266 | LS->linelasttoken = LS->linenumber; |
@@ -286,7 +279,7 @@ int luaY_lex (YYSTYPE *l) | |||
286 | save_and_next(LS); | 279 | save_and_next(LS); |
287 | if (LS->current != '-') return '-'; | 280 | if (LS->current != '-') return '-'; |
288 | do { next(LS); } while (LS->current != '\n' && LS->current != EOZ); | 281 | do { next(LS); } while (LS->current != '\n' && LS->current != EOZ); |
289 | LS->textbuff.tokensize = 0; | 282 | luaL_resetbuffer(); |
290 | continue; | 283 | continue; |
291 | 284 | ||
292 | case '[': | 285 | case '[': |
@@ -325,15 +318,15 @@ int luaY_lex (YYSTYPE *l) | |||
325 | switch (LS->current) { | 318 | switch (LS->current) { |
326 | case EOZ: | 319 | case EOZ: |
327 | case '\n': | 320 | case '\n': |
328 | save(LS, 0); | 321 | save(0); |
329 | return WRONGTOKEN; | 322 | return WRONGTOKEN; |
330 | case '\\': | 323 | case '\\': |
331 | next(LS); /* do not save the '\' */ | 324 | next(LS); /* do not save the '\' */ |
332 | switch (LS->current) { | 325 | switch (LS->current) { |
333 | case 'n': save(LS, '\n'); next(LS); break; | 326 | case 'n': save('\n'); next(LS); break; |
334 | case 't': save(LS, '\t'); next(LS); break; | 327 | case 't': save('\t'); next(LS); break; |
335 | case 'r': save(LS, '\r'); next(LS); break; | 328 | case 'r': save('\r'); next(LS); break; |
336 | case '\n': save(LS, '\n'); inclinenumber(LS); break; | 329 | case '\n': save('\n'); inclinenumber(LS); break; |
337 | default : save_and_next(LS); break; | 330 | default : save_and_next(LS); break; |
338 | } | 331 | } |
339 | break; | 332 | break; |
@@ -342,9 +335,9 @@ int luaY_lex (YYSTYPE *l) | |||
342 | } | 335 | } |
343 | } | 336 | } |
344 | next(LS); /* skip delimiter */ | 337 | next(LS); /* skip delimiter */ |
345 | save(LS, 0); | 338 | save(0); |
346 | l->pTStr = luaS_new(LS->textbuff.text+1); | 339 | l->pTStr = luaS_new(L->Mbuffbase+1); |
347 | LS->textbuff.text[LS->textbuff.tokensize-1] = del; /* restore delimiter */ | 340 | L->Mbuffer[L->Mbuffnext-1] = del; /* restore delimiter */ |
348 | return STRING; | 341 | return STRING; |
349 | } | 342 | } |
350 | 343 | ||
@@ -375,7 +368,7 @@ int luaY_lex (YYSTYPE *l) | |||
375 | if (LS->current == '.') { | 368 | if (LS->current == '.') { |
376 | save_and_next(LS); | 369 | save_and_next(LS); |
377 | if (LS->current == '.') { | 370 | if (LS->current == '.') { |
378 | save(LS, 0); | 371 | save(0); |
379 | luaY_error( | 372 | luaY_error( |
380 | "ambiguous syntax (decimal point x string concatenation)"); | 373 | "ambiguous syntax (decimal point x string concatenation)"); |
381 | } | 374 | } |
@@ -396,7 +389,7 @@ int luaY_lex (YYSTYPE *l) | |||
396 | neg=(LS->current=='-'); | 389 | neg=(LS->current=='-'); |
397 | if (LS->current == '+' || LS->current == '-') save_and_next(LS); | 390 | if (LS->current == '+' || LS->current == '-') save_and_next(LS); |
398 | if (!isdigit(LS->current)) { | 391 | if (!isdigit(LS->current)) { |
399 | save(LS, 0); return WRONGTOKEN; } | 392 | save(0); return WRONGTOKEN; } |
400 | do { | 393 | do { |
401 | e=10.0*e+(LS->current-'0'); | 394 | e=10.0*e+(LS->current-'0'); |
402 | save_and_next(LS); | 395 | save_and_next(LS); |
@@ -412,23 +405,24 @@ int luaY_lex (YYSTYPE *l) | |||
412 | } | 405 | } |
413 | 406 | ||
414 | case EOZ: | 407 | case EOZ: |
415 | save(LS, 0); | 408 | save(0); |
416 | if (LS->iflevel > 0) | 409 | if (LS->iflevel > 0) |
417 | luaY_syntaxerror("input ends inside a $if", ""); | 410 | luaY_syntaxerror("input ends inside a $if", ""); |
418 | return 0; | 411 | return 0; |
419 | 412 | ||
420 | default: | 413 | default: |
421 | if (LS->current != '_' && !isalpha(LS->current)) { | 414 | if (LS->current != '_' && !isalpha(LS->current)) { |
415 | int c = LS->current; | ||
422 | save_and_next(LS); | 416 | save_and_next(LS); |
423 | return LS->textbuff.text[0]; | 417 | return c; |
424 | } | 418 | } |
425 | else { /* identifier or reserved word */ | 419 | else { /* identifier or reserved word */ |
426 | TaggedString *ts; | 420 | TaggedString *ts; |
427 | do { | 421 | do { |
428 | save_and_next(LS); | 422 | save_and_next(LS); |
429 | } while (isalnum(LS->current) || LS->current == '_'); | 423 | } while (isalnum(LS->current) || LS->current == '_'); |
430 | save(LS, 0); | 424 | save(0); |
431 | ts = luaS_new(LS->textbuff.text); | 425 | ts = luaS_new(L->Mbuffbase); |
432 | if (ts->head.marked > 255) | 426 | if (ts->head.marked > 255) |
433 | return ts->head.marked; /* reserved word */ | 427 | return ts->head.marked; /* reserved word */ |
434 | l->pTStr = ts; | 428 | l->pTStr = ts; |