Skip to content

Commit 947e41f

Browse files
committed
Fixed tokenize() triggering an error instead of returning the error message.
1 parent 13f5540 commit 947e41f

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

preprocess.lua

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ local IS_LUA_51_OR_LATER = (major == 5 and minor >= 1) or (major ~= nil and majo
172172
local IS_LUA_52_OR_LATER = (major == 5 and minor >= 2) or (major ~= nil and major > 5)
173173
local IS_LUA_53_OR_LATER = (major == 5 and minor >= 3) or (major ~= nil and major > 5)
174174

175+
local NOOP = function()end
176+
175177
local _error = error -- We redefine error() later.
176178

177179
local metaEnv = nil
@@ -205,6 +207,7 @@ local outputLineNumber, maybeOutputLineNumber
205207
local pack, unpack
206208
local parseStringlike
207209
local printf, printTokens, printTraceback
210+
local pushErrorHandler, pushErrorHandlerIfOverridingDefault, popErrorHandler
208211
local serialize, toLua
209212
local tokenize
210213

@@ -265,6 +268,7 @@ function errorOnLine(path, ln, agent, s, ...)
265268
printf("Error @ %s:%d: %s", path, ln, s)
266269
end
267270
currentErrorHandler(s, 2)
271+
return s
268272
end
269273
function errorInFile(contents, path, ptr, agent, s, ...)
270274
s = s:format(...)
@@ -911,6 +915,24 @@ function getLineNumber(s, ptr)
911915
return 1+nlCount
912916
end
913917

918+
do
919+
local errorHandlers = {_error}
920+
function pushErrorHandler(errHand)
921+
table.insert(errorHandlers, errHand)
922+
currentErrorHandler = errHand
923+
end
924+
function pushErrorHandlerIfOverridingDefault(errHand)
925+
pushErrorHandler(currentErrorHandler == _error and errHand or currentErrorHandler)
926+
end
927+
function popErrorHandler()
928+
table.remove(errorHandlers)
929+
if not errorHandlers[1] then
930+
_error("Could not pop error handler.", 2)
931+
end
932+
currentErrorHandler = errorHandlers[#errorHandlers]
933+
end
934+
end
935+
914936
--==============================================================
915937
--= Preprocessor Functions =====================================
916938
--==============================================================
@@ -1030,9 +1052,15 @@ end
10301052
-- tokenize()
10311053
-- Convert Lua code to tokens. Returns nil and a message on error. (See newToken() for token types.)
10321054
-- tokens, error = tokenize( luaString [, allowPreprocessorTokens=false ] )
1033-
-- token = { type=tokenType, representation=representation, value=value, line=lineNumber, lineEnd=lineNumber, position=bytePosition, ... }
1055+
-- token = {
1056+
-- type=tokenType, representation=representation, value=value,
1057+
-- line=lineNumber, lineEnd=lineNumber, position=bytePosition,
1058+
-- ...
1059+
-- }
10341060
function metaFuncs.tokenize(lua, allowMetaTokens)
1035-
local tokens, err = tokenize(lua, "<string>", false, allowMetaTokens)
1061+
pushErrorHandler(NOOP)
1062+
local tokens, err = tokenize(lua, "<string>", allowMetaTokens, allowMetaTokens)
1063+
popErrorHandler()
10361064
return tokens, err
10371065
end
10381066

@@ -1736,15 +1764,15 @@ local function processFileOrString(params, isFile)
17361764
local errorToReturn = nil
17371765

17381766
isDebug = params.debug
1739-
currentErrorHandler = function(err, levelFromOurError)
1767+
pushErrorHandler(function(err, levelFromOurError)
17401768
errorToReturn = err
17411769

17421770
if not levelFromOurError then
17431771
printTraceback(tryToFormatError(err), 2)
17441772
end
17451773

17461774
if params.onError then params.onError(errorToReturn) end
1747-
end
1775+
end)
17481776

17491777
xpcall(
17501778
function()
@@ -1754,7 +1782,7 @@ local function processFileOrString(params, isFile)
17541782
)
17551783

17561784
isDebug = false
1757-
currentErrorHandler = _error
1785+
popErrorHandler()
17581786

17591787
-- Cleanup in case an error happened.
17601788
isRunningMeta = false

0 commit comments

Comments
 (0)