reuse of 'addinfo' by lexical errors

This commit is contained in:
Roberto Ierusalimschy 2015-05-22 14:45:56 -03:00
parent 6dc20ff293
commit 6142e663e4
3 changed files with 21 additions and 19 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ldebug.c,v 2.113 2015/03/11 16:10:41 roberto Exp roberto $ ** $Id: ldebug.c,v 2.114 2015/03/28 19:14:47 roberto Exp roberto $
** Debug Interface ** Debug Interface
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -599,19 +599,16 @@ l_noret luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
} }
static void addinfo (lua_State *L, const char *msg) { /* add src:line information to 'msg' */
CallInfo *ci = L->ci; const char *luaG_addinfo (lua_State *L, const char *msg, TString *src,
if (isLua(ci)) { /* is Lua code? */ int line) {
char buff[LUA_IDSIZE]; /* add file:line information */ char buff[LUA_IDSIZE];
int line = currentline(ci); if (src)
TString *src = ci_func(ci)->p->source; luaO_chunkid(buff, getstr(src), LUA_IDSIZE);
if (src) else { /* no source available; use "?" instead */
luaO_chunkid(buff, getstr(src), LUA_IDSIZE); buff[0] = '?'; buff[1] = '\0';
else { /* no source available; use "?" instead */
buff[0] = '?'; buff[1] = '\0';
}
luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
} }
return luaO_pushfstring(L, "%s:%d: %s", buff, line, msg);
} }
@ -628,10 +625,14 @@ l_noret luaG_errormsg (lua_State *L) {
l_noret luaG_runerror (lua_State *L, const char *fmt, ...) { l_noret luaG_runerror (lua_State *L, const char *fmt, ...) {
CallInfo *ci = L->ci;
const char *msg;
va_list argp; va_list argp;
va_start(argp, fmt); va_start(argp, fmt);
addinfo(L, luaO_pushvfstring(L, fmt, argp)); msg = luaO_pushvfstring(L, fmt, argp); /* format message */
va_end(argp); va_end(argp);
if (isLua(ci)) /* if Lua function, add source:line information */
luaG_addinfo(L, msg, ci_func(ci)->p->source, currentline(ci));
luaG_errormsg(L); luaG_errormsg(L);
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: ldebug.h,v 2.12 2014/11/10 14:46:05 roberto Exp roberto $ ** $Id: ldebug.h,v 2.13 2015/03/11 16:10:41 roberto Exp roberto $
** Auxiliary functions from Debug Interface module ** Auxiliary functions from Debug Interface module
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -30,6 +30,8 @@ LUAI_FUNC l_noret luaG_tointerror (lua_State *L, const TValue *p1,
LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1, LUAI_FUNC l_noret luaG_ordererror (lua_State *L, const TValue *p1,
const TValue *p2); const TValue *p2);
LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...); LUAI_FUNC l_noret luaG_runerror (lua_State *L, const char *fmt, ...);
LUAI_FUNC const char *luaG_addinfo (lua_State *L, const char *msg,
TString *src, int line);
LUAI_FUNC l_noret luaG_errormsg (lua_State *L); LUAI_FUNC l_noret luaG_errormsg (lua_State *L);
LUAI_FUNC void luaG_traceexec (lua_State *L); LUAI_FUNC void luaG_traceexec (lua_State *L);

7
llex.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: llex.c,v 2.91 2015/03/28 19:14:47 roberto Exp roberto $ ** $Id: llex.c,v 2.92 2015/04/03 18:41:57 roberto Exp roberto $
** Lexical Analyzer ** Lexical Analyzer
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -16,6 +16,7 @@
#include "lua.h" #include "lua.h"
#include "lctype.h" #include "lctype.h"
#include "ldebug.h"
#include "ldo.h" #include "ldo.h"
#include "lgc.h" #include "lgc.h"
#include "llex.h" #include "llex.h"
@ -106,9 +107,7 @@ static const char *txtToken (LexState *ls, int token) {
static l_noret lexerror (LexState *ls, const char *msg, int token) { static l_noret lexerror (LexState *ls, const char *msg, int token) {
char buff[LUA_IDSIZE]; msg = luaG_addinfo(ls->L, msg, ls->source, ls->linenumber);
luaO_chunkid(buff, getstr(ls->source), LUA_IDSIZE);
msg = luaO_pushfstring(ls->L, "%s:%d: %s", buff, ls->linenumber, msg);
if (token) if (token)
luaO_pushfstring(ls->L, "%s near %s", msg, txtToken(ls, token)); luaO_pushfstring(ls->L, "%s near %s", msg, txtToken(ls, token));
luaD_throw(ls->L, LUA_ERRSYNTAX); luaD_throw(ls->L, LUA_ERRSYNTAX);