static int llex(LexState *ls, TValue *tv) { lj_str_resetbuf(&ls->sb); for (;;) { if (lj_char_isident(ls->current)) { GCstr *s; if (lj_char_isdigit(ls->current)) { /* Numeric literal. */ lex_number(ls, tv); return TK_number; } /* Identifier or reserved word. */ do { save_and_next(ls); } while (lj_char_isident(ls->current)); s = lj_parse_keepstr(ls, ls->sb.buf, ls->sb.n); setstrV(ls->L, tv, s); if (s->reserved > 0) /* Reserved word? */ return TK_OFS + s->reserved; return TK_name; } switch (ls->current) { case '\n': case '\r': inclinenumber(ls); continue; case ' ': case '\t': case '\v': case '\f': next(ls); continue; case '-': next(ls); if (ls->current != '-') return '-'; /* else is a comment */ next(ls); if (ls->current == '[') { int sep = skip_sep(ls); lj_str_resetbuf(&ls->sb); /* `skip_sep' may dirty the buffer */ if (sep >= 0) { read_long_string(ls, NULL, sep); /* long comment */ lj_str_resetbuf(&ls->sb); continue; } } /* else short comment */ while (!currIsNewline(ls) && ls->current != END_OF_STREAM) next(ls); continue; case '[': { int sep = skip_sep(ls); if (sep >= 0) { read_long_string(ls, tv, sep); return TK_string; } else if (sep == -1) { return '['; } else { lj_lex_error(ls, TK_string, LJ_ERR_XLDELIM); continue; } } case '=': next(ls); if (ls->current != '=') return '='; else { next(ls); return TK_eq; } case '<': next(ls); if (ls->current != '=') return '<'; else { next(ls); return TK_le; } case '>': next(ls); if (ls->current != '=') return '>'; else { next(ls); return TK_ge; } case '~': next(ls); if (ls->current != '=') return '~'; else { next(ls); return TK_ne; } case ':': next(ls); if (ls->current != ':') return ':'; else { next(ls); return TK_label; } case '"': case '\'': read_string(ls, ls->current, tv); return TK_string; case '.': save_and_next(ls); if (ls->current == '.') { next(ls); if (ls->current == '.') { next(ls); return TK_dots; /* ... */ } return TK_concat; /* .. */ } else if (!lj_char_isdigit(ls->current)) { return '.'; } else { lex_number(ls, tv); return TK_number; } case END_OF_STREAM: return TK_eof; default: { int c = ls->current; next(ls); return c; /* Single-char tokens (+ - / ...). */ } } } }
static int llex (LexState *ls, SemInfo *seminfo) { luaZ_resetbuffer(ls->buff); for (;;) { switch (ls->current) { case '\n': case '\r': { /* line breaks */ inclinenumber(ls); break; } case ' ': case '\f': case '\t': case '\v': { /* spaces */ next(ls); break; } case '-': { /* '-' or '--' (comment) */ next(ls); if (ls->current != '-') return '-'; /* else is a comment */ next(ls); if (ls->current == '[') { /* long comment? */ int sep = skip_sep(ls); luaZ_resetbuffer(ls->buff); /* 'skip_sep' may dirty the buffer */ if (sep >= 0) { read_long_string(ls, NULL, sep); /* skip long comment */ luaZ_resetbuffer(ls->buff); /* previous call may dirty the buff. */ break; } } /* else short comment */ while (!currIsNewline(ls) && ls->current != EOZ) next(ls); /* skip until end of line (or end of file) */ break; } case '[': { /* long string or simply '[' */ int sep = skip_sep(ls); if (sep >= 0) { read_long_string(ls, seminfo, sep); return TK_STRING; } else if (sep != -1) /* '[=...' missing second bracket */ lexerror(ls, "invalid long string delimiter", TK_STRING); return '['; } case '=': { next(ls); if (check_next1(ls, '=')) return TK_EQ; else return '='; } case '<': { next(ls); if (check_next1(ls, '=')) return TK_LE; else if (check_next1(ls, '<')) return TK_SHL; else return '<'; } case '>': { next(ls); if (check_next1(ls, '=')) return TK_GE; else if (check_next1(ls, '>')) return TK_SHR; else return '>'; } case '/': { next(ls); if (check_next1(ls, '/')) return TK_IDIV; else return '/'; } case '~': { next(ls); if (check_next1(ls, '=')) return TK_NE; else return '~'; } case ':': { next(ls); if (check_next1(ls, ':')) return TK_DBCOLON; else return ':'; } case '"': case '\'': { /* short literal strings */ read_string(ls, ls->current, seminfo); return TK_STRING; } case '`': { /* relative paths */ read_string(ls, ls->current, seminfo); return TK_PATH; } case '.': { /* '.', '..', '...', or number */ save_and_next(ls); if (check_next1(ls, '.')) { if (check_next1(ls, '.')) return TK_DOTS; /* '...' */ else return TK_CONCAT; /* '..' */ } else if (!lisdigit(ls->current)) return '.'; else return read_numeral(ls, seminfo); } case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': { return read_numeral(ls, seminfo); } case EOZ: { return TK_EOS; } default: { if (lislalpha(ls->current)) { /* identifier or reserved word? */ TString *ts; do { save_and_next(ls); } while (lislalnum(ls->current)); ts = luaX_newstring(ls, luaZ_buffer(ls->buff), luaZ_bufflen(ls->buff)); seminfo->ts = ts; if (isreserved(ts)) /* reserved word? */ return ts->extra - 1 + FIRST_RESERVED; else { return TK_NAME; } } else { /* single-char tokens (+ - / ...) */ int c = ls->current; next(ls); return c; } } } } }
int luaX_lex (LexState *LS, SemInfo *seminfo) { for (;;) { switch (LS->current) { case '\n': { inclinenumber(LS); continue; } case '-': { next(LS); if (LS->current != '-') return '-'; /* else is a comment */ next(LS); if (LS->current == '[' && (next(LS), LS->current == '[')) read_long_string(LS, NULL); /* long comment */ else /* short comment */ while (LS->current != '\n' && LS->current != EOZ) next(LS); continue; } case '[': { next(LS); if (LS->current != '[') return '['; else { read_long_string(LS, seminfo); return TK_STRING; } } case '=': { next(LS); if (LS->current != '=') return '='; else { next(LS); return TK_EQ; } } case '<': { next(LS); if (LS->current == '<') { next(LS); return TK_SHL; } else if (LS->current != '=') return '<'; else { next(LS); return TK_LE; } } case '>': { next(LS); if (LS->current == '>') { next(LS); return TK_SHR; } else if (LS->current != '=') return '>'; else { next(LS); return TK_GE; } } case '~': { next(LS); if (LS->current != '=') return '~'; else { next(LS); return TK_NE; } } case '"': case '\'': { read_string(LS, LS->current, seminfo); return TK_STRING; } case '.': { next(LS); if (LS->current == '.') { next(LS); if (LS->current == '.') { next(LS); return TK_DOTS; /* ... */ } else return TK_CONCAT; /* .. */ } else if (!lex_isdigit(LS->current)) return '.'; else { return read_numeral(LS, 1, seminfo); } } case EOZ: { return TK_EOS; } default: { if (isspace(LS->current)) { next(LS); continue; } else if (lex_isdigit(LS->current)) { return (read_numeral(LS, 0, seminfo)); } else if (lex_isalpha(LS->current) || LS->current == '_') { char saveCh = 0; size_t l; TString *ts; if (LS->current == 'L') { next(LS); if (LS->current == '"') { read_wstring(LS, LS->current, seminfo); return TK_WSTRING; } saveCh = 'L'; } /* identifier or reserved word */ l = readname(LS, saveCh); ts = luaS_newlstr(LS->L, luaZ_buffer(LS->buff), l); if (ts->tsv.reserved > 0) /* reserved word? */ return ts->tsv.reserved - 1 + FIRST_RESERVED; seminfo->ts = ts; return TK_NAME; } else { int c = LS->current; if (iscntrl(c)) luaX_error(LS, "invalid control char", luaO_pushfstring(LS->L, "char(%d)", c)); next(LS); return c; /* single-char tokens (+ - / ...) */ } } } } }
int luaX_lex (LexState *LS, SemInfo *seminfo) { for (;;) { switch (LS->current) { case ' ': case '\t': case '\r': /* `\r' to avoid problems with DOS */ next(LS); continue; case '\n': inclinenumber(LS); continue; case '$': luaX_error(LS, "unexpected `$' (pragmas are no longer supported)", '$'); break; case '-': next(LS); if (LS->current != '-') return '-'; do { next(LS); } while (LS->current != '\n' && LS->current != EOZ); continue; case '[': next(LS); if (LS->current != '[') return '['; else { read_long_string(LS, seminfo); return TK_STRING; } case '=': next(LS); if (LS->current != '=') return '='; else { next(LS); return TK_EQ; } case '<': next(LS); if (LS->current != '=') return '<'; else { next(LS); return TK_LE; } case '>': next(LS); if (LS->current != '=') return '>'; else { next(LS); return TK_GE; } case '~': next(LS); if (LS->current != '=') return '~'; else { next(LS); return TK_NE; } case '"': case '\'': read_string(LS, LS->current, seminfo); return TK_STRING; case '.': next(LS); if (LS->current == '.') { next(LS); if (LS->current == '.') { next(LS); return TK_DOTS; /* ... */ } else return TK_CONCAT; /* .. */ } else if (!isdigit(LS->current)) return '.'; else { read_number(LS, 1, seminfo); return TK_NUMBER; } case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': read_number(LS, 0, seminfo); return TK_NUMBER; case EOZ: return TK_EOS; case '_': goto tname; default: if (!isalpha(LS->current)) { int c = LS->current; if (iscntrl(c)) luaX_invalidchar(LS, c); next(LS); return c; } tname: { /* identifier or reserved word */ TString *ts = luaS_new(LS->L, readname(LS)); if (ts->marked >= RESERVEDMARK) /* reserved word? */ return ts->marked-RESERVEDMARK+FIRST_RESERVED; seminfo->ts = ts; return TK_NAME; } } } }
int luaX_lex (LexState *LS) { luaL_resetbuffer(); for (;;) { switch (LS->current) { case ' ': case '\t': case '\r': /* CR: to avoid problems with DOS */ next(LS); continue; case '\n': inclinenumber(LS); continue; case '-': save_and_next(LS); if (LS->current != '-') return '-'; do { next(LS); } while (LS->current != '\n' && LS->current != EOZ); luaL_resetbuffer(); continue; case '[': save_and_next(LS); if (LS->current != '[') return '['; else { save_and_next(LS); /* pass the second '[' */ return read_long_string(LS); } case '=': save_and_next(LS); if (LS->current != '=') return '='; else { save_and_next(LS); return EQ; } case '<': save_and_next(LS); if (LS->current != '=') return '<'; else { save_and_next(LS); return LE; } case '>': save_and_next(LS); if (LS->current != '=') return '>'; else { save_and_next(LS); return GE; } case '~': save_and_next(LS); if (LS->current != '=') return '~'; else { save_and_next(LS); return NE; } case '"': case '\'': { int del = LS->current; save_and_next(LS); while (LS->current != del) { switch (LS->current) { case EOZ: case '\n': luaX_error(LS, "unfinished string"); return EOS; /* to avoid warnings */ case '\\': next(LS); /* do not save the '\' */ switch (LS->current) { case 'a': save('\a'); next(LS); break; case 'b': save('\b'); next(LS); break; case 'f': save('\f'); next(LS); break; case 'n': save('\n'); next(LS); break; case 'r': save('\r'); next(LS); break; case 't': save('\t'); next(LS); break; case 'v': save('\v'); next(LS); break; case '\n': save('\n'); inclinenumber(LS); break; default : { if (isdigit(LS->current)) { int c = 0; int i = 0; do { c = 10*c + (LS->current-'0'); next(LS); } while (++i<3 && isdigit(LS->current)); if (c != (unsigned char)c) luaX_error(LS, "escape sequence too large"); save(c); } else { /* handles \, ", ', and ? */ save(LS->current); next(LS); } break; } } break; default: save_and_next(LS); } } save_and_next(LS); /* skip delimiter */ LS->seminfo.ts = luaS_newlstr(L->Mbuffer+(L->Mbuffbase+1), L->Mbuffnext-L->Mbuffbase-2); return STRING; } case '.': save_and_next(LS); if (LS->current == '.') { save_and_next(LS); if (LS->current == '.') { save_and_next(LS); return DOTS; /* ... */ } else return CONC; /* .. */ } else if (!isdigit(LS->current)) return '.'; goto fraction; /* LS->current is a digit: goes through to number */ case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': do { save_and_next(LS); } while (isdigit(LS->current)); if (LS->current == '.') { save_and_next(LS); if (LS->current == '.') { save('.'); luaX_error(LS, "ambiguous syntax (decimal point x string concatenation)"); } } fraction: while (isdigit(LS->current)) save_and_next(LS); if (toupper(LS->current) == 'E') { save_and_next(LS); /* read 'E' */ save_and_next(LS); /* read '+', '-' or first digit */ while (isdigit(LS->current)) save_and_next(LS); } save('\0'); LS->seminfo.r = luaO_str2d(L->Mbuffer+L->Mbuffbase); if (LS->seminfo.r < 0) luaX_error(LS, "invalid numeric format"); return NUMBER; case EOZ: if (LS->iflevel > 0) luaX_error(LS, "input ends inside a $if"); return EOS; default: if (LS->current != '_' && !isalpha(LS->current)) { int c = LS->current; if (iscntrl(c)) luaX_invalidchar(LS, c); save_and_next(LS); return c; } else { /* identifier or reserved word */ TaggedString *ts; do { save_and_next(LS); } while (isalnum(LS->current) || LS->current == '_'); save('\0'); ts = luaS_new(L->Mbuffer+L->Mbuffbase); if (ts->head.marked >= FIRST_RESERVED) return ts->head.marked; /* reserved word */ LS->seminfo.ts = ts; return NAME; } } } }
static int llex (tt_Lexer *L) { for (;;) { switch (ttL_current(L)) { case '\n': case '\r': { /* line breaks */ ttL_inclinenumber(L); break; } case ' ': case '\f': case '\t': case '\v': { /* spaces */ ttL_next(L); break; } case '-': { /* '-' or '--' (comment) */ ttL_next(L); if (ttL_current(L) != '-') return '-'; /* else is a comment */ ttL_next(L); if (ttL_current(L) == '[') { /* long comment? */ int sep = skip_sep(L); ttL_resetbuffer(L); /* `skip_sep' may dirty the buffer */ if (sep >= 0) { read_long_string(L, 0, sep); /* skip long comment */ ttL_resetbuffer(L); /* previous call may dirty the buff. */ break; } } /* else short comment */ while (!ttL_isnewline(L) && ttL_current(L) != TTL_EOZ) ttL_next(L); /* skip until end of line (or end of file) */ break; } case '[': { /* long string or simply '[' */ int sep = skip_sep(L); if (sep >= 0) { read_long_string(L, 1, sep); return TTK_STRING; } else if (sep == -1) return '['; else ttL_lexerror(L, "invalid long string delimiter", TTK_STRING); } case '=': { ttL_next(L); if (ttL_current(L) != '=') return '='; else { ttL_next(L); return TTK_EQ; } } case '<': { ttL_next(L); if (ttL_current(L) != '=') return '<'; else { ttL_next(L); return TTK_LE; } } case '>': { ttL_next(L); if (ttL_current(L) != '=') return '>'; else { ttL_next(L); return TTK_GE; } } case '~': { ttL_next(L); if (ttL_current(L) != '=') return '~'; else { ttL_next(L); return TTK_NE; } } case ':': { ttL_next(L); if (ttL_current(L) != ':') return ':'; else { ttL_next(L); return TTK_DBCOLON; } } case '"': case '\'': { /* short literal strings */ read_string(L, ttL_current(L)); return TTK_STRING; } case '.': { /* '.', '..', '...', or number */ ttL_save_and_next(L); if (ttL_check_next(L, ".")) { if (ttL_check_next(L, ".")) return TTK_DOTS; /* '...' */ else return TTK_CONCAT; /* '..' */ } else if (!isdigit(ttL_current(L))) return '.'; /* else go through */ } case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': { read_numeral(L); return TTK_NUMBER; } case TTL_EOZ: { return TTK_EOS; } default: { if (isalpha(ttL_current(L)) || ttL_current(L) == '_') { /* identifier or reserved word? */ int reversed; do { ttL_save_and_next(L); } while (isalnum(ttL_current(L)) || ttL_current(L) == '_'); if ((reversed = ttL_iskeyword(L, ltokens, TTK_LUA_NUM_RESERVED)) != 0) return reversed + TTK_FIRST_REVERSED; return TTK_NAME; } else { /* single-char tokens (+ - / ...) */ int c = ttL_current(L); ttL_next(L); return c; } } } } }
int32 luaY_lex(YYSTYPE *l) { LexState *LS = lua_state->lexstate; double a; luaL_resetbuffer(); if (lua_debug) luaY_codedebugline(LS->linelasttoken); LS->linelasttoken = LS->linenumber; while (1) { switch (LS->current) { case ' ': case '\t': case '\r': // CR: to avoid problems with DOS next(LS); continue; case '\n': inclinenumber(LS); LS->linelasttoken = LS->linenumber; continue; case '-': save_and_next(LS); if (LS->current != '-') return '-'; do { next(LS); } while (LS->current != '\n' && LS->current != EOZ); luaL_resetbuffer(); continue; case '[': save_and_next(LS); if (LS->current != '[') return '['; else { save_and_next(LS); // pass the second '[' return read_long_string(LS, l); } case '=': save_and_next(LS); if (LS->current != '=') return '='; else { save_and_next(LS); return EQ; } case '<': save_and_next(LS); if (LS->current != '=') return '<'; else { save_and_next(LS); return LE; } case '>': save_and_next(LS); if (LS->current != '=') return '>'; else { save_and_next(LS); return GE; } case '~': save_and_next(LS); if (LS->current != '=') return '~'; else { save_and_next(LS); return NE; } case '"': case '\'': { int32 del = LS->current; save_and_next(LS); while (LS->current != del) { switch (LS->current) { case EOZ: case '\n': save(0); return WRONGTOKEN; case '\\': next(LS); // do not save the '\' switch (LS->current) { case 'n': save('\n'); next(LS); break; case 't': save('\t'); next(LS); break; case 'r': save('\r'); next(LS); break; case '\n': save('\n'); inclinenumber(LS); break; default : save_and_next(LS); break; } break; default: save_and_next(LS); } } next(LS); // skip delimiter save(0); l->pTStr = luaS_new(Mbuffbase + 1); Mbuffer[Mbuffnext - 1] = del; // restore delimiter return STRING; } case '.': save_and_next(LS); if (LS->current == '.') { save_and_next(LS); if (LS->current == '.') { save_and_next(LS); return DOTS; // ... } else return CONC; // .. } else if (!Common::isDigit(LS->current)) return '.'; // LS->current is a digit: goes through to number/ a = 0.0; goto fraction; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': a = 0.0; do { a = 10.0 * a + (LS->current - '0'); save_and_next(LS); } while (Common::isDigit(LS->current)); if (LS->current == '.') { save_and_next(LS); if (LS->current == '.') { save(0); luaY_error("ambiguous syntax (decimal point x string concatenation)"); } } fraction: { double da = 0.1; while (Common::isDigit(LS->current)) { a += (LS->current - '0') * da; da /= 10.0; save_and_next(LS); } if (toupper(LS->current) == 'E') { int32 e = 0; int32 neg; double ea; save_and_next(LS); neg = (LS->current == '-'); if (LS->current == '+' || LS->current == '-') save_and_next(LS); if (!Common::isDigit(LS->current)) { save(0); return WRONGTOKEN; } do { e = 10 * e + (LS->current - '0'); save_and_next(LS); } while (Common::isDigit(LS->current)); for (ea = neg ? 0.1 : 10.0; e > 0; e >>= 1) { if (e & 1) a *= ea; ea *= ea; } } l->vReal = a; return NUMBER; } case EOZ: save(0); if (LS->iflevel > 0) luaY_syntaxerror("input ends inside a $if", ""); return 0; default: if (LS->current != '_' && !Common::isAlpha(LS->current)) { int32 c = LS->current; save_and_next(LS); return c; } else { // identifier or reserved word TaggedString *ts; do { save_and_next(LS); } while (Common::isAlnum(LS->current) || LS->current == '_'); save(0); ts = luaS_new(Mbuffbase); if (ts->head.marked >= 255) return ts->head.marked; // reserved word l->pTStr = ts; return NAME; } } }