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); luaX_error(LS, "unfinished long string", TK_STRING); 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); continue; default: save_and_next(L, LS, l); } } endloop: save_and_next(L, LS, l); /* skip the second ']' */ save(L, '\0', l); seminfo->ts = luaS_newlstr(L, L->Mbuffer+2, l-5); }
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 (+ - / ...) */ } } } } }
static void luaX_lexerror (LexState *ls, const char *s, int token) { if (token == TK_EOS) luaX_error(ls, s, luaX_token2str(ls, token)); else luaX_error(ls, s, luaZ_buffer(ls->buff)); }
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; } } } }
void luaK_error (LexState *ls, const char *msg) { luaX_error(ls, msg, ls->t.token); }