aboutsummaryrefslogtreecommitdiff
path: root/llex.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-11-21 17:00:46 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-11-21 17:00:46 -0200
commitaccd7bc25355be4350db6d117515c672121a67f2 (patch)
treee351b89ca86b67a3c60874c230626a13517524b8 /llex.c
parent6153200bc25bd99f9d3d25d7caa486b03b6535d5 (diff)
downloadlua-accd7bc25355be4350db6d117515c672121a67f2.tar.gz
lua-accd7bc25355be4350db6d117515c672121a67f2.tar.bz2
lua-accd7bc25355be4350db6d117515c672121a67f2.zip
small modifications (format, small optimizations, etc)
Diffstat (limited to 'llex.c')
-rw-r--r--llex.c318
1 files changed, 159 insertions, 159 deletions
diff --git a/llex.c b/llex.c
index e2afba0e..ee87d57d 100644
--- a/llex.c
+++ b/llex.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: llex.c,v 1.6 1997/11/19 17:29:23 roberto Exp roberto $ 2** $Id: llex.c,v 1.7 1997/11/19 17:35:47 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*/
@@ -23,7 +23,7 @@
23int lua_debug=0; 23int lua_debug=0;
24 24
25 25
26#define next(LL) (LL->current = zgetc(LL->lex_z)) 26#define next(LS) (LS->current = zgetc(LS->lex_z))
27 27
28 28
29static struct { 29static struct {
@@ -46,29 +46,29 @@ void luaX_init (void)
46} 46}
47 47
48 48
49static void firstline (LexState *LL) 49static void firstline (LexState *LS)
50{ 50{
51 int c = zgetc(LL->lex_z); 51 int c = zgetc(LS->lex_z);
52 if (c == '#') 52 if (c == '#')
53 while((c=zgetc(LL->lex_z)) != '\n' && c != EOZ) /* skip first line */; 53 while ((c=zgetc(LS->lex_z)) != '\n' && c != EOZ) /* skip first line */;
54 zungetc(LL->lex_z); 54 zungetc(LS->lex_z);
55} 55}
56 56
57 57
58void luaX_setinput (ZIO *z) 58void luaX_setinput (ZIO *z)
59{ 59{
60 LexState *LL = L->lexstate; 60 LexState *LS = L->lexstate;
61 LL->current = '\n'; 61 LS->current = '\n';
62 LL->linelasttoken = 0; 62 LS->linelasttoken = 0;
63 LL->lastline = 0; 63 LS->lastline = 0;
64 LL->linenumber = 0; 64 LS->linenumber = 0;
65 LL->iflevel = 0; 65 LS->iflevel = 0;
66 LL->ifstate[0].skip = 0; 66 LS->ifstate[0].skip = 0;
67 LL->ifstate[0].elsepart = 1; /* to avoid a free $else */ 67 LS->ifstate[0].elsepart = 1; /* to avoid a free $else */
68 LL->lex_z = z; 68 LS->lex_z = z;
69 firstline(LL); 69 firstline(LS);
70 LL->textbuff.buffsize = 20; 70 LS->textbuff.buffsize = 20;
71 LL->textbuff.text = luaM_buffer(LL->textbuff.buffsize); 71 LS->textbuff.text = luaM_buffer(LS->textbuff.buffsize);
72} 72}
73 73
74 74
@@ -81,10 +81,10 @@ void luaX_setinput (ZIO *z)
81 81
82#define PRAGMASIZE 20 82#define PRAGMASIZE 20
83 83
84static void skipspace (LexState *LL) 84static void skipspace (LexState *LS)
85{ 85{
86 while (LL->current == ' ' || LL->current == '\t' || LL->current == '\r') 86 while (LS->current == ' ' || LS->current == '\t' || LS->current == '\r')
87 next(LL); 87 next(LS);
88} 88}
89 89
90 90
@@ -102,49 +102,49 @@ static int checkcond (char *buff)
102} 102}
103 103
104 104
105static void readname (LexState *LL, char *buff) 105static void readname (LexState *LS, char *buff)
106{ 106{
107 int i = 0; 107 int i = 0;
108 skipspace(LL); 108 skipspace(LS);
109 while (isalnum(LL->current) || LL->current == '_') { 109 while (isalnum(LS->current) || LS->current == '_') {
110 if (i >= PRAGMASIZE) { 110 if (i >= PRAGMASIZE) {
111 buff[PRAGMASIZE] = 0; 111 buff[PRAGMASIZE] = 0;
112 luaY_syntaxerror("pragma too long", buff); 112 luaY_syntaxerror("pragma too long", buff);
113 } 113 }
114 buff[i++] = LL->current; 114 buff[i++] = LS->current;
115 next(LL); 115 next(LS);
116 } 116 }
117 buff[i] = 0; 117 buff[i] = 0;
118} 118}
119 119
120 120
121static void inclinenumber (LexState *LL); 121static void inclinenumber (LexState *LS);
122 122
123 123
124static void ifskip (LexState *LL) 124static void ifskip (LexState *LS)
125{ 125{
126 while (LL->ifstate[LL->iflevel].skip) { 126 while (LS->ifstate[LS->iflevel].skip) {
127 if (LL->current == '\n') 127 if (LS->current == '\n')
128 inclinenumber(LL); 128 inclinenumber(LS);
129 else if (LL->current == EOZ) 129 else if (LS->current == EOZ)
130 luaY_syntaxerror("input ends inside a $if", ""); 130 luaY_syntaxerror("input ends inside a $if", "");
131 else next(LL); 131 else next(LS);
132 } 132 }
133} 133}
134 134
135 135
136static void inclinenumber (LexState *LL) 136static void inclinenumber (LexState *LS)
137{ 137{
138 static char *pragmas [] = 138 static char *pragmas [] =
139 {"debug", "nodebug", "endinput", "end", "ifnot", "if", "else", NULL}; 139 {"debug", "nodebug", "endinput", "end", "ifnot", "if", "else", NULL};
140 next(LL); /* skip '\n' */ 140 next(LS); /* skip '\n' */
141 ++LL->linenumber; 141 ++LS->linenumber;
142 if (LL->current == '$') { /* is a pragma? */ 142 if (LS->current == '$') { /* is a pragma? */
143 char buff[PRAGMASIZE+1]; 143 char buff[PRAGMASIZE+1];
144 int ifnot = 0; 144 int ifnot = 0;
145 int skip = LL->ifstate[LL->iflevel].skip; 145 int skip = LS->ifstate[LS->iflevel].skip;
146 next(LL); /* skip $ */ 146 next(LS); /* skip $ */
147 readname(LL, buff); 147 readname(LS, buff);
148 switch (luaO_findstring(buff, pragmas)) { 148 switch (luaO_findstring(buff, pragmas)) {
149 case 0: /* debug */ 149 case 0: /* debug */
150 if (!skip) lua_debug = 1; 150 if (!skip) lua_debug = 1;
@@ -154,42 +154,42 @@ static void inclinenumber (LexState *LL)
154 break; 154 break;
155 case 2: /* endinput */ 155 case 2: /* endinput */
156 if (!skip) { 156 if (!skip) {
157 LL->current = EOZ; 157 LS->current = EOZ;
158 LL->iflevel = 0; /* to allow $endinput inside a $if */ 158 LS->iflevel = 0; /* to allow $endinput inside a $if */
159 } 159 }
160 break; 160 break;
161 case 3: /* end */ 161 case 3: /* end */
162 if (LL->iflevel-- == 0) 162 if (LS->iflevel-- == 0)
163 luaY_syntaxerror("unmatched $end", "$end"); 163 luaY_syntaxerror("unmatched $end", "$end");
164 break; 164 break;
165 case 4: /* ifnot */ 165 case 4: /* ifnot */
166 ifnot = 1; 166 ifnot = 1;
167 /* go through */ 167 /* go through */
168 case 5: /* if */ 168 case 5: /* if */
169 if (LL->iflevel == MAX_IFS-1) 169 if (LS->iflevel == MAX_IFS-1)
170 luaY_syntaxerror("too many nested `$ifs'", "$if"); 170 luaY_syntaxerror("too many nested `$ifs'", "$if");
171 readname(LL, buff); 171 readname(LS, buff);
172 LL->iflevel++; 172 LS->iflevel++;
173 LL->ifstate[LL->iflevel].elsepart = 0; 173 LS->ifstate[LS->iflevel].elsepart = 0;
174 LL->ifstate[LL->iflevel].condition = checkcond(buff) ? !ifnot : ifnot; 174 LS->ifstate[LS->iflevel].condition = checkcond(buff) ? !ifnot : ifnot;
175 LL->ifstate[LL->iflevel].skip = skip || !LL->ifstate[LL->iflevel].condition; 175 LS->ifstate[LS->iflevel].skip = skip || !LS->ifstate[LS->iflevel].condition;
176 break; 176 break;
177 case 6: /* else */ 177 case 6: /* else */
178 if (LL->ifstate[LL->iflevel].elsepart) 178 if (LS->ifstate[LS->iflevel].elsepart)
179 luaY_syntaxerror("unmatched $else", "$else"); 179 luaY_syntaxerror("unmatched $else", "$else");
180 LL->ifstate[LL->iflevel].elsepart = 1; 180 LS->ifstate[LS->iflevel].elsepart = 1;
181 LL->ifstate[LL->iflevel].skip = LL->ifstate[LL->iflevel-1].skip || 181 LS->ifstate[LS->iflevel].skip = LS->ifstate[LS->iflevel-1].skip ||
182 LL->ifstate[LL->iflevel].condition; 182 LS->ifstate[LS->iflevel].condition;
183 break; 183 break;
184 default: 184 default:
185 luaY_syntaxerror("invalid pragma", buff); 185 luaY_syntaxerror("invalid pragma", buff);
186 } 186 }
187 skipspace(LL); 187 skipspace(LS);
188 if (LL->current == '\n') /* pragma must end with a '\n' ... */ 188 if (LS->current == '\n') /* pragma must end with a '\n' ... */
189 inclinenumber(LL); 189 inclinenumber(LS);
190 else if (LL->current != EOZ) /* or eof */ 190 else if (LS->current != EOZ) /* or eof */
191 luaY_syntaxerror("invalid pragma format", buff); 191 luaY_syntaxerror("invalid pragma format", buff);
192 ifskip(LL); 192 ifskip(LS);
193 } 193 }
194} 194}
195 195
@@ -202,11 +202,11 @@ static void inclinenumber (LexState *LL)
202 202
203 203
204 204
205static void save (LexState *LL, int c) 205static void save (LexState *LS, int c)
206{ 206{
207 if (LL->textbuff.tokensize >= LL->textbuff.buffsize) 207 if (LS->textbuff.tokensize >= LS->textbuff.buffsize)
208 LL->textbuff.text = luaM_buffer(LL->textbuff.buffsize *= 2); 208 LS->textbuff.text = luaM_buffer(LS->textbuff.buffsize *= 2);
209 LL->textbuff.text[LL->textbuff.tokensize++] = c; 209 LS->textbuff.text[LS->textbuff.tokensize++] = c;
210} 210}
211 211
212 212
@@ -217,44 +217,44 @@ char *luaX_lasttoken (void)
217} 217}
218 218
219 219
220#define save_and_next(LL) (save(LL, LL->current), next(LL)) 220#define save_and_next(LS) (save(LS, LS->current), next(LS))
221 221
222 222
223static int read_long_string (LexState *LL, YYSTYPE *l) 223static int read_long_string (LexState *LS, YYSTYPE *l)
224{ 224{
225 int cont = 0; 225 int cont = 0;
226 while (1) { 226 while (1) {
227 switch (LL->current) { 227 switch (LS->current) {
228 case EOZ: 228 case EOZ:
229 save(LL, 0); 229 save(LS, 0);
230 return WRONGTOKEN; 230 return WRONGTOKEN;
231 case '[': 231 case '[':
232 save_and_next(LL); 232 save_and_next(LS);
233 if (LL->current == '[') { 233 if (LS->current == '[') {
234 cont++; 234 cont++;
235 save_and_next(LL); 235 save_and_next(LS);
236 } 236 }
237 continue; 237 continue;
238 case ']': 238 case ']':
239 save_and_next(LL); 239 save_and_next(LS);
240 if (LL->current == ']') { 240 if (LS->current == ']') {
241 if (cont == 0) goto endloop; 241 if (cont == 0) goto endloop;
242 cont--; 242 cont--;
243 save_and_next(LL); 243 save_and_next(LS);
244 } 244 }
245 continue; 245 continue;
246 case '\n': 246 case '\n':
247 save(LL, '\n'); 247 save(LS, '\n');
248 inclinenumber(LL); 248 inclinenumber(LS);
249 continue; 249 continue;
250 default: 250 default:
251 save_and_next(LL); 251 save_and_next(LS);
252 } 252 }
253 } endloop: 253 } endloop:
254 save_and_next(LL); /* pass the second ']' */ 254 save_and_next(LS); /* pass the second ']' */
255 LL->textbuff.text[LL->textbuff.tokensize-2] = 0; /* erases ']]' */ 255 LS->textbuff.text[LS->textbuff.tokensize-2] = 0; /* erases ']]' */
256 l->pTStr = luaS_new(LL->textbuff.text+2); 256 l->pTStr = luaS_new(LS->textbuff.text+2);
257 LL->textbuff.text[LL->textbuff.tokensize-2] = ']'; /* restores ']]' */ 257 LS->textbuff.text[LS->textbuff.tokensize-2] = ']'; /* restores ']]' */
258 return STRING; 258 return STRING;
259} 259}
260 260
@@ -266,103 +266,103 @@ static int read_long_string (LexState *LL, YYSTYPE *l)
266int luaY_lex (YYSTYPE *l); 266int luaY_lex (YYSTYPE *l);
267int luaY_lex (YYSTYPE *l) 267int luaY_lex (YYSTYPE *l)
268{ 268{
269 LexState *LL = L->lexstate; 269 LexState *LS = L->lexstate;
270 double a; 270 double a;
271 LL->textbuff.tokensize = 0; 271 LS->textbuff.tokensize = 0;
272 if (lua_debug) 272 if (lua_debug)
273 luaY_codedebugline(LL->linelasttoken); 273 luaY_codedebugline(LS->linelasttoken);
274 LL->linelasttoken = LL->linenumber; 274 LS->linelasttoken = LS->linenumber;
275 while (1) { 275 while (1) {
276 switch (LL->current) { 276 switch (LS->current) {
277 case '\n': 277 case '\n':
278 inclinenumber(LL); 278 inclinenumber(LS);
279 LL->linelasttoken = LL->linenumber; 279 LS->linelasttoken = LS->linenumber;
280 continue; 280 continue;
281 281
282 case ' ': case '\t': case '\r': /* CR: to avoid problems with DOS */ 282 case ' ': case '\t': case '\r': /* CR: to avoid problems with DOS */
283 next(LL); 283 next(LS);
284 continue; 284 continue;
285 285
286 case '-': 286 case '-':
287 save_and_next(LL); 287 save_and_next(LS);
288 if (LL->current != '-') return '-'; 288 if (LS->current != '-') return '-';
289 do { next(LL); } while (LL->current != '\n' && LL->current != EOZ); 289 do { next(LS); } while (LS->current != '\n' && LS->current != EOZ);
290 LL->textbuff.tokensize = 0; 290 LS->textbuff.tokensize = 0;
291 continue; 291 continue;
292 292
293 case '[': 293 case '[':
294 save_and_next(LL); 294 save_and_next(LS);
295 if (LL->current != '[') return '['; 295 if (LS->current != '[') return '[';
296 else { 296 else {
297 save_and_next(LL); /* pass the second '[' */ 297 save_and_next(LS); /* pass the second '[' */
298 return read_long_string(LL, l); 298 return read_long_string(LS, l);
299 } 299 }
300 300
301 case '=': 301 case '=':
302 save_and_next(LL); 302 save_and_next(LS);
303 if (LL->current != '=') return '='; 303 if (LS->current != '=') return '=';
304 else { save_and_next(LL); return EQ; } 304 else { save_and_next(LS); return EQ; }
305 305
306 case '<': 306 case '<':
307 save_and_next(LL); 307 save_and_next(LS);
308 if (LL->current != '=') return '<'; 308 if (LS->current != '=') return '<';
309 else { save_and_next(LL); return LE; } 309 else { save_and_next(LS); return LE; }
310 310
311 case '>': 311 case '>':
312 save_and_next(LL); 312 save_and_next(LS);
313 if (LL->current != '=') return '>'; 313 if (LS->current != '=') return '>';
314 else { save_and_next(LL); return GE; } 314 else { save_and_next(LS); return GE; }
315 315
316 case '~': 316 case '~':
317 save_and_next(LL); 317 save_and_next(LS);
318 if (LL->current != '=') return '~'; 318 if (LS->current != '=') return '~';
319 else { save_and_next(LL); return NE; } 319 else { save_and_next(LS); return NE; }
320 320
321 case '"': 321 case '"':
322 case '\'': { 322 case '\'': {
323 int del = LL->current; 323 int del = LS->current;
324 save_and_next(LL); 324 save_and_next(LS);
325 while (LL->current != del) { 325 while (LS->current != del) {
326 switch (LL->current) { 326 switch (LS->current) {
327 case EOZ: 327 case EOZ:
328 case '\n': 328 case '\n':
329 save(LL, 0); 329 save(LS, 0);
330 return WRONGTOKEN; 330 return WRONGTOKEN;
331 case '\\': 331 case '\\':
332 next(LL); /* do not save the '\' */ 332 next(LS); /* do not save the '\' */
333 switch (LL->current) { 333 switch (LS->current) {
334 case 'n': save(LL, '\n'); next(LL); break; 334 case 'n': save(LS, '\n'); next(LS); break;
335 case 't': save(LL, '\t'); next(LL); break; 335 case 't': save(LS, '\t'); next(LS); break;
336 case 'r': save(LL, '\r'); next(LL); break; 336 case 'r': save(LS, '\r'); next(LS); break;
337 case '\n': save(LL, '\n'); inclinenumber(LL); break; 337 case '\n': save(LS, '\n'); inclinenumber(LS); break;
338 default : save_and_next(LL); break; 338 default : save_and_next(LS); break;
339 } 339 }
340 break; 340 break;
341 default: 341 default:
342 save_and_next(LL); 342 save_and_next(LS);
343 } 343 }
344 } 344 }
345 next(LL); /* skip delimiter */ 345 next(LS); /* skip delimiter */
346 save(LL, 0); 346 save(LS, 0);
347 l->pTStr = luaS_new(LL->textbuff.text+1); 347 l->pTStr = luaS_new(LS->textbuff.text+1);
348 LL->textbuff.text[LL->textbuff.tokensize-1] = del; /* restore delimiter */ 348 LS->textbuff.text[LS->textbuff.tokensize-1] = del; /* restore delimiter */
349 return STRING; 349 return STRING;
350 } 350 }
351 351
352 case '.': 352 case '.':
353 save_and_next(LL); 353 save_and_next(LS);
354 if (LL->current == '.') 354 if (LS->current == '.')
355 { 355 {
356 save_and_next(LL); 356 save_and_next(LS);
357 if (LL->current == '.') 357 if (LS->current == '.')
358 { 358 {
359 save_and_next(LL); 359 save_and_next(LS);
360 return DOTS; /* ... */ 360 return DOTS; /* ... */
361 } 361 }
362 else return CONC; /* .. */ 362 else return CONC; /* .. */
363 } 363 }
364 else if (!isdigit(LL->current)) return '.'; 364 else if (!isdigit(LS->current)) return '.';
365 /* LL->current is a digit: goes through to number */ 365 /* LS->current is a digit: goes through to number */
366 a=0.0; 366 a=0.0;
367 goto fraction; 367 goto fraction;
368 368
@@ -370,38 +370,38 @@ int luaY_lex (YYSTYPE *l)
370 case '5': case '6': case '7': case '8': case '9': 370 case '5': case '6': case '7': case '8': case '9':
371 a=0.0; 371 a=0.0;
372 do { 372 do {
373 a=10.0*a+(LL->current-'0'); 373 a=10.0*a+(LS->current-'0');
374 save_and_next(LL); 374 save_and_next(LS);
375 } while (isdigit(LL->current)); 375 } while (isdigit(LS->current));
376 if (LL->current == '.') { 376 if (LS->current == '.') {
377 save_and_next(LL); 377 save_and_next(LS);
378 if (LL->current == '.') { 378 if (LS->current == '.') {
379 save(LL, 0); 379 save(LS, 0);
380 luaY_error( 380 luaY_error(
381 "ambiguous syntax (decimal point x string concatenation)"); 381 "ambiguous syntax (decimal point x string concatenation)");
382 } 382 }
383 } 383 }
384 fraction: 384 fraction:
385 { double da=0.1; 385 { double da=0.1;
386 while (isdigit(LL->current)) 386 while (isdigit(LS->current))
387 { 387 {
388 a+=(LL->current-'0')*da; 388 a+=(LS->current-'0')*da;
389 da/=10.0; 389 da/=10.0;
390 save_and_next(LL); 390 save_and_next(LS);
391 } 391 }
392 if (toupper(LL->current) == 'E') { 392 if (toupper(LS->current) == 'E') {
393 int e=0; 393 int e=0;
394 int neg; 394 int neg;
395 double ea; 395 double ea;
396 save_and_next(LL); 396 save_and_next(LS);
397 neg=(LL->current=='-'); 397 neg=(LS->current=='-');
398 if (LL->current == '+' || LL->current == '-') save_and_next(LL); 398 if (LS->current == '+' || LS->current == '-') save_and_next(LS);
399 if (!isdigit(LL->current)) { 399 if (!isdigit(LS->current)) {
400 save(LL, 0); return WRONGTOKEN; } 400 save(LS, 0); return WRONGTOKEN; }
401 do { 401 do {
402 e=10.0*e+(LL->current-'0'); 402 e=10.0*e+(LS->current-'0');
403 save_and_next(LL); 403 save_and_next(LS);
404 } while (isdigit(LL->current)); 404 } while (isdigit(LS->current));
405 for (ea=neg?0.1:10.0; e>0; e>>=1) 405 for (ea=neg?0.1:10.0; e>0; e>>=1)
406 { 406 {
407 if (e & 1) a*=ea; 407 if (e & 1) a*=ea;
@@ -413,23 +413,23 @@ int luaY_lex (YYSTYPE *l)
413 } 413 }
414 414
415 case EOZ: 415 case EOZ:
416 save(LL, 0); 416 save(LS, 0);
417 if (LL->iflevel > 0) 417 if (LS->iflevel > 0)
418 luaY_error("missing $endif"); 418 luaY_error("missing $endif");
419 return 0; 419 return 0;
420 420
421 default: 421 default:
422 if (LL->current != '_' && !isalpha(LL->current)) { 422 if (LS->current != '_' && !isalpha(LS->current)) {
423 save_and_next(LL); 423 save_and_next(LS);
424 return LL->textbuff.text[0]; 424 return LS->textbuff.text[0];
425 } 425 }
426 else { /* identifier or reserved word */ 426 else { /* identifier or reserved word */
427 TaggedString *ts; 427 TaggedString *ts;
428 do { 428 do {
429 save_and_next(LL); 429 save_and_next(LS);
430 } while (isalnum(LL->current) || LL->current == '_'); 430 } while (isalnum(LS->current) || LS->current == '_');
431 save(LL, 0); 431 save(LS, 0);
432 ts = luaS_new(LL->textbuff.text); 432 ts = luaS_new(LS->textbuff.text);
433 if (ts->head.marked > 255) 433 if (ts->head.marked > 255)
434 return ts->head.marked; /* reserved word */ 434 return ts->head.marked; /* reserved word */
435 l->pTStr = ts; 435 l->pTStr = ts;