Skip to content

Commit b563c3f

Browse files
committed
Added isProcessing().
callMacro() can take the macro function directly. Updated changelog.
1 parent fac01f8 commit b563c3f

File tree

4 files changed

+70
-18
lines changed

4 files changed

+70
-18
lines changed

Changelog.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
Changelog
22
LuaPreprocess
33

4+
v1.21 (2022-07-08)
5+
Library:
6+
- Added params.strictMacroArguments (which is enabled by default). Macro arguments are validated to be Lua expressions once again, unless strictMacroArguments is disabled.
7+
- Added the metaprogram code as an argument to params.onBeforeMeta().
8+
- Added functions: callMacro(), isProcessing().
9+
- params.onBeforeMeta() is now only called, and .meta.lua files are only written, if processing is necessary (i.e. not for plain Lua files in most cases).
10+
- Plain Lua files should process a bit faster.
11+
- Checking that params.pathMeta isn't the same as the input or output path.
12+
- Error messages showing lots of code are now shortened.
13+
- Fixed evaluate() treating empty code as a valid expression.
14+
Command line program:
15+
- Added option: --nostrictmacroarguments.
16+
- Added the metaprogram code as an argument to the "beforemeta" message handler.
17+
- The "beforemeta" message is now only sent, and .meta.lua files are only written, if processing is necessary (i.e. not for plain Lua files in most cases).
18+
419
v1.20 (2022-07-01)
520
Library:
621
- getOutputSoFar() can now take an output buffer argument.

preprocess-cl.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ Handler messages:
144144
name: The name of the resource to be inserted (could be a file path or anything).
145145
146146
"beforemeta"
147-
Sent before a file's metaprogram runs.
147+
Sent before a file's metaprogram runs, if a metaprogram is generated.
148148
Arguments:
149149
path: The file being processed.
150150
luaString: The generated metaprogram.

