Skip to content

Commit 0599396

Browse files
committed
params.pathMeta is now optional. Added --meta to the command line program.
Omitting params.pathMeta will prevent a file with the metaprogram from being written to disk.
1 parent 00be234 commit 0599396

File tree

2 files changed

+47
-31
lines changed

2 files changed

+47
-31
lines changed

main.lua

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ exec lua "$0" "$@"
1515
--==============================================================
1616
1717
Script usage:
18-
lua main.lua [options] [--] path1 [path2 ...]
18+
lua main.lua [options] [--] filepath1 [filepath2 ...]
1919
2020
Options:
2121
--handler=pathToMessageHandler
@@ -26,6 +26,11 @@ exec lua "$0" "$@"
2626
--linenumbers
2727
Add comments with line numbers to the output.
2828
29+
--meta
30+
Output the metaprogram to a temporary file (*.meta.lua). Useful
31+
if an error happens in the metaprogram. This file is removed if
32+
there's no error and --debug isn't enabled.
33+
2934
--outputextension=fileExtension
3035
Specify what file extension generated files should have. The
3136
default is "lua". If any input files end in .lua then you must
@@ -42,7 +47,8 @@ exec lua "$0" "$@"
4247
4348
--debug
4449
Enable some preprocessing debug features. Useful if you want
45-
to inspect the generated metaprogram (*.meta.lua).
50+
to inspect the generated metaprogram (*.meta.lua). (This also
51+
enables the --meta option.)
4652
4753
--
4854
Stop options from being parsed further. Needed if you have
@@ -144,6 +150,7 @@ math.random() -- Must kickstart...
144150

145151
local processOptions = true
146152
local messageHandlerPath = ""
153+
local outputMeta = false
147154
local paths = {}
148155

149156
for _, arg in ipairs(args) do
@@ -161,14 +168,18 @@ for _, arg in ipairs(args) do
161168
addLineNumbers = true
162169

163170
elseif arg == "--debug" then
164-
isDebug = true
171+
isDebug = true
172+
outputMeta = true
165173

166174
elseif arg:find"^%-%-saveinfo=" then
167175
processingInfoPath = arg:match"^%-%-saveinfo=(.*)$"
168176

169177
elseif arg:find"^%-%-outputextension=" then
170178
outputExtension = arg:match"^%-%-outputextension=(.*)$"
171179

180+
elseif arg == "--meta" then
181+
outputMeta = true
182+
172183
else
173184
errorline("Unknown option '"..arg.."'.")
174185
end
@@ -240,9 +251,13 @@ for _, path in ipairs(paths) do
240251
local pathMeta = path:gsub("%.%w+$", "")..".meta.lua"
241252
local pathOut = path:gsub("%.%w+$", "").."."..outputExtension
242253

254+
if not outputMeta then
255+
pathMeta = nil
256+
end
257+
243258
local info = pp.processFile{
244259
pathIn = path,
245-
pathMeta = pathMeta, --@Cleanup: Get rid of parameters.pathMeta?
260+
pathMeta = pathMeta,
246261
pathOut = pathOut,
247262

248263
debug = isDebug,

preprocess.lua

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,16 @@ local ESCAPE_SEQUENCES = {
116116

117117
local ERROR_UNFINISHED_VALUE = 1
118118

119-
local _error = error
119+
local _error = error
120120

121-
local metaEnv = nil
121+
local metaEnv = nil
122122

123-
local isDebug = false
124-
local onError = _error
123+
local isDebug = false
124+
local onError = _error
125125

126-
local isRunningMeta = false
127-
local currentMetaPath = ""
128-
local outputFromMeta = nil
126+
local isRunningMeta = false
127+
local metaPathForErrorMessages = ""
128+
local outputFromMeta = nil
129129

130130
--==============================================================
131131
--= Local Functions ============================================
@@ -740,7 +740,7 @@ function metaFuncs.outputValue(...)
740740
if argCount == 0 then
741741
error("No values to output.", 2)
742742
-- local ln = debug.getinfo(2, "l").currentline
743-
-- errorOnLine(currentMetaPath, ln, "MetaProgram", "No values to output.")
743+
-- errorOnLine(metaPathForErrorMessages, ln, "MetaProgram", "No values to output.")
744744
end
745745

746746
for i = 1, argCount do
@@ -749,7 +749,7 @@ function metaFuncs.outputValue(...)
749749

750750
if not ok then
751751
local ln = debug.getinfo(2, "l").currentline
752-
errorOnLine(currentMetaPath, ln, "MetaProgram", "%s", err)
752+
errorOnLine(metaPathForErrorMessages, ln, "MetaProgram", "%s", err)
753753
end
754754
end
755755
end
@@ -764,7 +764,7 @@ function metaFuncs.outputLua(...)
764764
if argCount == 0 then
765765
error("No Lua code to output.", 2)
766766
-- local ln = debug.getinfo(2, "l").currentline
767-
-- errorOnLine(currentMetaPath, ln, "MetaProgram", "No Lua code to output.")
767+
-- errorOnLine(metaPathForErrorMessages, ln, "MetaProgram", "No Lua code to output.")
768768
end
769769

770770
for i = 1, argCount do
@@ -936,9 +936,8 @@ metaEnv.__LUA = metaEnv.outputLua
936936

937937

938938
local function _processFile(params)
939-
if not params.pathIn then error("Missing 'pathIn' in params.", 2) end
940-
if not params.pathMeta then error("Missing 'pathMeta' in params.", 2) end
941-
if not params.pathOut then error("Missing 'pathOut' in params.", 2) end
939+
if not params.pathIn then error("Missing 'pathIn' in params.", 2) end
940+
if not params.pathOut then error("Missing 'pathOut' in params.", 2) end
942941

943942
local luaUnprocessed, err = getFileContents(params.pathIn)
944943
if not luaUnprocessed then
@@ -1205,17 +1204,19 @@ local function _processFile(params)
12051204
print("====================================")
12061205
--]]
12071206

