]>
git.proxmox.com Git - ceph.git/blob - ceph/src/civetweb/src/third_party/lua-5.3.1/src/llex.c
2 ** $Id: llex.c,v 2.93 2015/05/22 17:45:56 roberto Exp $
4 ** See Copyright Notice in lua.h
32 #define next(ls) (ls->current = zgetc(ls->z))
36 #define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r')
40 static const char *const luaX_tokens
[] = {
41 "and", "break", "do", "else", "elseif",
42 "end", "false", "for", "function", "goto", "if",
43 "in", "local", "nil", "not", "or", "repeat",
44 "return", "then", "true", "until", "while",
45 "//", "..", "...", "==", ">=", "<=", "~=",
46 "<<", ">>", "::", "<eof>",
47 "<number>", "<integer>", "<name>", "<string>"
51 #define save_and_next(ls) (save(ls, ls->current), next(ls))
54 static l_noret
lexerror (LexState
*ls
, const char *msg
, int token
);
57 static void save (LexState
*ls
, int c
) {
58 Mbuffer
*b
= ls
->buff
;
59 if (luaZ_bufflen(b
) + 1 > luaZ_sizebuffer(b
)) {
61 if (luaZ_sizebuffer(b
) >= MAX_SIZE
/2)
62 lexerror(ls
, "lexical element too long", 0);
63 newsize
= luaZ_sizebuffer(b
) * 2;
64 luaZ_resizebuffer(ls
->L
, b
, newsize
);
66 b
->buffer
[luaZ_bufflen(b
)++] = cast(char, c
);
70 void luaX_init (lua_State
*L
) {
72 TString
*e
= luaS_newliteral(L
, LUA_ENV
); /* create env name */
73 luaC_fix(L
, obj2gco(e
)); /* never collect this name */
74 for (i
=0; i
<NUM_RESERVED
; i
++) {
75 TString
*ts
= luaS_new(L
, luaX_tokens
[i
]);
76 luaC_fix(L
, obj2gco(ts
)); /* reserved words are never collected */
77 ts
->extra
= cast_byte(i
+1); /* reserved word */
82 const char *luaX_token2str (LexState
*ls
, int token
) {
83 if (token
< FIRST_RESERVED
) { /* single-byte symbols? */
84 lua_assert(token
== cast_uchar(token
));
85 return luaO_pushfstring(ls
->L
, "'%c'", token
);
88 const char *s
= luaX_tokens
[token
- FIRST_RESERVED
];
89 if (token
< TK_EOS
) /* fixed format (symbols and reserved words)? */
90 return luaO_pushfstring(ls
->L
, "'%s'", s
);
91 else /* names, strings, and numerals */
97 static const char *txtToken (LexState
*ls
, int token
) {
99 case TK_NAME
: case TK_STRING
:
100 case TK_FLT
: case TK_INT
:
102 return luaO_pushfstring(ls
->L
, "'%s'", luaZ_buffer(ls
->buff
));
104 return luaX_token2str(ls
, token
);
109 static l_noret
lexerror (LexState
*ls
, const char *msg
, int token
) {
110 msg
= luaG_addinfo(ls
->L
, msg
, ls
->source
, ls
->linenumber
);
112 luaO_pushfstring(ls
->L
, "%s near %s", msg
, txtToken(ls
, token
));
113 luaD_throw(ls
->L
, LUA_ERRSYNTAX
);
117 l_noret
luaX_syntaxerror (LexState
*ls
, const char *msg
) {
118 lexerror(ls
, msg
, ls
->t
.token
);
123 ** creates a new string and anchors it in scanner's table so that
124 ** it will not be collected until the end of the compilation
125 ** (by that time it should be anchored somewhere)
127 TString
*luaX_newstring (LexState
*ls
, const char *str
, size_t l
) {
128 lua_State
*L
= ls
->L
;
129 TValue
*o
; /* entry for 'str' */
130 TString
*ts
= luaS_newlstr(L
, str
, l
); /* create new string */
131 setsvalue2s(L
, L
->top
++, ts
); /* temporarily anchor it in stack */
132 o
= luaH_set(L
, ls
->h
, L
->top
- 1);
133 if (ttisnil(o
)) { /* not in use yet? */
134 /* boolean value does not need GC barrier;
135 table has no metatable, so it does not need to invalidate cache */
136 setbvalue(o
, 1); /* t[string] = true */
139 else { /* string already present */
140 ts
= tsvalue(keyfromval(o
)); /* re-use value previously stored */
142 L
->top
--; /* remove string from stack */
148 ** increment line number and skips newline sequence (any of
149 ** \n, \r, \n\r, or \r\n)
151 static void inclinenumber (LexState
*ls
) {
152 int old
= ls
->current
;
153 lua_assert(currIsNewline(ls
));
154 next(ls
); /* skip '\n' or '\r' */
155 if (currIsNewline(ls
) && ls
->current
!= old
)
156 next(ls
); /* skip '\n\r' or '\r\n' */
157 if (++ls
->linenumber
>= MAX_INT
)
158 lexerror(ls
, "chunk has too many lines", 0);
162 void luaX_setinput (lua_State
*L
, LexState
*ls
, ZIO
*z
, TString
*source
,
167 ls
->current
= firstchar
;
168 ls
->lookahead
.token
= TK_EOS
; /* no look-ahead token */
174 ls
->envn
= luaS_newliteral(L
, LUA_ENV
); /* get env name */
175 luaZ_resizebuffer(ls
->L
, ls
->buff
, LUA_MINBUFFER
); /* initialize buffer */
181 ** =======================================================
183 ** =======================================================
187 static int check_next1 (LexState
*ls
, int c
) {
188 if (ls
->current
== c
) {
197 ** Check whether current char is in set 'set' (with two chars) and
200 static int check_next2 (LexState
*ls
, const char *set
) {
201 lua_assert(set
[2] == '\0');
202 if (ls
->current
== set
[0] || ls
->current
== set
[1]) {
211 ** change all characters 'from' in buffer to 'to'
213 static void buffreplace (LexState
*ls
, char from
, char to
) {
215 size_t n
= luaZ_bufflen(ls
->buff
);
216 char *p
= luaZ_buffer(ls
->buff
);
218 if (p
[n
] == from
) p
[n
] = to
;
223 #define buff2num(b,o) (luaO_str2num(luaZ_buffer(b), o) != 0)
226 ** in case of format error, try to change decimal point separator to
227 ** the one defined in the current locale and check again
229 static void trydecpoint (LexState
*ls
, TValue
*o
) {
230 char old
= ls
->decpoint
;
231 ls
->decpoint
= lua_getlocaledecpoint();
232 buffreplace(ls
, old
, ls
->decpoint
); /* try new decimal separator */
233 if (!buff2num(ls
->buff
, o
)) {
234 /* format error with correct decimal point: no more options */
235 buffreplace(ls
, ls
->decpoint
, '.'); /* undo change (for error message) */
236 lexerror(ls
, "malformed number", TK_FLT
);
243 ** this function is quite liberal in what it accepts, as 'luaO_str2num'
244 ** will reject ill-formed numerals.
246 static int read_numeral (LexState
*ls
, SemInfo
*seminfo
) {
248 const char *expo
= "Ee";
249 int first
= ls
->current
;
250 lua_assert(lisdigit(ls
->current
));
252 if (first
== '0' && check_next2(ls
, "xX")) /* hexadecimal? */
255 if (check_next2(ls
, expo
)) /* exponent part? */
256 check_next2(ls
, "-+"); /* optional exponent sign */
257 if (lisxdigit(ls
->current
))
259 else if (ls
->current
== '.')
264 buffreplace(ls
, '.', ls
->decpoint
); /* follow locale for decimal point */
265 if (!buff2num(ls
->buff
, &obj
)) /* format error? */
266 trydecpoint(ls
, &obj
); /* try to update decimal point separator */
267 if (ttisinteger(&obj
)) {
268 seminfo
->i
= ivalue(&obj
);
272 lua_assert(ttisfloat(&obj
));
273 seminfo
->r
= fltvalue(&obj
);
280 ** skip a sequence '[=*[' or ']=*]'; if sequence is wellformed, return
281 ** its number of '='s; otherwise, return a negative number (-1 iff there
282 ** are no '='s after initial bracket)
284 static int skip_sep (LexState
*ls
) {
287 lua_assert(s
== '[' || s
== ']');
289 while (ls
->current
== '=') {
293 return (ls
->current
== s
) ? count
: (-count
) - 1;
297 static void read_long_string (LexState
*ls
, SemInfo
*seminfo
, int sep
) {
298 int line
= ls
->linenumber
; /* initial line (for error message) */
299 save_and_next(ls
); /* skip 2nd '[' */
300 if (currIsNewline(ls
)) /* string starts with a newline? */
301 inclinenumber(ls
); /* skip it */
303 switch (ls
->current
) {
304 case EOZ
: { /* error */
305 const char *what
= (seminfo
? "string" : "comment");
306 const char *msg
= luaO_pushfstring(ls
->L
,
307 "unfinished long %s (starting at line %d)", what
, line
);
308 lexerror(ls
, msg
, TK_EOS
);
309 break; /* to avoid warnings */
312 if (skip_sep(ls
) == sep
) {
313 save_and_next(ls
); /* skip 2nd ']' */
318 case '\n': case '\r': {
321 if (!seminfo
) luaZ_resetbuffer(ls
->buff
); /* avoid wasting space */
325 if (seminfo
) save_and_next(ls
);
331 seminfo
->ts
= luaX_newstring(ls
, luaZ_buffer(ls
->buff
) + (2 + sep
),
332 luaZ_bufflen(ls
->buff
) - 2*(2 + sep
));
336 static void esccheck (LexState
*ls
, int c
, const char *msg
) {
338 if (ls
->current
!= EOZ
)
339 save_and_next(ls
); /* add current to buffer for error message */
340 lexerror(ls
, msg
, TK_STRING
);
345 static int gethexa (LexState
*ls
) {
347 esccheck (ls
, lisxdigit(ls
->current
), "hexadecimal digit expected");
348 return luaO_hexavalue(ls
->current
);
352 static int readhexaesc (LexState
*ls
) {
354 r
= (r
<< 4) + gethexa(ls
);
355 luaZ_buffremove(ls
->buff
, 2); /* remove saved chars from buffer */
360 static unsigned long readutf8esc (LexState
*ls
) {
362 int i
= 4; /* chars to be removed: '\', 'u', '{', and first digit */
363 save_and_next(ls
); /* skip 'u' */
364 esccheck(ls
, ls
->current
== '{', "missing '{'");
365 r
= gethexa(ls
); /* must have at least one digit */
366 while ((save_and_next(ls
), lisxdigit(ls
->current
))) {
368 r
= (r
<< 4) + luaO_hexavalue(ls
->current
);
369 esccheck(ls
, r
<= 0x10FFFF, "UTF-8 value too large");
371 esccheck(ls
, ls
->current
== '}', "missing '}'");
372 next(ls
); /* skip '}' */
373 luaZ_buffremove(ls
->buff
, i
); /* remove saved chars from buffer */
378 static void utf8esc (LexState
*ls
) {
379 char buff
[UTF8BUFFSZ
];
380 int n
= luaO_utf8esc(buff
, readutf8esc(ls
));
381 for (; n
> 0; n
--) /* add 'buff' to string */
382 save(ls
, buff
[UTF8BUFFSZ
- n
]);
386 static int readdecesc (LexState
*ls
) {
388 int r
= 0; /* result accumulator */
389 for (i
= 0; i
< 3 && lisdigit(ls
->current
); i
++) { /* read up to 3 digits */
390 r
= 10*r
+ ls
->current
- '0';
393 esccheck(ls
, r
<= UCHAR_MAX
, "decimal escape too large");
394 luaZ_buffremove(ls
->buff
, i
); /* remove read digits from buffer */
399 static void read_string (LexState
*ls
, int del
, SemInfo
*seminfo
) {
400 save_and_next(ls
); /* keep delimiter (for error messages) */
401 while (ls
->current
!= del
) {
402 switch (ls
->current
) {
404 lexerror(ls
, "unfinished string", TK_EOS
);
405 break; /* to avoid warnings */
408 lexerror(ls
, "unfinished string", TK_STRING
);
409 break; /* to avoid warnings */
410 case '\\': { /* escape sequences */
411 int c
; /* final character to be saved */
412 save_and_next(ls
); /* keep '\\' for error messages */
413 switch (ls
->current
) {
414 case 'a': c
= '\a'; goto read_save
;
415 case 'b': c
= '\b'; goto read_save
;
416 case 'f': c
= '\f'; goto read_save
;
417 case 'n': c
= '\n'; goto read_save
;
418 case 'r': c
= '\r'; goto read_save
;
419 case 't': c
= '\t'; goto read_save
;
420 case 'v': c
= '\v'; goto read_save
;
421 case 'x': c
= readhexaesc(ls
); goto read_save
;
422 case 'u': utf8esc(ls
); goto no_save
;
423 case '\n': case '\r':
424 inclinenumber(ls
); c
= '\n'; goto only_save
;
425 case '\\': case '\"': case '\'':
426 c
= ls
->current
; goto read_save
;
427 case EOZ
: goto no_save
; /* will raise an error next loop */
428 case 'z': { /* zap following span of spaces */
429 luaZ_buffremove(ls
->buff
, 1); /* remove '\\' */
430 next(ls
); /* skip the 'z' */
431 while (lisspace(ls
->current
)) {
432 if (currIsNewline(ls
)) inclinenumber(ls
);
438 esccheck(ls
, lisdigit(ls
->current
), "invalid escape sequence");
439 c
= readdecesc(ls
); /* digital escape '\ddd' */
447 luaZ_buffremove(ls
->buff
, 1); /* remove '\\' */
456 save_and_next(ls
); /* skip delimiter */
457 seminfo
->ts
= luaX_newstring(ls
, luaZ_buffer(ls
->buff
) + 1,
458 luaZ_bufflen(ls
->buff
) - 2);
462 static int llex (LexState
*ls
, SemInfo
*seminfo
) {
463 luaZ_resetbuffer(ls
->buff
);
465 switch (ls
->current
) {
466 case '\n': case '\r': { /* line breaks */
470 case ' ': case '\f': case '\t': case '\v': { /* spaces */
474 case '-': { /* '-' or '--' (comment) */
476 if (ls
->current
!= '-') return '-';
477 /* else is a comment */
479 if (ls
->current
== '[') { /* long comment? */
480 int sep
= skip_sep(ls
);
481 luaZ_resetbuffer(ls
->buff
); /* 'skip_sep' may dirty the buffer */
483 read_long_string(ls
, NULL
, sep
); /* skip long comment */
484 luaZ_resetbuffer(ls
->buff
); /* previous call may dirty the buff. */
488 /* else short comment */
489 while (!currIsNewline(ls
) && ls
->current
!= EOZ
)
490 next(ls
); /* skip until end of line (or end of file) */
493 case '[': { /* long string or simply '[' */
494 int sep
= skip_sep(ls
);
496 read_long_string(ls
, seminfo
, sep
);
499 else if (sep
!= -1) /* '[=...' missing second bracket */
500 lexerror(ls
, "invalid long string delimiter", TK_STRING
);
505 if (check_next1(ls
, '=')) return TK_EQ
;
510 if (check_next1(ls
, '=')) return TK_LE
;
511 else if (check_next1(ls
, '<')) return TK_SHL
;
516 if (check_next1(ls
, '=')) return TK_GE
;
517 else if (check_next1(ls
, '>')) return TK_SHR
;
522 if (check_next1(ls
, '/')) return TK_IDIV
;
527 if (check_next1(ls
, '=')) return TK_NE
;
532 if (check_next1(ls
, ':')) return TK_DBCOLON
;
535 case '"': case '\'': { /* short literal strings */
536 read_string(ls
, ls
->current
, seminfo
);
539 case '.': { /* '.', '..', '...', or number */
541 if (check_next1(ls
, '.')) {
542 if (check_next1(ls
, '.'))
543 return TK_DOTS
; /* '...' */
544 else return TK_CONCAT
; /* '..' */
546 else if (!lisdigit(ls
->current
)) return '.';
547 else return read_numeral(ls
, seminfo
);
549 case '0': case '1': case '2': case '3': case '4':
550 case '5': case '6': case '7': case '8': case '9': {
551 return read_numeral(ls
, seminfo
);
557 if (lislalpha(ls
->current
)) { /* identifier or reserved word? */
561 } while (lislalnum(ls
->current
));
562 ts
= luaX_newstring(ls
, luaZ_buffer(ls
->buff
),
563 luaZ_bufflen(ls
->buff
));
565 if (isreserved(ts
)) /* reserved word? */
566 return ts
->extra
- 1 + FIRST_RESERVED
;
571 else { /* single-char tokens (+ - / ...) */
582 void luaX_next (LexState
*ls
) {
583 ls
->lastline
= ls
->linenumber
;
584 if (ls
->lookahead
.token
!= TK_EOS
) { /* is there a look-ahead token? */
585 ls
->t
= ls
->lookahead
; /* use this one */
586 ls
->lookahead
.token
= TK_EOS
; /* and discharge it */
589 ls
->t
.token
= llex(ls
, &ls
->t
.seminfo
); /* read next token */
593 int luaX_lookahead (LexState
*ls
) {
594 lua_assert(ls
->lookahead
.token
== TK_EOS
);
595 ls
->lookahead
.token
= llex(ls
, &ls
->lookahead
.seminfo
);
596 return ls
->lookahead
.token
;