aboutsummaryrefslogtreecommitdiff
path: root/llex.c
diff options
context:
space:
mode:
Diffstat (limited to 'llex.c')
-rw-r--r--llex.c201
1 files changed, 100 insertions, 101 deletions
diff --git a/llex.c b/llex.c
index 670da132..6fb02f17 100644
--- a/llex.c
+++ b/llex.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: llex.c,v 1.91 2001/08/31 19:46:07 roberto Exp $ 2** $Id: llex.c,v 1.92 2001/11/16 16:29:10 roberto Exp $
3** Lexical Analyzer 3** Lexical Analyzer
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -9,7 +9,6 @@
9#include <stdio.h> 9#include <stdio.h>
10#include <string.h> 10#include <string.h>
11 11
12#define LUA_PRIVATE
13#include "lua.h" 12#include "lua.h"
14 13
15#include "llex.h" 14#include "llex.h"
@@ -26,13 +25,13 @@
26 25
27 26
28/* ORDER RESERVED */ 27/* ORDER RESERVED */
29static const l_char *const token2string [] = { 28static const char *const token2string [] = {
30 l_s("and"), l_s("break"), l_s("do"), l_s("else"), l_s("elseif"), 29 "and", "break", "do", "else", "elseif",
31 l_s("end"), l_s("for"), l_s("function"), l_s("global"), l_s("if"), 30 "end", "for", "function", "global", "if",
32 l_s("in"), l_s("local"), l_s("nil"), l_s("not"), l_s("or"), l_s("repeat"), 31 "in", "local", "nil", "not", "or", "repeat",
33 l_s("return"), l_s("then"), l_s("until"), l_s("while"), l_s(""), 32 "return", "then", "until", "while", "",
34 l_s(".."), l_s("..."), l_s("=="), l_s(">="), l_s("<="), l_s("~="), 33 "..", "...", "==", ">=", "<=", "~=",
35 l_s(""), l_s(""), l_s("<eof>") 34 "", "", "<eof>"
36}; 35};
37 36
38 37
@@ -49,62 +48,62 @@ void luaX_init (lua_State *L) {
49#define MAXSRC 80 48#define MAXSRC 80
50 49
51 50
52void luaX_checklimit (LexState *ls, int val, int limit, const l_char *msg) { 51void luaX_checklimit (LexState *ls, int val, int limit, const char *msg) {
53 if (val > limit) { 52 if (val > limit) {
54 l_char buff[90]; 53 char buff[90];
55 sprintf(buff, l_s("too many %.40s (limit=%d)"), msg, limit); 54 sprintf(buff, "too many %.40s (limit=%d)", msg, limit);
56 luaX_error(ls, buff, ls->t.token); 55 luaX_error(ls, buff, ls->t.token);
57 } 56 }
58} 57}
59 58
60 59
61static void luaX_syntaxerror (LexState *ls, const l_char *s, 60static void luaX_syntaxerror (LexState *ls, const char *s,
62 const l_char *token) { 61 const char *token) {
63 l_char buff[MAXSRC]; 62 char buff[MAXSRC];
64 luaO_chunkid(buff, getstr(ls->source), MAXSRC); 63 luaO_chunkid(buff, getstr(ls->source), MAXSRC);
65 luaO_verror(ls->L, 64 luaO_verror(ls->L,
66 l_s("%.99s;\n last token read: `%.30s' at line %d in %.80s"), 65 "%.99s;\n last token read: `%.30s' at line %d in %.80s",
67 s, token, ls->linenumber, buff); 66 s, token, ls->linenumber, buff);
68} 67}
69 68
70 69
71void luaX_token2str (int token, l_char *s) { 70void luaX_token2str (int token, char *s) {
72 if (token < FIRST_RESERVED) { 71 if (token < FIRST_RESERVED) {
73 lua_assert(token == (l_char)token); 72 lua_assert(token == (char)token);
74 s[0] = (l_char)token; 73 s[0] = (char)token;
75 s[1] = l_c('\0'); 74 s[1] = '\0';
76 } 75 }
77 else 76 else
78 strcpy(s, token2string[token-FIRST_RESERVED]); 77 strcpy(s, token2string[token-FIRST_RESERVED]);
79} 78}
80 79
81 80
82static l_char *token2str_all (LexState *ls, int token, l_char *s) { 81static char *token2str_all (LexState *ls, int token, char *s) {
83 luaX_token2str(token, s); 82 luaX_token2str(token, s);
84 if (s[0] == l_c('\0')) 83 if (s[0] == '\0')
85 return cast(l_char *, G(ls->L)->Mbuffer); 84 return cast(char *, G(ls->L)->Mbuffer);
86 else 85 else
87 return s; 86 return s;
88} 87}
89 88
90 89
91void luaX_error (LexState *ls, const l_char *s, int token) { 90void luaX_error (LexState *ls, const char *s, int token) {
92 l_char buff[TOKEN_LEN]; 91 char buff[TOKEN_LEN];
93 luaX_syntaxerror(ls, s, token2str_all(ls, token, buff)); 92 luaX_syntaxerror(ls, s, token2str_all(ls, token, buff));
94} 93}
95 94
96 95
97static void luaX_invalidchar (LexState *ls, int c) { 96static void luaX_invalidchar (LexState *ls, int c) {
98 l_char buff[8]; 97 char buff[8];
99 sprintf(buff, l_s("0x%02X"), uchar(c)); 98 sprintf(buff, "0x%02X", (unsigned char)c);
100 luaX_syntaxerror(ls, l_s("invalid control char"), buff); 99 luaX_syntaxerror(ls, "invalid control char", buff);
101} 100}
102 101
103 102
104static void inclinenumber (LexState *LS) { 103static void inclinenumber (LexState *LS) {
105 next(LS); /* skip `\n' */ 104 next(LS); /* skip `\n' */
106 ++LS->linenumber; 105 ++LS->linenumber;
107 luaX_checklimit(LS, LS->linenumber, MAX_INT, l_s("lines in a chunk")); 106 luaX_checklimit(LS, LS->linenumber, MAX_INT, "lines in a chunk");
108} 107}
109 108
110 109
@@ -117,10 +116,10 @@ void luaX_setinput (lua_State *L, LexState *LS, ZIO *z, TString *source) {
117 LS->lastline = 1; 116 LS->lastline = 1;
118 LS->source = source; 117 LS->source = source;
119 next(LS); /* read first char */ 118 next(LS); /* read first char */
120 if (LS->current == l_c('#')) { 119 if (LS->current == '#') {
121 do { /* skip first line */ 120 do { /* skip first line */
122 next(LS); 121 next(LS);
123 } while (LS->current != l_c('\n') && LS->current != EOZ); 122 } while (LS->current != '\n' && LS->current != EOZ);
124 } 123 }
125} 124}
126 125
@@ -137,10 +136,10 @@ void luaX_setinput (lua_State *L, LexState *LS, ZIO *z, TString *source) {
137 136
138#define EXTRABUFF 128 137#define EXTRABUFF 128
139#define checkbuffer(L, n, len) \ 138#define checkbuffer(L, n, len) \
140 if (((len)+(n))*sizeof(l_char) > G(L)->Mbuffsize) \ 139 if (((len)+(n))*sizeof(char) > G(L)->Mbuffsize) \
141 luaO_openspace(L, (len)+(n)+EXTRABUFF, l_char) 140 luaO_openspace(L, (len)+(n)+EXTRABUFF, char)
142 141
143#define save(L, c, l) (cast(l_char *, G(L)->Mbuffer)[l++] = (l_char)c) 142#define save(L, c, l) (cast(char *, G(L)->Mbuffer)[l++] = (char)c)
144#define save_and_next(L, LS, l) (save(L, LS->current, l), next(LS)) 143#define save_and_next(L, LS, l) (save(L, LS->current, l), next(LS))
145 144
146 145
@@ -151,8 +150,8 @@ static size_t readname (LexState *LS) {
151 do { 150 do {
152 checkbuffer(L, 10, l); 151 checkbuffer(L, 10, l);
153 save_and_next(L, LS, l); 152 save_and_next(L, LS, l);
154 } while (isalnum(LS->current) || LS->current == l_c('_')); 153 } while (isalnum(LS->current) || LS->current == '_');
155 save(L, l_c('\0'), l); 154 save(L, '\0', l);
156 return l-1; 155 return l-1;
157} 156}
158 157
@@ -162,18 +161,18 @@ static void read_number (LexState *LS, int comma, SemInfo *seminfo) {
162 lua_State *L = LS->L; 161 lua_State *L = LS->L;
163 size_t l = 0; 162 size_t l = 0;
164 checkbuffer(L, 10, l); 163 checkbuffer(L, 10, l);
165 if (comma) save(L, l_c('.'), l); 164 if (comma) save(L, '.', l);
166 while (isdigit(LS->current)) { 165 while (isdigit(LS->current)) {
167 checkbuffer(L, 10, l); 166 checkbuffer(L, 10, l);
168 save_and_next(L, LS, l); 167 save_and_next(L, LS, l);
169 } 168 }
170 if (LS->current == l_c('.')) { 169 if (LS->current == '.') {
171 save_and_next(L, LS, l); 170 save_and_next(L, LS, l);
172 if (LS->current == l_c('.')) { 171 if (LS->current == '.') {
173 save_and_next(L, LS, l); 172 save_and_next(L, LS, l);
174 save(L, l_c('\0'), l); 173 save(L, '\0', l);
175 luaX_error(LS, 174 luaX_error(LS,
176 l_s("ambiguous syntax (decimal point x string concatenation)"), 175 "ambiguous syntax (decimal point x string concatenation)",
177 TK_NUMBER); 176 TK_NUMBER);
178 } 177 }
179 } 178 }
@@ -181,18 +180,18 @@ static void read_number (LexState *LS, int comma, SemInfo *seminfo) {
181 checkbuffer(L, 10, l); 180 checkbuffer(L, 10, l);
182 save_and_next(L, LS, l); 181 save_and_next(L, LS, l);
183 } 182 }
184 if (LS->current == l_c('e') || LS->current == l_c('E')) { 183 if (LS->current == 'e' || LS->current == 'E') {
185 save_and_next(L, LS, l); /* read `E' */ 184 save_and_next(L, LS, l); /* read `E' */
186 if (LS->current == l_c('+') || LS->current == l_c('-')) 185 if (LS->current == '+' || LS->current == '-')
187 save_and_next(L, LS, l); /* optional exponent sign */ 186 save_and_next(L, LS, l); /* optional exponent sign */
188 while (isdigit(LS->current)) { 187 while (isdigit(LS->current)) {
189 checkbuffer(L, 10, l); 188 checkbuffer(L, 10, l);
190 save_and_next(L, LS, l); 189 save_and_next(L, LS, l);
191 } 190 }
192 } 191 }
193 save(L, l_c('\0'), l); 192 save(L, '\0', l);
194 if (!luaO_str2d(cast(l_char *, G(L)->Mbuffer), &seminfo->r)) 193 if (!luaO_str2d(cast(char *, G(L)->Mbuffer), &seminfo->r))
195 luaX_error(LS, l_s("malformed number"), TK_NUMBER); 194 luaX_error(LS, "malformed number", TK_NUMBER);
196} 195}
197 196
198 197
@@ -201,34 +200,34 @@ static void read_long_string (LexState *LS, SemInfo *seminfo) {
201 int cont = 0; 200 int cont = 0;
202 size_t l = 0; 201 size_t l = 0;
203 checkbuffer(L, 10, l); 202 checkbuffer(L, 10, l);
204 save(L, l_c('['), l); /* save first `[' */ 203 save(L, '[', l); /* save first `[' */
205 save_and_next(L, LS, l); /* pass the second `[' */ 204 save_and_next(L, LS, l); /* pass the second `[' */
206 if (LS->current == l_c('\n')) /* string starts with a newline? */ 205 if (LS->current == '\n') /* string starts with a newline? */
207 inclinenumber(LS); /* skip it */ 206 inclinenumber(LS); /* skip it */
208 for (;;) { 207 for (;;) {
209 checkbuffer(L, 10, l); 208 checkbuffer(L, 10, l);
210 switch (LS->current) { 209 switch (LS->current) {
211 case EOZ: 210 case EOZ:
212 save(L, l_c('\0'), l); 211 save(L, '\0', l);
213 luaX_error(LS, l_s("unfinished long string"), TK_STRING); 212 luaX_error(LS, "unfinished long string", TK_STRING);
214 break; /* to avoid warnings */ 213 break; /* to avoid warnings */
215 case l_c('['): 214 case '[':
216 save_and_next(L, LS, l); 215 save_and_next(L, LS, l);
217 if (LS->current == l_c('[')) { 216 if (LS->current == '[') {
218 cont++; 217 cont++;
219 save_and_next(L, LS, l); 218 save_and_next(L, LS, l);
220 } 219 }
221 continue; 220 continue;
222 case l_c(']'): 221 case ']':
223 save_and_next(L, LS, l); 222 save_and_next(L, LS, l);
224 if (LS->current == l_c(']')) { 223 if (LS->current == ']') {
225 if (cont == 0) goto endloop; 224 if (cont == 0) goto endloop;
226 cont--; 225 cont--;
227 save_and_next(L, LS, l); 226 save_and_next(L, LS, l);
228 } 227 }
229 continue; 228 continue;
230 case l_c('\n'): 229 case '\n':
231 save(L, l_c('\n'), l); 230 save(L, '\n', l);
232 inclinenumber(LS); 231 inclinenumber(LS);
233 continue; 232 continue;
234 default: 233 default:
@@ -236,8 +235,8 @@ static void read_long_string (LexState *LS, SemInfo *seminfo) {
236 } 235 }
237 } endloop: 236 } endloop:
238 save_and_next(L, LS, l); /* skip the second `]' */ 237 save_and_next(L, LS, l); /* skip the second `]' */
239 save(L, l_c('\0'), l); 238 save(L, '\0', l);
240 seminfo->ts = luaS_newlstr(L, cast(l_char *, G(L)->Mbuffer)+2, l-5); 239 seminfo->ts = luaS_newlstr(L, cast(char *, G(L)->Mbuffer)+2, l-5);
241} 240}
242 241
243 242
@@ -249,21 +248,21 @@ static void read_string (LexState *LS, int del, SemInfo *seminfo) {
249 while (LS->current != del) { 248 while (LS->current != del) {
250 checkbuffer(L, 10, l); 249 checkbuffer(L, 10, l);
251 switch (LS->current) { 250 switch (LS->current) {
252 case EOZ: case l_c('\n'): 251 case EOZ: case '\n':
253 save(L, l_c('\0'), l); 252 save(L, '\0', l);
254 luaX_error(LS, l_s("unfinished string"), TK_STRING); 253 luaX_error(LS, "unfinished string", TK_STRING);
255 break; /* to avoid warnings */ 254 break; /* to avoid warnings */
256 case l_c('\\'): 255 case '\\':
257 next(LS); /* do not save the `\' */ 256 next(LS); /* do not save the `\' */
258 switch (LS->current) { 257 switch (LS->current) {
259 case l_c('a'): save(L, l_c('\a'), l); next(LS); break; 258 case 'a': save(L, '\a', l); next(LS); break;
260 case l_c('b'): save(L, l_c('\b'), l); next(LS); break; 259 case 'b': save(L, '\b', l); next(LS); break;
261 case l_c('f'): save(L, l_c('\f'), l); next(LS); break; 260 case 'f': save(L, '\f', l); next(LS); break;
262 case l_c('n'): save(L, l_c('\n'), l); next(LS); break; 261 case 'n': save(L, '\n', l); next(LS); break;
263 case l_c('r'): save(L, l_c('\r'), l); next(LS); break; 262 case 'r': save(L, '\r', l); next(LS); break;
264 case l_c('t'): save(L, l_c('\t'), l); next(LS); break; 263 case 't': save(L, '\t', l); next(LS); break;
265 case l_c('v'): save(L, l_c('\v'), l); next(LS); break; 264 case 'v': save(L, '\v', l); next(LS); break;
266 case l_c('\n'): save(L, l_c('\n'), l); inclinenumber(LS); break; 265 case '\n': save(L, '\n', l); inclinenumber(LS); break;
267 default: { 266 default: {
268 if (!isdigit(LS->current)) 267 if (!isdigit(LS->current))
269 save_and_next(L, LS, l); /* handles \\, \", \', and \? */ 268 save_and_next(L, LS, l); /* handles \\, \", \', and \? */
@@ -271,12 +270,12 @@ static void read_string (LexState *LS, int del, SemInfo *seminfo) {
271 int c = 0; 270 int c = 0;
272 int i = 0; 271 int i = 0;
273 do { 272 do {
274 c = 10*c + (LS->current-l_c('0')); 273 c = 10*c + (LS->current-'0');
275 next(LS); 274 next(LS);
276 } while (++i<3 && isdigit(LS->current)); 275 } while (++i<3 && isdigit(LS->current));
277 if (c > UCHAR_MAX) { 276 if (c > UCHAR_MAX) {
278 save(L, l_c('\0'), l); 277 save(L, '\0', l);
279 luaX_error(LS, l_s("escape sequence too large"), TK_STRING); 278 luaX_error(LS, "escape sequence too large", TK_STRING);
280 } 279 }
281 save(L, c, l); 280 save(L, c, l);
282 } 281 }
@@ -288,8 +287,8 @@ static void read_string (LexState *LS, int del, SemInfo *seminfo) {
288 } 287 }
289 } 288 }
290 save_and_next(L, LS, l); /* skip delimiter */ 289 save_and_next(L, LS, l); /* skip delimiter */
291 save(L, l_c('\0'), l); 290 save(L, '\0', l);
292 seminfo->ts = luaS_newlstr(L, cast(l_char *, G(L)->Mbuffer)+1, l-3); 291 seminfo->ts = luaS_newlstr(L, cast(char *, G(L)->Mbuffer)+1, l-3);
293} 292}
294 293
295 294
@@ -297,70 +296,70 @@ int luaX_lex (LexState *LS, SemInfo *seminfo) {
297 for (;;) { 296 for (;;) {
298 switch (LS->current) { 297 switch (LS->current) {
299 298
300 case l_c(' '): case l_c('\t'): case l_c('\r'): /* `\r' to avoid problems with DOS */ 299 case ' ': case '\t': case '\r': /* `\r' to avoid problems with DOS */
301 next(LS); 300 next(LS);
302 continue; 301 continue;
303 302
304 case l_c('\n'): 303 case '\n':
305 inclinenumber(LS); 304 inclinenumber(LS);
306 continue; 305 continue;
307 306
308 case l_c('$'): 307 case '$':
309 luaX_error(LS, 308 luaX_error(LS,
310 l_s("unexpected `$' (pragmas are no longer supported)"), 309 "unexpected `$' (pragmas are no longer supported)",
311 LS->current); 310 LS->current);
312 break; 311 break;
313 312
314 case l_c('-'): 313 case '-':
315 next(LS); 314 next(LS);
316 if (LS->current != l_c('-')) return l_c('-'); 315 if (LS->current != '-') return '-';
317 do { next(LS); } while (LS->current != l_c('\n') && LS->current != EOZ); 316 do { next(LS); } while (LS->current != '\n' && LS->current != EOZ);
318 continue; 317 continue;
319 318
320 case l_c('['): 319 case '[':
321 next(LS); 320 next(LS);
322 if (LS->current != l_c('[')) return l_c('['); 321 if (LS->current != '[') return '[';
323 else { 322 else {
324 read_long_string(LS, seminfo); 323 read_long_string(LS, seminfo);
325 return TK_STRING; 324 return TK_STRING;
326 } 325 }
327 326
328 case l_c('='): 327 case '=':
329 next(LS); 328 next(LS);
330 if (LS->current != l_c('=')) return l_c('='); 329 if (LS->current != '=') return '=';
331 else { next(LS); return TK_EQ; } 330 else { next(LS); return TK_EQ; }
332 331
333 case l_c('<'): 332 case '<':
334 next(LS); 333 next(LS);
335 if (LS->current != l_c('=')) return l_c('<'); 334 if (LS->current != '=') return '<';
336 else { next(LS); return TK_LE; } 335 else { next(LS); return TK_LE; }
337 336
338 case l_c('>'): 337 case '>':
339 next(LS); 338 next(LS);
340 if (LS->current != l_c('=')) return l_c('>'); 339 if (LS->current != '=') return '>';
341 else { next(LS); return TK_GE; } 340 else { next(LS); return TK_GE; }
342 341
343 case l_c('~'): 342 case '~':
344 next(LS); 343 next(LS);
345 if (LS->current != l_c('=')) return l_c('~'); 344 if (LS->current != '=') return '~';
346 else { next(LS); return TK_NE; } 345 else { next(LS); return TK_NE; }
347 346
348 case l_c('"'): 347 case '"':
349 case l_c('\''): 348 case '\'':
350 read_string(LS, LS->current, seminfo); 349 read_string(LS, LS->current, seminfo);
351 return TK_STRING; 350 return TK_STRING;
352 351
353 case l_c('.'): 352 case '.':
354 next(LS); 353 next(LS);
355 if (LS->current == l_c('.')) { 354 if (LS->current == '.') {
356 next(LS); 355 next(LS);
357 if (LS->current == l_c('.')) { 356 if (LS->current == '.') {
358 next(LS); 357 next(LS);
359 return TK_DOTS; /* ... */ 358 return TK_DOTS; /* ... */
360 } 359 }
361 else return TK_CONCAT; /* .. */ 360 else return TK_CONCAT; /* .. */
362 } 361 }
363 else if (!isdigit(LS->current)) return l_c('.'); 362 else if (!isdigit(LS->current)) return '.';
364 else { 363 else {
365 read_number(LS, 1, seminfo); 364 read_number(LS, 1, seminfo);
366 return TK_NUMBER; 365 return TK_NUMBER;
@@ -374,17 +373,17 @@ int luaX_lex (LexState *LS, SemInfo *seminfo) {
374 read_number(LS, 0, seminfo); 373 read_number(LS, 0, seminfo);
375 return TK_NUMBER; 374 return TK_NUMBER;
376 } 375 }
377 else if (isalpha(LS->current) || LS->current == l_c('_')) { 376 else if (isalpha(LS->current) || LS->current == '_') {
378 /* identifier or reserved word */ 377 /* identifier or reserved word */
379 size_t l = readname(LS); 378 size_t l = readname(LS);
380 TString *ts = luaS_newlstr(LS->L, cast(l_char *, G(LS->L)->Mbuffer), l); 379 TString *ts = luaS_newlstr(LS->L, cast(char *, G(LS->L)->Mbuffer), l);
381 if (ts->tsv.marked >= RESERVEDMARK) /* reserved word? */ 380 if (ts->tsv.marked >= RESERVEDMARK) /* reserved word? */
382 return ts->tsv.marked-RESERVEDMARK+FIRST_RESERVED; 381 return ts->tsv.marked-RESERVEDMARK+FIRST_RESERVED;
383 seminfo->ts = ts; 382 seminfo->ts = ts;
384 return TK_NAME; 383 return TK_NAME;
385 } 384 }
386 else { 385 else {
387 l_charint c = LS->current; 386 int c = LS->current;
388 if (iscntrl(c)) 387 if (iscntrl(c))
389 luaX_invalidchar(LS, c); 388 luaX_invalidchar(LS, c);
390 next(LS); 389 next(LS);