1208-
currentMetaPath = params.pathMeta
1207+
metaPathForErrorMessages = params.pathMeta or "<meta>"
12091208
outputFromMeta = {}
12101209

1211-
local file = assert(io.open(currentMetaPath, "wb"))
1212-
file:write(luaMeta)
1213-
file:close()
1210+
if params.pathMeta then
1211+
local file = assert(io.open(params.pathMeta, "wb"))
1212+
file:write(luaMeta)
1213+
file:close()
1214+
end
12141215

1215-
local chunk, err = loadstring(luaMeta, currentMetaPath)
1216+
local chunk, err = loadstring(luaMeta, metaPathForErrorMessages)
12161217
if not chunk then
12171218
local ln, err = err:match'^%[string ".-"%]:(%d+): (.*)'
1218-
errorOnLine(currentMetaPath, tonumber(ln), nil, "%s", err)
1219+
errorOnLine(metaPathForErrorMessages, tonumber(ln), nil, "%s", err)
12191220
end
12201221
setfenv(chunk, metaEnv)
12211222

@@ -1232,8 +1233,8 @@ local function _processFile(params)
12321233
end)
12331234
isRunningMeta = false
12341235

1235-
if not isDebug then
1236-
os.remove(currentMetaPath)
1236+
if not isDebug and params.pathMeta then
1237+
os.remove(params.pathMeta)
12371238
end
12381239

12391240
local lua = table.concat(outputFromMeta)
@@ -1243,8 +1244,8 @@ local function _processFile(params)
12431244
print("====================================")
12441245
--]]
12451246

1246-
currentMetaPath = ""
1247-
outputFromMeta = nil
1247+
metaPathForErrorMessages = ""
1248+
outputFromMeta = nil
12481249

12491250
if params.onAfterMeta then params.onAfterMeta(lua) end
12501251

@@ -1295,9 +1296,9 @@ local function processFile(params)
12951296
onError = _error
12961297

12971298
-- Cleanup in case an error happened.
1298-
isRunningMeta = false
1299-
currentMetaPath = ""
1300-
outputFromMeta = nil
1299+
isRunningMeta = false
1300+
metaPathForErrorMessages = ""
1301+
outputFromMeta = nil
13011302

13021303
return info, err
13031304
end
@@ -1319,7 +1320,7 @@ local lib = {
13191320
--
13201321
-- params: Table with these fields:
13211322
-- pathIn = pathToInputFile -- [Required]
1322-
-- pathMeta = pathForMetaprogram -- [Required] You can inspect this temporary output file if an error ocurrs in the metaprogram.
1323+
-- pathMeta = pathForMetaprogram -- [Optional] You can inspect this temporary output file if an error ocurrs in the metaprogram.
13231324
-- pathOut = pathToOutputFile -- [Required]
13241325
--
13251326
-- addLineNumbers = boolean -- [Optional] Add comments with line numbers to the output.

0 commit comments

Comments
 (0)