]> git.proxmox.com Git - ceph.git/blob - ceph/src/civetweb/src/third_party/lua-5.3.1/src/llex.c
bump version to 12.2.12-pve1
[ceph.git] / ceph / src / civetweb / src / third_party / lua-5.3.1 / src / llex.c
1 /*
2 ** $Id: llex.c,v 2.93 2015/05/22 17:45:56 roberto Exp $
3 ** Lexical Analyzer
4 ** See Copyright Notice in lua.h
5 */
6
7 #define llex_c
8 #define LUA_CORE
9
10 #include "lprefix.h"
11
12
13 #include <locale.h>
14 #include <string.h>
15
16 #include "lua.h"
17
18 #include "lctype.h"
19 #include "ldebug.h"
20 #include "ldo.h"
21 #include "lgc.h"
22 #include "llex.h"
23 #include "lobject.h"
24 #include "lparser.h"
25 #include "lstate.h"
26 #include "lstring.h"
27 #include "ltable.h"
28 #include "lzio.h"
29
30
31
32 #define next(ls) (ls->current = zgetc(ls->z))
33
34
35
36 #define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r')
37
38
39 /* ORDER RESERVED */
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>"
48 };
49
50
51 #define save_and_next(ls) (save(ls, ls->current), next(ls))
52
53
54 static l_noret lexerror (LexState *ls, const char *msg, int token);
55
56
57 static void save (LexState *ls, int c) {
58 Mbuffer *b = ls->buff;
59 if (luaZ_bufflen(b) + 1 > luaZ_sizebuffer(b)) {
60 size_t newsize;
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);
65 }
66 b->buffer[luaZ_bufflen(b)++] = cast(char, c);
67 }
68
69
70 void luaX_init (lua_State *L) {
71 int i;
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 */
78 }
79 }
80
81
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);
86 }
87 else {
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 */
92 return s;
93 }
94 }
95
96
97 static const char *txtToken (LexState *ls, int token) {
98 switch (token) {
99 case TK_NAME: case TK_STRING:
100 case TK_FLT: case TK_INT:
101 save(ls, '\0');
102 return luaO_pushfstring(ls->L, "'%s'", luaZ_buffer(ls->buff));
103 default:
104 return luaX_token2str(ls, token);
105 }
106 }
107
108
109 static l_noret lexerror (LexState *ls, const char *msg, int token) {
110 msg = luaG_addinfo(ls->L, msg, ls->source, ls->linenumber);
111 if (token)
112 luaO_pushfstring(ls->L, "%s near %s", msg, txtToken(ls, token));
113 luaD_throw(ls->L, LUA_ERRSYNTAX);
114 }
115
116
117 l_noret luaX_syntaxerror (LexState *ls, const char *msg) {
118 lexerror(ls, msg, ls->t.token);
119 }
120
121
122 /*
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)
126 */
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 */
137 luaC_checkGC(L);
138 }
139 else { /* string already present */
140 ts = tsvalue(keyfromval(o)); /* re-use value previously stored */
141 }
142 L->top--; /* remove string from stack */
143 return ts;
144 }
145
146
147 /*
148 ** increment line number and skips newline sequence (any of
149 ** \n, \r, \n\r, or \r\n)
150 */
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);
159 }
160
161
162 void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source,
163 int firstchar) {
164 ls->t.token = 0;
165 ls->decpoint = '.';
166 ls->L = L;
167 ls->current = firstchar;
168 ls->lookahead.token = TK_EOS; /* no look-ahead token */
169 ls->z = z;
170 ls->fs = NULL;
171 ls->linenumber = 1;
172 ls->lastline = 1;
173 ls->source = source;
174 ls->envn = luaS_newliteral(L, LUA_ENV); /* get env name */
175 luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */
176 }
177
178
179
180 /*
181 ** =======================================================
182 ** LEXICAL ANALYZER
183 ** =======================================================
184 */
185
186
187 static int check_next1 (LexState *ls, int c) {
188 if (ls->current == c) {
189 next(ls);
190 return 1;
191 }
192 else return 0;
193 }
194
195
196 /*
197 ** Check whether current char is in set 'set' (with two chars) and
198 ** saves it
199 */
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]) {
203 save_and_next(ls);
204 return 1;
205 }
206 else return 0;
207 }
208
209
210 /*
211 ** change all characters 'from' in buffer to 'to'
212 */
213 static void buffreplace (LexState *ls, char from, char to) {
214 if (from != to) {
215 size_t n = luaZ_bufflen(ls->buff);
216 char *p = luaZ_buffer(ls->buff);
217 while (n--)
218 if (p[n] == from) p[n] = to;
219 }
220 }
221
222
223 #define buff2num(b,o) (luaO_str2num(luaZ_buffer(b), o) != 0)
224
225 /*
226 ** in case of format error, try to change decimal point separator to
227 ** the one defined in the current locale and check again
228 */
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);
237 }
238 }
239
240
241 /* LUA_NUMBER */
242 /*
243 ** this function is quite liberal in what it accepts, as 'luaO_str2num'
244 ** will reject ill-formed numerals.
245 */
246 static int read_numeral (LexState *ls, SemInfo *seminfo) {
247 TValue obj;
248 const char *expo = "Ee";
249 int first = ls->current;
250 lua_assert(lisdigit(ls->current));
251 save_and_next(ls);
252 if (first == '0' && check_next2(ls, "xX")) /* hexadecimal? */
253 expo = "Pp";
254 for (;;) {
255 if (check_next2(ls, expo)) /* exponent part? */
256 check_next2(ls, "-+"); /* optional exponent sign */
257 if (lisxdigit(ls->current))
258 save_and_next(ls);
259 else if (ls->current == '.')
260 save_and_next(ls);
261 else break;
262 }
263 save(ls, '\0');
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);
269 return TK_INT;
270 }
271 else {
272 lua_assert(ttisfloat(&obj));
273 seminfo->r = fltvalue(&obj);
274 return TK_FLT;
275 }
276 }
277
278
279 /*
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)
283 */
284 static int skip_sep (LexState *ls) {
285 int count = 0;
286 int s = ls->current;
287 lua_assert(s == '[' || s == ']');
288 save_and_next(ls);
289 while (ls->current == '=') {
290 save_and_next(ls);
291 count++;
292 }
293 return (ls->current == s) ? count : (-count) - 1;
294 }
295
296
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 */
302 for (;;) {
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 */
310 }
311 case ']': {
312 if (skip_sep(ls) == sep) {
313 save_and_next(ls); /* skip 2nd ']' */
314 goto endloop;
315 }
316 break;
317 }
318 case '\n': case '\r': {
319 save(ls, '\n');
320 inclinenumber(ls);
321 if (!seminfo) luaZ_resetbuffer(ls->buff); /* avoid wasting space */
322 break;
323 }
324 default: {
325 if (seminfo) save_and_next(ls);
326 else next(ls);
327 }
328 }
329 } endloop:
330 if (seminfo)
331 seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + (2 + sep),
332 luaZ_bufflen(ls->buff) - 2*(2 + sep));
333 }
334
335
336 static void esccheck (LexState *ls, int c, const char *msg) {
337 if (!c) {
338 if (ls->current != EOZ)
339 save_and_next(ls); /* add current to buffer for error message */
340 lexerror(ls, msg, TK_STRING);
341 }
342 }
343
344
345 static int gethexa (LexState *ls) {
346 save_and_next(ls);
347 esccheck (ls, lisxdigit(ls->current), "hexadecimal digit expected");
348 return luaO_hexavalue(ls->current);
349 }
350
351
352 static int readhexaesc (LexState *ls) {
353 int r = gethexa(ls);
354 r = (r << 4) + gethexa(ls);
355 luaZ_buffremove(ls->buff, 2); /* remove saved chars from buffer */
356 return r;
357 }
358
359
360 static unsigned long readutf8esc (LexState *ls) {
361 unsigned long r;
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))) {
367 i++;
368 r = (r << 4) + luaO_hexavalue(ls->current);
369 esccheck(ls, r <= 0x10FFFF, "UTF-8 value too large");
370 }
371 esccheck(ls, ls->current == '}', "missing '}'");
372 next(ls); /* skip '}' */
373 luaZ_buffremove(ls->buff, i); /* remove saved chars from buffer */
374 return r;
375 }
376
377
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]);
383 }
384
385
386 static int readdecesc (LexState *ls) {
387 int i;
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';
391 save_and_next(ls);
392 }
393 esccheck(ls, r <= UCHAR_MAX, "decimal escape too large");
394 luaZ_buffremove(ls->buff, i); /* remove read digits from buffer */
395 return r;
396 }
397
398
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) {
403 case EOZ:
404 lexerror(ls, "unfinished string", TK_EOS);
405 break; /* to avoid warnings */
406 case '\n':
407 case '\r':
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);
433 else next(ls);
434 }
435 goto no_save;
436 }
437 default: {
438 esccheck(ls, lisdigit(ls->current), "invalid escape sequence");
439 c = readdecesc(ls); /* digital escape '\ddd' */
440 goto only_save;
441 }
442 }
443 read_save:
444 next(ls);
445 /* go through */
446 only_save:
447 luaZ_buffremove(ls->buff, 1); /* remove '\\' */
448 save(ls, c);
449 /* go through */
450 no_save: break;
451 }
452 default:
453 save_and_next(ls);
454 }
455 }
456 save_and_next(ls); /* skip delimiter */
457 seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + 1,
458 luaZ_bufflen(ls->buff) - 2);
459 }
460
461
462 static int llex (LexState *ls, SemInfo *seminfo) {
463 luaZ_resetbuffer(ls->buff);
464 for (;;) {
465 switch (ls->current) {
466 case '\n': case '\r': { /* line breaks */
467 inclinenumber(ls);
468 break;
469 }
470 case ' ': case '\f': case '\t': case '\v': { /* spaces */
471 next(ls);
472 break;
473 }
474 case '-': { /* '-' or '--' (comment) */
475 next(ls);
476 if (ls->current != '-') return '-';
477 /* else is a comment */
478 next(ls);
479 if (ls->current == '[') { /* long comment? */
480 int sep = skip_sep(ls);
481 luaZ_resetbuffer(ls->buff); /* 'skip_sep' may dirty the buffer */
482 if (sep >= 0) {
483 read_long_string(ls, NULL, sep); /* skip long comment */
484 luaZ_resetbuffer(ls->buff); /* previous call may dirty the buff. */
485 break;
486 }
487 }
488 /* else short comment */
489 while (!currIsNewline(ls) && ls->current != EOZ)
490 next(ls); /* skip until end of line (or end of file) */
491 break;
492 }
493 case '[': { /* long string or simply '[' */
494 int sep = skip_sep(ls);
495 if (sep >= 0) {
496 read_long_string(ls, seminfo, sep);
497 return TK_STRING;
498 }
499 else if (sep != -1) /* '[=...' missing second bracket */
500 lexerror(ls, "invalid long string delimiter", TK_STRING);
501 return '[';
502 }
503 case '=': {
504 next(ls);
505 if (check_next1(ls, '=')) return TK_EQ;
506 else return '=';
507 }
508 case '<': {
509 next(ls);
510 if (check_next1(ls, '=')) return TK_LE;
511 else if (check_next1(ls, '<')) return TK_SHL;
512 else return '<';
513 }
514 case '>': {
515 next(ls);
516 if (check_next1(ls, '=')) return TK_GE;
517 else if (check_next1(ls, '>')) return TK_SHR;
518 else return '>';
519 }
520 case '/': {
521 next(ls);
522 if (check_next1(ls, '/')) return TK_IDIV;
523 else return '/';
524 }
525 case '~': {
526 next(ls);
527 if (check_next1(ls, '=')) return TK_NE;
528 else return '~';
529 }
530 case ':': {
531 next(ls);
532 if (check_next1(ls, ':')) return TK_DBCOLON;
533 else return ':';
534 }
535 case '"': case '\'': { /* short literal strings */
536 read_string(ls, ls->current, seminfo);
537 return TK_STRING;
538 }
539 case '.': { /* '.', '..', '...', or number */
540 save_and_next(ls);
541 if (check_next1(ls, '.')) {
542 if (check_next1(ls, '.'))
543 return TK_DOTS; /* '...' */
544 else return TK_CONCAT; /* '..' */
545 }
546 else if (!lisdigit(ls->current)) return '.';
547 else return read_numeral(ls, seminfo);
548 }
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);
552 }
553 case EOZ: {
554 return TK_EOS;
555 }
556 default: {
557 if (lislalpha(ls->current)) { /* identifier or reserved word? */
558 TString *ts;
559 do {
560 save_and_next(ls);
561 } while (lislalnum(ls->current));
562 ts = luaX_newstring(ls, luaZ_buffer(ls->buff),
563 luaZ_bufflen(ls->buff));
564 seminfo->ts = ts;
565 if (isreserved(ts)) /* reserved word? */
566 return ts->extra - 1 + FIRST_RESERVED;
567 else {
568 return TK_NAME;
569 }
570 }
571 else { /* single-char tokens (+ - / ...) */
572 int c = ls->current;
573 next(ls);
574 return c;
575 }
576 }
577 }
578 }
579 }
580
581
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 */
587 }
588 else
589 ls->t.token = llex(ls, &ls->t.seminfo); /* read next token */
590 }
591
592
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;
597 }
598