style
[RRG-proxmark3.git] / client / deps / liblua / llex.c
blobbde26fa4f85d2ea55cfcbbda57e17a4da3318e20
1 /*
2 ** $Id: llex.c,v 2.63 2013/03/16 21:10:18 roberto Exp $
3 ** Lexical Analyzer
4 ** See Copyright Notice in lua.h
5 */
8 #include <locale.h>
9 #include <string.h>
11 #define llex_c
12 #define LUA_CORE
14 #include "lua.h"
16 #include "lctype.h"
17 #include "ldo.h"
18 #include "llex.h"
19 #include "lobject.h"
20 #include "lparser.h"
21 #include "lstate.h"
22 #include "lstring.h"
23 #include "ltable.h"
24 #include "lzio.h"
28 #define next(ls) (ls->current = zgetc(ls->z))
32 #define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r')
35 /* ORDER RESERVED */
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)) {
55 size_t newsize;
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) {
66 int i;
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);
80 } else {
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 */
85 return s;
90 static const char *txtToken(LexState *ls, int token) {
91 switch (token) {
92 case TK_NAME:
93 case TK_STRING:
94 case TK_NUMBER:
95 save(ls, '\0');
96 return luaO_pushfstring(ls->L, LUA_QS, luaZ_buffer(ls->buff));
97 default:
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);
107 if (token)
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 */
133 luaC_checkGC(L);
135 L->top--; /* remove string from stack */
136 return ts;
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,
156 int firstchar) {
157 ls->decpoint = '.';
158 ls->L = L;
159 ls->current = firstchar;
160 ls->lookahead.token = TK_EOS; /* no look-ahead token */
161 ls->z = z;
162 ls->fs = NULL;
163 ls->linenumber = 1;
164 ls->lastline = 1;
165 ls->source = source;
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 ** =======================================================
175 ** LEXICAL ANALYZER
176 ** =======================================================
181 static int check_next(LexState *ls, const char *set) {
182 if (ls->current == '\0' || !strchr(set, ls->current))
183 return 0;
184 save_and_next(ls);
185 return 1;
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);
195 while (n--)
196 if (p[n] == from) p[n] = to;
200 #if defined(ANDROID)
201 #define getlocaldecpoint() '.'
202 #elif !defined(getlocaledecpoint)
203 #define getlocaledecpoint() (localeconv()->decimal_point[0])
204 #endif
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);
225 /* LUA_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));
234 save_and_next(ls);
235 if (first == '0' && check_next(ls, "Xx")) /* hexadecimal? */
236 expo = "Pp";
237 for (;;) {
238 if (check_next(ls, expo)) /* exponent part? */
239 check_next(ls, "+-"); /* optional exponent sign */
240 if (lisxdigit(ls->current) || ls->current == '.')
241 save_and_next(ls);
242 else break;
244 save(ls, '\0');
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) {
256 int count = 0;
257 int s = ls->current;
258 lua_assert(s == '[' || s == ']');
259 save_and_next(ls);
260 while (ls->current == '=') {
261 save_and_next(ls);
262 count++;
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 */
272 for (;;) {
273 switch (ls->current) {
274 case EOZ:
275 lexerror(ls, (seminfo) ? "unfinished long string" :
276 "unfinished long comment", TK_EOS);
277 break; /* to avoid warnings */
278 case ']': {
279 if (skip_sep(ls) == sep) {
280 save_and_next(ls); /* skip 2nd `]' */
281 goto endloop;
283 break;
285 case '\n':
286 case '\r': {
287 save(ls, '\n');
288 inclinenumber(ls);
289 if (!seminfo) luaZ_resetbuffer(ls->buff); /* avoid wasting space */
290 break;
292 default: {
293 if (seminfo) save_and_next(ls);
294 else next(ls);
298 endloop:
299 if (seminfo)
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) {
306 int i;
307 luaZ_resetbuffer(ls->buff); /* prepare error message */
308 save(ls, '\\');
309 for (i = 0; i < n && c[i] != EOZ; i++)
310 save(ls, c[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 */
320 c[i] = next(ls);
321 if (!lisxdigit(c[i]))
322 escerror(ls, c, i + 1, "hexadecimal digit expected");
323 r = (r << 4) + luaO_hexavalue(c[i]);
325 return r;
329 static int readdecesc(LexState *ls) {
330 int c[3], i;
331 int r = 0; /* result accumulator */
332 for (i = 0; i < 3 && lisdigit(ls->current); i++) { /* read up to 3 digits */
333 c[i] = ls->current;
334 r = 10 * r + c[i] - '0';
335 next(ls);
337 if (r > UCHAR_MAX)
338 escerror(ls, c, i, "decimal escape too large");
339 return r;
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) {
347 case EOZ:
348 lexerror(ls, "unfinished string", TK_EOS);
349 break; /* to avoid warnings */
350 case '\n':
351 case '\r':
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) {
358 case 'a':
359 c = '\a';
360 goto read_save;
361 case 'b':
362 c = '\b';
363 goto read_save;
364 case 'f':
365 c = '\f';
366 goto read_save;
367 case 'n':
368 c = '\n';
369 goto read_save;
370 case 'r':
371 c = '\r';
372 goto read_save;
373 case 't':
374 c = '\t';
375 goto read_save;
376 case 'v':
377 c = '\v';
378 goto read_save;
379 case 'x':
380 c = readhexaesc(ls);
381 goto read_save;
382 case '\n':
383 case '\r':
384 inclinenumber(ls);
385 c = '\n';
386 goto only_save;
387 case '\\':
388 case '\"':
389 case '\'':
390 c = ls->current;
391 goto read_save;
392 case EOZ:
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);
398 else next(ls);
400 goto no_save;
402 default: {
403 if (!lisdigit(ls->current))
404 escerror(ls, &ls->current, 1, "invalid escape sequence");
405 /* digital escape \ddd */
406 c = readdecesc(ls);
407 goto only_save;
410 read_save:
411 next(ls); /* read next character */
412 only_save:
413 save(ls, c); /* save 'c' */
414 no_save:
415 break;
417 default:
418 save_and_next(ls);
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);
429 for (;;) {
430 switch (ls->current) {
431 case '\n':
432 case '\r': { /* line breaks */
433 inclinenumber(ls);
434 break;
436 case ' ':
437 case '\f':
438 case '\t':
439 case '\v': { /* spaces */
440 next(ls);
441 break;
443 case '-': { /* '-' or '--' (comment) */
444 next(ls);
445 if (ls->current != '-') return '-';
446 /* else is a comment */
447 next(ls);
448 if (ls->current == '[') { /* long comment? */
449 int sep = skip_sep(ls);
450 luaZ_resetbuffer(ls->buff); /* `skip_sep' may dirty the buffer */
451 if (sep >= 0) {
452 read_long_string(ls, NULL, sep); /* skip long comment */
453 luaZ_resetbuffer(ls->buff); /* previous call may dirty the buff. */
454 break;
457 /* else short comment */
458 while (!currIsNewline(ls) && ls->current != EOZ)
459 next(ls); /* skip until end of line (or end of file) */
460 break;
462 case '[': { /* long string or simply '[' */
463 int sep = skip_sep(ls);
464 if (sep >= 0) {
465 read_long_string(ls, seminfo, sep);
466 return TK_STRING;
467 } else if (sep == -1) return '[';
468 else lexerror(ls, "invalid long string delimiter", TK_STRING);
470 case '=': {
471 next(ls);
472 if (ls->current != '=') return '=';
473 else { next(ls); return TK_EQ; }
475 case '<': {
476 next(ls);
477 if (ls->current != '=') return '<';
478 else { next(ls); return TK_LE; }
480 case '>': {
481 next(ls);
482 if (ls->current != '=') return '>';
483 else { next(ls); return TK_GE; }
485 case '~': {
486 next(ls);
487 if (ls->current != '=') return '~';
488 else { next(ls); return TK_NE; }
490 case ':': {
491 next(ls);
492 if (ls->current != ':') return ':';
493 else { next(ls); return TK_DBCOLON; }
495 case '"':
496 case '\'': { /* short literal strings */
497 read_string(ls, ls->current, seminfo);
498 return TK_STRING;
500 case '.': { /* '.', '..', '...', or number */
501 save_and_next(ls);
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 */
509 case '0':
510 case '1':
511 case '2':
512 case '3':
513 case '4':
514 case '5':
515 case '6':
516 case '7':
517 case '8':
518 case '9': {
519 read_numeral(ls, seminfo);
520 return TK_NUMBER;
522 case EOZ: {
523 return TK_EOS;
525 default: {
526 if (lislalpha(ls->current)) { /* identifier or reserved word? */
527 TString *ts;
528 do {
529 save_and_next(ls);
530 } while (lislalnum(ls->current));
531 ts = luaX_newstring(ls, luaZ_buffer(ls->buff),
532 luaZ_bufflen(ls->buff));
533 seminfo->ts = ts;
534 if (isreserved(ts)) /* reserved word? */
535 return ts->tsv.extra - 1 + FIRST_RESERVED;
536 else {
537 return TK_NAME;
539 } else { /* single-char tokens (+ - / ...) */
540 int c = ls->current;
541 next(ls);
542 return c;
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 */
555 } else
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;