Coverage Report

Created: 2023-09-15 06:19

/src/testdir/build/lua-master/source/llex.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
** $Id: llex.c $
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
6.59M
#define next(ls)  (ls->current = zgetc(ls->z))
33
34
35
36
674k
#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
4.73M
#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
4.89M
static void save (LexState *ls, int c) {
58
4.89M
  Mbuffer *b = ls->buff;
59
4.89M
  if (luaZ_bufflen(b) + 1 > luaZ_sizebuffer(b)) {
60
0
    size_t newsize;
61
0
    if (luaZ_sizebuffer(b) >= MAX_SIZE/2)
62
0
      lexerror(ls, "lexical element too long", 0);
63
0
    newsize = luaZ_sizebuffer(b) * 2;
64
0
    luaZ_resizebuffer(ls->L, b, newsize);
65
0
  }
66
4.89M
  b->buffer[luaZ_bufflen(b)++] = cast_char(c);
67
4.89M
}
68
69
70
952
void luaX_init (lua_State *L) {
71
952
  int i;
72
952
  TString *e = luaS_newliteral(L, LUA_ENV);  /* create env name */
73
952
  luaC_fix(L, obj2gco(e));  /* never collect this name */
74
21.8k
  for (i=0; i<NUM_RESERVED; i++) {
75
20.9k
    TString *ts = luaS_new(L, luaX_tokens[i]);
76
20.9k
    luaC_fix(L, obj2gco(ts));  /* reserved words are never collected */
77
20.9k
    ts->extra = cast_byte(i+1);  /* reserved word */
78
20.9k
  }
79
952
}
80
81
82
36
const char *luaX_token2str (LexState *ls, int token) {
83
36
  if (token < FIRST_RESERVED) {  /* single-byte symbols? */
84
11
    if (lisprint(token))
85
11
      return luaO_pushfstring(ls->L, "'%c'", token);
86
0
    else  /* control character */
87
0
      return luaO_pushfstring(ls->L, "'<\\%d>'", token);
88
11
  }
89
25
  else {
90
25
    const char *s = luaX_tokens[token - FIRST_RESERVED];
91
25
    if (token < TK_EOS)  /* fixed format (symbols and reserved words)? */
92
22
      return luaO_pushfstring(ls->L, "'%s'", s);
93
3
    else  /* names, strings, and numerals */
94
3
      return s;
95
25
  }
96
36
}
97
98
99
34
static const char *txtToken (LexState *ls, int token) {
100
34
  switch (token) {
101
0
    case TK_NAME: case TK_STRING:
102
0
    case TK_FLT: case TK_INT:
103
0
      save(ls, '\0');
104
0
      return luaO_pushfstring(ls->L, "'%s'", luaZ_buffer(ls->buff));
105
34
    default:
106
34
      return luaX_token2str(ls, token);
107
34
  }
108
34
}
109
110
111
34
static l_noret lexerror (LexState *ls, const char *msg, int token) {
112
34
  msg = luaG_addinfo(ls->L, msg, ls->source, ls->linenumber);
113
34
  if (token)
114
34
    luaO_pushfstring(ls->L, "%s near %s", msg, txtToken(ls, token));
115
34
  luaD_throw(ls->L, LUA_ERRSYNTAX);
116
34
}
117
118
119
34
l_noret luaX_syntaxerror (LexState *ls, const char *msg) {
120
34
  lexerror(ls, msg, ls->t.token);
121
34
}
122
123
124
/*
125
** Creates a new string and anchors it in scanner's table so that it
126
** will not be collected until the end of the compilation; by that time
127
** it should be anchored somewhere. It also internalizes long strings,
128
** ensuring there is only one copy of each unique string.  The table
129
** here is used as a set: the string enters as the key, while its value
130
** is irrelevant. We use the string itself as the value only because it
131
** is a TValue readily available. Later, the code generation can change
132
** this value.
133
*/
134
876k
TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
135
876k
  lua_State *L = ls->L;
