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