2 ** $Id: llex.c,v 2.20.1.2 2009/11/23 14:58:22 roberto Exp $
4 ** See Copyright Notice in lua.h
29 #define next(ls) (ls->current = zgetc(ls->z))
34 #define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r')
38 const char *const luaX_tokens [] = {
39 "and", "break", "do", "else", "elseif",
40 "end", "false", "for", "function", "if",
41 "in", "local", "nil", "not", "or", "repeat",
42 "return", "then", "true", "until", "while",
43 "..", "...", "==", ">=", "<=", "~=",
44 "<number>", "<name>", "<string>", "<eof>",
53 #define save_and_next(ls) (save(ls, ls->current), next(ls))
56 static void save (LexState *ls, int c) {
57 Mbuffer *b = ls->buff;
58 if (b->n + 1 > b->buffsize) {
60 if (b->buffsize >= MAX_SIZET/2)
61 luaX_lexerror(ls, "lexical element too long", 0);
62 newsize = b->buffsize * 2;
63 luaZ_resizebuffer(ls->L, b, newsize);
65 b->buffer[b->n++] = cast(char, c);
69 void luaX_init (lua_State *L) {
71 for (i=0; i<NUM_RESERVED; i++) {
72 TString *ts = luaS_new(L, luaX_tokens[i]);
73 luaS_fix(ts); /* reserved words are never collected */
74 lua_assert(strlen(luaX_tokens[i])+1 <= TOKEN_LEN);
75 ts->tsv.reserved = cast_byte(i+1); /* reserved word */
83 const char *luaX_token2str (LexState *ls, int token) {
84 if (token < FIRST_RESERVED) {
85 lua_assert(token == cast(unsigned char, token));
86 return (iscntrl(token)) ? luaO_pushfstring(ls->L, "char(%d)", token) :
87 luaO_pushfstring(ls->L, "%c", token);
90 return luaX_tokens[token-FIRST_RESERVED];
94 static const char *txtToken (LexState *ls, int token) {
104 return luaZ_buffer(ls->buff);
106 return luaX_token2str(ls, token);
111 void luaX_lexerror (LexState *ls, const char *msg, int token) {
113 luaO_chunkid(buff, getstr(ls->source), MAXSRC);
114 msg = luaO_pushfstring(ls->L, "%s:%d: %s", buff, ls->linenumber, msg);
116 luaO_pushfstring(ls->L, "%s near " LUA_QS, msg, txtToken(ls, token));
117 luaD_throw(ls->L, LUA_ERRSYNTAX);
121 void luaX_syntaxerror (LexState *ls, const char *msg) {
122 luaX_lexerror(ls, msg, ls->t.token);
126 TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
127 lua_State *L = ls->L;
128 TString *ts = luaS_newlstr(L, str, l);
129 TValue *o = luaH_setstr(L, ls->fs->h, ts); /* entry for `str' */
131 setbvalue(o, 1); /* make sure `str' will not be collected */
138 static void inclinenumber (LexState *ls) {
139 int old = ls->current;
140 lua_assert(currIsNewline(ls));
141 next(ls); /* skip `\n' or `\r' */
142 if (currIsNewline(ls) && ls->current != old)
143 next(ls); /* skip `\n\r' or `\r\n' */
144 if (++ls->linenumber >= MAX_INT)
145 luaX_syntaxerror(ls, "chunk has too many lines");
149 void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source) {
152 ls->lookahead.token = TK_EOS; /* no look-ahead token */
158 luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */
159 next(ls); /* read first char */
165 ** =======================================================
167 ** =======================================================
172 static int check_next (LexState *ls, const char *set) {
173 if (!strchr(set, ls->current))
180 static void buffreplace (LexState *ls, char from, char to) {
181 size_t n = luaZ_bufflen(ls->buff);
182 char *p = luaZ_buffer(ls->buff);
184 if (p[n] == from) p[n] = to;
187 /* TK_NUMBER (/ TK_NUMBER2) */
188 static int trydecpoint (LexState *ls, SemInfo *seminfo) {
189 /* format error: try to update decimal point separator */
190 struct lconv *cv = localeconv();
191 char old = ls->decpoint;
193 ls->decpoint = (cv ? cv->decimal_point[0] : '.');
194 buffreplace(ls, old, ls->decpoint); /* try updated decimal separator */
195 ret= luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r, NULL);
197 /* format error with correct decimal point: no more options */
198 buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */
199 luaX_lexerror(ls, "malformed number", TK_NUMBER);
205 /* TK_NUMBER / TK_INT (/TK_NUMBER2) */
206 static int read_numeral (LexState *ls, SemInfo *seminfo) {
208 lua_assert(isdigit(ls->current));
211 } while (isdigit(ls->current) || ls->current == '.');
212 if (check_next(ls, "Ee")) /* `E'? */
213 check_next(ls, "+-"); /* optional exponent sign */
214 while (isalnum(ls->current) || ls->current == '_')
217 buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */
218 ret= luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r, &seminfo->i );
219 if (!ret) return trydecpoint(ls, seminfo); /* try to update decimal point separator */
224 static int skip_sep (LexState *ls) {
227 lua_assert(s == '[' || s == ']');
229 while (ls->current == '=') {
233 return (ls->current == s) ? count : (-count) - 1;
237 static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) {
239 (void)(cont); /* avoid warnings when `cont' is not used */
240 save_and_next(ls); /* skip 2nd `[' */
241 if (currIsNewline(ls)) /* string starts with a newline? */
242 inclinenumber(ls); /* skip it */
244 switch (ls->current) {
246 luaX_lexerror(ls, (seminfo) ? "unfinished long string" :
247 "unfinished long comment", TK_EOS);
248 break; /* to avoid warnings */
249 #if defined(LUA_COMPAT_LSTR)
251 if (skip_sep(ls) == sep) {
252 save_and_next(ls); /* skip 2nd `[' */
254 #if LUA_COMPAT_LSTR == 1
256 luaX_lexerror(ls, "nesting of [[...]] is deprecated", '[');
263 if (skip_sep(ls) == sep) {
264 save_and_next(ls); /* skip 2nd `]' */
265 #if defined(LUA_COMPAT_LSTR) && LUA_COMPAT_LSTR == 2
267 if (sep == 0 && cont >= 0) break;
277 if (!seminfo) luaZ_resetbuffer(ls->buff); /* avoid wasting space */
281 if (seminfo) save_and_next(ls);
287 seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + (2 + sep),
288 luaZ_bufflen(ls->buff) - 2*(2 + sep));
292 static void read_string (LexState *ls, int del, SemInfo *seminfo) {
294 while (ls->current != del) {
295 switch (ls->current) {
297 luaX_lexerror(ls, "unfinished string", TK_EOS);
298 continue; /* to avoid warnings */
301 luaX_lexerror(ls, "unfinished string", TK_STRING);
302 continue; /* to avoid warnings */
305 next(ls); /* do not save the `\' */
306 switch (ls->current) {
307 case 'a': c = '\a'; break;
308 case 'b': c = '\b'; break;
309 case 'f': c = '\f'; break;
310 case 'n': c = '\n'; break;
311 case 'r': c = '\r'; break;
312 case 't': c = '\t'; break;
313 case 'v': c = '\v'; break;
314 case '\n': /* go through */
315 case '\r': save(ls, '\n'); inclinenumber(ls); continue;
316 case EOZ: continue; /* will raise an error next loop */
318 if (!isdigit(ls->current))
319 save_and_next(ls); /* handles \\, \", \', and \? */
324 c = 10*c + (ls->current-'0');
326 } while (++i<3 && isdigit(ls->current));
328 luaX_lexerror(ls, "escape sequence too large", TK_STRING);
342 save_and_next(ls); /* skip delimiter */
343 seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + 1,
344 luaZ_bufflen(ls->buff) - 2);
349 static int llex (LexState *ls, SemInfo *seminfo) {
350 luaZ_resetbuffer(ls->buff);
352 switch (ls->current) {
360 if (ls->current != '-') return '-';
361 /* else is a comment */
363 if (ls->current == '[') {
364 int sep = skip_sep(ls);
365 luaZ_resetbuffer(ls->buff); /* `skip_sep' may dirty the buffer */
367 read_long_string(ls, NULL, sep); /* long comment */
368 luaZ_resetbuffer(ls->buff);
372 /* else short comment */
373 while (!currIsNewline(ls) && ls->current != EOZ)
378 int sep = skip_sep(ls);
380 read_long_string(ls, seminfo, sep);
383 else if (sep == -1) return '[';
384 else luaX_lexerror(ls, "invalid long string delimiter", TK_STRING);
388 if (ls->current != '=') return '=';
389 else { next(ls); return TK_EQ; }
393 if (ls->current != '=') return '<';
394 else { next(ls); return TK_LE; }
398 if (ls->current != '=') return '>';
399 else { next(ls); return TK_GE; }
403 if (ls->current != '=') return '~';
404 else { next(ls); return TK_NE; }
408 read_string(ls, ls->current, seminfo);
413 if (check_next(ls, ".")) {
414 if (check_next(ls, "."))
415 return TK_DOTS; /* ... */
416 else return TK_CONCAT; /* .. */
418 else if (!isdigit(ls->current)) return '.';
420 return read_numeral(ls, seminfo);
427 if (isspace(ls->current)) {
428 lua_assert(!currIsNewline(ls));
432 else if (isdigit(ls->current)) {
433 return read_numeral(ls, seminfo);
435 else if (isalpha(ls->current) || ls->current == '_') {
436 /* identifier or reserved word */
440 } while (isalnum(ls->current) || ls->current == '_');
441 ts = luaX_newstring(ls, luaZ_buffer(ls->buff),
442 luaZ_bufflen(ls->buff));
443 if (ts->tsv.reserved > 0) /* reserved word? */
444 return ts->tsv.reserved - 1 + FIRST_RESERVED;
453 return c; /* single-char tokens (+ - / ...) */
461 void luaX_next (LexState *ls) {
462 ls->lastline = ls->linenumber;
463 if (ls->lookahead.token != TK_EOS) { /* is there a look-ahead token? */
464 ls->t = ls->lookahead; /* use this one */
465 ls->lookahead.token = TK_EOS; /* and discharge it */
468 ls->t.token = llex(ls, &ls->t.seminfo); /* read next token */
472 void luaX_lookahead (LexState *ls) {
473 lua_assert(ls->lookahead.token == TK_EOS);
474 ls->lookahead.token = llex(ls, &ls->lookahead.seminfo);