136
876k
  TString *ts = luaS_newlstr(L, str, l);  /* create new string */
137
876k
  const TValue *o = luaH_getstr(ls->h, ts);
138
876k
  if (!ttisnil(o))  /* string already present? */
139
807k
    ts = keystrval(nodefromval(o));  /* get saved copy */
140
68.9k
  else {  /* not in use yet */
141
68.9k
    TValue *stv = s2v(L->top.p++);  /* reserve stack space for string */
142
68.9k
    setsvalue(L, stv, ts);  /* temporarily anchor the string */
143
68.9k
    luaH_finishset(L, ls->h, stv, o, stv);  /* t[string] = string */
144
    /* table is not a metatable, so it does not need to invalidate cache */
145
68.9k
    luaC_checkGC(L);
146
68.9k
    L->top.p--;  /* remove string from stack */
147
68.9k
  }
148
876k
  return ts;
149
876k
}
150
151
152
/*
153
** increment line number and skips newline sequence (any of
154
** \n, \r, \n\r, or \r\n)
155
*/
156
337k
static void inclinenumber (LexState *ls) {
157
337k
  int old = ls->current;
158
337k
  lua_assert(currIsNewline(ls));
159
337k
  next(ls);  /* skip '\n' or '\r' */
160
337k
  if (currIsNewline(ls) && ls->current != old)
161
0
    next(ls);  /* skip '\n\r' or '\r\n' */
162
337k
  if (++ls->linenumber >= MAX_INT)
163
0
    lexerror(ls, "chunk has too many lines", 0);
164
337k
}
165
166
167
void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source,
168
952
                    int firstchar) {
169
952
  ls->t.token = 0;
170
952
  ls->L = L;
171
952
  ls->current = firstchar;
172
952
  ls->lookahead.token = TK_EOS;  /* no look-ahead token */
173
952
  ls->z = z;
174
952
  ls->fs = NULL;
175
952
  ls->linenumber = 1;
176
952
  ls->lastline = 1;
177
952
  ls->source = source;
178
952
  ls->envn = luaS_newliteral(L, LUA_ENV);  /* get env name */
179
952
  luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER);  /* initialize buffer */
