Skip to content

Commit af5ae0e

Browse files
committed
Added params.strictMacroArguments and --nostrictmacroarguments.
Macro argument validation is on by default. Fixed evaluate() treating empty code as a valid expression.
1 parent 7e9959a commit af5ae0e

File tree

5 files changed

+254
-189
lines changed

5 files changed

+254
-189
lines changed

preprocess-cl.lua

Lines changed: 37 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Options:
6060
6161
--jitsyntax
6262
Allow LuaJIT-specific syntax, specifically literals for 64-bit
63-
integers and complex numbers.
63+
integers, complex numbers and binary numbers.
6464
(https://luajit.org/ext_ffi_api.html#literals)
6565
6666
--linenumbers
@@ -86,7 +86,10 @@ Options:
8686
Stop the garbage collector. This may speed up the preprocessing.
8787
8888
--nonil
89-
Disallow !(...) and outputValue(...) from outputting nil.
89+
Disallow !(expression) and outputValue() from outputting nil.
90+
91+
--nostrictmacroarguments
92+
Disable checks that macro arguments are valid Lua expressions.
9093
9194
--novalidate
9295
Disable validation of outputted Lua.
@@ -178,20 +181,6 @@ local startClock = os.clock()
178181

179182
local args = arg
180183

181-
local major, minor = _VERSION:match"Lua (%d+)%.(%d+)"
182-
if not major then
183-
io.stderr:write("[LuaPreprocess] Warning: Could not detect Lua version.\n")
184-
else
185-
major = tonumber(major)
186-
minor = tonumber(minor)
187-
end
188-
local IS_LUA_51 = (major == 5 and minor == 1)
189-
local IS_LUA_52 = (major == 5 and minor == 2)
190-
local IS_LUA_53 = (major == 5 and minor == 3)
191-
local IS_LUA_51_OR_LATER = (major == 5 and minor >= 1) or (major ~= nil and major > 5)
192-
local IS_LUA_52_OR_LATER = (major == 5 and minor >= 2) or (major ~= nil and major > 5)
193-
local IS_LUA_53_OR_LATER = (major == 5 and minor >= 3) or (major ~= nil and major > 5)
194-
195184
if not args[0] then error("Expected to run from the Lua interpreter.") end
196185
local pp = dofile((args[0]:gsub("[^/\\]+$", "preprocess.lua")))
197186

@@ -214,17 +203,14 @@ local macroPrefix = ""
214203
local macroSuffix = ""
215204
local releaseMode = false
216205
local maxLogLevel = "trace"
206+
local strictMacroArguments = true
217207

218208
--==============================================================
219-
--= Local Functions ============================================
209+
--= Local functions ============================================
220210
--==============================================================
221-
local errorLine
222-
local F, formatBytes, formatInt
223-
local loadLuaFile
224-
local printf, printfNoise, printError, printfError
211+
local F = string.format
225212

226-
F = string.format
227-
function formatBytes(n)
213+
local function formatBytes(n)
228214
if n >= 1024*1024*1024 then
229215
return F("%.2f GiB", n/(1024*1024*1024))
230216
elseif n >= 1024*1024 then
@@ -237,49 +223,38 @@ function formatBytes(n)
237223
return F("%d bytes", n)
238224
end
239225
end
240-
-- function formatInt(n)
241-
-- return
242-
-- F("%.0f", n)
243-
-- :reverse()
244-
-- :gsub("%d%d%d", "%0,")
245-
-- :gsub(",$", ""):gsub(",%-$", "-")
246-
-- :reverse()
247-
-- end
248-
249-
function printf(s, ...)
226+
227+
local function printfNoise(s, ...)
250228
print(s:format(...))
251229
end
252-
printfNoise = printf
253-
254-
function printError(s)
230+
local function printError(s)
255231
io.stderr:write(s, "\n")
256232
end
257-
function printfError(s, ...)
233+
local function printfError(s, ...)
258234
io.stderr:write(s:format(...), "\n")
259235
end
260236

261-
function errorLine(err)
237+
local function errorLine(err)
262238
printError(pp.tryToFormatError(err))
263239
os.exit(1)
264240
end
265241

266-
if IS_LUA_52_OR_LATER then
267-
function loadLuaFile(path, env)
242+
local loadLuaFile = (
243+
(_VERSION >= "Lua 5.2" or jit) and function(path, env)
268244
return loadfile(path, "bt", env)
269245
end
270-
else
271-
function loadLuaFile(path, env)
246+
or function(path, env)
272247
local mainChunk, err = loadfile(path)
273248
if not mainChunk then return mainChunk, err end
274249

275250
if env then setfenv(mainChunk, env) end
276251

277252
return mainChunk
278253
end
279-
end
254+
)
280255

281256
--==============================================================
282-
--= Preprocessor Script ========================================
257+
--= Preprocessor script ========================================
283258
--==============================================================
284259

285260
io.stdout:setvbuf("no")
@@ -383,6 +358,9 @@ for _, arg in ipairs(args) do
383358
io.stdout:write(pp.VERSION)
384359
os.exit()
385360

361+
elseif arg == "--nostrictmacroarguments" then
362+
strictMacroArguments = false
363+
386364
else
387365
errorLine("Unknown option '"..arg:gsub("=.*", "").."'.")
388366
end
@@ -529,24 +507,25 @@ for i, pathIn in ipairs(pathsIn) do
529507
end
530508

531509
local info, err = pp.processFile{
532-
pathIn = pathIn,
533-
pathMeta = pathMeta,
534-
pathOut = pathOut,
510+
pathIn = pathIn,
511+
pathMeta = pathMeta,
512+
pathOut = pathOut,
535513

536-
debug = isDebug,
537-
addLineNumbers = addLineNumbers,
514+
debug = isDebug,
515+
addLineNumbers = addLineNumbers,
538516

539-
backtickStrings = allowBacktickStrings,
540-
jitSyntax = allowJitSyntax,
541-
canOutputNil = canOutputNil,
542-
fastStrings = fastStrings,
543-
validate = validate,
517+
backtickStrings = allowBacktickStrings,
518+
jitSyntax = allowJitSyntax,
519+
canOutputNil = canOutputNil,
520+
fastStrings = fastStrings,
521+
validate = validate,
522+
strictMacroArguments = strictMacroArguments,
544523

545-
macroPrefix = macroPrefix,
546-
macroSuffix = macroSuffix,
524+
macroPrefix = macroPrefix,
525+
macroSuffix = macroSuffix,
547526

548-
release = releaseMode,
549-
logLevel = maxLogLevel,
527+
release = releaseMode,
528+
logLevel = maxLogLevel,
550529

551530
onInsert = (hasMessageHandler("insert") or nil) and function(name)
552531
local lua = sendMessage("insert", pathIn, name)

0 commit comments

Comments
 (0)