Skip to content

Commit 75a36eb

Browse files
committed
Added macro prefix/suffix settings. (E.g. change @@Assert() into @@macro_assert().)
1 parent c4889b5 commit 75a36eb

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed

preprocess-cl.lua

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ exec lua "$0" "$@"
5959
--linenumbers
6060
Add comments with line numbers to the output.
6161
62+
--macroprefix=prefix
63+
String to prepend to macro names.
64+
65+
--macrosuffix=suffix
66+
String to append to macro names.
67+
6268
--meta
6369
Output the metaprogram to a temporary file (*.meta.lua). Useful if
6470
an error happens when the metaprogram runs. This file is removed
@@ -181,6 +187,8 @@ local outputMeta = false
181187
local processingInfoPath = ""
182188
local silent = false
183189
local validate = true
190+
local macroPrefix = ""
191+
local macroSuffix = ""
184192

185193
--==============================================================
186194
--= Local Functions ============================================
@@ -269,7 +277,7 @@ for _, arg in ipairs(args) do
269277
processOptions = false
270278

271279
elseif arg:find"^%-%-data=" or arg:find"^%-d=" then
272-
customData = arg:match"^%-%-data=(.*)$" or arg:match"^%-d=(.*)$"
280+
customData = arg:gsub("^.-=", "")
273281

274282
elseif arg == "--backtickstrings" then
275283
allowBacktickStrings = true
@@ -279,7 +287,7 @@ for _, arg in ipairs(args) do
279287
outputMeta = true
280288

281289
elseif arg:find"^%-%-handler=" or arg:find"^%-h=" then
282-
messageHandlerPath = arg:match"^%-%-handler=(.*)$" or arg:match"^%-h=(.*)$"
290+
messageHandlerPath = arg:gsub("^.-=", "")
283291

284292
elseif arg == "--jitsyntax" then
285293
allowJitSyntax = true
@@ -301,7 +309,7 @@ for _, arg in ipairs(args) do
301309
errorLine("Cannot specify both --outputextension and --outputpaths")
302310
end
303311
hasOutputExtension = true
304-
outputExtension = arg:match"^%-%-outputextension=(.*)$"
312+
outputExtension = arg:gsub("^.-=", "")
305313

306314
elseif arg == "--outputpaths" or arg == "-o" then
307315
if hasOutputExtension then
@@ -312,7 +320,7 @@ for _, arg in ipairs(args) do
312320
hasOutputPaths = true
313321

314322
elseif arg:find"^%-%-saveinfo=" or arg:find"^%-i=" then
315-
processingInfoPath = arg:match"^%-%-saveinfo=(.*)$" or arg:match"^%-i=(.*)$"
323+
processingInfoPath = arg:gsub("^.-=", "")
316324

317325
elseif arg == "--silent" then
318326
silent = true
@@ -323,6 +331,12 @@ for _, arg in ipairs(args) do
323331
elseif arg == "--nogc" then
324332
collectgarbage("stop")
325333

334+
elseif arg:find"^%-%-macroprefix=" then
335+
macroPrefix = arg:gsub("^.-=", "")
336+
337+
elseif arg:find"^%-%-macrosuffix=" then
338+
macroSuffix = arg:gsub("^.-=", "")
339+
326340
else
327341
errorLine("Unknown option '"..arg:gsub("=.*", "").."'.")
328342
end
@@ -479,6 +493,9 @@ for i, pathIn in ipairs(pathsIn) do
479493
fastStrings = fastStrings,
480494
validate = validate,
481495

496+
macroPrefix = macroPrefix,
497+
macroSuffix = macroSuffix,
498+
482499
onInsert = (hasMessageHandler("insert") or nil) and function(name)
483500
local lua = sendMessage("insert", pathIn, name)
484501

preprocess.lua

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@
124124

125125

126126

127-
local PP_VERSION = "1.16.0"
127+
local PP_VERSION = "1.16.0-dev"
128128

129129
local MAX_DUPLICATE_FILE_INSERTS = 1000 -- @Incomplete: Make this a parameter for processFile()/processString().
130130

@@ -193,6 +193,8 @@ local outputFromMetaStack = nil
193193
local outputFromMeta = nil -- Top item in outputFromMetaStack.
194194
local canOutputNil = true
195195
local fastStrings = false
196+
local macroPrefix = ""
197+
local macroSuffix = ""
196198

197199

198200

