Skip to content

Commit

Permalink
Added isProcessing().
Browse files Browse the repository at this point in the history
callMacro() can take the macro function directly.
Updated changelog.
  • Loading branch information
ReFreezed committed Jul 8, 2022
1 parent fac01f8 commit b563c3f
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 18 deletions.
15 changes: 15 additions & 0 deletions Changelog.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
Changelog
LuaPreprocess

v1.21 (2022-07-08)
Library:
- Added params.strictMacroArguments (which is enabled by default). Macro arguments are validated to be Lua expressions once again, unless strictMacroArguments is disabled.
- Added the metaprogram code as an argument to params.onBeforeMeta().
- Added functions: callMacro(), isProcessing().
- 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).
- Plain Lua files should process a bit faster.
- Checking that params.pathMeta isn't the same as the input or output path.
- Error messages showing lots of code are now shortened.
- Fixed evaluate() treating empty code as a valid expression.
Command line program:
- Added option: --nostrictmacroarguments.
- Added the metaprogram code as an argument to the "beforemeta" message handler.
- 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).

v1.20 (2022-07-01)
Library:
- getOutputSoFar() can now take an output buffer argument.
Expand Down
2 changes: 1 addition & 1 deletion preprocess-cl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ Handler messages:
name: The name of the resource to be inserted (could be a file path or anything).
"beforemeta"
Sent before a file's metaprogram runs.
Sent before a file's metaprogram runs, if a metaprogram is generated.
Arguments:
path: The file being processed.
luaString: The generated metaprogram.
Expand Down
42 changes: 29 additions & 13 deletions preprocess.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
--[[============================================================
--=
--= LuaPreprocess v1.20-dev - preprocessing library
--= LuaPreprocess v1.21 - preprocessing library
--= by Marcus 'ReFreezed' Thunström
--=
--= License: MIT (see the bottom of this file)
Expand All @@ -17,6 +17,7 @@
- copyTable
- escapePattern
- getIndentation
- isProcessing
- pack
- pairsSorted
- printf
Expand Down Expand Up @@ -131,7 +132,7 @@



local PP_VERSION = "1.20.0-dev"
local PP_VERSION = "1.21.0"

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

-- callMacro()
-- luaString = callMacro( macroName, argument1, ... )
-- Call a macro function (which must be a global in metaEnvironment).
-- luaString = callMacro( function|macroName, argument1, ... )
-- Call a macro function (which must be a global in metaEnvironment if macroName is given).
-- The arguments should be Lua code strings.
function metaFuncs.callMacro(name, ...)
function metaFuncs.callMacro(nameOrFunc, ...)
errorIfNotRunningMeta(2)

local nameResult = current_parsingAndMeta_macroPrefix .. name .. current_parsingAndMeta_macroSuffix
local f = metaEnv[nameResult]
assertarg(1, nameOrFunc, "string","function")
local f

if not isCallable(f) then
if name == nameResult
then errorf(2, "'%s' is not a macro/global function. (Got %s)", name, type(f))
else errorf(2, "'%s' (resolving to '%s') is not a macro/global function. (Got %s)", name, nameResult, type(f)) end
if type(nameOrFunc) == "string" then
local nameResult = current_parsingAndMeta_macroPrefix .. nameOrFunc .. current_parsingAndMeta_macroSuffix
f = metaEnv[nameResult]

if not isCallable(f) then
if nameOrFunc == nameResult
then errorf(2, "'%s' is not a macro/global function. (Got %s)", nameOrFunc, type(f))
else errorf(2, "'%s' (resolving to '%s') is not a macro/global function. (Got %s)", nameOrFunc, nameResult, type(f)) end
end

else
f = nameOrFunc
end

return (metaEnv.__M()(f(...)))
end

-- isProcessing()
-- bool = isProcessing( )
-- Returns true if a file or string is currently being processed.
function metaFuncs.isProcessing()
return current_parsingAndMeta_isProcessing
end

-- :PredefinedMacros

-- ASSERT()
Expand Down Expand Up @@ -3816,7 +3832,7 @@ local pp = {
-- 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)
--
-- 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.
-- onBeforeMeta = function( luaString ) -- [Optional] Called before the metaprogram runs. luaString contains the metaprogram.
-- onBeforeMeta = function( luaString ) -- [Optional] Called before the metaprogram runs, if a metaprogram is generated. luaString contains the metaprogram.
-- onAfterMeta = function( luaString ) -- [Optional] Here you can modify and return the Lua code before it's written to 'pathOut'.
-- onError = function( error ) -- [Optional] You can use this to get traceback information. 'error' is the same value as what is returned from processFile().
--
Expand Down Expand Up @@ -3849,7 +3865,7 @@ local pp = {
-- 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)
--
-- 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.
-- onBeforeMeta = function( luaString ) -- [Optional] Called before the metaprogram runs. luaString contains the metaprogram.
-- onBeforeMeta = function( luaString ) -- [Optional] Called before the metaprogram runs, if a metaprogram is generated. luaString contains the metaprogram.
-- onError = function( error ) -- [Optional] You can use this to get traceback information. 'error' is the same value as the second returned value from processString().
--
processString = processString,
Expand Down
29 changes: 25 additions & 4 deletions tests/suite.lua
Original file line number Diff line number Diff line change
Expand Up @@ -775,15 +775,31 @@ doTest("Misc.", function()
local luaOut = assert(pp.processString{ code=[[
!!(callMacro("ASSERT", "x", "foo()"))
]]})
assertCodeOutput(luaOut, "if not (x) then error((foo())) end")
assertCodeOutput(luaOut, [[if not (x) then error((foo())) end]])

local luaOut = assert(pp.processString{ code=[[
!!(callMacro(ASSERT, "x", "foo()"))
]]})
assertCodeOutput(luaOut, [[if not (x) then error((foo())) end]])

local luaOut = assert(pp.processString{ code=[[
!callMacro(ASSERT, "x", "foo()")
]]})
assertCodeOutput(luaOut, [[]])

local luaOut = assert(pp.processString{ macroPrefix="MACRO_", code=[[
!function _G.MACRO_MOO() return "foo()" end -- Must be global!
!!(callMacro("MOO"))
]]})
assertCodeOutput(luaOut, "foo()")
assertCodeOutput(luaOut, [[foo()]])
pp.metaEnvironment.MACRO_MOO = nil

local luaOut = assert(pp.processString{ macroPrefix="MACRO_", code=[[
!local function MACRO_MOO() return "foo()" end
!!(callMacro(MACRO_MOO))
]]})
assertCodeOutput(luaOut, [[foo()]])

assert(not pp.processString{ macroPrefix="MACRO_", code=[[
!local function MACRO_MOO() return "foo()" end -- Not a global!
!!(callMacro("MOO"))
Expand All @@ -794,6 +810,11 @@ doTest("Misc.", function()
!!(callMacro("MACRO_MOO")) -- Calls MACRO_MACRO_MOO!
]]})
pp.metaEnvironment.MACRO_MOO = nil

-- Processing, or not processing... that's the real question here, mate.
assert(not pp.isProcessing())
assert(pp.processString{ code=[[ !assert(isProcessing()) ]]})
assert(not pp.isProcessing())
end)


Expand Down Expand Up @@ -986,7 +1007,7 @@ doTest("Messages", function()
cycle == 1 and [[ return {
init = function(inPaths, outPaths ) assert(not outPaths) ; table.insert(inPaths, "temp/generatedTest.lua2p") end,
insert = function(path, name ) assert(name == "foo()") ; return "un"..name end,
beforemeta = function(path, lua ) end,
beforemeta = function(path, lua ) assert(type(lua) == "string") end,
aftermeta = function(path, lua ) return "-- Hello\n"..lua end,
filedone = function(path, outPath, info) assert(outPath == "temp/generatedTest.lua") ; assert(type(info) == "table") end,
fileerror = function(path, err ) end,
Expand All @@ -995,7 +1016,7 @@ doTest("Messages", function()
or [[ return function(message, ...)
if message == "init" then local inPaths, outPaths = ... ; assert(not outPaths) ; table.insert(inPaths, "temp/generatedTest.lua2p")
elseif message == "insert" then local path, name = ... ; assert(name == "foo()") ; return "un"..name
elseif message == "beforemeta" then local path, lua = ...
elseif message == "beforemeta" then local path, lua = ... ; assert(type(lua) == "string")
elseif message == "aftermeta" then local path, lua = ... ; return "-- Hello\n"..lua
elseif message == "filedone" then local path, outPath, info = ... ; assert(outPath == "temp/generatedTest.lua") ; assert(type(info) == "table")
elseif message == "fileerror" then local path, err = ...
Expand Down

0 comments on commit b563c3f

Please sign in to comment.