180
952
}
181
182
183
184
/*
185
** =======================================================
186
** LEXICAL ANALYZER
187
** =======================================================
188
*/
189
190
191
259k
static int check_next1 (LexState *ls, int c) {
192
259k
  if (ls->current == c) {
193
13.5k
    next(ls);
194
13.5k
    return 1;
195
13.5k
  }
196
245k
  else return 0;
197
259k
}
198
199
200
/*
201
** Check whether current char is in set 'set' (with two chars) and
202
** saves it
203
*/
204
311k
static int check_next2 (LexState *ls, const char *set) {
205
311k
  lua_assert(set[2] == '\0');
206
311k
  if (ls->current == set[0] || ls->current == set[1]) {
207
0
    save_and_next(ls);
208
0
    return 1;
209
0
  }
210
311k
  else return 0;
211
311k
}
212
213
214
/* LUA_NUMBER */
215
/*
216
** This function is quite liberal in what it accepts, as 'luaO_str2num'
217
** will reject ill-formed numerals. Roughly, it accepts the following
218
** pattern:
219
**
220
**   %d(%x|%.|([Ee][+-]?))* | 0[Xx](%x|%.|([Pp][+-]?))*
221
**
222
** The only tricky part is to accept [+-] only after a valid exponent
223
** mark, to avoid reading '3-4' or '0xe+1' as a single number.
224
**
225
** The caller might have already read an initial dot.
226
*/
227
166k
static int read_numeral (LexState *ls, SemInfo *seminfo) {
228
166k
  TValue obj;
229
166k
  const char *expo = "Ee";
230
166k
  int first = ls->current;
231
166k
  lua_assert(lisdigit(ls->current));
232
166k
  save_and_next(ls);
233
166k
  if (first == '0' && check_next2(ls, "xX"))  /* hexadecimal? */
234
0
    expo = "Pp";
235
253k
  for (;;) {
236
253k
    if (check_next2(ls, expo))  /* exponent mark? */
237
0
      check_next2(ls, "-+");  /* optional exponent sign */
238
253k
    else if (lisxdigit(ls->current) || ls->current == '.')  /* '%x|%.' */
239
87.2k
      save_and_next(ls);
240
166k
    else break;
241
253k
  }
242
166k
  if (lislalpha(ls->current))  /* is numeral touching a letter? */
243
0
    save_and_next(ls);  /* force an error */
244
166k
  save(ls, '\0');
245
166k
  if (luaO_str2num(luaZ_buffer(ls->buff), &obj) == 0)  /* format error? */
246
0
    lexerror(ls, "malformed number", TK_FLT);
247
166k
  if (ttisinteger(&obj)) {
248
155k
    seminfo->i = ivalue(&obj);
249
155k
    return TK_INT;
250
155k
  }
251
10.7k
  else {
252
10.7k
    lua_assert(ttisfloat(&obj));
253
10.7k
    seminfo->r = fltvalue(&obj);
254
10.7k
    return TK_FLT;
255
10.7k
  }
256
166k
}
257
258
259
/*
260
** read a sequence '[=*[' or ']=*]', leaving the last bracket. If
261
** sequence is well formed, return its number of '='s + 2; otherwise,
262
** return 1 if it is a single bracket (no '='s and no 2nd bracket);
263
** otherwise (an unfinished '[==...') return 0.
264
*/
265
5.45k
static size_t skip_sep (LexState *ls) {
266
5.45k
  size_t count = 0;
267
5.45k
  int s = ls->current;
268
5.45k
  lua_assert(s == '[' || s == ']');
269
5.45k
  save_and_next(ls);
270
5.45k
  while (ls->current == '=') {
271
0
    save_and_next(ls);
272
0
    count++;
273
0
  }
274
5.45k
  return (ls->current == s) ? count + 2
275
5.45k
         : (count == 0) ? 1
276
5.45k
         : 0;
277
5.45k
}
278
279
280
0
static void read_long_string (LexState *ls, SemInfo *seminfo, size_t sep) {
281
0
  int line = ls->linenumber;  /* initial line (for error message) */
282
0
  save_and_next(ls);  /* skip 2nd '[' */
283
0
  if (currIsNewline(ls))  /* string starts with a newline? */
284
0
    inclinenumber(ls);  /* skip it */
285
0
  for (;;) {
286
0
    switch (ls->current) {
287
0
      case EOZ: {  /* error */
288
0
        const char *what = (seminfo ? "string" : "comment");
289
0
        const char *msg = luaO_pushfstring(ls->L,
290
0
                     "unfinished long %s (starting at line %d)", what, line);
291
0
        lexerror(ls, msg, TK_EOS);
292
0
        break;  /* to avoid warnings */
293
0
      }
294
0
      case ']': {
295
0
        if (skip_sep(ls) == sep) {
296
0
          save_and_next(ls);  /* skip 2nd ']' */
297
0
          goto endloop;
298
0
        }
299
0
        break;
300
0
      }
301
0
      case '\n': case '\r': {
302
0
        save(ls, '\n');
303
0
        inclinenumber(ls);
304
0
        if (!seminfo) luaZ_resetbuffer(ls->buff);  /* avoid wasting space */
305
0
        break;
306
0
      }
307
0
      default: {
308
0
        if (seminfo) save_and_next(ls);
309
0
        else next(ls);
310
0
      }
311
0
    }
312
0
  } endloop:
313
0
  if (seminfo)
314
0
    seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + sep,
315
0
                                     luaZ_bufflen(ls->buff) - 2 * sep);