@@ -2206,10 +2208,10 @@ local function expandMacro(tokens, fileBuffers, tokenStack, macroStartTok, isNes
22062208
end
22072209

22082210
-- Start macro wrapper.
2209-
tableInsert(tokens, newTokenAt({type="identifier", value="__MACRO", representation="__MACRO" }, macroStartTok))
2210-
tableInsert(tokens, newTokenAt({type="punctuation", value="(", representation="(" }, macroStartTok))
2211-
tableInsert(tokens, newTokenAt({type="punctuation", value=")", representation=")" }, macroStartTok))
2212-
tableInsert(tokens, newTokenAt({type="punctuation", value="(", representation="(" }, macroStartTok))
2211+
tableInsert(tokens, newTokenAt({type="identifier", value="__MACRO", representation="__MACRO"}, macroStartTok))
2212+
tableInsert(tokens, newTokenAt({type="punctuation", value="(", representation="(" }, macroStartTok))
2213+
tableInsert(tokens, newTokenAt({type="punctuation", value=")", representation=")" }, macroStartTok))
2214+
tableInsert(tokens, newTokenAt({type="punctuation", value="(", representation="(" }, macroStartTok))
22132215

22142216
--
22152217
-- Callee.
@@ -2225,6 +2227,10 @@ local function expandMacro(tokens, fileBuffers, tokenStack, macroStartTok, isNes
22252227
tableInsert(tokens, tokNext)
22262228
local lastCalleeTok = tokNext
22272229

2230+
-- Add macro prefix and suffix. (Note: We only edit the initial identifier in the callee if there are more.)
2231+
lastCalleeTok.value = macroPrefix .. lastCalleeTok.value .. macroSuffix
2232+
lastCalleeTok.representation = lastCalleeTok.value
2233+
22282234
-- Maybe add '.field[expr]:method' for rest of callee.
22292235
tokNext, iNext = getNextUsableToken(tokenStack, #tokenStack, nil, -1)
22302236

@@ -2670,11 +2676,17 @@ local function _processFileOrString(params, isFile)
26702676
end
26712677
end
26722678

2679+
macroPrefix = params.macroPrefix or ""
2680+
macroSuffix = params.macroSuffix or ""
2681+
26732682
tokens = doEarlyExpansions (tokens, fileBuffers, params, stats)
26742683
tokens = doLateExpansionsResources(tokens, fileBuffers, params, stats)
26752684
tokens = doLateExpansionsMacros (tokens, fileBuffers, params, stats)
26762685
stats.tokenCount = #tokens
26772686

2687+
macroPrefix = ""
2688+
macroSuffix = ""
2689+
26782690
-- Generate metaprogram.
26792691
--==============================================================
26802692

@@ -3169,6 +3181,8 @@ local function processFileOrString(params, isFile)
31693181
outputFromMeta = nil
31703182
canOutputNil = true
31713183
fastStrings = false
3184+
macroPrefix = ""
3185+
macroSuffix = ""
31723186

31733187
if xpcallOk then
31743188
return unpack(returnValues, 1, returnValues.n)
@@ -3217,6 +3231,9 @@ local pp = {
32173231
-- fastStrings = boolean -- [Optional] Force fast serialization of string values. (Non-ASCII characters will look ugly.) (Default: false)
32183232
-- validate = boolean -- [Optional] Validate output. (Default: true)
32193233
--
3234+
-- macroPrefix = prefix -- [Optional] String to prepend to macro names. (Default: "")
3235+
-- macroSuffix = suffix -- [Optional] String to append to macro names. (Default: "")
3236+
--
32203237
-- 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.
32213238
-- onBeforeMeta = function( ) -- [Optional] Called before the metaprogram runs.
32223239
-- onAfterMeta = function( luaString ) -- [Optional] Here you can modify and return the Lua code before it's written to 'pathOut'.
@@ -3243,6 +3260,9 @@ local pp = {
32433260
-- fastStrings = boolean -- [Optional] Force fast serialization of string values. (Non-ASCII characters will look ugly.) (Default: false)
32443261
-- validate = boolean -- [Optional] Validate output. (Default: true)
32453262
--
3263+
-- macroPrefix = prefix -- [Optional] String to prepend to macro names. (Default: "")
3264+
-- macroSuffix = suffix -- [Optional] String to append to macro names. (Default: "")
3265+
--
32463266
-- 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.
32473267
-- onBeforeMeta = function( ) -- [Optional] Called before the metaprogram runs.
32483268
-- onError = function( error ) -- [Optional] You can use this to get traceback information. 'error' is the same value as the second returned value from processString().

0 commit comments

Comments
 (0)