static int read_id_or_reserved(lexer_state *ls, token_info *info) { while(lisalnum(ls->current)) save_and_next(ls); // check for reserved words for(int i = 0; i < NUM_TOK; ++i) { if(!strcmp(ls->buf->buf, tokens[i])) return FIRST_TOK + i; } // not a reserved word, must be an id info->string = strdup(ls->buf->buf); return TK_ID; }
static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) { int line = ls->linenumber; /* initial line (for error message) */ save_and_next(ls); /* skip 2nd '[' */ if (currIsNewline(ls)) /* string starts with a newline? */ inclinenumber(ls); /* skip it */ for (;;) { switch (ls->current) { case EOZ: { /* error */ const char *what = (seminfo ? "string" : "comment"); const char *msg = luaO_pushfstring(ls->L, "unfinished long %s (starting at line %d)", what, line); lexerror(ls, msg, TK_EOS); break; /* to avoid warnings */ } case ']': { if (skip_sep(ls) == sep) { save_and_next(ls); /* skip 2nd ']' */ goto endloop; } break; } case '\n': case '\r': { save(ls, '\n'); inclinenumber(ls); if (!seminfo) luaZ_resetbuffer(ls->buff); /* avoid wasting space */ break; } default: { if (seminfo) save_and_next(ls); else next(ls); } } } endloop: if (seminfo) seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + (2 + sep), luaZ_bufflen(ls->buff) - 2*(2 + sep)); }
static size_t readname (LexState *LS, char ch) { size_t l = 0; checkbuffer(LS, l); if (ch != 0) { checkbuffer(LS, l); save(LS, ch, l); } do { checkbuffer(LS, l); save_and_next(LS, l); } while (lex_isalnum(LS->current) || LS->current == '_'); save(LS, '\0', l); return l-1; }
static void read_long_string (LexState *LS, SemInfo *seminfo) { lua_State *L = LS->L; int cont = 0; size_t l = 0; checkbuffer(L, 10, l); save(L, '[', l); /* save first '[' */ save_and_next(L, LS, l); /* pass the second '[' */ for (;;) { checkbuffer(L, 10, l); switch (LS->current) { case EOZ: save(L, '\0', l); if (seminfo) luaX_error(LS, "unfinished long string", TK_STRING); else luaX_error(LS, "unfinished comment", TK_EOS); break; /* to avoid warnings */ case '[': save_and_next(L, LS, l); if (LS->current == '[') { cont++; save_and_next(L, LS, l); } continue; case ']': save_and_next(L, LS, l); if (LS->current == ']') { if (cont == 0) goto endloop; cont--; save_and_next(L, LS, l); } continue; case '\n': save(L, '\n', l); inclinenumber(LS); if (LS->current == '\r') next(LS); continue; case '\r': save(L, '\n', l); inclinenumber(LS); if (LS->current == '\n') next(LS); continue; default: if (seminfo) /* no need to save complete comment */ save(L, LS->current, l); next(LS); } } endloop: save_and_next(L, LS, l); /* skip the second ']' */ save(L, '\0', l); if (seminfo) seminfo->ts = luaS_newlstr(L, L->Mbuffer+2, l-5); }
static void read_long_string (LexState *LS, SemInfo *seminfo) { int cont = 0; size_t l = 0; checkbuffer(LS, l); save(LS, '[', l); /* save first `[' */ save_and_next(LS, l); /* pass the second `[' */ if (LS->current == '\n') /* string starts with a newline? */ inclinenumber(LS); /* skip it */ for (;;) { checkbuffer(LS, l); switch (LS->current) { case EOZ: save(LS, '\0', l); luaX_lexerror(LS, (seminfo) ? "unfinished long string" : "unfinished long comment", TK_EOS); break; /* to avoid warnings */ case '[': save_and_next(LS, l); if (LS->current == '[') { cont++; save_and_next(LS, l); } continue; case ']': save_and_next(LS, l); if (LS->current == ']') { if (cont == 0) goto endloop; cont--; save_and_next(LS, l); } continue; case '\n': save(LS, '\n', l); inclinenumber(LS); if (!seminfo) l = 0; /* reset buffer to avoid wasting space */ continue; default: save_and_next(LS, l); } } endloop: save_and_next(LS, l); /* skip the second `]' */ save(LS, '\0', l); if (seminfo) seminfo->ts = luaS_newlstr(LS->L, luaZ_buffer(LS->buff) + 2, l - 5); }
/* Parse a number literal. */ static void lex_number(LexState *ls, TValue *tv) { int c; lua_assert(lj_char_isdigit(ls->current)); do { c = ls->current; save_and_next(ls); } while (lj_char_isident(ls->current) || ls->current == '.' || ((ls->current == '-' || ls->current == '+') && ((c & ~0x20) == 'E' || (c & ~0x20) == 'P'))); #if LJ_HASFFI c &= ~0x20; if ((c == 'I' || c == 'L' || c == 'U') && !ctype_ctsG(G(ls->L))) lex_loadffi(ls->L); if (c == 'I') /* Parse imaginary part of complex number. */ ls->sb.n--; #endif save(ls, '\0'); #if LJ_HASFFI if ((c == 'L' || c == 'U') && lex_number64(ls, tv)) { /* Parse 64 bit int. */ return; } else #endif if (lj_str_numconv(ls->sb.buf, tv)) { #if LJ_HASFFI if (c == 'I') { /* Return cdata holding a complex number. */ GCcdata *cd = lj_cdata_new_(ls->L, CTID_COMPLEX_DOUBLE, 2*sizeof(double)); ((double *)cdataptr(cd))[0] = 0; ((double *)cdataptr(cd))[1] = numberVnum(tv); lj_parse_keepcdata(ls, tv, cd); } #endif if (LJ_DUALNUM && tvisnum(tv)) { int32_t k = lj_num2int(numV(tv)); if ((lua_Number)k == numV(tv)) /* -0 cannot end up here. */ setintV(tv, k); } return; } lj_lex_error(ls, TK_number, LJ_ERR_XNUMBER); }
static int read_long_string(LexState *LS, YYSTYPE *l) { int cont = 0; while (1) { switch (LS->current) { case EOZ: save(0); return WRONGTOKEN; case '[': save_and_next(LS); if (LS->current == '[') { cont++; save_and_next(LS); } continue; case ']': save_and_next(LS); if (LS->current == ']') { if (cont == 0) goto endloop; cont--; save_and_next(LS); } continue; case '\n': save('\n'); inclinenumber(LS); continue; default: save_and_next(LS); } } endloop: save_and_next(LS); // pass the second ']' Mbuffer[Mbuffnext - 2] = 0; // erases ']]' l->pTStr = luaS_new(Mbuffbase + 2); Mbuffer[Mbuffnext - 2] = ']'; // restores ']]' return STRING; }
static int gethexa (LexState *ls) { save_and_next(ls); esccheck (ls, lisxdigit(ls->current), "hexadecimal digit expected"); return luaO_hexavalue(ls->current); }
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 void read_string(LexState *ls, int delim, TValue *tv) { save_and_next(ls); while (ls->current != delim) { switch (ls->current) { case END_OF_STREAM: lj_lex_error(ls, TK_eof, LJ_ERR_XSTR); continue; case '\n': case '\r': lj_lex_error(ls, TK_string, LJ_ERR_XSTR); continue; case '\\': { int c = next(ls); /* Skip the '\\'. */ switch (c) { case 'a': c = '\a'; break; case 'b': c = '\b'; break; case 'f': c = '\f'; break; case 'n': c = '\n'; break; case 'r': c = '\r'; break; case 't': c = '\t'; break; case 'v': c = '\v'; break; case 'x': /* Hexadecimal escape '\xXX'. */ c = (next(ls) & 15u) << 4; if (!lj_char_isdigit(ls->current)) { if (!lj_char_isxdigit(ls->current)) goto err_xesc; c += 9 << 4; } c += (next(ls) & 15u); if (!lj_char_isdigit(ls->current)) { if (!lj_char_isxdigit(ls->current)) goto err_xesc; c += 9; } break; case 'z': /* Skip whitespace. */ next(ls); while (lj_char_isspace(ls->current)) if (currIsNewline(ls)) inclinenumber(ls); else next(ls); continue; case '\n': case '\r': save(ls, '\n'); inclinenumber(ls); continue; case '\\': case '\"': case '\'': break; case END_OF_STREAM: continue; default: if (!lj_char_isdigit(c)) goto err_xesc; c -= '0'; /* Decimal escape '\ddd'. */ if (lj_char_isdigit(next(ls))) { c = c*10 + (ls->current - '0'); if (lj_char_isdigit(next(ls))) { c = c*10 + (ls->current - '0'); if (c > 255) { err_xesc: lj_lex_error(ls, TK_string, LJ_ERR_XESC); } next(ls); } } save(ls, c); continue; } save(ls, c); next(ls); continue; } default: save_and_next(ls); break; } } save_and_next(ls); /* skip delimiter */ setstrV(ls->L, tv, lj_parse_keepstr(ls, ls->sb.buf + 1, ls->sb.n - 2)); }
static void read_string (LexState *LS, int del, SemInfo *seminfo) { size_t l = 0; checkbuffer(LS, l); save_and_next(LS, l); while (LS->current != del) { checkbuffer(LS, l); switch (LS->current) { case EOZ: save(LS, '\0', l); luaX_lexerror(LS, "unfinished string", TK_EOS); break; /* to avoid warnings */ case '\n': save(LS, '\0', l); luaX_lexerror(LS, "unfinished string", TK_STRING); break; /* to avoid warnings */ case '\\': next(LS); /* do not save the `\' */ switch (LS->current) { case 'a': save(LS, '\a', l); next(LS); break; case 'b': save(LS, '\b', l); next(LS); break; case 'f': save(LS, '\f', l); next(LS); break; case 'n': save(LS, '\n', l); next(LS); break; case 'r': save(LS, '\r', l); next(LS); break; case 't': save(LS, '\t', l); next(LS); break; case 'v': save(LS, '\v', l); next(LS); break; case '\n': save(LS, '\n', l); inclinenumber(LS); break; case EOZ: break; /* will raise an error next loop */ case 'x': { int ch; next(LS); ch = tolower(LS->current); if (!lex_isdigit(ch) && !(ch >= 'a' && ch <= 'f') ) save(LS, 'x', l); /* handles \\, \", \', and \? */ else { /* \xxx */ int c = 0; int i = 0; int numDigits = 2; do { ch = tolower(LS->current); if (lex_isdigit(ch)) c = 16*c + (ch-'0'); else if (ch >= 'a' && ch <= 'f') c = 16*c + (ch-'a') + 10; next(LS); ch = tolower(LS->current); } while (++i<numDigits && (lex_isdigit(ch) || (ch >= 'a' && ch <= 'f'))); save(LS, c, l); } break; } default: { if (!lex_isdigit(LS->current)) save_and_next(LS, l); /* handles \\, \", \', and \? */ else { /* \xxx */ int c = 0; int i = 0; do { c = 10*c + (LS->current-'0'); next(LS); } while (++i<3 && lex_isdigit(LS->current)); if (c > UCHAR_MAX) { save(LS, '\0', l); luaX_lexerror(LS, "escape sequence too large", TK_STRING); } save(LS, c, l); } } } break; default: save_and_next(LS, l); } } save_and_next(LS, l); /* skip delimiter */ save(LS, '\0', l); seminfo->ts = luaS_newlstr(LS->L, luaZ_buffer(LS->buff) + 1, l - 3); }
static void read_string(LexState *ls) { save_and_next(ls); while (ls->current != '"') { switch (ls->current) { case END_OF_STREAM: lj_lex_error(ls, TK_eof, LJ_ERR_XSTR); continue; case '\\': { int c = next(ls); /* Skip the '\\'. */ switch (c) { case 'a': c = '\a'; break; case 'b': c = '\b'; break; case 'f': c = '\f'; break; case 'n': c = '\n'; break; case 'r': c = '\r'; break; case 't': c = '\t'; break; case 'v': c = '\v'; break; case 'x': /* Hexadecimal escape '\xXX'. */ c = (next(ls) & 15u) << 4; if (!lj_char_isdigit(ls->current)) { if (!lj_char_isxdigit(ls->current)) goto err_xesc; c += 9 << 4; } c += (next(ls) & 15u); if (!lj_char_isdigit(ls->current)) { if (!lj_char_isxdigit(ls->current)) goto err_xesc; c += 9; } break; case 'u': /* Unicode escape '\uXXXX'. */ c = (next(ls) & 15u) << 12; if (!lj_char_isdigit(ls->current)) { if (!lj_char_isxdigit(ls->current)) goto err_xesc; c += 9 << 12; } c += (next(ls) & 15u) << 8; if (!lj_char_isdigit(ls->current)) { if (!lj_char_isxdigit(ls->current)) goto err_xesc; c += 9 << 8; } c += (next(ls) & 15u) << 4; if (!lj_char_isdigit(ls->current)) { if (!lj_char_isxdigit(ls->current)) goto err_xesc; c += 9 << 4; } c += (next(ls) & 15u); if (!lj_char_isdigit(ls->current)) { if (!lj_char_isxdigit(ls->current)) goto err_xesc; c += 9; } if (c >= 0x0800) { save(ls, 0xE0 | (c >> 12)); save(ls, 0x80 | ((c >> 6) & 0x3f)); c = 0x80 | (c & 0x3f); } else if (c >= 0x0080) { save(ls, 0xC0 | (c >> 6)); c = 0x80 | (c & 0x3f); } break; case 'U': /* Unicode escape '\UXXXXXXXX'. */ c = (next(ls) & 15u) << 28; if (!lj_char_isdigit(ls->current)) { if (!lj_char_isxdigit(ls->current)) goto err_xesc; c += 9 << 28; } c += (next(ls) & 15u) << 24; if (!lj_char_isdigit(ls->current)) { if (!lj_char_isxdigit(ls->current)) goto err_xesc; c += 9 << 24; } c += (next(ls) & 15u) << 20; if (!lj_char_isdigit(ls->current)) { if (!lj_char_isxdigit(ls->current)) goto err_xesc; c += 9 << 20; } c += (next(ls) & 15u) << 16; if (!lj_char_isdigit(ls->current)) { if (!lj_char_isxdigit(ls->current)) goto err_xesc; c += 9 << 16; } c += (next(ls) & 15u) << 12; if (!lj_char_isdigit(ls->current)) { if (!lj_char_isxdigit(ls->current)) goto err_xesc; c += 9 << 12; } c += (next(ls) & 15u) << 8; if (!lj_char_isdigit(ls->current)) { if (!lj_char_isxdigit(ls->current)) goto err_xesc; c += 9 << 8; } c += (next(ls) & 15u) << 4; if (!lj_char_isdigit(ls->current)) { if (!lj_char_isxdigit(ls->current)) goto err_xesc; c += 9 << 4; } c += (next(ls) & 15u); if (!lj_char_isdigit(ls->current)) { if (!lj_char_isxdigit(ls->current)) goto err_xesc; c += 9; } if (c >= 0x4000000) { save(ls, 0xFC | (c >> 30)); save(ls, 0x80 | ((c >> 24) & 0x3f)); save(ls, 0x80 | ((c >> 18) & 0x3f)); save(ls, 0x80 | ((c >> 12) & 0x3f)); save(ls, 0x80 | ((c >> 6) & 0x3f)); c = 0x80 | (c & 0x3f); } else if (c >= 0x200000) {
static void read_string (LexState *LS, int del, SemInfo *seminfo) { size_t l = 0; checkbuffer(LS, l); save_and_next(LS, l); while (LS->current != del) { checkbuffer(LS, l); unsigned char b_current = (unsigned char)LS->current; if (b_current & 0x80) { save_and_next(LS, l); save_and_next(LS, l); } else { switch (b_current) { case EOZ: save(LS, '\0', l); luaX_lexerror(LS, "unfinished string", TK_EOS); break; /* to avoid warnings */ case '\n': save(LS, '\0', l); luaX_lexerror(LS, "unfinished string", TK_STRING); break; /* to avoid warnings */ case '\\': next(LS); /* do not save the `\' */ switch (LS->current) { case 'a': save(LS, '\a', l); next(LS); break; case 'b': save(LS, '\b', l); next(LS); break; case 'f': save(LS, '\f', l); next(LS); break; case 'n': save(LS, '\n', l); next(LS); break; case 'r': save(LS, '\r', l); next(LS); break; case 't': save(LS, '\t', l); next(LS); break; case 'v': save(LS, '\v', l); next(LS); break; case '\n': save(LS, '\n', l); inclinenumber(LS); break; case EOZ: break; /* will raise an error next loop */ default: { if (!isdigit(LS->current)) save_and_next(LS, l); /* handles \\, \", \', and \? */ else { /* \xxx */ int c = 0; int i = 0; do { c = 10*c + (LS->current-'0'); next(LS); } while (++i<3 && isdigit(LS->current)); if (c > UCHAR_MAX) { save(LS, '\0', l); luaX_lexerror(LS, "escape sequence too large", TK_STRING); } save(LS, c, l); } } } break; default: save_and_next(LS, l); } } } save_and_next(LS, l); /* skip delimiter */ save(LS, '\0', l); seminfo->ts = luaS_newlstr(LS->L, luaZ_buffer(LS->buff) + 1, l - 3); }
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; } } } }
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; } } }
static void read_string (LexState *ls, int del, SemInfo *seminfo) { save_and_next(ls); /* keep delimiter (for error messages) */ while (ls->current != del) { switch (ls->current) { case EOZ: lexerror(ls, "unfinished string", TK_EOS); break; /* to avoid warnings */ case '\n': case '\r': lexerror(ls, "unfinished string", TK_STRING); break; /* to avoid warnings */ case '\\': { /* escape sequences */ int c; /* final character to be saved */ save_and_next(ls); /* keep '\\' for error messages */ switch (ls->current) { case 'a': c = '\a'; goto read_save; case 'b': c = '\b'; goto read_save; case 'f': c = '\f'; goto read_save; case 'n': c = '\n'; goto read_save; case 'r': c = '\r'; goto read_save; case 't': c = '\t'; goto read_save; case 'v': c = '\v'; goto read_save; case 'x': c = readhexaesc(ls); goto read_save; case 'u': utf8esc(ls); goto no_save; case '\n': case '\r': inclinenumber(ls); c = '\n'; goto only_save; case '\\': case '\"': case '\'': c = ls->current; goto read_save; case EOZ: goto no_save; /* will raise an error next loop */ case 'z': { /* zap following span of spaces */ luaZ_buffremove(ls->buff, 1); /* remove '\\' */ next(ls); /* skip the 'z' */ while (lisspace(ls->current)) { if (currIsNewline(ls)) inclinenumber(ls); else next(ls); } goto no_save; } default: { esccheck(ls, lisdigit(ls->current), "invalid escape sequence"); c = readdecesc(ls); /* digital escape '\ddd' */ goto only_save; } } read_save: next(ls); /* go through */ only_save: luaZ_buffremove(ls->buff, 1); /* remove '\\' */ save(ls, c); /* go through */ no_save: break; } default: save_and_next(ls); } } save_and_next(ls); /* skip delimiter */ seminfo->ts = luaX_newstring(ls, luaZ_buffer(ls->buff) + 1, luaZ_bufflen(ls->buff) - 2); }
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; } } } } }
/* LUA_NUMBER */ static int read_numeral (LexState *LS, int period, SemInfo *seminfo) { int isReal = 0; int startsWithZero = LS->current == '0'; size_t l = 0; checkbuffer(LS, l); if (period) { save(LS, '.', l); isReal = 1; } if (startsWithZero) { next(LS); if (LS->current == 'x') { /* Process a hex number */ int ch = 0; int c = 0; int i = 0; int numDigits = 8; next(LS); do { ch = tolower(LS->current); if (lex_isdigit(ch)) c = 16*c + (ch-'0'); else if (ch >= 'a' && ch <= 'f') c = 16*c + (ch-'a') + 10; next(LS); ch = tolower(LS->current); } while (++i<numDigits && (lex_isdigit(ch) || (ch >= 'a' && ch <= 'f'))); seminfo->r = c; return TK_NUMBER; } else { checkbuffer(LS, 1); save(LS, '0', l); } } while (lex_isdigit(LS->current)) { checkbuffer(LS, l); save_and_next(LS, l); } if (LS->current == '.') { isReal = 1; save_and_next(LS, l); if (LS->current == '.') { save_and_next(LS, l); save(LS, '\0', l); luaX_lexerror(LS, "ambiguous syntax (decimal point x string concatenation)", TK_NUMBER); } } while (lex_isdigit(LS->current)) { checkbuffer(LS, l); save_and_next(LS, l); } if (LS->current == 'e' || LS->current == 'E') { isReal = 1; save_and_next(LS, l); /* read `E' */ if (LS->current == '+' || LS->current == '-') save_and_next(LS, l); /* optional exponent sign */ while (lex_isdigit(LS->current)) { checkbuffer(LS, l); save_and_next(LS, l); } } save(LS, '\0', l); if (isReal) { if (!luaO_str2d(luaZ_buffer(LS->buff), &seminfo->r)) luaX_lexerror(LS, "malformed number", TK_NUMBER); return TK_NUMBER; } else { if (!luaO_str2d(luaZ_buffer(LS->buff), &seminfo->r)) luaX_lexerror(LS, "malformed integer", TK_NUMBER); return TK_NUMBER; } }