2 ** $Id: llex.c,v 2.63 2013/03/16 21:10:18 roberto Exp $
4 ** See Copyright Notice in lua.h
28 #define next(ls) (ls->current = zgetc(ls->z))
32 #define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r')
36 static const char *const luaX_tokens
[] = {
37 "and", "break", "do", "else", "elseif",
38 "end", "false", "for", "function", "goto", "if",
39 "in", "local", "nil", "not", "or", "repeat",
40 "return", "then", "true", "until", "while",
41 "..", "...", "==", ">=", "<=", "~=", "::", "<eof>",
42 "<number>", "<name>", "<string>"
46 #define save_and_next(ls) (save(ls, ls->current), next(ls))
49 static l_noret
lexerror(LexState
*ls
, const char *msg
, int token
);
52 static void save(LexState
*ls
, int c
) {
53 Mbuffer
*b
= ls
->buff
;
54 if (luaZ_bufflen(b
) + 1 > luaZ_sizebuffer(b
)) {
56 if (luaZ_sizebuffer(b
) >= MAX_SIZET
/ 2)
57 lexerror(ls
, "lexical element too long", 0);
58 newsize
= luaZ_sizebuffer(b
) * 2;
59 luaZ_resizebuffer(ls
->L
, b
, newsize
);
61 b
->buffer
[luaZ_bufflen(b
)++] = cast(char, c
);
65 void luaX_init(lua_State
*L
) {
67 for (i
= 0; i
< NUM_RESERVED
; i
++) {
68 TString
*ts
= luaS_new(L
, luaX_tokens
[i
]);
69 luaS_fix(ts
); /* reserved words are never collected */
70 ts
->tsv
.extra
= cast_byte(i
+ 1); /* reserved word */
75 const char *luaX_token2str(LexState
*ls
, int token
) {
76 if (token
< FIRST_RESERVED
) { /* single-byte symbols? */
77 lua_assert(token
== cast(unsigned char, token
));
78 return (lisprint(token
)) ? luaO_pushfstring(ls
->L
, LUA_QL("%c"), token
) :
79 luaO_pushfstring(ls
->L
, "char(%d)", token
);
81 const char *s
= luaX_tokens
[token
- FIRST_RESERVED
];
82 if (token
< TK_EOS
) /* fixed format (symbols and reserved words)? */
83 return luaO_pushfstring(ls
->L
, LUA_QS
, s
);
84 else /* names, strings, and numerals */
90 static const char *txtToken(LexState
*ls
, int token
) {
96 return luaO_pushfstring(ls
->L
, LUA_QS
, luaZ_buffer(ls
->buff
));
98 return luaX_token2str(ls
, token
);
103 static l_noret
lexerror(LexState
*ls
, const char *msg
, int token
) {
104 char buff
[LUA_IDSIZE
];
105 luaO_chunkid(buff
, getstr(ls
->source
), LUA_IDSIZE
);
106 msg
= luaO_pushfstring(ls
->L
, "%s:%d: %s", buff
, ls
->linenumber
, msg
);
108 luaO_pushfstring(ls
->L
, "%s near %s", msg
, txtToken(ls
, token
));
109 luaD_throw(ls
->L
, LUA_ERRSYNTAX
);
113 l_noret
luaX_syntaxerror(LexState
*ls
, const char *msg
) {
114 lexerror(ls
, msg
, ls
->t
.token
);
119 ** creates a new string and anchors it in function's table so that
120 ** it will not be collected until the end of the function's compilation
121 ** (by that time it should be anchored in function's prototype)
123 TString
*luaX_newstring(LexState
*ls
, const char *str
, size_t l
) {
124 lua_State
*L
= ls
->L
;
125 TValue
*o
; /* entry for `str' */
126 TString
*ts
= luaS_newlstr(L
, str
, l
); /* create new string */
127 setsvalue2s(L
, L
->top
++, ts
); /* temporarily anchor it in stack */
128 o
= luaH_set(L
, ls
->fs
->h
, L
->top
- 1);
129 if (ttisnil(o
)) { /* not in use yet? (see 'addK') */
130 /* boolean value does not need GC barrier;
131 table has no metatable, so it does not need to invalidate cache */
132 setbvalue(o
, 1); /* t[string] = true */
135 L
->top
--; /* remove string from stack */
141 ** increment line number and skips newline sequence (any of
142 ** \n, \r, \n\r, or \r\n)
144 static void inclinenumber(LexState
*ls
) {
145 int old
= ls
->current
;
146 lua_assert(currIsNewline(ls
));
147 next(ls
); /* skip `\n' or `\r' */
148 if (currIsNewline(ls
) && ls
->current
!= old
)
149 next(ls
); /* skip `\n\r' or `\r\n' */
150 if (++ls
->linenumber
>= MAX_INT
)
151 luaX_syntaxerror(ls
, "chunk has too many lines");
155 void luaX_setinput(lua_State
*L
, LexState
*ls
, ZIO
*z
, TString
*source
,
159 ls
->current
= firstchar
;
160 ls
->lookahead
.token
= TK_EOS
; /* no look-ahead token */
166 ls
->envn
= luaS_new(L
, LUA_ENV
); /* create env name */
167 luaS_fix(ls
->envn
); /* never collect this name */
168 luaZ_resizebuffer(ls
->L
, ls
->buff
, LUA_MINBUFFER
); /* initialize buffer */
174 ** =======================================================
176 ** =======================================================
181 static int check_next(LexState
*ls
, const char *set
) {
182 if (ls
->current
== '\0' || !strchr(set
, ls
->current
))
190 ** change all characters 'from' in buffer to 'to'
192 static void buffreplace(LexState
*ls
, char from
, char to
) {
193 size_t n
= luaZ_bufflen(ls
->buff
);
194 char *p
= luaZ_buffer(ls
->buff
);
196 if (p
[n
] == from
) p
[n
] = to
;
201 #define getlocaldecpoint() '.'
202 #elif !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 `]' */
289 if (!seminfo
) luaZ_resetbuffer(ls
->buff
); /* avoid wasting space */
293 if (seminfo
) save_and_next(ls
);
300 seminfo
->ts
= luaX_newstring(ls
, luaZ_buffer(ls
->buff
) + (2 + sep
),
301 luaZ_bufflen(ls
->buff
) - 2 * (2 + sep
));
305 static void escerror(LexState
*ls
, int *c
, int n
, const char *msg
) {
307 luaZ_resetbuffer(ls
->buff
); /* prepare error message */
309 for (i
= 0; i
< n
&& c
[i
] != EOZ
; i
++)
311 lexerror(ls
, msg
, TK_STRING
);
315 static int readhexaesc(LexState
*ls
) {
316 int c
[3], i
; /* keep input for error message */
317 int r
= 0; /* result accumulator */
318 c
[0] = 'x'; /* for error message */
319 for (i
= 1; i
< 3; i
++) { /* read two hexadecimal digits */
321 if (!lisxdigit(c
[i
]))
322 escerror(ls
, c
, i
+ 1, "hexadecimal digit expected");
323 r
= (r
<< 4) + luaO_hexavalue(c
[i
]);
329 static int readdecesc(LexState
*ls
) {
331 int r
= 0; /* result accumulator */
332 for (i
= 0; i
< 3 && lisdigit(ls
->current
); i
++) { /* read up to 3 digits */
334 r
= 10 * r
+ c
[i
] - '0';
338 escerror(ls
, c
, i
, "decimal escape too large");
343 static void read_string(LexState
*ls
, int del
, SemInfo
*seminfo
) {
344 save_and_next(ls
); /* keep delimiter (for error messages) */
345 while (ls
->current
!= del
) {
346 switch (ls
->current
) {
348 lexerror(ls
, "unfinished string", TK_EOS
);
349 break; /* to avoid warnings */
352 lexerror(ls
, "unfinished string", TK_STRING
);
353 break; /* to avoid warnings */
354 case '\\': { /* escape sequences */
355 int c
; /* final character to be saved */
356 next(ls
); /* do not save the `\' */
357 switch (ls
->current
) {
393 goto no_save
; /* will raise an error next loop */
394 case 'z': { /* zap following span of spaces */
395 next(ls
); /* skip the 'z' */
396 while (lisspace(ls
->current
)) {
397 if (currIsNewline(ls
)) inclinenumber(ls
);
403 if (!lisdigit(ls
->current
))
404 escerror(ls
, &ls
->current
, 1, "invalid escape sequence");
405 /* digital escape \ddd */
411 next(ls
); /* read next character */
413 save(ls
, c
); /* save 'c' */
421 save_and_next(ls
); /* skip delimiter */
422 seminfo
->ts
= luaX_newstring(ls
, luaZ_buffer(ls
->buff
) + 1,
423 luaZ_bufflen(ls
->buff
) - 2);
427 static int llex(LexState
*ls
, SemInfo
*seminfo
) {
428 luaZ_resetbuffer(ls
->buff
);
430 switch (ls
->current
) {
432 case '\r': { /* line breaks */
439 case '\v': { /* spaces */
443 case '-': { /* '-' or '--' (comment) */
445 if (ls
->current
!= '-') return '-';
446 /* else is a comment */
448 if (ls
->current
== '[') { /* long comment? */
449 int sep
= skip_sep(ls
);
450 luaZ_resetbuffer(ls
->buff
); /* `skip_sep' may dirty the buffer */
452 read_long_string(ls
, NULL
, sep
); /* skip long comment */
453 luaZ_resetbuffer(ls
->buff
); /* previous call may dirty the buff. */
457 /* else short comment */
458 while (!currIsNewline(ls
) && ls
->current
!= EOZ
)
459 next(ls
); /* skip until end of line (or end of file) */
462 case '[': { /* long string or simply '[' */
463 int sep
= skip_sep(ls
);
465 read_long_string(ls
, seminfo
, sep
);
467 } else if (sep
== -1) return '[';
468 else lexerror(ls
, "invalid long string delimiter", TK_STRING
);
472 if (ls
->current
!= '=') return '=';
473 else { next(ls
); return TK_EQ
; }
477 if (ls
->current
!= '=') return '<';
478 else { next(ls
); return TK_LE
; }
482 if (ls
->current
!= '=') return '>';
483 else { next(ls
); return TK_GE
; }
487 if (ls
->current
!= '=') return '~';
488 else { next(ls
); return TK_NE
; }
492 if (ls
->current
!= ':') return ':';
493 else { next(ls
); return TK_DBCOLON
; }
496 case '\'': { /* short literal strings */
497 read_string(ls
, ls
->current
, seminfo
);
500 case '.': { /* '.', '..', '...', or number */
502 if (check_next(ls
, ".")) {
503 if (check_next(ls
, "."))
504 return TK_DOTS
; /* '...' */
505 else return TK_CONCAT
; /* '..' */
506 } else if (!lisdigit(ls
->current
)) return '.';
507 /* else go through */
519 read_numeral(ls
, seminfo
);
526 if (lislalpha(ls
->current
)) { /* identifier or reserved word? */
530 } while (lislalnum(ls
->current
));
531 ts
= luaX_newstring(ls
, luaZ_buffer(ls
->buff
),
532 luaZ_bufflen(ls
->buff
));
534 if (isreserved(ts
)) /* reserved word? */
535 return ts
->tsv
.extra
- 1 + FIRST_RESERVED
;
539 } else { /* single-char tokens (+ - / ...) */
550 void luaX_next(LexState
*ls
) {
551 ls
->lastline
= ls
->linenumber
;
552 if (ls
->lookahead
.token
!= TK_EOS
) { /* is there a look-ahead token? */
553 ls
->t
= ls
->lookahead
; /* use this one */
554 ls
->lookahead
.token
= TK_EOS
; /* and discharge it */
556 ls
->t
.token
= llex(ls
, &ls
->t
.seminfo
); /* read next token */
560 int luaX_lookahead(LexState
*ls
) {
561 lua_assert(ls
->lookahead
.token
== TK_EOS
);
562 ls
->lookahead
.token
= llex(ls
, &ls
->lookahead
.seminfo
);
563 return ls
->lookahead
.token
;