preprocess.lua

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
--[[============================================================
22
--=
3-
--= LuaPreprocess v1.20-dev - preprocessing library
3+
--= LuaPreprocess v1.21 - preprocessing library
44
--= by Marcus 'ReFreezed' Thunström
55
--=
66
--= License: MIT (see the bottom of this file)
@@ -17,6 +17,7 @@
1717
- copyTable
1818
- escapePattern
1919
- getIndentation
20+
- isProcessing
2021
- pack
2122
- pairsSorted
2223
- printf
@@ -131,7 +132,7 @@
131132

132133

133134

134-
local PP_VERSION = "1.20.0-dev"
135+
local PP_VERSION = "1.21.0"
135136

136137
local MAX_DUPLICATE_FILE_INSERTS = 1000 -- @Incomplete: Make this a parameter for processFile()/processString().
137138
local MAX_CODE_LENGTH_IN_MESSAGES = 60
@@ -2258,24 +2259,39 @@ local function isCallable(v)
22582259
end
22592260

22602261
-- callMacro()
2261-
-- luaString = callMacro( macroName, argument1, ... )
2262-
-- Call a macro function (which must be a global in metaEnvironment).
2262+
-- luaString = callMacro( function|macroName, argument1, ... )
2263+
-- Call a macro function (which must be a global in metaEnvironment if macroName is given).
22632264
-- The arguments should be Lua code strings.
2264-
function metaFuncs.callMacro(name, ...)
2265+
function metaFuncs.callMacro(nameOrFunc, ...)
22652266
errorIfNotRunningMeta(2)
22662267

2267-
local nameResult = current_parsingAndMeta_macroPrefix .. name .. current_parsingAndMeta_macroSuffix
2268-
local f = metaEnv[nameResult]
2268+
assertarg(1, nameOrFunc, "string","function")
2269+
local f
22692270

2270-
if not isCallable(f) then
2271-
if name == nameResult
2272-
then errorf(2, "'%s' is not a macro/global function. (Got %s)", name, type(f))
2273-
else errorf(2, "'%s' (resolving to '%s') is not a macro/global function. (Got %s)", name, nameResult, type(f)) end
2271+
if type(nameOrFunc) == "string" then
2272+
local nameResult = current_parsingAndMeta_macroPrefix .. nameOrFunc .. current_parsingAndMeta_macroSuffix
2273+
f = metaEnv[nameResult]
2274+
2275+
if not isCallable(f) then
2276+
if nameOrFunc == nameResult
2277+
then errorf(2, "'%s' is not a macro/global function. (Got %s)", nameOrFunc, type(f))
2278+
else errorf(2, "'%s' (resolving to '%s') is not a macro/global function. (Got %s)", nameOrFunc, nameResult, type(f)) end
2279+
end
2280+
2281+
else
2282+
f = nameOrFunc
22742283
end
22752284

22762285
return (metaEnv.__M()(f(...)))
22772286
end
22782287

2288+
-- isProcessing()
2289+
-- bool = isProcessing( )
2290+
-- Returns true if a file or string is currently being processed.
2291+
function metaFuncs.isProcessing()
2292+
return current_parsingAndMeta_isProcessing
2293+
end
2294+
22792295
-- :PredefinedMacros
22802296

22812297
-- ASSERT()
@@ -3816,7 +3832,7 @@ local pp = {
38163832
-- logLevel = levelName -- [Optional] Maximum log level for the @@LOG() macro. Can be "off", "error", "warning", "info", "debug" or "trace". (Default: "trace", which enables all logging)
38173833
--
38183834
-- onInsert = function( name ) -- [Optional] Called for each @insert"name" instruction. It's expected to return a Lua code string. By default 'name' is a path to a file to be inserted.
3819-
-- onBeforeMeta = function( luaString ) -- [Optional] Called before the metaprogram runs. luaString contains the metaprogram.
3835+
-- onBeforeMeta = function( luaString ) -- [Optional] Called before the metaprogram runs, if a metaprogram is generated. luaString contains the metaprogram.
38203836
-- onAfterMeta = function( luaString ) -- [Optional] Here you can modify and return the Lua code before it's written to 'pathOut'.
38213837
-- onError = function( error ) -- [Optional] You can use this to get traceback information. 'error' is the same value as what is returned from processFile().
38223838
--
@@ -3849,7 +3865,7 @@ local pp = {
38493865
-- logLevel = levelName -- [Optional] Maximum log level for the @@LOG() macro. Can be "off", "error", "warning", "info", "debug" or "trace". (Default: "trace", which enables all logging)
38503866
--
38513867
-- onInsert = function( name ) -- [Optional] Called for each @insert"name" instruction. It's expected to return a Lua code string. By default 'name' is a path to a file to be inserted.
3852-
-- onBeforeMeta = function( luaString ) -- [Optional] Called before the metaprogram runs. luaString contains the metaprogram.
3868+
-- onBeforeMeta = function( luaString ) -- [Optional] Called before the metaprogram runs, if a metaprogram is generated. luaString contains the metaprogram.
38533869
-- onError = function( error ) -- [Optional] You can use this to get traceback information. 'error' is the same value as the second returned value from processString().
38543870
--
38553871
processString = processString,

tests/suite.lua

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -775,15 +775,31 @@ doTest("Misc.", function()
775775
local luaOut = assert(pp.processString{ code=[[
776776
!!(callMacro("ASSERT", "x", "foo()"))
777777
]]})
778-
assertCodeOutput(luaOut, "if not (x) then error((foo())) end")
778+
assertCodeOutput(luaOut, [[if not (x) then error((foo())) end]])
779+
780+
local luaOut = assert(pp.processString{ code=[[
781+
!!(callMacro(ASSERT, "x", "foo()"))
782+
]]})
783+
assertCodeOutput(luaOut, [[if not (x) then error((foo())) end]])
784+
785+
local luaOut = assert(pp.processString{ code=[[
786+
!callMacro(ASSERT, "x", "foo()")
787+
]]})
788+
assertCodeOutput(luaOut, [[]])
779789

780790
local luaOut = assert(pp.processString{ macroPrefix="MACRO_", code=[[
781791
!function _G.MACRO_MOO() return "foo()" end -- Must be global!
782792
!!(callMacro("MOO"))
783793
]]})
784-
assertCodeOutput(luaOut, "foo()")
794+
assertCodeOutput(luaOut, [[foo()]])
785795
pp.metaEnvironment.MACRO_MOO = nil
786796

797+
local luaOut = assert(pp.processString{ macroPrefix="MACRO_", code=[[
798+
!local function MACRO_MOO() return "foo()" end
799+
!!(callMacro(MACRO_MOO))
800+
]]})
801+
assertCodeOutput(luaOut, [[foo()]])
802+
787803
assert(not pp.processString{ macroPrefix="MACRO_", code=[[
788804
!local function MACRO_MOO() return "foo()" end -- Not a global!
789805
!!(callMacro("MOO"))
@@ -794,6 +810,11 @@ doTest("Misc.", function()
794810
!!(callMacro("MACRO_MOO")) -- Calls MACRO_MACRO_MOO!
795811
]]})
796812
pp.metaEnvironment.MACRO_MOO = nil
813+
814+
-- Processing, or not processing... that's the real question here, mate.
815+
assert(not pp.isProcessing())
816+
assert(pp.processString{ code=[[ !assert(isProcessing()) ]]})
817+
assert(not pp.isProcessing())
797818
end)
798819

799820

@@ -986,7 +1007,7 @@ doTest("Messages", function()
9861007
cycle == 1 and [[ return {
9871008
init = function(inPaths, outPaths ) assert(not outPaths) ; table.insert(inPaths, "temp/generatedTest.lua2p") end,
9881009
insert = function(path, name ) assert(name == "foo()") ; return "un"..name end,
989-
beforemeta = function(path, lua ) end,
1010+
beforemeta = function(path, lua ) assert(type(lua) == "string") end,
9901011
aftermeta = function(path, lua ) return "-- Hello\n"..lua end,
9911012
filedone = function(path, outPath, info) assert(outPath == "temp/generatedTest.lua") ; assert(type(info) == "table") end,
9921013
fileerror = function(path, err ) end,
@@ -995,7 +1016,7 @@ doTest("Messages", function()
9951016
or [[ return function(message, ...)
9961017
if message == "init" then local inPaths, outPaths = ... ; assert(not outPaths) ; table.insert(inPaths, "temp/generatedTest.lua2p")
9971018
elseif message == "insert" then local path, name = ... ; assert(name == "foo()") ; return "un"..name
998-
elseif message == "beforemeta" then local path, lua = ...
1019+
elseif message == "beforemeta" then local path, lua = ... ; assert(type(lua) == "string")
9991020
elseif message == "aftermeta" then local path, lua = ... ; return "-- Hello\n"..lua
10001021
elseif message == "filedone" then local path, outPath, info = ... ; assert(outPath == "temp/generatedTest.lua") ; assert(type(info) == "table")
10011022
elseif message == "fileerror" then local path, err = ...

0 commit comments

Comments
 (0)