| Line | Branch | Decision | Exec | Source |
|---|---|---|---|---|
| 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 | #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 | 40309 | static void save (LexState *ls, int c) { | ||
| 58 | 40309 | Mbuffer *b = ls->buff; | ||
| 59 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 40309 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 40309 times.
|
40309 | 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 | 40309 | b->buffer[luaZ_bufflen(b)++] = cast_char(c); | ||
| 67 | 40309 | } | ||
| 68 | ||||
| 69 | ||||
| 70 | 297 | void luaX_init (lua_State *L) { | ||
| 71 | int i; | |||
| 72 | 297 | TString *e = luaS_newliteral(L, LUA_ENV); /* create env name */ | ||
| 73 | 297 | luaC_fix(L, obj2gco(e)); /* never collect this name */ | ||
| 74 |
2/2✓ Branch 0 taken 6534 times.
✓ Branch 1 taken 297 times.
|
2/2✓ Decision 'true' taken 6534 times.
✓ Decision 'false' taken 297 times.
|
6831 | for (i=0; i<NUM_RESERVED; i++) { |
| 75 | 6534 | TString *ts = luaS_new(L, luaX_tokens[i]); | ||
| 76 | 6534 | luaC_fix(L, obj2gco(ts)); /* reserved words are never collected */ | ||
| 77 | 6534 | ts->extra = cast_byte(i+1); /* reserved word */ | ||
| 78 | } | |||
| 79 | 297 | } | ||
| 80 | ||||
| 81 | ||||
| 82 | 1 | const char *luaX_token2str (LexState *ls, int token) { | ||
| 83 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 1 time.
|
1 | if (token < FIRST_RESERVED) { /* single-byte symbols? */ |
| 84 | ✗ | if (lisprint(token)) | ||
| 85 | ✗ | return luaO_pushfstring(ls->L, "'%c'", token); | ||
| 86 | else /* control character */ | |||
| 87 | ✗ | return luaO_pushfstring(ls->L, "'<\\%d>'", token); | ||
| 88 | } | |||
| 89 | else { | |||
| 90 | 1 | const char *s = luaX_tokens[token - FIRST_RESERVED]; | ||
| 91 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 1 time.
|
1 | if (token < TK_EOS) /* fixed format (symbols and reserved words)? */ |
| 92 | ✗ | return luaO_pushfstring(ls->L, "'%s'", s); | ||
| 93 | else /* names, strings, and numerals */ | |||
| 94 | 1 | return s; | ||
| 95 | } | |||
| 96 | } | |||
| 97 | ||||
| 98 | ||||
| 99 | 1 | static const char *txtToken (LexState *ls, int token) { | ||
| 100 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
|
1 | switch (token) { | |
| 101 | ✗ | case TK_NAME: case TK_STRING: | ||
| 102 | case TK_FLT: case TK_INT: | |||
| 103 | ✗ | save(ls, '\0'); | ||
| 104 | ✗ | return luaO_pushfstring(ls->L, "'%s'", luaZ_buffer(ls->buff)); | ||
| 105 |
1/1✓ Decision 'true' taken 1 time.
|
1 | default: | |
| 106 | 1 | return luaX_token2str(ls, token); | ||
| 107 | } | |||
| 108 | } | |||
| 109 | ||||
| 110 | ||||
| 111 | 1 | static l_noret lexerror (LexState *ls, const char *msg, int token) { | ||
| 112 | 1 | msg = luaG_addinfo(ls->L, msg, ls->source, ls->linenumber); | ||
| 113 |
1/2✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1/2✓ Decision 'true' taken 1 time.
✗ Decision 'false' not taken.
|
1 | if (token) |
| 114 | 1 | luaO_pushfstring(ls->L, "%s near %s", msg, txtToken(ls, token)); | ||
| 115 | 1 | luaD_throw(ls->L, LUA_ERRSYNTAX); | ||
| 116 | } | |||
| 117 | ||||
| 118 | ||||
| 119 | 1 | l_noret luaX_syntaxerror (LexState *ls, const char *msg) { | ||
| 120 | 1 | lexerror(ls, msg, ls->t.token); | ||
| 121 | } | |||
| 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 | 5204 | TString *luaX_newstring (LexState *ls, const char *str, size_t l) { | ||
| 135 | 5204 | lua_State *L = ls->L; | ||
| 136 | 5204 | TString *ts = luaS_newlstr(L, str, l); /* create new string */ | ||
| 137 | 5204 | const TValue *o = luaH_getstr(ls->h, ts); | ||
| 138 |
2/2✓ Branch 0 taken 3224 times.
✓ Branch 1 taken 1980 times.
|
2/2✓ Decision 'true' taken 3224 times.
✓ Decision 'false' taken 1980 times.
|
5204 | if (!ttisnil(o)) /* string already present? */ |
| 139 | 3224 | ts = keystrval(nodefromval(o)); /* get saved copy */ | ||
| 140 | else { /* not in use yet */ | |||
| 141 | 1980 | TValue *stv = s2v(L->top.p++); /* reserve stack space for string */ | ||
| 142 | 1980 | setsvalue(L, stv, ts); /* temporarily anchor the string */ | ||
| 143 | 1980 | 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 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1980 times.
|
1980 | luaC_checkGC(L); | |
| 146 | 1980 | L->top.p--; /* remove string from stack */ | ||
| 147 | } | |||
| 148 | 5204 | return ts; | ||
| 149 | } | |||
| 150 | ||||
| 151 | ||||
| 152 | /* | |||
| 153 | ** increment line number and skips newline sequence (any of | |||
| 154 | ** \n, \r, \n\r, or \r\n) | |||
| 155 | */ | |||
| 156 | 1108 | static void inclinenumber (LexState *ls) { | ||
| 157 | 1108 | int old = ls->current; | ||
| 158 | lua_assert(currIsNewline(ls)); | |||
| 159 |
2/2✓ Branch 0 taken 1103 times.
✓ Branch 1 taken 5 times.
|
1108 | next(ls); /* skip '\n' or '\r' */ | |
| 160 |
4/6✓ Branch 0 taken 936 times.
✓ Branch 1 taken 172 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 936 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 172 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 1108 times.
|
1108 | if (currIsNewline(ls) && ls->current != old) |
| 161 | ✗ | next(ls); /* skip '\n\r' or '\r\n' */ | ||
| 162 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1108 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 1108 times.
|
1108 | if (++ls->linenumber >= MAX_INT) |
| 163 | ✗ | lexerror(ls, "chunk has too many lines", 0); | ||
| 164 | 1108 | } | ||
| 165 | ||||
| 166 | ||||
| 167 | 297 | void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source, | ||
| 168 | int firstchar) { | |||
| 169 | 297 | ls->t.token = 0; | ||
| 170 | 297 | ls->L = L; | ||
| 171 | 297 | ls->current = firstchar; | ||
| 172 | 297 | ls->lookahead.token = TK_EOS; /* no look-ahead token */ | ||
| 173 | 297 | ls->z = z; | ||
| 174 | 297 | ls->fs = NULL; | ||
| 175 | 297 | ls->linenumber = 1; | ||
| 176 | 297 | ls->lastline = 1; | ||
| 177 | 297 | ls->source = source; | ||
| 178 | 297 | ls->envn = luaS_newliteral(L, LUA_ENV); /* get env name */ | ||
| 179 | 297 | luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */ | ||
| 180 | 297 | } | ||
| 181 | ||||
| 182 | ||||
| 183 | ||||
| 184 | /* | |||
| 185 | ** ======================================================= | |||
| 186 | ** LEXICAL ANALYZER | |||
| 187 | ** ======================================================= | |||
| 188 | */ | |||
| 189 | ||||
| 190 | ||||
| 191 | 1486 | static int check_next1 (LexState *ls, int c) { | ||
| 192 |
2/2✓ Branch 0 taken 330 times.
✓ Branch 1 taken 1156 times.
|
2/2✓ Decision 'true' taken 330 times.
✓ Decision 'false' taken 1156 times.
|
1486 | if (ls->current == c) { |
| 193 |
1/2✓ Branch 0 taken 330 times.
✗ Branch 1 not taken.
|
330 | next(ls); | |
| 194 | 330 | return 1; | ||
| 195 | } | |||
| 196 | 1156 | else return 0; | ||
| 197 | } | |||
| 198 | ||||
| 199 | ||||
| 200 | /* | |||
| 201 | ** Check whether current char is in set 'set' (with two chars) and | |||
| 202 | ** saves it | |||
| 203 | */ | |||
| 204 | 4282 | static int check_next2 (LexState *ls, const char *set) { | ||
| 205 | lua_assert(set[2] == '\0'); | |||
| 206 |
3/4✓ Branch 0 taken 3698 times.
✓ Branch 1 taken 584 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3698 times.
|
2/2✓ Decision 'true' taken 584 times.
✓ Decision 'false' taken 3698 times.
|
4282 | if (ls->current == set[0] || ls->current == set[1]) { |
| 207 |
1/2✓ Branch 1 taken 584 times.
✗ Branch 2 not taken.
|
584 | save_and_next(ls); | |
| 208 | 584 | return 1; | ||
| 209 | } | |||
| 210 | 3698 | else return 0; | ||
| 211 | } | |||
| 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 | 1782 | static int read_numeral (LexState *ls, SemInfo *seminfo) { | ||
| 228 | TValue obj; | |||
| 229 | 1782 | const char *expo = "Ee"; | ||
| 230 | 1782 | int first = ls->current; | ||
| 231 | lua_assert(lisdigit(ls->current)); | |||
| 232 |
1/2✓ Branch 1 taken 1782 times.
✗ Branch 2 not taken.
|
1782 | save_and_next(ls); | |
| 233 |
4/4✓ Branch 0 taken 736 times.
✓ Branch 1 taken 1046 times.
✓ Branch 3 taken 584 times.
✓ Branch 4 taken 152 times.
|
2/2✓ Decision 'true' taken 584 times.
✓ Decision 'false' taken 1198 times.
|
1782 | if (first == '0' && check_next2(ls, "xX")) /* hexadecimal? */ |
| 234 | 584 | expo = "Pp"; | ||
| 235 | for (;;) { | |||
| 236 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3546 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 3546 times.
|
3546 | if (check_next2(ls, expo)) /* exponent mark? */ |
| 237 | ✗ | check_next2(ls, "-+"); /* optional exponent sign */ | ||
| 238 |
4/4✓ Branch 0 taken 1849 times.
✓ Branch 1 taken 1697 times.
✓ Branch 2 taken 67 times.
✓ Branch 3 taken 1782 times.
|
2/2✓ Decision 'true' taken 1764 times.
✓ Decision 'false' taken 1782 times.
|
3546 | else if (lisxdigit(ls->current) || ls->current == '.') /* '%x|%.' */ |
| 239 |
1/2✓ Branch 1 taken 1764 times.
✗ Branch 2 not taken.
|
1764 | save_and_next(ls); | |
| 240 | else break; | |||
| 241 | } | |||
| 242 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1782 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 1782 times.
|
1782 | if (lislalpha(ls->current)) /* is numeral touching a letter? */ |
| 243 | ✗ | save_and_next(ls); /* force an error */ | ||
| 244 | 1782 | save(ls, '\0'); | ||
| 245 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1782 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 1782 times.
|
1782 | if (luaO_str2num(luaZ_buffer(ls->buff), &obj) == 0) /* format error? */ |
| 246 | ✗ | lexerror(ls, "malformed number", TK_FLT); | ||
| 247 |
2/2✓ Branch 0 taken 1715 times.
✓ Branch 1 taken 67 times.
|
2/2✓ Decision 'true' taken 1715 times.
✓ Decision 'false' taken 67 times.
|
1782 | if (ttisinteger(&obj)) { |
| 248 | 1715 | seminfo->i = ivalue(&obj); | ||
| 249 | 1715 | return TK_INT; | ||
| 250 | } | |||
| 251 | else { | |||
| 252 | lua_assert(ttisfloat(&obj)); | |||
| 253 | 67 | seminfo->r = fltvalue(&obj); | ||
| 254 | 67 | return TK_FLT; | ||
| 255 | } | |||
| 256 | } | |||
| 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 | 293 | static size_t skip_sep (LexState *ls) { | ||
| 266 | 293 | size_t count = 0; | ||
| 267 | 293 | int s = ls->current; | ||
| 268 | lua_assert(s == '[' || s == ']'); | |||
| 269 |
1/2✓ Branch 1 taken 293 times.
✗ Branch 2 not taken.
|
293 | save_and_next(ls); | |
| 270 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 293 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 293 times.
|
293 | while (ls->current == '=') { |
| 271 | ✗ | save_and_next(ls); | ||
| 272 | ✗ | count++; | ||
| 273 | } | |||
| 274 | 293 | return (ls->current == s) ? count + 2 | ||
| 275 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 293 times.
|
586 | : (count == 0) ? 1 | |
| 276 | 293 | : 0; | ||
| 277 | } | |||
| 278 | ||||
| 279 | ||||
| 280 | ✗ | static void read_long_string (LexState *ls, SemInfo *seminfo, size_t sep) { | ||
| 281 | ✗ | int line = ls->linenumber; /* initial line (for error message) */ | ||
| 282 | ✗ | save_and_next(ls); /* skip 2nd '[' */ | ||
| 283 | ✗ | if (currIsNewline(ls)) /* string starts with a newline? */ | ||
| 284 | ✗ | inclinenumber(ls); /* skip it */ | ||
| 285 | for (;;) { | |||
| 286 | ✗ | switch (ls->current) { | ||
| 287 | ✗ | case EOZ: { /* error */ | ||
| 288 | ✗ | const char *what = (seminfo ? "string" : "comment"); | ||
| 289 | ✗ | const char *msg = luaO_pushfstring(ls->L, | ||
| 290 | "unfinished long %s (starting at line %d)", what, line); | |||
| 291 | ✗ | lexerror(ls, msg, TK_EOS); | ||
| 292 | break; /* to avoid warnings */ | |||
| 293 | } | |||
| 294 | ✗ | case ']': { | ||
| 295 | ✗ | if (skip_sep(ls) == sep) { | ||
| 296 | ✗ | save_and_next(ls); /* skip 2nd ']' */ | ||
| 297 | ✗ | goto endloop; | ||
| 298 | } | |||
| 299 | ✗ | break; | ||
| 300 | } | |||
| 301 | ✗ | case '\n': case '\r': { | ||
| 302 | ✗ | save(ls, '\n'); | ||
| 303 | ✗ | inclinenumber(ls); | ||
| 304 | ✗ | if (!seminfo) luaZ_resetbuffer(ls->buff); /* avoid wasting space */ | ||
| 305 | ✗ | break; | ||
| 306 | } | |||
| 307 | ✗ | default: { | ||
| 308 | ✗ | if (seminfo) save_and_next(ls); | ||
| 309 | ✗ | else next(ls); | ||
| 310 | } | |||
| 311 | } | |||
| 312 | ✗ | } endloop: | ||
| 313 | ✗ | if (seminfo) | ||
| 314 | ✗ | seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + sep, | ||
| 315 | ✗ | luaZ_bufflen(ls->buff) - 2 * sep); | ||
| 316 | ✗ | } | ||
| 317 | ||||
| 318 | ||||
| 319 | ✗ | static void esccheck (LexState *ls, int c, const char *msg) { | ||
| 320 | ✗ | if (!c) { | ||
| 321 | ✗ | if (ls->current != EOZ) | ||
| 322 | ✗ | save_and_next(ls); /* add current to buffer for error message */ | ||
| 323 | ✗ | lexerror(ls, msg, TK_STRING); | ||
| 324 | } | |||
| 325 | ✗ | } | ||
| 326 | ||||
| 327 | ||||
| 328 | ✗ | static int gethexa (LexState *ls) { | ||
| 329 | ✗ | save_and_next(ls); | ||
| 330 | ✗ | esccheck (ls, lisxdigit(ls->current), "hexadecimal digit expected"); | ||
| 331 | ✗ | return luaO_hexavalue(ls->current); | ||
| 332 | } | |||
| 333 | ||||
| 334 | ||||
| 335 | ✗ | static int readhexaesc (LexState *ls) { | ||
| 336 | ✗ | int r = gethexa(ls); | ||
| 337 | ✗ | r = (r << 4) + gethexa(ls); | ||
| 338 | ✗ | luaZ_buffremove(ls->buff, 2); /* remove saved chars from buffer */ | ||
| 339 | ✗ | return r; | ||
| 340 | } | |||
| 341 | ||||
| 342 | ||||
| 343 | ✗ | static unsigned long readutf8esc (LexState *ls) { | ||
| 344 | unsigned long r; | |||
| 345 | ✗ | int i = 4; /* chars to be removed: '\', 'u', '{', and first digit */ | ||
| 346 | ✗ | save_and_next(ls); /* skip 'u' */ | ||
| 347 | ✗ | esccheck(ls, ls->current == '{', "missing '{'"); | ||
| 348 | ✗ | r = gethexa(ls); /* must have at least one digit */ | ||
| 349 | ✗ | while (cast_void(save_and_next(ls)), lisxdigit(ls->current)) { | ||
| 350 | ✗ | i++; | ||
| 351 | ✗ | esccheck(ls, r <= (0x7FFFFFFFu >> 4), "UTF-8 value too large"); | ||
| 352 | ✗ | r = (r << 4) + luaO_hexavalue(ls->current); | ||
| 353 | } | |||
| 354 | ✗ | esccheck(ls, ls->current == '}', "missing '}'"); | ||
| 355 | ✗ | next(ls); /* skip '}' */ | ||
| 356 | ✗ | luaZ_buffremove(ls->buff, i); /* remove saved chars from buffer */ | ||
| 357 | ✗ | return r; | ||
| 358 | } | |||
| 359 | ||||
| 360 | ||||
| 361 | ✗ | static void utf8esc (LexState *ls) { | ||
| 362 | char buff[UTF8BUFFSZ]; | |||
| 363 | ✗ | int n = luaO_utf8esc(buff, readutf8esc(ls)); | ||
| 364 | ✗ | for (; n > 0; n--) /* add 'buff' to string */ | ||
| 365 | ✗ | save(ls, buff[UTF8BUFFSZ - n]); | ||
| 366 | ✗ | } | ||
| 367 | ||||
| 368 | ||||
| 369 | ✗ | static int readdecesc (LexState *ls) { | ||
| 370 | int i; | |||
| 371 | ✗ | int r = 0; /* result accumulator */ | ||
| 372 | ✗ | for (i = 0; i < 3 && lisdigit(ls->current); i++) { /* read up to 3 digits */ | ||
| 373 | ✗ | r = 10*r + ls->current - '0'; | ||
| 374 | ✗ | save_and_next(ls); | ||
| 375 | } | |||
| 376 | ✗ | esccheck(ls, r <= UCHAR_MAX, "decimal escape too large"); | ||
| 377 | ✗ | luaZ_buffremove(ls->buff, i); /* remove read digits from buffer */ | ||
| 378 | ✗ | return r; | ||
| 379 | } | |||
| 380 | ||||
| 381 | ||||
| 382 | 154 | static void read_string (LexState *ls, int del, SemInfo *seminfo) { | ||
| 383 |
1/2✓ Branch 1 taken 154 times.
✗ Branch 2 not taken.
|
154 | save_and_next(ls); /* keep delimiter (for error messages) */ | |
| 384 |
2/2✓ Branch 0 taken 381 times.
✓ Branch 1 taken 154 times.
|
2/2✓ Decision 'true' taken 381 times.
✓ Decision 'false' taken 154 times.
|
535 | while (ls->current != del) { |
| 385 |
1/4✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 381 times.
|
381 | switch (ls->current) { | |
| 386 | ✗ | case EOZ: | ||
| 387 | ✗ | lexerror(ls, "unfinished string", TK_EOS); | ||
| 388 | break; /* to avoid warnings */ | |||
| 389 | ✗ | case '\n': | ||
| 390 | case '\r': | |||
| 391 | ✗ | lexerror(ls, "unfinished string", TK_STRING); | ||
| 392 | break; /* to avoid warnings */ | |||
| 393 | ✗ | case '\\': { /* escape sequences */ | ||
| 394 | int c; /* final character to be saved */ | |||
| 395 | ✗ | save_and_next(ls); /* keep '\\' for error messages */ | ||
| 396 | ✗ | switch (ls->current) { | ||
| 397 | ✗ | case 'a': c = '\a'; goto read_save; | ||
| 398 | ✗ | case 'b': c = '\b'; goto read_save; | ||
| 399 | ✗ | case 'f': c = '\f'; goto read_save; | ||
| 400 | ✗ | case 'n': c = '\n'; goto read_save; | ||
| 401 | ✗ | case 'r': c = '\r'; goto read_save; | ||
| 402 | ✗ | case 't': c = '\t'; goto read_save; | ||
| 403 | ✗ | case 'v': c = '\v'; goto read_save; | ||
| 404 | ✗ | case 'x': c = readhexaesc(ls); goto read_save; | ||
| 405 | ✗ | case 'u': utf8esc(ls); goto no_save; | ||
| 406 | ✗ | case '\n': case '\r': | ||
| 407 | ✗ | inclinenumber(ls); c = '\n'; goto only_save; | ||
| 408 | ✗ | case '\\': case '\"': case '\'': | ||
| 409 | ✗ | c = ls->current; goto read_save; | ||
| 410 | ✗ | case EOZ: goto no_save; /* will raise an error next loop */ | ||
| 411 | ✗ | case 'z': { /* zap following span of spaces */ | ||
| 412 | ✗ | luaZ_buffremove(ls->buff, 1); /* remove '\\' */ | ||
| 413 | ✗ | next(ls); /* skip the 'z' */ | ||
| 414 | ✗ | while (lisspace(ls->current)) { | ||
| 415 | ✗ | if (currIsNewline(ls)) inclinenumber(ls); | ||
| 416 | ✗ | else next(ls); | ||
| 417 | } | |||
| 418 | ✗ | goto no_save; | ||
| 419 | } | |||
| 420 | ✗ | default: { | ||
| 421 | ✗ | esccheck(ls, lisdigit(ls->current), "invalid escape sequence"); | ||
| 422 | ✗ | c = readdecesc(ls); /* digital escape '\ddd' */ | ||
| 423 | ✗ | goto only_save; | ||
| 424 | } | |||
| 425 | } | |||
| 426 | ✗ | read_save: | ||
| 427 | ✗ | next(ls); | ||
| 428 | /* go through */ | |||
| 429 | ✗ | only_save: | ||
| 430 | ✗ | luaZ_buffremove(ls->buff, 1); /* remove '\\' */ | ||
| 431 | ✗ | save(ls, c); | ||
| 432 | /* go through */ | |||
| 433 | ✗ | no_save: break; | ||
| 434 | } | |||
| 435 |
1/1✓ Decision 'true' taken 381 times.
|
381 | default: | |
| 436 |
1/2✓ Branch 1 taken 381 times.
✗ Branch 2 not taken.
|
381 | save_and_next(ls); | |
| 437 | } | |||
| 438 | } | |||
| 439 |
1/2✓ Branch 1 taken 154 times.
✗ Branch 2 not taken.
|
154 | save_and_next(ls); /* skip delimiter */ | |
| 440 | 308 | seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + 1, | ||
| 441 | 154 | luaZ_bufflen(ls->buff) - 2); | ||
| 442 | 154 | } | ||
| 443 | ||||
| 444 | ||||
| 445 | 13179 | static int llex (LexState *ls, SemInfo *seminfo) { | ||
| 446 | 13179 | luaZ_resetbuffer(ls->buff); | ||
| 447 | for (;;) { | |||
| 448 |
15/15✓ Branch 0 taken 1108 times.
✓ Branch 1 taken 10975 times.
✓ Branch 2 taken 229 times.
✓ Branch 3 taken 293 times.
✓ Branch 4 taken 682 times.
✓ Branch 5 taken 77 times.
✓ Branch 6 taken 152 times.
✓ Branch 7 taken 43 times.
✓ Branch 8 taken 91 times.
✓ Branch 9 taken 20 times.
✓ Branch 10 taken 154 times.
✓ Branch 11 taken 129 times.
✓ Branch 12 taken 1782 times.
✓ Branch 13 taken 297 times.
✓ Branch 14 taken 9240 times.
|
25272 | switch (ls->current) { | |
| 449 |
1/1✓ Decision 'true' taken 1108 times.
|
1108 | case '\n': case '\r': { /* line breaks */ | |
| 450 | 1108 | inclinenumber(ls); | ||
| 451 | 1108 | break; | ||
| 452 | } | |||
| 453 |
1/1✓ Decision 'true' taken 10975 times.
|
10975 | case ' ': case '\f': case '\t': case '\v': { /* spaces */ | |
| 454 |
2/2✓ Branch 0 taken 10908 times.
✓ Branch 1 taken 67 times.
|
10975 | next(ls); | |
| 455 | 10975 | break; | ||
| 456 | } | |||
| 457 |
1/1✓ Decision 'true' taken 229 times.
|
229 | case '-': { /* '-' or '--' (comment) */ | |
| 458 |
1/2✓ Branch 0 taken 229 times.
✗ Branch 1 not taken.
|
229 | next(ls); | |
| 459 |
2/2✓ Branch 0 taken 219 times.
✓ Branch 1 taken 10 times.
|
2/2✓ Decision 'true' taken 219 times.
✓ Decision 'false' taken 10 times.
|
229 | if (ls->current != '-') return '-'; |
| 460 | /* else is a comment */ | |||
| 461 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | next(ls); | |
| 462 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 10 times.
|
10 | if (ls->current == '[') { /* long comment? */ |
| 463 | ✗ | size_t sep = skip_sep(ls); | ||
| 464 | ✗ | luaZ_resetbuffer(ls->buff); /* 'skip_sep' may dirty the buffer */ | ||
| 465 | ✗ | if (sep >= 2) { | ||
| 466 | ✗ | read_long_string(ls, NULL, sep); /* skip long comment */ | ||
| 467 | ✗ | luaZ_resetbuffer(ls->buff); /* previous call may dirty the buff. */ | ||
| 468 | ✗ | break; | ||
| 469 | } | |||
| 470 | } | |||
| 471 | /* else short comment */ | |||
| 472 |
4/6✓ Branch 0 taken 238 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 238 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 238 times.
✗ Branch 5 not taken.
|
0/1? Decision couldn't be analyzed.
|
248 | while (!currIsNewline(ls) && ls->current != EOZ) |
| 473 |
1/2✓ Branch 0 taken 238 times.
✗ Branch 1 not taken.
|
238 | next(ls); /* skip until end of line (or end of file) */ | |
| 474 | 10 | break; | ||
| 475 | } | |||
| 476 |
1/1✓ Decision 'true' taken 293 times.
|
293 | case '[': { /* long string or simply '[' */ | |
| 477 | 293 | size_t sep = skip_sep(ls); | ||
| 478 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 293 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 293 times.
|
293 | if (sep >= 2) { |
| 479 | ✗ | read_long_string(ls, seminfo, sep); | ||
| 480 | ✗ | return TK_STRING; | ||
| 481 | } | |||
| 482 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 293 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 293 times.
|
293 | else if (sep == 0) /* '[=...' missing second bracket? */ |
| 483 | ✗ | lexerror(ls, "invalid long string delimiter", TK_STRING); | ||
| 484 | 293 | return '['; | ||
| 485 | } | |||
| 486 |
1/1✓ Decision 'true' taken 682 times.
|
682 | case '=': { | |
| 487 |
1/2✓ Branch 0 taken 682 times.
✗ Branch 1 not taken.
|
682 | next(ls); | |
| 488 |
2/2✓ Branch 1 taken 26 times.
✓ Branch 2 taken 656 times.
|
2/2✓ Decision 'true' taken 26 times.
✓ Decision 'false' taken 656 times.
|
682 | if (check_next1(ls, '=')) return TK_EQ; /* '==' */ |
| 489 | 656 | else return '='; | ||
| 490 | } | |||
| 491 |
1/1✓ Decision 'true' taken 77 times.
|
77 | case '<': { | |
| 492 |
1/2✓ Branch 0 taken 77 times.
✗ Branch 1 not taken.
|
77 | next(ls); | |
| 493 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 77 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 77 times.
|
77 | if (check_next1(ls, '=')) return TK_LE; /* '<=' */ |
| 494 |
1/2✓ Branch 1 taken 77 times.
✗ Branch 2 not taken.
|
1/2✓ Decision 'true' taken 77 times.
✗ Decision 'false' not taken.
|
77 | else if (check_next1(ls, '<')) return TK_SHL; /* '<<' */ |
| 495 | ✗ | else return '<'; | ||
| 496 | } | |||
| 497 |
1/1✓ Decision 'true' taken 152 times.
|
152 | case '>': { | |
| 498 |
1/2✓ Branch 0 taken 152 times.
✗ Branch 1 not taken.
|
152 | next(ls); | |
| 499 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 152 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 152 times.
|
152 | if (check_next1(ls, '=')) return TK_GE; /* '>=' */ |
| 500 |
2/2✓ Branch 1 taken 95 times.
✓ Branch 2 taken 57 times.
|
2/2✓ Decision 'true' taken 95 times.
✓ Decision 'false' taken 57 times.
|
152 | else if (check_next1(ls, '>')) return TK_SHR; /* '>>' */ |
| 501 | 57 | else return '>'; | ||
| 502 | } | |||
| 503 |
1/1✓ Decision 'true' taken 43 times.
|
43 | case '/': { | |
| 504 |
1/2✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
|
43 | next(ls); | |
| 505 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 43 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 43 times.
|
43 | if (check_next1(ls, '/')) return TK_IDIV; /* '//' */ |
| 506 | 43 | else return '/'; | ||
| 507 | } | |||
| 508 |
1/1✓ Decision 'true' taken 91 times.
|
91 | case '~': { | |
| 509 |
1/2✓ Branch 0 taken 91 times.
✗ Branch 1 not taken.
|
91 | next(ls); | |
| 510 |
2/2✓ Branch 1 taken 69 times.
✓ Branch 2 taken 22 times.
|
2/2✓ Decision 'true' taken 69 times.
✓ Decision 'false' taken 22 times.
|
91 | if (check_next1(ls, '=')) return TK_NE; /* '~=' */ |
| 511 | 22 | else return '~'; | ||
| 512 | } | |||
| 513 |
1/1✓ Decision 'true' taken 20 times.
|
20 | case ':': { | |
| 514 |
1/2✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
|
20 | next(ls); | |
| 515 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 20 times.
|
20 | if (check_next1(ls, ':')) return TK_DBCOLON; /* '::' */ |
| 516 | 20 | else return ':'; | ||
| 517 | } | |||
| 518 |
1/1✓ Decision 'true' taken 154 times.
|
154 | case '"': case '\'': { /* short literal strings */ | |
| 519 | 154 | read_string(ls, ls->current, seminfo); | ||
| 520 | 154 | return TK_STRING; | ||
| 521 | } | |||
| 522 |
1/1✓ Decision 'true' taken 129 times.
|
129 | case '.': { /* '.', '..', '...', or number */ | |
| 523 |
1/2✓ Branch 1 taken 129 times.
✗ Branch 2 not taken.
|
129 | save_and_next(ls); | |
| 524 |
2/2✓ Branch 1 taken 63 times.
✓ Branch 2 taken 66 times.
|
2/2✓ Decision 'true' taken 63 times.
✓ Decision 'false' taken 66 times.
|
129 | if (check_next1(ls, '.')) { |
| 525 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 63 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 63 times.
|
63 | if (check_next1(ls, '.')) |
| 526 | ✗ | return TK_DOTS; /* '...' */ | ||
| 527 | 63 | else return TK_CONCAT; /* '..' */ | ||
| 528 | } | |||
| 529 |
1/2✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
|
1/2✓ Decision 'true' taken 66 times.
✗ Decision 'false' not taken.
|
66 | else if (!lisdigit(ls->current)) return '.'; |
| 530 | ✗ | else return read_numeral(ls, seminfo); | ||
| 531 | } | |||
| 532 |
1/1✓ Decision 'true' taken 1782 times.
|
1782 | case '0': case '1': case '2': case '3': case '4': | |
| 533 | case '5': case '6': case '7': case '8': case '9': { | |||
| 534 |
1/1✓ Decision 'true' taken 1782 times.
|
1782 | return read_numeral(ls, seminfo); | |
| 535 | } | |||
| 536 |
1/1✓ Decision 'true' taken 297 times.
|
297 | case EOZ: { | |
| 537 | 297 | return TK_EOS; | ||
| 538 | } | |||
| 539 |
1/1✓ Decision 'true' taken 9240 times.
|
9240 | default: { | |
| 540 |
2/2✓ Branch 0 taken 5035 times.
✓ Branch 1 taken 4205 times.
|
2/2✓ Decision 'true' taken 5035 times.
✓ Decision 'false' taken 4205 times.
|
9240 | if (lislalpha(ls->current)) { /* identifier or reserved word? */ |
| 541 | TString *ts; | |||
| 542 | do { | |||
| 543 |
2/2✓ Branch 1 taken 33258 times.
✓ Branch 2 taken 28 times.
|
33286 | save_and_next(ls); | |
| 544 |
2/2✓ Branch 0 taken 28251 times.
✓ Branch 1 taken 5035 times.
|
2/2✓ Decision 'true' taken 28251 times.
✓ Decision 'false' taken 5035 times.
|
33286 | } while (lislalnum(ls->current)); |
| 545 | 5035 | ts = luaX_newstring(ls, luaZ_buffer(ls->buff), | ||
| 546 | 5035 | luaZ_bufflen(ls->buff)); | ||
| 547 | 5035 | seminfo->ts = ts; | ||
| 548 |
3/4✓ Branch 0 taken 5035 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1695 times.
✓ Branch 3 taken 3340 times.
|
2/2✓ Decision 'true' taken 1695 times.
✓ Decision 'false' taken 3340 times.
|
5035 | if (isreserved(ts)) /* reserved word? */ |
| 549 | 1695 | return ts->extra - 1 + FIRST_RESERVED; | ||
| 550 | else { | |||
| 551 | 3340 | return TK_NAME; | ||
| 552 | } | |||
| 553 | } | |||
| 554 | else { /* single-char tokens ('+', '*', '%', '{', '}', ...) */ | |||
| 555 | 4205 | int c = ls->current; | ||
| 556 |
2/2✓ Branch 0 taken 4008 times.
✓ Branch 1 taken 197 times.
|
4205 | next(ls); | |
| 557 | 4205 | return c; | ||
| 558 | } | |||
| 559 | } | |||
| 560 | } | |||
| 561 | } | |||
| 562 | } | |||
| 563 | ||||
| 564 | ||||
| 565 | 13179 | void luaX_next (LexState *ls) { | ||
| 566 | 13179 | ls->lastline = ls->linenumber; | ||
| 567 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 13179 times.
|
1/2✗ Decision 'true' not taken.
✓ Decision 'false' taken 13179 times.
|
13179 | if (ls->lookahead.token != TK_EOS) { /* is there a look-ahead token? */ |
| 568 | ✗ | ls->t = ls->lookahead; /* use this one */ | ||
| 569 | ✗ | ls->lookahead.token = TK_EOS; /* and discharge it */ | ||
| 570 | } | |||
| 571 | else | |||
| 572 | 13179 | ls->t.token = llex(ls, &ls->t.seminfo); /* read next token */ | ||
| 573 | 13179 | } | ||
| 574 | ||||
| 575 | ||||
| 576 | ✗ | int luaX_lookahead (LexState *ls) { | ||
| 577 | lua_assert(ls->lookahead.token == TK_EOS); | |||
| 578 | ✗ | ls->lookahead.token = llex(ls, &ls->lookahead.seminfo); | ||
| 579 | ✗ | return ls->lookahead.token; | ||
| 580 | } | |||
| 581 | ||||
| 582 |