Skip to content

Commit 3727cc4

Browse files
committed
Added outputLuaTemplate(). Fixed some errors not stopping the processing properly.
1 parent 812c692 commit 3727cc4

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

misc/runExamples.cmd

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
@ECHO OFF
22
CD /D "%~dp0.."
33

4+
SETLOCAL EnableDelayedExpansion
5+
46
FOR /R examples %%G IN (*.lua2p) DO (
57
ECHO. & ECHO Processing example '%%~nxG'...
68
lua preprocess-cl.lua "%%G" --debug --silent
7-
lua "%%~dpnG.lua"
9+
IF !ERRORLEVEL! EQU 0 lua "%%~dpnG.lua"
810
)
911

1012
ECHO. & ECHO All examples finished!

preprocess-cl.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ exec lua "$0" "$@"
8383
"init"
8484
Sent before any other message.
8585
Arguments:
86-
paths: Array of file paths to process. Paths can be added or removed freely.
86+
inputPaths: Array of file paths to process. Paths can be added or removed freely.
8787
outputPaths: If the --outputpaths option is present this is an array of output paths for the respective path in inputPaths, otherwise it's nil.
8888
8989
"beforemeta"

preprocess.lua

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
- toLua, serialize
2424
Only during processing:
2525
- getCurrentPathIn, getCurrentPathOut
26-
- outputValue, outputLua
26+
- outputValue, outputLua, outputLuaTemplate
2727
Search this file for 'EnvironmentTable' for more info.
2828
2929
Exported stuff from the library:
@@ -270,6 +270,7 @@ function printTraceback(message, level)
270270
end
271271

272272
function error(err, level)
273+
-- @Check: Should we prepend the path? And, in all or just some cases?
273274
level = 1+(level or 1)
274275
printTraceback(tryToFormatError(err), level)
275276
currentErrorHandler(err, level)
@@ -281,9 +282,9 @@ end
281282
function errorOnLine(path, ln, agent, s, ...)
282283
s = s:format(...)
283284
if agent then
284-
printf("Error @ %s:%d: [%s] %s", path, ln, agent, s)
285+
printf("Error @ %s:%d: [%s] %s\n", path, ln, agent, s)
285286
else
286-
printf("Error @ %s:%d: %s", path, ln, s)
287+
printf("Error @ %s:%d: %s\n", path, ln, s)
287288
end
288289
currentErrorHandler(s, 2)
289290
return s
@@ -1074,6 +1075,35 @@ function metaFuncs.outputLua(...)
10741075
end
10751076
end
10761077

1078+
-- outputLuaTemplate()
1079+
-- Use a string as a template for outputting Lua code with values.
1080+
-- Question marks (?) are replaced with the values.
1081+
-- outputLuaTemplate( luaStringTemplate, value1, ... )
1082+
-- Examples:
1083+
-- outputLuaTemplate("local name, age = ?, ?", "Harry", 48)
1084+
-- outputLuaTemplate("dogs[?] = ?", "greyhound", {italian=false, count=5})
1085+
function metaFuncs.outputLuaTemplate(lua, ...)
1086+
errorIfNotRunningMeta(2)
1087+
assertarg(1, lua, "string")
1088+
1089+
local args = {...}
1090+
local n = 0
1091+
local v, err
1092+
1093+
lua = lua:gsub("%?", function()
1094+
n = n+1
1095+
v, err = toLua(args[n])
1096+
1097+
if not v then
1098+
error(F("Bad argument %d: %s", 1+n, err), 3)
1099+
end
1100+
1101+
return assert(v)
1102+
end)
1103+
1104+
table.insert(outputFromMeta, lua)
1105+
end
1106+
10771107
-- getCurrentPathIn()
10781108
-- Get what file is currently being processed, if any.
10791109
-- path = getCurrentPathIn( )
@@ -1841,19 +1871,25 @@ local function _processFileOrString(params, isFile)
18411871
end
18421872
end
18431873

1874+
local ERROR_REDIRECTION = setmetatable({}, {__tostring=function()return"Internal redirection of error."end})
1875+
18441876
local function processFileOrString(params, isFile)
18451877
local returnValues = nil
18461878
local errorToReturn = nil
18471879

18481880
isDebug = params.debug
18491881
pushErrorHandler(function(err, levelFromOurError)
1882+
if err == ERROR_REDIRECTION then return end
1883+
18501884
errorToReturn = err
18511885

18521886
if not levelFromOurError then
18531887
printTraceback(tryToFormatError(err), 2)
18541888
end
18551889

18561890
if params.onError then params.onError(errorToReturn) end
1891+
1892+
if levelFromOurError then _error(ERROR_REDIRECTION) end
18571893
end)
18581894

18591895
xpcall(

0 commit comments

Comments
 (0)