316
0
}
317
318
319
0
static void esccheck (LexState *ls, int c, const char *msg) {
320
0
  if (!c) {
321
0
    if (ls->current != EOZ)
322
0
      save_and_next(ls);  /* add current to buffer for error message */
323
0
    lexerror(ls, msg, TK_STRING);
324
0
  }
325
0
}
326
327
328
0
static int gethexa (LexState *ls) {
329
0
  save_and_next(ls);
330
0
  esccheck (ls, lisxdigit(ls->current), "hexadecimal digit expected");
331
0
  return luaO_hexavalue(ls->current);
332
0
}
333
334
335
0
static int readhexaesc (LexState *ls) {
336
0
  int r = gethexa(ls);
337
0
  r = (r << 4) + gethexa(ls);
338
0
  luaZ_buffremove(ls->buff, 2);  /* remove saved chars from buffer */
339
0
  return r;
340
0
}
341
342
343
0
static unsigned long readutf8esc (LexState *ls) {
344
0
  unsigned long r;
345
0
  int i = 4;  /* chars to be removed: '\', 'u', '{', and first digit */
346
0
  save_and_next(ls);  /* skip 'u' */
347
0
  esccheck(ls, ls->current == '{', "missing '{'");
348
0
  r = gethexa(ls);  /* must have at least one digit */
349
0
  while (cast_void(save_and_next(ls)), lisxdigit(ls->current)) {
350
0
    i++;
351
0
    esccheck(ls, r <= (0x7FFFFFFFu >> 4), "UTF-8 value too large");
352
0
    r = (r << 4) + luaO_hexavalue(ls->current);
353
0
  }
354
0
  esccheck(ls, ls->current == '}', "missing '}'");
355
0
  next(ls);  /* skip '}' */
356
0
  luaZ_buffremove(ls->buff, i);  /* remove saved chars from buffer */
357
0
  return r;
358
0
}
359
360
361
0
static void utf8esc (LexState *ls) {
362
0
  char buff[UTF8BUFFSZ];
363
0
  int n = luaO_utf8esc(buff, readutf8esc(ls));
364
0
  for (; n > 0; n--)  /* add 'buff' to string */
365
0
    save(ls, buff[UTF8BUFFSZ - n]);
366
0
}
367
368
369
0
static int readdecesc (LexState *ls) {
370
0
  int i;
371
0
  int r = 0;  /* result accumulator */
372
0
  for (i = 0; i < 3 && lisdigit(ls->current); i++) {  /* read up to 3 digits */
373
0
    r = 10*r + ls->current - '0';
374
0
    save_and_next(ls);
375
0
  }
376
0
  esccheck(ls, r <= UCHAR_MAX, "decimal escape too large");
377
0
  luaZ_buffremove(ls->buff, i);  /* remove read digits from buffer */
378
0
  return r;
379
0
}
380
381
382
67.9k
static void read_string (LexState *ls, int del, SemInfo *seminfo) {
383
67.9k
  save_and_next(ls);  /* keep delimiter (for error messages) */
384
352k
  while (ls->current != del) {
385
284k
    switch (ls->current) {
386
0
      case EOZ:
387
0
        lexerror(ls, "unfinished string", TK_EOS);
388
0
        break;  /* to avoid warnings */
389
0
      case '\n':
390
0
      case '\r':
391
0
        lexerror(ls, "unfinished string", TK_STRING);
392
0
        break;  /* to avoid warnings */
393
0
      case '\\': {  /* escape sequences */
394
0
        int c;  /* final character to be saved */
395
0
        save_and_next(ls);  /* keep '\\' for error messages */
396
0
        switch (ls->current) {
397
0
          case 'a': c = '\a'; goto read_save;
398
0
          case 'b': c = '\b'; goto read_save;
399
0
          case 'f': c = '\f'; goto read_save;
400
0
          case 'n': c = '\n'; goto read_save;
401
0
          case 'r': c = '\r'; goto read_save;
402
0
          case 't': c = '\t'; goto read_save;
403
0
          case 'v': c = '\v'; goto read_save;
404
0
          case 'x': c = readhexaesc(ls); goto read_save;
405
0
          case 'u': utf8esc(ls);  goto no_save;
406
0
          case '\n': case '\r':
407
0
            inclinenumber(ls); c = '\n'; goto only_save;
408
0
          case '\\': case '\"': case '\'':
409
0
            c = ls->current; goto read_save;
410
0
          case EOZ: goto no_save;  /* will raise an error next loop */
411
0
          case 'z': {  /* zap following span of spaces */
412
0
            luaZ_buffremove(ls->buff, 1);  /* remove '\\' */
413
0
            next(ls);  /* skip the 'z' */
414
0
            while (lisspace(ls->current)) {
415
0
              if (currIsNewline(ls)) inclinenumber(ls);
416
0
              else next(ls);
417
0
            }
418
0
            goto no_save;
419
0
          }
420
0
          default: {
421
0
            esccheck(ls, lisdigit(ls->current), "invalid escape sequence");
422
0
            c = readdecesc(ls);  /* digital escape '\ddd' */
423
0
            goto only_save;
424
0
          }
425
0
        }
426
0
       read_save:
427
0
         next(ls);
428
         /* go through */
429
0
       only_save:
430
0
         luaZ_buffremove(ls->buff, 1);  /* remove '\\' */
431
0
         save(ls, c);
432
         /* go through */
433
0
       no_save: break;
434
0
      }
435
284k
      default:
436
284k
        save_and_next(ls);
437
284k
    }
438
284k
  }
439
67.9k
  save_and_next(ls);  /* skip delimiter */
440
67.9k
  seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + 1,
441
67.9k
                                   luaZ_bufflen(ls->buff) - 2);
