2 ** $Id: llex.c,v 2.63.1.3 2015/02/09 17:56:34 roberto Exp $
4 ** See Copyright Notice in lua.h
7 #include <sys/zfs_context.h>
26 #define next(ls) (ls->current = zgetc(ls->z))
30 #define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r')
34 static const char *const luaX_tokens [] = {
35 "and", "break", "do", "else", "elseif",
36 "end", "false", "for", "function", "goto", "if",
37 "in", "local", "nil", "not", "or", "repeat",
38 "return", "then", "true", "until", "while",
39 "..", "...", "==", ">=", "<=", "~=", "::", "<eof>",
40 "<number>", "<name>", "<string>"
44 #define save_and_next(ls) (save(ls, ls->current), next(ls))
47 static l_noret lexerror (LexState *ls, const char *msg, int token);
50 static void save (LexState *ls, int c) {
51 Mbuffer *b = ls->buff;
52 if (luaZ_bufflen(b) + 1 > luaZ_sizebuffer(b)) {
54 if (luaZ_sizebuffer(b) >= MAX_SIZET/2)
55 lexerror(ls, "lexical element too long", 0);
56 newsize = luaZ_sizebuffer(b) * 2;
57 luaZ_resizebuffer(ls->L, b, newsize);
59 b->buffer[luaZ_bufflen(b)++] = cast(char, c);
63 void luaX_init (lua_State *L) {
65 for (i=0; i<NUM_RESERVED; i++) {
66 TString *ts = luaS_new(L, luaX_tokens[i]);
67 luaS_fix(ts); /* reserved words are never collected */
68 ts->tsv.extra = cast_byte(i+1); /* reserved word */
73 const char *luaX_token2str (LexState *ls, int token) {
74 if (token < FIRST_RESERVED) { /* single-byte symbols? */
75 lua_assert(token == cast(unsigned char, token));
76 return (lisprint(token)) ? luaO_pushfstring(ls->L, LUA_QL("%c"), token) :
77 luaO_pushfstring(ls->L, "char(%d)", token);
80 const char *s = luaX_tokens[token - FIRST_RESERVED];
81 if (token < TK_EOS) /* fixed format (symbols and reserved words)? */
82 return luaO_pushfstring(ls->L, LUA_QS, s);
83 else /* names, strings, and numerals */
89 static const char *txtToken (LexState *ls, int token) {
95 return luaO_pushfstring(ls->L, LUA_QS, luaZ_buffer(ls->buff));
97 return luaX_token2str(ls, token);
102 static l_noret lexerror (LexState *ls, const char *msg, int token) {
103 char buff[LUA_IDSIZE];
104 luaO_chunkid(buff, getstr(ls->source), LUA_IDSIZE);
105 msg = luaO_pushfstring(ls->L, "%s:%d: %s", buff, ls->linenumber, msg);
107 luaO_pushfstring(ls->L, "%s near %s", msg, txtToken(ls, token));
108 luaD_throw(ls->L, LUA_ERRSYNTAX);
112 l_noret luaX_syntaxerror (LexState *ls, const char *msg) {
113 lexerror(ls, msg, ls->t.token);
118 ** creates a new string and anchors it in function's table so that
119 ** it will not be collected until the end of the function's compilation
120 ** (by that time it should be anchored in function's prototype)
122 TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
123 lua_State *L = ls->L;
124 TValue *o; /* entry for `str' */
125 TString *ts = luaS_newlstr(L, str, l); /* create new string */
126 setsvalue2s(L, L->top++, ts); /* temporarily anchor it in stack */
127 o = luaH_set(L, ls->fs->h, L->top - 1);
128 if (ttisnil(o)) { /* not in use yet? (see 'addK') */
129 /* boolean value does not need GC barrier;
130 table has no metatable, so it does not need to invalidate cache */
131 setbvalue(o, 1); /* t[string] = true */
134 else { /* string already present */
135 ts = rawtsvalue(keyfromval(o)); /* re-use value previously stored */
137 L->top--; /* remove string from stack */
143 ** increment line number and skips newline sequence (any of
144 ** \n, \r, \n\r, or \r\n)
146 static void inclinenumber (LexState *ls) {
147 int old = ls->current;
148 lua_assert(currIsNewline(ls));
149 next(ls); /* skip `\n' or `\r' */
150 if (currIsNewline(ls) && ls->current != old)
151 next(ls); /* skip `\n\r' or `\r\n' */
152 if (++ls->linenumber >= MAX_INT)
153 lexerror(ls, "chunk has too many lines", 0);
157 void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source,
161 ls->current = firstchar;
162 ls->lookahead.token = TK_EOS; /* no look-ahead token */
168 ls->envn = luaS_new(L, LUA_ENV); /* create env name */
169 luaS_fix(ls->envn); /* never collect this name */
170 luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */
176 ** =======================================================
178 ** =======================================================
183 static int check_next (LexState *ls, const char *set) {
184 if (ls->current == '\0' || !strchr(set, ls->current))
192 ** change all characters 'from' in buffer to 'to'
194 static void buffreplace (LexState *ls, char from, char to) {
195 size_t n = luaZ_bufflen(ls->buff);
196 char *p = luaZ_buffer(ls->buff);
198 if (p[n] == from) p[n] = to;
202 #if !defined(getlocaledecpoint)
203 #define getlocaledecpoint() (localeconv()->decimal_point[0])
207 #define buff2d(b,e) luaO_str2d(luaZ_buffer(b), luaZ_bufflen(b) - 1, e)
210 ** in case of format error, try to change decimal point separator to
211 ** the one defined in the current locale and check again
213 static void trydecpoint (LexState *ls, SemInfo *seminfo) {
214 char old = ls->decpoint;
215 ls->decpoint = getlocaledecpoint();
216 buffreplace(ls, old, ls->decpoint); /* try new decimal separator */
217 if (!buff2d(ls->buff, &seminfo->r)) {
218 /* format error with correct decimal point: no more options */
219 buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */
220 lexerror(ls, "malformed number", TK_NUMBER);
227 ** this function is quite liberal in what it accepts, as 'luaO_str2d'
228 ** will reject ill-formed numerals.
230 static void read_numeral (LexState *ls, SemInfo *seminfo) {
231 const char *expo = "Ee";
232 int first = ls->current;
233 lua_assert(lisdigit(ls->current));
235 if (first == '0' && check_next(ls, "Xx")) /* hexadecimal? */
238 if (check_next(ls, expo)) /* exponent part? */
239 check_next(ls, "+-"); /* optional exponent sign */
240 if (lisxdigit(ls->current) || ls->current == '.')
245 buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */
246 if (!buff2d(ls->buff, &seminfo->r)) /* format error? */
247 trydecpoint(ls, seminfo); /* try to update decimal point separator */
252 ** skip a sequence '[=*[' or ']=*]' and return its number of '='s or
253 ** -1 if sequence is malformed
255 static int skip_sep (LexState *ls) {
258 lua_assert(s == '[' || s == ']');
260 while (ls->current == '=') {
264 return (ls->current == s) ? count : (-count) - 1;
268 static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) {
269 save_and_next(ls); /* skip 2nd `[' */
270 if (currIsNewline(ls)) /* string starts with a newline? */
271 inclinenumber(ls); /* skip it */
273 switch (ls->current) {
275 lexerror(ls, (seminfo) ? "unfinished long string" :
276 "unfinished long comment", TK_EOS);
277 break; /* to avoid warnings */
279 if (skip_sep(ls) == sep) {
280 save_and_next(ls); /* skip 2nd `]' */
285 case '\n': case '\r': {
288 if (!seminfo) luaZ_resetbuffer(ls->buff); /* avoid wasting space */
292 if (seminfo) save_and_next(ls);
298 seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + (2 + sep),
299 luaZ_bufflen(ls->buff) - 2*(2 + sep));
303 static void escerror (LexState *ls, int *c, int n, const char *msg) {
305 luaZ_resetbuffer(ls->buff); /* prepare error message */
307 for (i = 0; i < n && c[i] != EOZ; i++)
309 lexerror(ls, msg, TK_STRING);
313 static int readhexaesc (LexState *ls) {
314 int c[3], i; /* keep input for error message */
315 int r = 0; /* result accumulator */
316 c[0] = 'x'; /* for error message */
317 for (i = 1; i < 3; i++) { /* read two hexadecimal digits */
319 if (!lisxdigit(c[i]))
320 escerror(ls, c, i + 1, "hexadecimal digit expected");
321 r = (r << 4) + luaO_hexavalue(c[i]);
327 static int readdecesc (LexState *ls) {
329 int r = 0; /* result accumulator */
330 for (i = 0; i < 3 && lisdigit(ls->current); i++) { /* read up to 3 digits */
332 r = 10*r + c[i] - '0';
336 escerror(ls, c, i, "decimal escape too large");
341 static void read_string (LexState *ls, int del, SemInfo *seminfo) {
342 save_and_next(ls); /* keep delimiter (for error messages) */
343 while (ls->current != del) {
344 switch (ls->current) {
346 lexerror(ls, "unfinished string", TK_EOS);
347 break; /* to avoid warnings */
350 lexerror(ls, "unfinished string", TK_STRING);
351 break; /* to avoid warnings */
352 case '\\': { /* escape sequences */
353 int c; /* final character to be saved */
354 next(ls); /* do not save the `\' */
355 switch (ls->current) {
356 case 'a': c = '\a'; goto read_save;
357 case 'b': c = '\b'; goto read_save;
358 case 'f': c = '\f'; goto read_save;
359 case 'n': c = '\n'; goto read_save;
360 case 'r': c = '\r'; goto read_save;
361 case 't': c = '\t'; goto read_save;
362 case 'v': c = '\v'; goto read_save;
363 case 'x': c = readhexaesc(ls); goto read_save;
364 case '\n': case '\r':
365 inclinenumber(ls); c = '\n'; goto only_save;
366 case '\\': case '\"': case '\'':
367 c = ls->current; goto read_save;
368 case EOZ: goto no_save; /* will raise an error next loop */
369 case 'z': { /* zap following span of spaces */
370 next(ls); /* skip the 'z' */
371 while (lisspace(ls->current)) {
372 if (currIsNewline(ls)) inclinenumber(ls);
378 if (!lisdigit(ls->current))
379 escerror(ls, &ls->current, 1, "invalid escape sequence");
380 /* digital escape \ddd */
385 read_save: next(ls); /* read next character */
386 only_save: save(ls, c); /* save 'c' */
393 save_and_next(ls); /* skip delimiter */
394 seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + 1,
395 luaZ_bufflen(ls->buff) - 2);
399 static int llex (LexState *ls, SemInfo *seminfo) {
400 luaZ_resetbuffer(ls->buff);
402 switch (ls->current) {
403 case '\n': case '\r': { /* line breaks */
407 case ' ': case '\f': case '\t': case '\v': { /* spaces */
411 case '-': { /* '-' or '--' (comment) */
413 if (ls->current != '-') return '-';
414 /* else is a comment */
416 if (ls->current == '[') { /* long comment? */
417 int sep = skip_sep(ls);
418 luaZ_resetbuffer(ls->buff); /* `skip_sep' may dirty the buffer */
420 read_long_string(ls, NULL, sep); /* skip long comment */
421 luaZ_resetbuffer(ls->buff); /* previous call may dirty the buff. */
425 /* else short comment */
426 while (!currIsNewline(ls) && ls->current != EOZ)
427 next(ls); /* skip until end of line (or end of file) */
430 case '[': { /* long string or simply '[' */
431 int sep = skip_sep(ls);
433 read_long_string(ls, seminfo, sep);
436 else if (sep == -1) return '[';
437 else lexerror(ls, "invalid long string delimiter", TK_STRING);
441 if (ls->current != '=') return '=';
442 else { next(ls); return TK_EQ; }
446 if (ls->current != '=') return '<';
447 else { next(ls); return TK_LE; }
451 if (ls->current != '=') return '>';
452 else { next(ls); return TK_GE; }
456 if (ls->current != '=') return '~';
457 else { next(ls); return TK_NE; }
461 if (ls->current != ':') return ':';
462 else { next(ls); return TK_DBCOLON; }
464 case '"': case '\'': { /* short literal strings */
465 read_string(ls, ls->current, seminfo);
468 case '.': { /* '.', '..', '...', or number */
470 if (check_next(ls, ".")) {
471 if (check_next(ls, "."))
472 return TK_DOTS; /* '...' */
473 else return TK_CONCAT; /* '..' */
475 else if (!lisdigit(ls->current)) return '.';
476 /* else go through */
479 case '0': case '1': case '2': case '3': case '4':
480 case '5': case '6': case '7': case '8': case '9': {
481 read_numeral(ls, seminfo);
488 if (lislalpha(ls->current)) { /* identifier or reserved word? */
492 } while (lislalnum(ls->current));
493 ts = luaX_newstring(ls, luaZ_buffer(ls->buff),
494 luaZ_bufflen(ls->buff));
496 if (isreserved(ts)) /* reserved word? */
497 return ts->tsv.extra - 1 + FIRST_RESERVED;
502 else { /* single-char tokens (+ - / ...) */
513 void luaX_next (LexState *ls) {
514 ls->lastline = ls->linenumber;
515 if (ls->lookahead.token != TK_EOS) { /* is there a look-ahead token? */
516 ls->t = ls->lookahead; /* use this one */
517 ls->lookahead.token = TK_EOS; /* and discharge it */
520 ls->t.token = llex(ls, &ls->t.seminfo); /* read next token */
524 int luaX_lookahead (LexState *ls) {
525 lua_assert(ls->lookahead.token == TK_EOS);
526 ls->lookahead.token = llex(ls, &ls->lookahead.seminfo);
527 return ls->lookahead.token;