Lines Matching refs:ls
32 #define next(ls) (ls->current = zgetc(ls->z)) argument
36 #define currIsNewline(ls) (ls->current == '\n' || ls->current == '\r') argument
51 #define save_and_next(ls) (save(ls, ls->current), next(ls)) argument
54 static l_noret lexerror (LexState *ls, const char *msg, int token);
57 static void save (LexState *ls, int c) { in save() argument
58 Mbuffer *b = ls->buff; in save()
62 lexerror(ls, "lexical element too long", 0); in save()
64 luaZ_resizebuffer(ls->L, b, newsize); in save()
82 const char *luaX_token2str (LexState *ls, int token) { in luaX_token2str() argument
85 return luaO_pushfstring(ls->L, "'%c'", token); in luaX_token2str()
87 return luaO_pushfstring(ls->L, "'<\\%d>'", token); in luaX_token2str()
92 return luaO_pushfstring(ls->L, "'%s'", s); in luaX_token2str()
99 static const char *txtToken (LexState *ls, int token) { in txtToken() argument
103 save(ls, '\0'); in txtToken()
104 return luaO_pushfstring(ls->L, "'%s'", luaZ_buffer(ls->buff)); in txtToken()
106 return luaX_token2str(ls, token); in txtToken()
111 static l_noret lexerror (LexState *ls, const char *msg, int token) { in lexerror() argument
112 msg = luaG_addinfo(ls->L, msg, ls->source, ls->linenumber); in lexerror()
114 luaO_pushfstring(ls->L, "%s near %s", msg, txtToken(ls, token)); in lexerror()
115 luaD_throw(ls->L, LUA_ERRSYNTAX); in lexerror()
119 l_noret luaX_syntaxerror (LexState *ls, const char *msg) { in luaX_syntaxerror() argument
120 lexerror(ls, msg, ls->t.token); in luaX_syntaxerror()
129 TString *luaX_newstring (LexState *ls, const char *str, size_t l) { in luaX_newstring() argument
130 lua_State *L = ls->L; in luaX_newstring()
134 o = luaH_set(L, ls->h, s2v(L->top - 1)); in luaX_newstring()
153 static void inclinenumber (LexState *ls) { in inclinenumber() argument
154 int old = ls->current; in inclinenumber()
155 lua_assert(currIsNewline(ls)); in inclinenumber()
156 next(ls); /* skip '\n' or '\r' */ in inclinenumber()
157 if (currIsNewline(ls) && ls->current != old) in inclinenumber()
158 next(ls); /* skip '\n\r' or '\r\n' */ in inclinenumber()
159 if (++ls->linenumber >= MAX_INT) in inclinenumber()
160 lexerror(ls, "chunk has too many lines", 0); in inclinenumber()
164 void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source, in luaX_setinput() argument
166 ls->t.token = 0; in luaX_setinput()
167 ls->L = L; in luaX_setinput()
168 ls->current = firstchar; in luaX_setinput()
169 ls->lookahead.token = TK_EOS; /* no look-ahead token */ in luaX_setinput()
170 ls->z = z; in luaX_setinput()
171 ls->fs = NULL; in luaX_setinput()
172 ls->linenumber = 1; in luaX_setinput()
173 ls->lastline = 1; in luaX_setinput()
174 ls->source = source; in luaX_setinput()
175 ls->envn = luaS_newliteral(L, LUA_ENV); /* get env name */ in luaX_setinput()
176 luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */ in luaX_setinput()
188 static int check_next1 (LexState *ls, int c) { in check_next1() argument
189 if (ls->current == c) { in check_next1()
190 next(ls); in check_next1()
201 static int check_next2 (LexState *ls, const char *set) { in check_next2() argument
203 if (ls->current == set[0] || ls->current == set[1]) { in check_next2()
204 save_and_next(ls); in check_next2()
224 static int read_numeral (LexState *ls, SemInfo *seminfo) { in read_numeral() argument
227 int first = ls->current; in read_numeral()
228 lua_assert(lisdigit(ls->current)); in read_numeral()
229 save_and_next(ls); in read_numeral()
230 if (first == '0' && check_next2(ls, "xX")) /* hexadecimal? */ in read_numeral()
233 if (check_next2(ls, expo)) /* exponent mark? */ in read_numeral()
234 check_next2(ls, "-+"); /* optional exponent sign */ in read_numeral()
235 else if (lisxdigit(ls->current) || ls->current == '.') /* '%x|%.' */ in read_numeral()
236 save_and_next(ls); in read_numeral()
239 if (lislalpha(ls->current)) /* is numeral touching a letter? */ in read_numeral()
240 save_and_next(ls); /* force an error */ in read_numeral()
241 save(ls, '\0'); in read_numeral()
242 if (luaO_str2num(luaZ_buffer(ls->buff), &obj) == 0) /* format error? */ in read_numeral()
243 lexerror(ls, "malformed number", TK_FLT); in read_numeral()
261 static size_t skip_sep (LexState *ls) { in skip_sep() argument
263 int s = ls->current; in skip_sep()
265 save_and_next(ls); in skip_sep()
266 while (ls->current == '=') { in skip_sep()
267 save_and_next(ls); in skip_sep()
270 return (ls->current == s) ? count + 2 in skip_sep()
276 static void read_long_string (LexState *ls, SemInfo *seminfo, size_t sep) { in read_long_string() argument
277 int line = ls->linenumber; /* initial line (for error message) */ in read_long_string()
278 save_and_next(ls); /* skip 2nd '[' */ in read_long_string()
279 if (currIsNewline(ls)) /* string starts with a newline? */ in read_long_string()
280 inclinenumber(ls); /* skip it */ in read_long_string()
282 switch (ls->current) { in read_long_string()
285 const char *msg = luaO_pushfstring(ls->L, in read_long_string()
287 lexerror(ls, msg, TK_EOS); in read_long_string()
291 if (skip_sep(ls) == sep) { in read_long_string()
292 save_and_next(ls); /* skip 2nd ']' */ in read_long_string()
298 save(ls, '\n'); in read_long_string()
299 inclinenumber(ls); in read_long_string()
300 if (!seminfo) luaZ_resetbuffer(ls->buff); /* avoid wasting space */ in read_long_string()
304 if (seminfo) save_and_next(ls); in read_long_string()
305 else next(ls); in read_long_string()
310 seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + sep, in read_long_string()
311 luaZ_bufflen(ls->buff) - 2 * sep); in read_long_string()
315 static void esccheck (LexState *ls, int c, const char *msg) { in esccheck() argument
317 if (ls->current != EOZ) in esccheck()
318 save_and_next(ls); /* add current to buffer for error message */ in esccheck()
319 lexerror(ls, msg, TK_STRING); in esccheck()
324 static int gethexa (LexState *ls) { in gethexa() argument
325 save_and_next(ls); in gethexa()
326 esccheck (ls, lisxdigit(ls->current), "hexadecimal digit expected"); in gethexa()
327 return luaO_hexavalue(ls->current); in gethexa()
331 static int readhexaesc (LexState *ls) { in readhexaesc() argument
332 int r = gethexa(ls); in readhexaesc()
333 r = (r << 4) + gethexa(ls); in readhexaesc()
334 luaZ_buffremove(ls->buff, 2); /* remove saved chars from buffer */ in readhexaesc()
339 static unsigned long readutf8esc (LexState *ls) { in readutf8esc() argument
342 save_and_next(ls); /* skip 'u' */ in readutf8esc()
343 esccheck(ls, ls->current == '{', "missing '{'"); in readutf8esc()
344 r = gethexa(ls); /* must have at least one digit */ in readutf8esc()
345 while (cast_void(save_and_next(ls)), lisxdigit(ls->current)) { in readutf8esc()
347 esccheck(ls, r <= (0x7FFFFFFFu >> 4), "UTF-8 value too large"); in readutf8esc()
348 r = (r << 4) + luaO_hexavalue(ls->current); in readutf8esc()
350 esccheck(ls, ls->current == '}', "missing '}'"); in readutf8esc()
351 next(ls); /* skip '}' */ in readutf8esc()
352 luaZ_buffremove(ls->buff, i); /* remove saved chars from buffer */ in readutf8esc()
357 static void utf8esc (LexState *ls) { in utf8esc() argument
359 int n = luaO_utf8esc(buff, readutf8esc(ls)); in utf8esc()
361 save(ls, buff[UTF8BUFFSZ - n]); in utf8esc()
365 static int readdecesc (LexState *ls) { in readdecesc() argument
368 for (i = 0; i < 3 && lisdigit(ls->current); i++) { /* read up to 3 digits */ in readdecesc()
369 r = 10*r + ls->current - '0'; in readdecesc()
370 save_and_next(ls); in readdecesc()
372 esccheck(ls, r <= UCHAR_MAX, "decimal escape too large"); in readdecesc()
373 luaZ_buffremove(ls->buff, i); /* remove read digits from buffer */ in readdecesc()
378 static void read_string (LexState *ls, int del, SemInfo *seminfo) { in read_string() argument
379 save_and_next(ls); /* keep delimiter (for error messages) */ in read_string()
380 while (ls->current != del) { in read_string()
381 switch (ls->current) { in read_string()
383 lexerror(ls, "unfinished string", TK_EOS); in read_string()
387 lexerror(ls, "unfinished string", TK_STRING); in read_string()
391 save_and_next(ls); /* keep '\\' for error messages */ in read_string()
392 switch (ls->current) { in read_string()
400 case 'x': c = readhexaesc(ls); goto read_save; in read_string()
401 case 'u': utf8esc(ls); goto no_save; in read_string()
403 inclinenumber(ls); c = '\n'; goto only_save; in read_string()
405 c = ls->current; goto read_save; in read_string()
408 luaZ_buffremove(ls->buff, 1); /* remove '\\' */ in read_string()
409 next(ls); /* skip the 'z' */ in read_string()
410 while (lisspace(ls->current)) { in read_string()
411 if (currIsNewline(ls)) inclinenumber(ls); in read_string()
412 else next(ls); in read_string()
417 esccheck(ls, lisdigit(ls->current), "invalid escape sequence"); in read_string()
418 c = readdecesc(ls); /* digital escape '\ddd' */ in read_string()
423 next(ls); in read_string()
426 luaZ_buffremove(ls->buff, 1); /* remove '\\' */ in read_string()
427 save(ls, c); in read_string()
432 save_and_next(ls); in read_string()
435 save_and_next(ls); /* skip delimiter */ in read_string()
436 seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + 1, in read_string()
437 luaZ_bufflen(ls->buff) - 2); in read_string()
441 static int llex (LexState *ls, SemInfo *seminfo) { in llex() argument
442 luaZ_resetbuffer(ls->buff); in llex()
444 switch (ls->current) { in llex()
446 inclinenumber(ls); in llex()
450 next(ls); in llex()
454 next(ls); in llex()
455 if (ls->current != '-') return '-'; in llex()
457 next(ls); in llex()
458 if (ls->current == '[') { /* long comment? */ in llex()
459 size_t sep = skip_sep(ls); in llex()
460 luaZ_resetbuffer(ls->buff); /* 'skip_sep' may dirty the buffer */ in llex()
462 read_long_string(ls, NULL, sep); /* skip long comment */ in llex()
463 luaZ_resetbuffer(ls->buff); /* previous call may dirty the buff. */ in llex()
468 while (!currIsNewline(ls) && ls->current != EOZ) in llex()
469 next(ls); /* skip until end of line (or end of file) */ in llex()
473 size_t sep = skip_sep(ls); in llex()
475 read_long_string(ls, seminfo, sep); in llex()
479 lexerror(ls, "invalid long string delimiter", TK_STRING); in llex()
483 next(ls); in llex()
484 if (check_next1(ls, '=')) return TK_EQ; in llex()
488 next(ls); in llex()
489 if (check_next1(ls, '=')) return TK_LE; in llex()
490 else if (check_next1(ls, '<')) return TK_SHL; in llex()
494 next(ls); in llex()
495 if (check_next1(ls, '=')) return TK_GE; in llex()
496 else if (check_next1(ls, '>')) return TK_SHR; in llex()
500 next(ls); in llex()
501 if (check_next1(ls, '/')) return TK_IDIV; in llex()
505 next(ls); in llex()
506 if (check_next1(ls, '=')) return TK_NE; in llex()
510 next(ls); in llex()
511 if (check_next1(ls, ':')) return TK_DBCOLON; in llex()
515 read_string(ls, ls->current, seminfo); in llex()
519 save_and_next(ls); in llex()
520 if (check_next1(ls, '.')) { in llex()
521 if (check_next1(ls, '.')) in llex()
525 else if (!lisdigit(ls->current)) return '.'; in llex()
526 else return read_numeral(ls, seminfo); in llex()
530 return read_numeral(ls, seminfo); in llex()
536 if (lislalpha(ls->current)) { /* identifier or reserved word? */ in llex()
539 save_and_next(ls); in llex()
540 } while (lislalnum(ls->current)); in llex()
541 ts = luaX_newstring(ls, luaZ_buffer(ls->buff), in llex()
542 luaZ_bufflen(ls->buff)); in llex()
551 int c = ls->current; in llex()
552 next(ls); in llex()
561 void luaX_next (LexState *ls) { in luaX_next() argument
562 ls->lastline = ls->linenumber; in luaX_next()
563 if (ls->lookahead.token != TK_EOS) { /* is there a look-ahead token? */ in luaX_next()
564 ls->t = ls->lookahead; /* use this one */ in luaX_next()
565 ls->lookahead.token = TK_EOS; /* and discharge it */ in luaX_next()
568 ls->t.token = llex(ls, &ls->t.seminfo); /* read next token */ in luaX_next()
572 int luaX_lookahead (LexState *ls) { in luaX_lookahead() argument
573 lua_assert(ls->lookahead.token == TK_EOS); in luaX_lookahead()
574 ls->lookahead.token = llex(ls, &ls->lookahead.seminfo); in luaX_lookahead()
575 return ls->lookahead.token; in luaX_lookahead()