1 /* $NetBSD: llex.c,v 1.6 2015/10/08 13:40:16 mbalmer Exp $ */
4 ** Id: llex.c,v 2.93 2015/05/22 17:45:56 roberto Exp
6 ** See Copyright Notice in lua.h
36 #define next(ls) (ls->current = zgetc(ls->z))
40 #define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r')
44 static const char *const luaX_tokens
[] = {
45 "and", "break", "do", "else", "elseif",
46 "end", "false", "for", "function", "goto", "if",
47 "in", "local", "nil", "not", "or", "repeat",
48 "return", "then", "true", "until", "while",
49 "//", "..", "...", "==", ">=", "<=", "~=",
50 "<<", ">>", "::", "<eof>",
51 "<number>", "<integer>", "<name>", "<string>"
55 #define save_and_next(ls) (save(ls, ls->current), next(ls))
58 static l_noret
lexerror (LexState
*ls
, const char *msg
, int token
);
61 static void save (LexState
*ls
, int c
) {
62 Mbuffer
*b
= ls
->buff
;
63 if (luaZ_bufflen(b
) + 1 > luaZ_sizebuffer(b
)) {
65 if (luaZ_sizebuffer(b
) >= MAX_SIZE
/2)
66 lexerror(ls
, "lexical element too long", 0);
67 newsize
= luaZ_sizebuffer(b
) * 2;
68 luaZ_resizebuffer(ls
->L
, b
, newsize
);
70 b
->buffer
[luaZ_bufflen(b
)++] = cast(char, c
);
74 void luaX_init (lua_State
*L
) {
76 TString
*e
= luaS_newliteral(L
, LUA_ENV
); /* create env name */
77 luaC_fix(L
, obj2gco(e
)); /* never collect this name */
78 for (i
=0; i
<NUM_RESERVED
; i
++) {
79 TString
*ts
= luaS_new(L
, luaX_tokens
[i
]);
80 luaC_fix(L
, obj2gco(ts
)); /* reserved words are never collected */
81 ts
->extra
= cast_byte(i
+1); /* reserved word */
86 const char *luaX_token2str (LexState
*ls
, int token
) {
87 if (token
< FIRST_RESERVED
) { /* single-byte symbols? */
88 lua_assert(token
== cast_uchar(token
));
89 return luaO_pushfstring(ls
->L
, "'%c'", token
);
92 const char *s
= luaX_tokens
[token
- FIRST_RESERVED
];
93 if (token
< TK_EOS
) /* fixed format (symbols and reserved words)? */
94 return luaO_pushfstring(ls
->L
, "'%s'", s
);
95 else /* names, strings, and numerals */
101 static const char *txtToken (LexState
*ls
, int token
) {
103 case TK_NAME
: case TK_STRING
:
105 case TK_FLT
: case TK_INT
:
110 return luaO_pushfstring(ls
->L
, "'%s'", luaZ_buffer(ls
->buff
));
112 return luaX_token2str(ls
, token
);
117 static l_noret
lexerror (LexState
*ls
, const char *msg
, int token
) {
118 msg
= luaG_addinfo(ls
->L
, msg
, ls
->source
, ls
->linenumber
);
120 luaO_pushfstring(ls
->L
, "%s near %s", msg
, txtToken(ls
, token
));
121 luaD_throw(ls
->L
, LUA_ERRSYNTAX
);
125 l_noret
luaX_syntaxerror (LexState
*ls
, const char *msg
) {
126 lexerror(ls
, msg
, ls
->t
.token
);
131 ** creates a new string and anchors it in scanner's table so that
132 ** it will not be collected until the end of the compilation
133 ** (by that time it should be anchored somewhere)
135 TString
*luaX_newstring (LexState
*ls
, const char *str
, size_t l
) {
136 lua_State
*L
= ls
->L
;
137 TValue
*o
; /* entry for 'str' */
138 TString
*ts
= luaS_newlstr(L
, str
, l
); /* create new string */
139 setsvalue2s(L
, L
->top
++, ts
); /* temporarily anchor it in stack */
140 o
= luaH_set(L
, ls
->h
, L
->top
- 1);
141 if (ttisnil(o
)) { /* not in use yet? */
142 /* boolean value does not need GC barrier;
143 table has no metatable, so it does not need to invalidate cache */
144 setbvalue(o
, 1); /* t[string] = true */
147 else { /* string already present */
148 ts
= tsvalue(keyfromval(o
)); /* re-use value previously stored */
150 L
->top
--; /* remove string from stack */
156 ** increment line number and skips newline sequence (any of
157 ** \n, \r, \n\r, or \r\n)
159 static void inclinenumber (LexState
*ls
) {
160 int old
= ls
->current
;
161 lua_assert(currIsNewline(ls
));
162 next(ls
); /* skip '\n' or '\r' */
163 if (currIsNewline(ls
) && ls
->current
!= old
)
164 next(ls
); /* skip '\n\r' or '\r\n' */
165 if (++ls
->linenumber
>= MAX_INT
)
166 lexerror(ls
, "chunk has too many lines", 0);
170 void luaX_setinput (lua_State
*L
, LexState
*ls
, ZIO
*z
, TString
*source
,
175 ls
->current
= firstchar
;
176 ls
->lookahead
.token
= TK_EOS
; /* no look-ahead token */
182 ls
->envn
= luaS_newliteral(L
, LUA_ENV
); /* get env name */
183 luaZ_resizebuffer(ls
->L
, ls
->buff
, LUA_MINBUFFER
); /* initialize buffer */
189 ** =======================================================
191 ** =======================================================
195 static int check_next1 (LexState
*ls
, int c
) {
196 if (ls
->current
== c
) {
205 ** Check whether current char is in set 'set' (with two chars) and
208 static int check_next2 (LexState
*ls
, const char *set
) {
209 lua_assert(set
[2] == '\0');
210 if (ls
->current
== set
[0] || ls
->current
== set
[1]) {
220 ** change all characters 'from' in buffer to 'to'
222 static void buffreplace (LexState
*ls
, char from
, char to
) {
224 size_t n
= luaZ_bufflen(ls
->buff
);
225 char *p
= luaZ_buffer(ls
->buff
);
227 if (p
[n
] == from
) p
[n
] = to
;
232 #define buff2num(b,o) (luaO_str2num(luaZ_buffer(b), o) != 0)
236 ** in case of format error, try to change decimal point separator to
237 ** the one defined in the current locale and check again
239 static void trydecpoint (LexState
*ls
, TValue
*o
) {
240 char old
= ls
->decpoint
;
241 ls
->decpoint
= lua_getlocaledecpoint();
242 buffreplace(ls
, old
, ls
->decpoint
); /* try new decimal separator */
243 if (!buff2num(ls
->buff
, o
)) {
244 /* format error with correct decimal point: no more options */
245 buffreplace(ls
, ls
->decpoint
, '.'); /* undo change (for error message) */
246 lexerror(ls
, "malformed number", TK_FLT
);
253 ** this function is quite liberal in what it accepts, as 'luaO_str2num'
254 ** will reject ill-formed numerals.
256 static int read_numeral (LexState
*ls
, SemInfo
*seminfo
) {
258 const char *expo
= "Ee";
259 int first
= ls
->current
;
260 lua_assert(lisdigit(ls
->current
));
262 if (first
== '0' && check_next2(ls
, "xX")) /* hexadecimal? */
265 if (check_next2(ls
, expo
)) /* exponent part? */
266 check_next2(ls
, "-+"); /* optional exponent sign */
267 if (lisxdigit(ls
->current
))
269 else if (ls
->current
== '.')
274 buffreplace(ls
, '.', ls
->decpoint
); /* follow locale for decimal point */
275 if (!buff2num(ls
->buff
, &obj
)) /* format error? */
276 trydecpoint(ls
, &obj
); /* try to update decimal point separator */
277 if (ttisinteger(&obj
)) {
278 seminfo
->i
= ivalue(&obj
);
282 lua_assert(ttisfloat(&obj
));
283 seminfo
->r
= fltvalue(&obj
);
290 static int read_numeral (LexState
*ls
, SemInfo
*seminfo
) {
292 int first
= ls
->current
;
293 lua_assert(lisdigit(ls
->current
));
296 check_next2(ls
, "xX"); /* hexadecimal? */
298 if (lisxdigit(ls
->current
))
303 if (!buff2num(ls
->buff
, &obj
)) /* format error? */
304 lexerror(ls
, "malformed number", TK_INT
);
305 lua_assert(ttisinteger(&obj
));
306 seminfo
->i
= ivalue(&obj
);
312 ** skip a sequence '[=*[' or ']=*]'; if sequence is wellformed, return
313 ** its number of '='s; otherwise, return a negative number (-1 iff there
314 ** are no '='s after initial bracket)
316 static int skip_sep (LexState
*ls
) {
319 lua_assert(s
== '[' || s
== ']');
321 while (ls
->current
== '=') {
325 return (ls
->current
== s
) ? count
: (-count
) - 1;
329 static void read_long_string (LexState
*ls
, SemInfo
*seminfo
, int sep
) {
330 int line
= ls
->linenumber
; /* initial line (for error message) */
331 save_and_next(ls
); /* skip 2nd '[' */
332 if (currIsNewline(ls
)) /* string starts with a newline? */
333 inclinenumber(ls
); /* skip it */
335 switch (ls
->current
) {
336 case EOZ
: { /* error */
337 const char *what
= (seminfo
? "string" : "comment");
338 const char *msg
= luaO_pushfstring(ls
->L
,
339 "unfinished long %s (starting at line %d)", what
, line
);
340 lexerror(ls
, msg
, TK_EOS
);
341 break; /* to avoid warnings */
344 if (skip_sep(ls
) == sep
) {
345 save_and_next(ls
); /* skip 2nd ']' */
350 case '\n': case '\r': {
353 if (!seminfo
) luaZ_resetbuffer(ls
->buff
); /* avoid wasting space */
357 if (seminfo
) save_and_next(ls
);
363 seminfo
->ts
= luaX_newstring(ls
, luaZ_buffer(ls
->buff
) + (2 + sep
),
364 luaZ_bufflen(ls
->buff
) - 2*(2 + sep
));
368 static void esccheck (LexState
*ls
, int c
, const char *msg
) {
370 if (ls
->current
!= EOZ
)
371 save_and_next(ls
); /* add current to buffer for error message */
372 lexerror(ls
, msg
, TK_STRING
);
377 static int gethexa (LexState
*ls
) {
379 esccheck (ls
, lisxdigit(ls
->current
), "hexadecimal digit expected");
380 return luaO_hexavalue(ls
->current
);
384 static int readhexaesc (LexState
*ls
) {
386 r
= (r
<< 4) + gethexa(ls
);
387 luaZ_buffremove(ls
->buff
, 2); /* remove saved chars from buffer */
392 static unsigned long readutf8esc (LexState
*ls
) {
394 int i
= 4; /* chars to be removed: '\', 'u', '{', and first digit */
395 save_and_next(ls
); /* skip 'u' */
396 esccheck(ls
, ls
->current
== '{', "missing '{'");
397 r
= gethexa(ls
); /* must have at least one digit */
398 while ((save_and_next(ls
), lisxdigit(ls
->current
))) {
400 r
= (r
<< 4) + luaO_hexavalue(ls
->current
);
401 esccheck(ls
, r
<= 0x10FFFF, "UTF-8 value too large");
403 esccheck(ls
, ls
->current
== '}', "missing '}'");
404 next(ls
); /* skip '}' */
405 luaZ_buffremove(ls
->buff
, i
); /* remove saved chars from buffer */
410 static void utf8esc (LexState
*ls
) {
411 char buff
[UTF8BUFFSZ
];
412 int n
= luaO_utf8esc(buff
, readutf8esc(ls
));
413 for (; n
> 0; n
--) /* add 'buff' to string */
414 save(ls
, buff
[UTF8BUFFSZ
- n
]);
418 static int readdecesc (LexState
*ls
) {
420 int r
= 0; /* result accumulator */
421 for (i
= 0; i
< 3 && lisdigit(ls
->current
); i
++) { /* read up to 3 digits */
422 r
= 10*r
+ ls
->current
- '0';
425 esccheck(ls
, r
<= UCHAR_MAX
, "decimal escape too large");
426 luaZ_buffremove(ls
->buff
, i
); /* remove read digits from buffer */
431 static void read_string (LexState
*ls
, int del
, SemInfo
*seminfo
) {
432 save_and_next(ls
); /* keep delimiter (for error messages) */
433 while (ls
->current
!= del
) {
434 switch (ls
->current
) {
436 lexerror(ls
, "unfinished string", TK_EOS
);
437 break; /* to avoid warnings */
440 lexerror(ls
, "unfinished string", TK_STRING
);
441 break; /* to avoid warnings */
442 case '\\': { /* escape sequences */
443 int c
; /* final character to be saved */
444 save_and_next(ls
); /* keep '\\' for error messages */
445 switch (ls
->current
) {
446 case 'a': c
= '\a'; goto read_save
;
447 case 'b': c
= '\b'; goto read_save
;
448 case 'f': c
= '\f'; goto read_save
;
449 case 'n': c
= '\n'; goto read_save
;
450 case 'r': c
= '\r'; goto read_save
;
451 case 't': c
= '\t'; goto read_save
;
452 case 'v': c
= '\v'; goto read_save
;
453 case 'x': c
= readhexaesc(ls
); goto read_save
;
454 case 'u': utf8esc(ls
); goto no_save
;
455 case '\n': case '\r':
456 inclinenumber(ls
); c
= '\n'; goto only_save
;
457 case '\\': case '\"': case '\'':
458 c
= ls
->current
; goto read_save
;
459 case EOZ
: goto no_save
; /* will raise an error next loop */
460 case 'z': { /* zap following span of spaces */
461 luaZ_buffremove(ls
->buff
, 1); /* remove '\\' */
462 next(ls
); /* skip the 'z' */
463 while (lisspace(ls
->current
)) {
464 if (currIsNewline(ls
)) inclinenumber(ls
);
470 esccheck(ls
, lisdigit(ls
->current
), "invalid escape sequence");
471 c
= readdecesc(ls
); /* digital escape '\ddd' */
479 luaZ_buffremove(ls
->buff
, 1); /* remove '\\' */
488 save_and_next(ls
); /* skip delimiter */
489 seminfo
->ts
= luaX_newstring(ls
, luaZ_buffer(ls
->buff
) + 1,
490 luaZ_bufflen(ls
->buff
) - 2);
494 static int llex (LexState
*ls
, SemInfo
*seminfo
) {
495 luaZ_resetbuffer(ls
->buff
);
497 switch (ls
->current
) {
498 case '\n': case '\r': { /* line breaks */
502 case ' ': case '\f': case '\t': case '\v': { /* spaces */
506 case '-': { /* '-' or '--' (comment) */
508 if (ls
->current
!= '-') return '-';
509 /* else is a comment */
511 if (ls
->current
== '[') { /* long comment? */
512 int sep
= skip_sep(ls
);
513 luaZ_resetbuffer(ls
->buff
); /* 'skip_sep' may dirty the buffer */
515 read_long_string(ls
, NULL
, sep
); /* skip long comment */
516 luaZ_resetbuffer(ls
->buff
); /* previous call may dirty the buff. */
520 /* else short comment */
521 while (!currIsNewline(ls
) && ls
->current
!= EOZ
)
522 next(ls
); /* skip until end of line (or end of file) */
525 case '[': { /* long string or simply '[' */
526 int sep
= skip_sep(ls
);
528 read_long_string(ls
, seminfo
, sep
);
531 else if (sep
!= -1) /* '[=...' missing second bracket */
532 lexerror(ls
, "invalid long string delimiter", TK_STRING
);
537 if (check_next1(ls
, '=')) return TK_EQ
;
542 if (check_next1(ls
, '=')) return TK_LE
;
543 else if (check_next1(ls
, '<')) return TK_SHL
;
548 if (check_next1(ls
, '=')) return TK_GE
;
549 else if (check_next1(ls
, '>')) return TK_SHR
;
554 if (check_next1(ls
, '/')) return TK_IDIV
;
559 if (check_next1(ls
, '=')) return TK_NE
;
564 if (check_next1(ls
, ':')) return TK_DBCOLON
;
567 case '"': case '\'': { /* short literal strings */
568 read_string(ls
, ls
->current
, seminfo
);
571 case '.': { /* '.', '..', '...', or number */
573 if (check_next1(ls
, '.')) {
574 if (check_next1(ls
, '.'))
575 return TK_DOTS
; /* '...' */
576 else return TK_CONCAT
; /* '..' */
579 else if (!lisdigit(ls
->current
)) return '.';
580 else return read_numeral(ls
, seminfo
);
585 case '0': case '1': case '2': case '3': case '4':
586 case '5': case '6': case '7': case '8': case '9': {
587 return read_numeral(ls
, seminfo
);
593 if (lislalpha(ls
->current
)) { /* identifier or reserved word? */
597 } while (lislalnum(ls
->current
));
598 ts
= luaX_newstring(ls
, luaZ_buffer(ls
->buff
),
599 luaZ_bufflen(ls
->buff
));
601 if (isreserved(ts
)) /* reserved word? */
602 return ts
->extra
- 1 + FIRST_RESERVED
;
607 else { /* single-char tokens (+ - / ...) */
618 void luaX_next (LexState
*ls
) {
619 ls
->lastline
= ls
->linenumber
;
620 if (ls
->lookahead
.token
!= TK_EOS
) { /* is there a look-ahead token? */
621 ls
->t
= ls
->lookahead
; /* use this one */
622 ls
->lookahead
.token
= TK_EOS
; /* and discharge it */
625 ls
->t
.token
= llex(ls
, &ls
->t
.seminfo
); /* read next token */
629 int luaX_lookahead (LexState
*ls
) {
630 lua_assert(ls
->lookahead
.token
== TK_EOS
);
631 ls
->lookahead
.token
= llex(ls
, &ls
->lookahead
.seminfo
);
632 return ls
->lookahead
.token
;