3 ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
5 ** Major portions taken verbatim or adapted from the Lua interpreter.
6 ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
27 #include "lj_strscan.h"
28 #include "lj_strfmt.h"
30 /* Lua lexer token names. */
31 static const char *const tokennames
[] = {
32 #define TKSTR1(name) #name,
33 #define TKSTR2(name, sym) #sym,
40 /* -- Buffer handling ----------------------------------------------------- */
43 #define lex_iseol(ls) (ls->c == '\n' || ls->c == '\r')
45 /* Get more input from reader. */
46 static LJ_NOINLINE LexChar
lex_more(LexState
*ls
)
49 const char *p
= ls
->rfunc(ls
->L
, ls
->rdata
, &sz
);
50 if (p
== NULL
|| sz
== 0) return LEX_EOF
;
51 if (sz
>= LJ_MAX_BUF
) {
52 if (sz
!= ~(size_t)0) lj_err_mem(ls
->L
);
53 sz
= ~(uintptr_t)0 - (uintptr_t)p
;
54 if (sz
>= LJ_MAX_BUF
) sz
= LJ_MAX_BUF
-1;
59 return (LexChar
)(uint8_t)p
[0];
62 /* Get next character. */
63 static LJ_AINLINE LexChar
lex_next(LexState
*ls
)
65 return (ls
->c
= ls
->p
< ls
->pe
? (LexChar
)(uint8_t)*ls
->p
++ : lex_more(ls
));
69 static LJ_AINLINE
void lex_save(LexState
*ls
, LexChar c
)
71 lj_buf_putb(&ls
->sb
, c
);
74 /* Save previous character and get next character. */
75 static LJ_AINLINE LexChar
lex_savenext(LexState
*ls
)
81 /* Skip line break. Handles "\n", "\r", "\r\n" or "\n\r". */
82 static void lex_newline(LexState
*ls
)
85 lj_assertLS(lex_iseol(ls
), "bad usage");
86 lex_next(ls
); /* Skip "\n" or "\r". */
87 if (lex_iseol(ls
) && ls
->c
!= old
) lex_next(ls
); /* Skip "\n\r" or "\r\n". */
88 if (++ls
->linenumber
>= LJ_MAX_LINE
)
89 lj_lex_error(ls
, ls
->tok
, LJ_ERR_XLINES
);
92 /* -- Scanner for terminals ----------------------------------------------- */
94 /* Parse a number literal. */
95 static void lex_number(LexState
*ls
, TValue
*tv
)
99 lj_assertLS(lj_char_isdigit(ls
->c
), "bad usage");
100 if ((c
= ls
->c
) == '0' && (lex_savenext(ls
) | 0x20) == 'x')
102 while (lj_char_isident(ls
->c
) || ls
->c
== '.' ||
103 ((ls
->c
== '-' || ls
->c
== '+') && (c
| 0x20) == xp
)) {
108 fmt
= lj_strscan_scan((const uint8_t *)ls
->sb
.b
, sbuflen(&ls
->sb
)-1, tv
,
109 (LJ_DUALNUM
? STRSCAN_OPT_TOINT
: STRSCAN_OPT_TONUM
) |
110 (LJ_HASFFI
? (STRSCAN_OPT_LL
|STRSCAN_OPT_IMAG
) : 0));
111 if (LJ_DUALNUM
&& fmt
== STRSCAN_INT
) {
112 setitype(tv
, LJ_TISNUM
);
113 } else if (fmt
== STRSCAN_NUM
) {
114 /* Already in correct format. */
116 } else if (fmt
!= STRSCAN_ERROR
) {
117 lua_State
*L
= ls
->L
;
119 lj_assertLS(fmt
== STRSCAN_I64
|| fmt
== STRSCAN_U64
|| fmt
== STRSCAN_IMAG
,
120 "unexpected number format %d", fmt
);
122 if (fmt
== STRSCAN_IMAG
) {
123 cd
= lj_cdata_new_(L
, CTID_COMPLEX_DOUBLE
, 2*sizeof(double));
124 ((double *)cdataptr(cd
))[0] = 0;
125 ((double *)cdataptr(cd
))[1] = numV(tv
);
127 cd
= lj_cdata_new_(L
, fmt
==STRSCAN_I64
? CTID_INT64
: CTID_UINT64
, 8);
128 *(uint64_t *)cdataptr(cd
) = tv
->u64
;
130 lj_parse_keepcdata(ls
, tv
, cd
);
133 lj_assertLS(fmt
== STRSCAN_ERROR
,
134 "unexpected number format %d", fmt
);
135 lj_lex_error(ls
, TK_number
, LJ_ERR_XNUMBER
);
139 /* Skip equal signs for "[=...=[" and "]=...=]" and return their count. */
140 static int lex_skipeq(LexState
*ls
)
144 lj_assertLS(s
== '[' || s
== ']', "bad usage");
145 while (lex_savenext(ls
) == '=' && count
< 0x20000000)
147 return (ls
->c
== s
) ? count
: (-count
) - 1;
150 /* Parse a long string or long comment (tv set to NULL). */
151 static void lex_longstring(LexState
*ls
, TValue
*tv
, int sep
)
153 lex_savenext(ls
); /* Skip second '['. */
154 if (lex_iseol(ls
)) /* Skip initial newline. */
159 lj_lex_error(ls
, TK_eof
, tv
? LJ_ERR_XLSTR
: LJ_ERR_XLCOM
);
162 if (lex_skipeq(ls
) == sep
) {
163 lex_savenext(ls
); /* Skip second ']'. */
171 if (!tv
) lj_buf_reset(&ls
->sb
); /* Don't waste space for comments. */
179 GCstr
*str
= lj_parse_keepstr(ls
, ls
->sb
.b
+ (2 + (MSize
)sep
),
180 sbuflen(&ls
->sb
) - 2*(2 + (MSize
)sep
));
181 setstrV(ls
->L
, tv
, str
);
185 /* Parse a string. */
186 static void lex_string(LexState
*ls
, TValue
*tv
)
188 LexChar delim
= ls
->c
; /* Delimiter is '\'' or '"'. */
190 while (ls
->c
!= delim
) {
193 lj_lex_error(ls
, TK_eof
, LJ_ERR_XSTR
);
197 lj_lex_error(ls
, TK_string
, LJ_ERR_XSTR
);
200 LexChar c
= lex_next(ls
); /* Skip the '\\'. */
202 case 'a': c
= '\a'; break;
203 case 'b': c
= '\b'; break;
204 case 'f': c
= '\f'; break;
205 case 'n': c
= '\n'; break;
206 case 'r': c
= '\r'; break;
207 case 't': c
= '\t'; break;
208 case 'v': c
= '\v'; break;
209 case 'x': /* Hexadecimal escape '\xXX'. */
210 c
= (lex_next(ls
) & 15u) << 4;
211 if (!lj_char_isdigit(ls
->c
)) {
212 if (!lj_char_isxdigit(ls
->c
)) goto err_xesc
;
215 c
+= (lex_next(ls
) & 15u);
216 if (!lj_char_isdigit(ls
->c
)) {
217 if (!lj_char_isxdigit(ls
->c
)) goto err_xesc
;
221 case 'u': /* Unicode escape '\u{XX...}'. */
222 if (lex_next(ls
) != '{') goto err_xesc
;
226 c
= (c
<< 4) | (ls
->c
& 15u);
227 if (!lj_char_isdigit(ls
->c
)) {
228 if (!lj_char_isxdigit(ls
->c
)) goto err_xesc
;
231 if (c
>= 0x110000) goto err_xesc
; /* Out of Unicode range. */
232 } while (lex_next(ls
) != '}');
235 lex_save(ls
, 0xc0 | (c
>> 6));
238 lex_save(ls
, 0xf0 | (c
>> 18));
239 lex_save(ls
, 0x80 | ((c
>> 12) & 0x3f));
241 if (c
>= 0xd800 && c
< 0xe000) goto err_xesc
; /* No surrogates. */
242 lex_save(ls
, 0xe0 | (c
>> 12));
244 lex_save(ls
, 0x80 | ((c
>> 6) & 0x3f));
246 c
= 0x80 | (c
& 0x3f);
248 case 'z': /* Skip whitespace. */
250 while (lj_char_isspace(ls
->c
))
251 if (lex_iseol(ls
)) lex_newline(ls
); else lex_next(ls
);
253 case '\n': case '\r': lex_save(ls
, '\n'); lex_newline(ls
); continue;
254 case '\\': case '\"': case '\'': break;
255 case LEX_EOF
: continue;
257 if (!lj_char_isdigit(c
))
259 c
-= '0'; /* Decimal escape '\ddd'. */
260 if (lj_char_isdigit(lex_next(ls
))) {
261 c
= c
*10 + (ls
->c
- '0');
262 if (lj_char_isdigit(lex_next(ls
))) {
263 c
= c
*10 + (ls
->c
- '0');
266 lj_lex_error(ls
, TK_string
, LJ_ERR_XESC
);
283 lex_savenext(ls
); /* Skip trailing delimiter. */
285 lj_parse_keepstr(ls
, ls
->sb
.b
+1, sbuflen(&ls
->sb
)-2));
288 /* -- Main lexical scanner ------------------------------------------------ */
290 /* Get next lexical token. */
291 static LexToken
lex_scan(LexState
*ls
, TValue
*tv
)
293 lj_buf_reset(&ls
->sb
);
295 if (lj_char_isident(ls
->c
)) {
297 if (lj_char_isdigit(ls
->c
)) { /* Numeric literal. */
301 /* Identifier or reserved word. */
304 } while (lj_char_isident(ls
->c
));
305 s
= lj_parse_keepstr(ls
, ls
->sb
.b
, sbuflen(&ls
->sb
));
306 setstrV(ls
->L
, tv
, s
);
307 if (s
->reserved
> 0) /* Reserved word? */
308 return TK_OFS
+ s
->reserved
;
324 if (ls
->c
!= '-') return '-';
326 if (ls
->c
== '[') { /* Long comment "--[=*[...]=*]". */
327 int sep
= lex_skipeq(ls
);
328 lj_buf_reset(&ls
->sb
); /* `lex_skipeq' may dirty the buffer */
330 lex_longstring(ls
, NULL
, sep
);
331 lj_buf_reset(&ls
->sb
);
335 /* Short comment "--.*\n". */
336 while (!lex_iseol(ls
) && ls
->c
!= LEX_EOF
)
340 int sep
= lex_skipeq(ls
);
342 lex_longstring(ls
, tv
, sep
);
344 } else if (sep
== -1) {
347 lj_lex_error(ls
, TK_string
, LJ_ERR_XLDELIM
);
353 if (ls
->c
!= '=') return '='; else { lex_next(ls
); return TK_eq
; }
356 if (ls
->c
!= '=') return '<'; else { lex_next(ls
); return TK_le
; }
359 if (ls
->c
!= '=') return '>'; else { lex_next(ls
); return TK_ge
; }
362 if (ls
->c
!= '=') return '~'; else { lex_next(ls
); return TK_ne
; }
365 if (ls
->c
!= ':') return ':'; else { lex_next(ls
); return TK_label
; }
371 if (lex_savenext(ls
) == '.') {
375 return TK_dots
; /* ... */
377 return TK_concat
; /* .. */
378 } else if (!lj_char_isdigit(ls
->c
)) {
389 return c
; /* Single-char tokens (+ - / ...). */
395 /* -- Lexer API ----------------------------------------------------------- */
397 /* Setup lexer state. */
398 int lj_lex_setup(lua_State
*L
, LexState
*ls
)
403 ls
->pe
= ls
->p
= NULL
;
410 ls
->lookahead
= TK_eof
; /* No look-ahead token. */
414 lex_next(ls
); /* Read-ahead first char. */
415 if (ls
->c
== 0xef && ls
->p
+ 2 <= ls
->pe
&& (uint8_t)ls
->p
[0] == 0xbb &&
416 (uint8_t)ls
->p
[1] == 0xbf) { /* Skip UTF-8 BOM (if buffered). */
421 if (ls
->c
== '#') { /* Skip POSIX #! header line. */
424 if (ls
->c
== LEX_EOF
) return 0;
425 } while (!lex_iseol(ls
));
429 if (ls
->c
== LUA_SIGNATURE
[0]) { /* Bytecode dump. */
432 ** Loading bytecode with an extra header is disabled for security
433 ** reasons. This may circumvent the usual check for bytecode vs.
434 ** Lua code by looking at the first char. Since this is a potential
435 ** security violation no attempt is made to echo the chunkname either.
437 setstrV(L
, L
->top
++, lj_err_str(L
, LJ_ERR_BCBAD
));
438 lj_err_throw(L
, LUA_ERRSYNTAX
);
445 /* Cleanup lexer state. */
446 void lj_lex_cleanup(lua_State
*L
, LexState
*ls
)
448 global_State
*g
= G(L
);
449 lj_mem_freevec(g
, ls
->bcstack
, ls
->sizebcstack
, BCInsLine
);
450 lj_mem_freevec(g
, ls
->vstack
, ls
->sizevstack
, VarInfo
);
451 lj_buf_free(g
, &ls
->sb
);
454 /* Return next lexical token. */
455 void lj_lex_next(LexState
*ls
)
457 ls
->lastline
= ls
->linenumber
;
458 if (LJ_LIKELY(ls
->lookahead
== TK_eof
)) { /* No lookahead token? */
459 ls
->tok
= lex_scan(ls
, &ls
->tokval
); /* Get next token. */
460 } else { /* Otherwise return lookahead token. */
461 ls
->tok
= ls
->lookahead
;
462 ls
->lookahead
= TK_eof
;
463 ls
->tokval
= ls
->lookaheadval
;
467 /* Look ahead for the next token. */
468 LexToken
lj_lex_lookahead(LexState
*ls
)
470 lj_assertLS(ls
->lookahead
== TK_eof
, "double lookahead");
471 ls
->lookahead
= lex_scan(ls
, &ls
->lookaheadval
);
472 return ls
->lookahead
;
475 /* Convert token to string. */
476 const char *lj_lex_token2str(LexState
*ls
, LexToken tok
)
479 return tokennames
[tok
-TK_OFS
-1];
480 else if (!lj_char_iscntrl(tok
))
481 return lj_strfmt_pushf(ls
->L
, "%c", tok
);
483 return lj_strfmt_pushf(ls
->L
, "char(%d)", tok
);
487 void lj_lex_error(LexState
*ls
, LexToken tok
, ErrMsg em
, ...)
493 } else if (tok
== TK_name
|| tok
== TK_string
|| tok
== TK_number
) {
497 tokstr
= lj_lex_token2str(ls
, tok
);
500 lj_err_lex(ls
->L
, ls
->chunkname
, tokstr
, ls
->linenumber
, em
, argp
);
504 /* Initialize strings for reserved words. */
505 void lj_lex_init(lua_State
*L
)
508 for (i
= 0; i
< TK_RESERVED
; i
++) {
509 GCstr
*s
= lj_str_newz(L
, tokennames
[i
]);
510 fixstring(s
); /* Reserved words are never collected. */
511 s
->reserved
= (uint8_t)(i
+1);