2
** $Id: llex.c,v 2.20.1.1 2007/12/27 13:02:25 roberto Exp $
4
** See Copyright Notice in lua.h
28
#define next(ls) (ls->current = zgetc(ls->z))
33
#define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r')
37
const char *const luaX_tokens [] = {
38
"and", "break", "do", "else", "elseif",
39
"end", "false", "for", "function", "if",
40
"in", "local", "nil", "not", "or", "repeat",
41
"return", "then", "true", "until", "while",
42
"..", "...", "==", ">=", "<=", "~=",
43
"<number>", "<name>", "<string>", "<eof>",
48
#define save_and_next(ls) (save(ls, ls->current), next(ls))
51
static void save (LexState *ls, int c) {
52
Mbuffer *b = ls->buff;
53
if (b->n + 1 > b->buffsize) {
55
if (b->buffsize >= MAX_SIZET/2)
56
luaX_lexerror(ls, "lexical element too long", 0);
57
newsize = b->buffsize * 2;
58
luaZ_resizebuffer(ls->L, b, newsize);
60
b->buffer[b->n++] = cast(char, c);
64
void luaX_init (lua_State *L) {
66
for (i=0; i<NUM_RESERVED; i++) {
67
TString *ts = luaS_new(L, luaX_tokens[i]);
68
luaS_fix(ts); /* reserved words are never collected */
69
lua_assert(strlen(luaX_tokens[i])+1 <= TOKEN_LEN);
70
ts->tsv.reserved = cast_byte(i+1); /* reserved word */
78
const char *luaX_token2str (LexState *ls, int token) {
79
if (token < FIRST_RESERVED) {
80
lua_assert(token == cast(unsigned char, token));
81
return (iscntrl(token)) ? luaO_pushfstring(ls->L, "char(%d)", token) :
82
luaO_pushfstring(ls->L, "%c", token);
85
return luaX_tokens[token-FIRST_RESERVED];
89
static const char *txtToken (LexState *ls, int token) {
95
return luaZ_buffer(ls->buff);
97
return luaX_token2str(ls, token);
102
void luaX_lexerror (LexState *ls, const char *msg, int token) {
104
luaO_chunkid(buff, getstr(ls->source), MAXSRC);
105
msg = luaO_pushfstring(ls->L, "%s:%d: %s", buff, ls->linenumber, msg);
107
luaO_pushfstring(ls->L, "%s near " LUA_QS, msg, txtToken(ls, token));
108
luaD_throw(ls->L, LUA_ERRSYNTAX);
112
void luaX_syntaxerror (LexState *ls, const char *msg) {
113
luaX_lexerror(ls, msg, ls->t.token);
117
TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
118
lua_State *L = ls->L;
119
TString *ts = luaS_newlstr(L, str, l);
120
TValue *o = luaH_setstr(L, ls->fs->h, ts); /* entry for `str' */
122
setbvalue(o, 1); /* make sure `str' will not be collected */
127
static void inclinenumber (LexState *ls) {
128
int old = ls->current;
129
lua_assert(currIsNewline(ls));
130
next(ls); /* skip `\n' or `\r' */
131
if (currIsNewline(ls) && ls->current != old)
132
next(ls); /* skip `\n\r' or `\r\n' */
133
if (++ls->linenumber >= MAX_INT)
134
luaX_syntaxerror(ls, "chunk has too many lines");
138
void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source) {
141
ls->lookahead.token = TK_EOS; /* no look-ahead token */
147
luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */
148
next(ls); /* read first char */
154
** =======================================================
156
** =======================================================
161
static int check_next (LexState *ls, const char *set) {
162
if (!strchr(set, ls->current))
169
static void buffreplace (LexState *ls, char from, char to) {
170
size_t n = luaZ_bufflen(ls->buff);
171
char *p = luaZ_buffer(ls->buff);
173
if (p[n] == from) p[n] = to;
177
static void trydecpoint (LexState *ls, SemInfo *seminfo) {
178
/* format error: try to update decimal point separator */
179
struct lconv *cv = localeconv();
180
char old = ls->decpoint;
181
ls->decpoint = (cv ? cv->decimal_point[0] : '.');
182
buffreplace(ls, old, ls->decpoint); /* try updated decimal separator */
183
if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) {
184
/* format error with correct decimal point: no more options */
185
buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */
186
luaX_lexerror(ls, "malformed number", TK_NUMBER);
192
static void read_numeral (LexState *ls, SemInfo *seminfo) {
193
lua_assert(isdigit(ls->current));
196
} while (isdigit(ls->current) || ls->current == '.');
197
if (check_next(ls, "Ee")) /* `E'? */
198
check_next(ls, "+-"); /* optional exponent sign */
199
while (isalnum(ls->current) || ls->current == '_')
202
buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */
203
if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) /* format error? */
204
trydecpoint(ls, seminfo); /* try to update decimal point separator */
208
static int skip_sep (LexState *ls) {
211
lua_assert(s == '[' || s == ']');
213
while (ls->current == '=') {
217
return (ls->current == s) ? count : (-count) - 1;
221
static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) {
223
(void)(cont); /* avoid warnings when `cont' is not used */
224
save_and_next(ls); /* skip 2nd `[' */
225
if (currIsNewline(ls)) /* string starts with a newline? */
226
inclinenumber(ls); /* skip it */
228
switch (ls->current) {
230
luaX_lexerror(ls, (seminfo) ? "unfinished long string" :
231
"unfinished long comment", TK_EOS);
232
break; /* to avoid warnings */
233
#if defined(LUA_COMPAT_LSTR)
235
if (skip_sep(ls) == sep) {
236
save_and_next(ls); /* skip 2nd `[' */
238
#if LUA_COMPAT_LSTR == 1
240
luaX_lexerror(ls, "nesting of [[...]] is deprecated", '[');
247
if (skip_sep(ls) == sep) {
248
save_and_next(ls); /* skip 2nd `]' */
249
#if defined(LUA_COMPAT_LSTR) && LUA_COMPAT_LSTR == 2
251
if (sep == 0 && cont >= 0) break;
261
if (!seminfo) luaZ_resetbuffer(ls->buff); /* avoid wasting space */
265
if (seminfo) save_and_next(ls);
271
seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + (2 + sep),
272
luaZ_bufflen(ls->buff) - 2*(2 + sep));
276
static void read_string (LexState *ls, int del, SemInfo *seminfo) {
278
while (ls->current != del) {
279
switch (ls->current) {
281
luaX_lexerror(ls, "unfinished string", TK_EOS);
282
continue; /* to avoid warnings */
285
luaX_lexerror(ls, "unfinished string", TK_STRING);
286
continue; /* to avoid warnings */
289
next(ls); /* do not save the `\' */
290
switch (ls->current) {
291
case 'a': c = '\a'; break;
292
case 'b': c = '\b'; break;
293
case 'f': c = '\f'; break;
294
case 'n': c = '\n'; break;
295
case 'r': c = '\r'; break;
296
case 't': c = '\t'; break;
297
case 'v': c = '\v'; break;
298
case '\n': /* go through */
299
case '\r': save(ls, '\n'); inclinenumber(ls); continue;
300
case EOZ: continue; /* will raise an error next loop */
302
if (!isdigit(ls->current))
303
save_and_next(ls); /* handles \\, \", \', and \? */
308
c = 10*c + (ls->current-'0');
310
} while (++i<3 && isdigit(ls->current));
312
luaX_lexerror(ls, "escape sequence too large", TK_STRING);
326
save_and_next(ls); /* skip delimiter */
327
seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + 1,
328
luaZ_bufflen(ls->buff) - 2);
332
static int llex (LexState *ls, SemInfo *seminfo) {
333
luaZ_resetbuffer(ls->buff);
335
switch (ls->current) {
343
if (ls->current != '-') return '-';
344
/* else is a comment */
346
if (ls->current == '[') {
347
int sep = skip_sep(ls);
348
luaZ_resetbuffer(ls->buff); /* `skip_sep' may dirty the buffer */
350
read_long_string(ls, NULL, sep); /* long comment */
351
luaZ_resetbuffer(ls->buff);
355
/* else short comment */
356
while (!currIsNewline(ls) && ls->current != EOZ)
361
int sep = skip_sep(ls);
363
read_long_string(ls, seminfo, sep);
366
else if (sep == -1) return '[';
367
else luaX_lexerror(ls, "invalid long string delimiter", TK_STRING);
371
if (ls->current != '=') return '=';
372
else { next(ls); return TK_EQ; }
376
if (ls->current != '=') return '<';
377
else { next(ls); return TK_LE; }
381
if (ls->current != '=') return '>';
382
else { next(ls); return TK_GE; }
386
if (ls->current != '=') return '~';
387
else { next(ls); return TK_NE; }
391
read_string(ls, ls->current, seminfo);
396
if (check_next(ls, ".")) {
397
if (check_next(ls, "."))
398
return TK_DOTS; /* ... */
399
else return TK_CONCAT; /* .. */
401
else if (!isdigit(ls->current)) return '.';
403
read_numeral(ls, seminfo);
411
if (isspace(ls->current)) {
412
lua_assert(!currIsNewline(ls));
416
else if (isdigit(ls->current)) {
417
read_numeral(ls, seminfo);
420
else if (isalpha(ls->current) || ls->current == '_') {
421
/* identifier or reserved word */
425
} while (isalnum(ls->current) || ls->current == '_');
426
ts = luaX_newstring(ls, luaZ_buffer(ls->buff),
427
luaZ_bufflen(ls->buff));
428
if (ts->tsv.reserved > 0) /* reserved word? */
429
return ts->tsv.reserved - 1 + FIRST_RESERVED;
438
return c; /* single-char tokens (+ - / ...) */
446
void luaX_next (LexState *ls) {
447
ls->lastline = ls->linenumber;
448
if (ls->lookahead.token != TK_EOS) { /* is there a look-ahead token? */
449
ls->t = ls->lookahead; /* use this one */
450
ls->lookahead.token = TK_EOS; /* and discharge it */
453
ls->t.token = llex(ls, &ls->t.seminfo); /* read next token */
457
void luaX_lookahead (LexState *ls) {
458
lua_assert(ls->lookahead.token == TK_EOS);
459
ls->lookahead.token = llex(ls, &ls->lookahead.seminfo);