442
67.9k
}
443
444
445
1.37M
static int llex (LexState *ls, SemInfo *seminfo) {
446
1.37M
  luaZ_resetbuffer(ls->buff);
447
2.79M
  for (;;) {
448
2.79M
    switch (ls->current) {
449
337k
      case '\n': case '\r': {  /* line breaks */
450
337k
        inclinenumber(ls);
451
337k
        break;
452
337k
      }
453
1.08M
      case ' ': case '\f': case '\t': case '\v': {  /* spaces */
454
1.08M
        next(ls);
455
1.08M
        break;
456
1.08M
      }
457
7.06k
      case '-': {  /* '-' or '--' (comment) */
458
7.06k
        next(ls);
459
7.06k
        if (ls->current != '-') return '-';
460
        /* else is a comment */
461
20
        next(ls);
462
20
        if (ls->current == '[') {  /* long comment? */
463
0
          size_t sep = skip_sep(ls);
464
0
          luaZ_resetbuffer(ls->buff);  /* 'skip_sep' may dirty the buffer */
465
0
          if (sep >= 2) {
466
0
            read_long_string(ls, NULL, sep);  /* skip long comment */
467
0
            luaZ_resetbuffer(ls->buff);  /* previous call may dirty the buff. */
468
0
            break;
469
0
          }
470
0
        }
471
        /* else short comment */
472
352
        while (!currIsNewline(ls) && ls->current != EOZ)
473
332
          next(ls);  /* skip until end of line (or end of file) */
474
20
        break;
475
20
      }
476
5.45k
      case '[': {  /* long string or simply '[' */
477
5.45k
        size_t sep = skip_sep(ls);
478
5.45k
        if (sep >= 2) {
479
0
          read_long_string(ls, seminfo, sep);
480
0
          return TK_STRING;
481
0
        }
482
5.45k
        else if (sep == 0)  /* '[=...' missing second bracket? */
483
0
          lexerror(ls, "invalid long string delimiter", TK_STRING);
484
5.45k
        return '[';
485
5.45k
      }
486
129k
      case '=': {
487
129k
        next(ls);
488
129k
        if (check_next1(ls, '=')) return TK_EQ;  /* '==' */
489
126k
        else return '=';
490
129k
      }
491
1.22k
      case '<': {
492
1.22k
        next(ls);
493
1.22k
        if (check_next1(ls, '=')) return TK_LE;  /* '<=' */
494
643
        else if (check_next1(ls, '<')) return TK_SHL;  /* '<<' */
495
643
        else return '<';
496
1.22k
      }
497
52.6k
      case '>': {
498
52.6k
        next(ls);
499
52.6k
        if (check_next1(ls, '=')) return TK_GE;  /* '>=' */
500
52.2k
        else if (check_next1(ls, '>')) return TK_SHR;  /* '>>' */
501
52.2k
        else return '>';
502
52.6k
      }
503
460
      case '/': {
504
460
        next(ls);
505
460
        if (check_next1(ls, '/')) return TK_IDIV;  /* '//' */
506
460
        else return '/';
507
460
      }
508
467
      case '~': {
509
467
        next(ls);
510
467
        if (check_next1(ls, '=')) return TK_NE;  /* '~=' */
511
0
        else return '~';
512
467
      }
513
7.86k
      case ':': {
514
7.86k
        next(ls);
515
7.86k
        if (check_next1(ls, ':')) return TK_DBCOLON;  /* '::' */
516
7.86k
        else return ':';
517
7.86k
      }
518
67.9k
      case '"': case '\'': {  /* short literal strings */
519
67.9k
        read_string(ls, ls->current, seminfo);
520
67.9k
        return TK_STRING;
521
0
      }
522
9.06k
      case '.': {  /* '.', '..', '...', or number */
523
9.06k
        save_and_next(ls);
524
9.06k
        if (check_next1(ls, '.')) {
525
4.85k
          if (check_next1(ls, '.'))
526
4.02k
            return TK_DOTS;   /* '...' */
527
824
          else return TK_CONCAT;   /* '..' */
528
4.85k
        }
529
4.21k
        else if (!lisdigit(ls->current)) return '.';
530
0
        else return read_numeral(ls, seminfo);
531
9.06k
      }
532
114k
      case '0': case '1': case '2': case '3': case '4':
533
166k
      case '5': case '6': case '7': case '8': case '9': {
534
166k
        return read_numeral(ls, seminfo);
535
166k
      }
536
918
      case EOZ: {
537
918
        return TK_EOS;
538
166k
      }
539
923k
      default: {
540
923k
        if (lislalpha(ls->current)) {  /* identifier or reserved word? */
541
705k
          TString *ts;
542
4.04M
          do {
543
4.04M
            save_and_next(ls);
544
4.04M
          } while (lislalnum(ls->current));
545
705k
          ts = luaX_newstring(ls, luaZ_buffer(ls->buff),
546
705k
                                  luaZ_bufflen(ls->buff));
547
705k
          seminfo->ts = ts;
548
705k
          if (isreserved(ts))  /* reserved word? */
549
401k
            return ts->extra - 1 + FIRST_RESERVED;
550
304k
          else {
551
304k
            return TK_NAME;
552
304k
          }
553
705k
        }
554
218k
        else {  /* single-char tokens ('+', '*', '%', '{', '}', ...) */
555
218k
          int c = ls->current;
556
218k
          next(ls);
557
218k
          return c;
558
218k
        }
559
923k
      }
560
2.79M
    }
561
2.79M
  }
562
1.37M
}
563
564
565
1.37M
void luaX_next (LexState *ls) {
566
1.37M
  ls->lastline = ls->linenumber;
567
1.37M
  if (ls->lookahead.token != TK_EOS) {  /* is there a look-ahead token? */
568
3.57k
    ls->t = ls->lookahead;  /* use this one */
569
3.57k
    ls->lookahead.token = TK_EOS;  /* and discharge it */
570
3.57k
  }
571
1.36M
  else
572
1.36M
    ls->t.token = llex(ls, &ls->t.seminfo);  /* read next token */
573
1.37M
}
574
575
576
3.57k
int luaX_lookahead (LexState *ls) {
577
3.57k
  lua_assert(ls->lookahead.token == TK_EOS);
578
3.57k
  ls->lookahead.token = llex(ls, &ls->lookahead.seminfo);
579
3.57k
  return ls->lookahead.token;
580
3.57k
}
581