Skip to content

Commit 44cad22

Browse files
committed
Error messages showing lots of code are shortened.
1 parent 5dab0b2 commit 44cad22

File tree

2 files changed

+59
-43
lines changed

2 files changed

+59
-43
lines changed

preprocess.lua

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@
133133

134134
local PP_VERSION = "1.20.0-dev"
135135

136-
local MAX_DUPLICATE_FILE_INSERTS = 1000 -- @Incomplete: Make this a parameter for processFile()/processString().
136+
local MAX_DUPLICATE_FILE_INSERTS = 1000 -- @Incomplete: Make this a parameter for processFile()/processString().
137+
local MAX_CODE_LENGTH_IN_MESSAGES = 60
137138

138139
local KEYWORDS = {
139140
"and","break","do","else","elseif","end","false","for","function","if","in",
@@ -357,7 +358,7 @@ end
357358
-- error("\0"..err, 0) -- The 0 tells our own error handler not to print the traceback.
358359
-- end
359360
local function errorfLine(s, ...)
360-
errorf(0, "\0"..s, ...) -- The 0 tells our own error handler not to print the traceback.
361+
errorf(0, (current_parsingAndMeta_isProcessing and "\0" or "")..s, ...) -- The \0 tells our own error handler not to print the traceback.
361362
end
362363

363364
-- errorOnLine( path, lineNumber, agent=nil, s, ... )
@@ -436,7 +437,7 @@ end
436437

437438
-- runtimeErrorAtToken( level, token, position=token.position, agent, s, ... )
438439
local function runtimeErrorAtToken(level, tok, pos, agent, s, ...)
439-
-- printErrorTraceback("errorAtToken", 2) -- DEBUG
440+
-- printErrorTraceback("runtimeErrorAtToken", 2) -- DEBUG
440441
runtimeErrorInFile(1+level, current_parsingAndMeta_fileBuffers[tok.file], tok.file, (pos or tok.position), agent, s, ...)
441442
end
442443

@@ -457,6 +458,18 @@ end
457458

458459

459460

461+
local function formatCodeForShortMessage(lua)
462+
lua = lua:gsub("^%s+", ""):gsub("%s+$", ""):gsub("%s+", " ")
463+
464+
if #lua > MAX_CODE_LENGTH_IN_MESSAGES then
465+
lua = lua:sub(1, MAX_CODE_LENGTH_IN_MESSAGES/2) .. "..." .. lua:sub(-MAX_CODE_LENGTH_IN_MESSAGES/2)
466+
end
467+
468+
return lua
469+
end
470+
471+
472+
460473
local ERROR_UNFINISHED_STRINGLIKE = 1
461474

462475
local function parseStringlikeToken(s, ptr)
@@ -1354,6 +1367,10 @@ else
13541367
end
13551368
end
13561369

1370+
local function isLuaStringValidExpression(lua)
1371+
return loadLuaString("return("..lua.."\n)", "@", nil) ~= nil
1372+
end
1373+
13571374

13581375

13591376
-- token, index = getNextUsableToken( tokens, startIndex, indexLimit=autoDependingOnDirection, direction )
@@ -1556,7 +1573,7 @@ local function _loadResource(resourceName, isParsing, nameTokOrErrLevel, stats)
15561573
errorAtToken(
15571574
nameTokOrErrLevel, nameTokOrErrLevel.position+1, "Parser",
15581575
"Too many duplicate inserts. We may be stuck in a recursive loop. (Unique files inserted so far: %s)",
1559-
table.concat(stats.insertedNames, ", ")
1576+
stats.insertedNames[1] and table.concat(stats.insertedNames, ", ") or "none"
15601577
)
15611578
end
15621579
end
@@ -1749,7 +1766,7 @@ function metaFuncs.outputLuaTemplate(lua, ...)
17491766
local v, err
17501767

17511768
lua = lua:gsub("%?", function()
1752-
n = n+1
1769+
n = n + 1
17531770
v, err = toLua(args[n])
17541771

17551772
if not v then
@@ -1989,7 +2006,7 @@ local numberFormatters = {
19892006
SCIENTIFIC = function(n) return F("%E", n):gsub("(%d)0+E", "%1E"):gsub("0+(%d+)$", "%1") end,
19902007
e = function(n) return F("%e", n):gsub("(%d)0+e", "%1e"):gsub("0+(%d+)$", "%1") end,
19912008
E = function(n) return F("%E", n):gsub("(%d)0+E", "%1E"):gsub("0+(%d+)$", "%1") end,
1992-
hexadecimal = function(n) return (n == math.floor(n) and F("0x%x", n) or error("Hexadecimal floats not supported yet.", 3)) end,
2009+
hexadecimal = function(n) return (n == math.floor(n) and F("0x%x", n) or error("Hexadecimal floats not supported yet.", 3)) end, -- @Incomplete
19932010
HEXADECIMAL = function(n) return (n == math.floor(n) and F("0x%X", n) or error("Hexadecimal floats not supported yet.", 3)) end,
19942011
hex = function(n) return (n == math.floor(n) and F("0x%x", n) or error("Hexadecimal floats not supported yet.", 3)) end,
19952012
HEX = function(n) return (n == math.floor(n) and F("0x%X", n) or error("Hexadecimal floats not supported yet.", 3)) end,
@@ -2240,6 +2257,10 @@ function metaFuncs.ASSERT(conditionCode, messageCode)
22402257
errorIfNotRunningMeta(2)
22412258
if not conditionCode then error("missing argument #1 to 'ASSERT'", 2) end
22422259

2260+
-- if not isLuaStringValidExpression(conditionCode) then
2261+
-- errorf(2, "Invalid condition expression: %s", formatCodeForShortMessage(conditionCode))
2262+
-- end
2263+
22432264
if current_meta_releaseMode then return end
22442265

22452266
tableInsert(current_meta_output, "if not (")
@@ -2272,11 +2293,11 @@ function metaFuncs.LOG(logLevelCode, valueOrFormatCode, ...)
22722293
if not logLevelCode then error("missing argument #1 to 'LOG'", 2) end
22732294
if not valueOrFormatCode then error("missing argument #2 to 'LOG'", 2) end
22742295

2275-
local chunk = loadLuaString("return 0,"..logLevelCode, "@", dummyEnv)
2276-
if not chunk then errorf(2, "Invalid logLevel expression. Got: %s", logLevelCode) end
2296+
local chunk = loadLuaString("return("..logLevelCode.."\n)", "@", dummyEnv)
2297+
if not chunk then errorf(2, "Invalid logLevel expression: %s", formatCodeForShortMessage(logLevelCode)) end
22772298

2278-
local ok, _, logLevel = pcall(chunk)
2279-
if not ok then errorf(2, "logLevel must be a constant expression. Got: %s", logLevelCode) end
2299+
local ok, logLevel = pcall(chunk)
2300+
if not ok then errorf(2, "logLevel must be a constant expression. Got: %s", formatCodeForShortMessage(logLevelCode)) end
22802301
if not LOG_LEVELS[logLevel] then errorf(2, "Invalid logLevel '%s'.", tostring(logLevel)) end
22812302
if logLevel == "off" then errorf(2, "Invalid logLevel '%s'.", tostring(logLevel)) end
22822303

@@ -2325,7 +2346,7 @@ local function finalizeMacro(lua)
23252346
if lua == nil then
23262347
return (_stopInterceptingOutput(2))
23272348
elseif type(lua) ~= "string" then
2328-
error("[Macro] Value is not Lua code.", 2)
2349+
errorf(2, "[Macro] Value is not Lua code. (Got %s)", type(lua))
23292350
elseif current_meta_output[1] then
23302351
error("[Macro] Got Lua code from both value expression and outputLua(). Only one method may be used.", 2) -- It's also possible interception calls are unbalanced.
23312352
else
@@ -2338,10 +2359,6 @@ function metaEnv.__M()
23382359
return finalizeMacro
23392360
end
23402361

2341-
local function isLuaStringValidExpression(lua)
2342-
return loadLuaString("return("..lua.."\n)", "@", nil) ~= nil
2343-
end
2344-
23452362
-- luaString = __ARG( locationTokenNumber, luaString|callback )
23462363
-- callback = function( )
23472364
function metaEnv.__ARG(locTokNum, v)
@@ -2355,7 +2372,7 @@ function metaEnv.__ARG(locTokNum, v)
23552372
end
23562373

23572374
if current_parsingAndMeta_strictMacroArguments and not isLuaStringValidExpression(lua) then
2358-
runtimeErrorAtToken(2, current_meta_locationTokens[locTokNum], nil, "MacroArgument", "Result is not a valid Lua expression: %s", lua)
2375+
runtimeErrorAtToken(2, current_meta_locationTokens[locTokNum], nil, "MacroArgument", "Argument result is not a valid Lua expression: %s", formatCodeForShortMessage(lua))
23592376
end
23602377

23612378
return lua
@@ -2730,8 +2747,7 @@ local function astParseMetaBlock(tokens)
27302747
if #lua > 100 then
27312748
lua = lua:sub(1, 50) .. "..." .. lua:sub(-50)
27322749
end
2733-
lua = lua:gsub("\n", " ")
2734-
errorAtToken(tokens[ppEntryTokIndex+1], nil, "Parser/MetaBlock", "Ambiguous expression '%s'. (Comma-separated list?)", lua)
2750+
errorAtToken(tokens[ppEntryTokIndex+1], nil, "Parser/MetaBlock", "Ambiguous expression '%s'. (Comma-separated list?)", formatCodeForShortMessage(lua))
27352751
end
27362752

27372753
local astOutNode = ((ppEntryTok.double and AstExpressionCode) or (isExpression and AstExpressionValue or AstMetaprogram))(ppEntryTok, outTokens)
@@ -3142,7 +3158,7 @@ local function astParseMacro(params, tokens)
31423158
tokens.nextI = iNext -- until '!' or '!!'
31433159

31443160
if not isTokenAndNotNil(tokens[tokens.nextI+1], "punctuation", "(") then
3145-
errorAfterToken(tokNext, "Parser/Macro", "Expected '(' after '!'.")
3161+
errorAfterToken(tokNext, "Parser/Macro", "Expected '(' after '%s'.", tokNext.value)
31463162
end
31473163

31483164
astMacro.arguments[1] = MacroArgument(tokNext, {astParseMetaBlock(tokens)}) -- The one and only argument for this macro variant.
@@ -3429,12 +3445,12 @@ local function _processFileOrString(params, isFile)
34293445
error("'pathIn' and 'pathOut' are the same in params.", 2)
34303446
end
34313447

3432-
if (params.pathMeta or "-") ~= "-" then
3433-
if params.pathMeta == params.pathIn then
3434-
error("'pathIn' and 'pathMeta' are the same in params.", 2)
3435-
elseif params.pathMeta == params.pathOut then
3436-
error("'pathOut' and 'pathMeta' are the same in params.", 2)
3437-
end
3448+
if (params.pathMeta or "-") == "-" then -- Should it be possible to output the metaprogram to stdout?
3449+
-- void
3450+
elseif params.pathMeta == params.pathIn then
3451+
error("'pathIn' and 'pathMeta' are the same in params.", 2)
3452+
elseif params.pathMeta == params.pathOut then
3453+
error("'pathOut' and 'pathMeta' are the same in params.", 2)
34383454
end
34393455

34403456
else

tests/suite.lua

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,24 @@ end)
527527

528528
addLabel("Library API")
529529

530+
doTest("Processing calls", function()
531+
local pp = ppChunk()
532+
pp.metaEnvironment.pp = pp
533+
534+
-- Path collisions.
535+
writeFile("temp/generatedTest.lua2p", [[]])
536+
assert( pp.processFile{ pathIn="temp/generatedTest.lua2p", pathOut="temp/generatedTest.lua" , pathMeta=nil })
537+
assert( pp.processFile{ pathIn="temp/generatedTest.lua2p", pathOut="temp/generatedTest.lua" , pathMeta="temp/generatedTest.meta.lua" })
538+
assert(not pp.processFile{ pathIn="temp/generatedTest.lua2p", pathOut="temp/generatedTest.lua2p", pathMeta=nil })
539+
assert(not pp.processFile{ pathIn="temp/generatedTest.lua2p", pathOut="temp/generatedTest.lua" , pathMeta="temp/generatedTest.lua2p" })
540+
assert(not pp.processFile{ pathIn="temp/generatedTest.lua2p", pathOut="temp/generatedTest.lua" , pathMeta="temp/generatedTest.lua" })
541+
542+
-- Recursive processing is not supported.
543+
assert(not pp.processString{ code=[[
544+
!pp.processString{ code="" }
545+
]]})
546+
end)
547+
530548
doTest("Create tokens", function()
531549
local pp = ppChunk()
532550

@@ -708,24 +726,6 @@ doTest("Indentation", function()
708726
local indent, expect = pp.getIndentation("\t \t", 4), 12 ; if indent ~= expect then error(expect.." "..indent) end
709727
end)
710728

711-
doTest("Processing calls", function()
712-
local pp = ppChunk()
713-
pp.metaEnvironment.pp = pp
714-
715-
-- Path collisions.
716-
writeFile("temp/generatedTest.lua2p", [[]])
717-
assert( pp.processFile{ pathIn="temp/generatedTest.lua2p", pathOut="temp/generatedTest.lua" , pathMeta=nil })
718-
assert( pp.processFile{ pathIn="temp/generatedTest.lua2p", pathOut="temp/generatedTest.lua" , pathMeta="temp/generatedTest.meta.lua" })
719-
assert(not pp.processFile{ pathIn="temp/generatedTest.lua2p", pathOut="temp/generatedTest.lua2p", pathMeta=nil })
720-
assert(not pp.processFile{ pathIn="temp/generatedTest.lua2p", pathOut="temp/generatedTest.lua" , pathMeta="temp/generatedTest.lua2p" })
721-
assert(not pp.processFile{ pathIn="temp/generatedTest.lua2p", pathOut="temp/generatedTest.lua" , pathMeta="temp/generatedTest.lua" })
722-
723-
-- Recursive processing. (Not supported!)
724-
assert(not pp.processString{ code=[[
725-
!pp.processString{ code="" }
726-
]]})
727-
end)
728-
729729
doTest("Misc.", function()
730730
local pp = ppChunk()
731731

0 commit comments

Comments
 (0)