Skip to content

Commit 489fc28

Browse files
committed
Added processString().
1 parent 0599396 commit 489fc28

File tree

1 file changed

+92
-35
lines changed

1 file changed

+92
-35
lines changed

preprocess.lua

Lines changed: 92 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,13 @@
8282
- (all the functions above)
8383
- VERSION
8484
- metaEnvironment
85+
- processFile, processString
8586
8687
Search this file for 'ExportTable' for more info.
8788
8889
--============================================================]]
8990

90-
local VERSION = "1.1.1"
91+
local VERSION = "1.2.0"
9192

9293
local KEYWORDS = {
9394
"and","break","do","else","elseif","end","false","for","function","if","in",
@@ -935,21 +936,37 @@ metaEnv.__LUA = metaEnv.outputLua
935936

936937

937938

938-
local function _processFile(params)
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
939+
local function _processFileOrString(params, isFile)
940+
if isFile then
941+
if not params.pathIn then error("Missing 'pathIn' in params.", 2) end
942+
if not params.pathOut then error("Missing 'pathOut' in params.", 2) end
943+
else
944+
if not params.code then error("Missing 'code' in params.", 2) end
945+
end
946+
947+
local luaUnprocessed, pathIn
948+
949+
if isFile then
950+
local err
941951

942-
local luaUnprocessed, err = getFileContents(params.pathIn)
943-
if not luaUnprocessed then
944-
errorline("Could not read file: "..err)
952+
pathIn = params.pathIn
953+
luaUnprocessed, err = getFileContents(pathIn)
954+
955+
if not luaUnprocessed then
956+
errorline("Could not read file: "..err)
957+
end
958+
959+
else
960+
pathIn = "<code>"
961+
luaUnprocessed = params.code
945962
end
946963

947964
local specialFirstLine, rest = luaUnprocessed:match"^(#[^\r\n]*\r?\n?)(.*)$"
948965
if specialFirstLine then
949966
luaUnprocessed = rest
950967
end
951968

952-
local tokens = tokenize(luaUnprocessed, params.pathIn, true)
969+
local tokens = tokenize(luaUnprocessed, pathIn, true)
953970
local lastToken = tokens[#tokens]
954971
-- printTokens(tokens)
955972

@@ -1033,7 +1050,7 @@ local function _processFile(params)
10331050

10341051
elseif tokType == "pp_entry" then
10351052
errorInFile(
1036-
luaUnprocessed, params.pathIn, tok.position, "Parser",
1053+
luaUnprocessed, pathIn, tok.position, "Parser",
10371054
"Preprocessor token inside metaprogram"
10381055
..(tok.line == metaStartLine and "." or " (starting at line %d)."),
10391056
metaStartLine
@@ -1052,7 +1069,7 @@ local function _processFile(params)
10521069

10531070
if bracketBalance < 0 then
10541071
errorInFile(
1055-
luaUnprocessed, params.pathIn, tok.position, "Parser",
1072+
luaUnprocessed, pathIn, tok.position, "Parser",
10561073
"Unexpected '%s'. Preprocessor line"
10571074
..(tok.line == metaStartLine and "" or " (starting at line %d)")
10581075
.." has unbalanced brackets.",
@@ -1092,7 +1109,7 @@ local function _processFile(params)
10921109
while true do
10931110
tok = tokens[tokenIndex]
10941111
if not tok then
1095-
errorInFile(luaUnprocessed, params.pathIn, startPos, "Parser", "Missing end of preprocessor block.")
1112+
errorInFile(luaUnprocessed, pathIn, startPos, "Parser", "Missing end of preprocessor block.")
10961113
end
10971114

10981115
tokType = tok.type
@@ -1106,7 +1123,7 @@ local function _processFile(params)
11061123

11071124
elseif tokType == "pp_entry" then
11081125
errorInFile(
1109-
luaUnprocessed, params.pathIn, tok.position, "Parser",
1126+
luaUnprocessed, pathIn, tok.position, "Parser",
11101127
"Preprocessor token inside metaprogram"..(tok.line == startLine and "." or " (starting at line %d)."),
11111128
startLine
11121129
)
@@ -1126,7 +1143,7 @@ local function _processFile(params)
11261143
elseif doOutputLua then
11271144
-- We could do something other than error here. Room for more functionality.
11281145
errorInFile(
1129-
luaUnprocessed, params.pathIn, startPos+3, "Parser",
1146+
luaUnprocessed, pathIn, startPos+3, "Parser",
11301147
"Preprocessor block variant does not contain a valid expression that results in a value."
11311148
)
11321149

@@ -1163,9 +1180,9 @@ local function _processFile(params)
11631180

11641181
elseif tokType == "pp_entry" then
11651182
if tok.double then
1166-
errorInFile(luaUnprocessed, params.pathIn, tok.position, "Parser", "Unexpected double preprocessor token.")
1183+
errorInFile(luaUnprocessed, pathIn, tok.position, "Parser", "Unexpected double preprocessor token.")
11671184
else
1168-
errorInFile(luaUnprocessed, params.pathIn, tok.position, "Parser", "Unexpected preprocessor token.")
1185+
errorInFile(luaUnprocessed, pathIn, tok.position, "Parser", "Unexpected preprocessor token.")
11691186
end
11701187

11711188
-- Non-meta.
@@ -1252,16 +1269,20 @@ local function _processFile(params)
12521269
-- Write output file.
12531270
----------------------------------------------------------------
12541271

1255-
local file = assert(io.open(params.pathOut, "wb"))
1256-
file:write(specialFirstLine or "")
1257-
file:write(lua)
1258-
file:close()
1272+
pathOut = isFile and params.pathOut or "<output>"
1273+
1274+
if isFile then
1275+
local file = assert(io.open(pathOut, "wb"))
1276+
file:write(specialFirstLine or "")
1277+
file:write(lua)
1278+
file:close()
1279+
end
12591280

12601281
-- Test if the output is valid Lua.
1261-
local chunk, err = loadstring(lua, params.pathOut)
1282+
local chunk, err = loadstring(lua, pathOut)
12621283
if not chunk then
12631284
local ln, err = err:match'^%[string ".-"%]:(%d+): (.*)'
1264-
errorOnLine(params.pathOut, tonumber(ln), nil, "%s", err)
1285+
errorOnLine(pathOut, tonumber(ln), nil, "%s", err)
12651286
end
12661287

12671288
-- :ProcessInfo
@@ -1272,12 +1293,18 @@ local function _processFile(params)
12721293
hasPreprocessorCode = hasPreprocessorCode,
12731294
}
12741295

1275-
return info
1296+
if isFile then
1297+
return info
1298+
else
1299+
if specialFirstLine then
1300+
lua = specialFirstLine..lua
1301+
end
1302+
return lua, info
1303+
end
12761304
end
12771305

1278-
local function processFile(params)
1279-
local info = nil
1280-
local err = nil
1306+
local function processFileOrString(params, isFile)
1307+
local returnValues = nil
12811308

12821309
isDebug = params.debug
12831310
onError = function(_err)
@@ -1287,7 +1314,7 @@ local function processFile(params)
12871314

12881315
xpcall(
12891316
function()
1290-
info = _processFile(params)
1317+
returnValues = pack(_processFileOrString(params, isFile))
12911318
end,
12921319
onError
12931320
)
@@ -1300,7 +1327,19 @@ local function processFile(params)
13001327
metaPathForErrorMessages = ""
13011328
outputFromMeta = nil
13021329

1303-
return info, err
1330+
if err then
1331+
return nil, err
1332+
else
1333+
return unpack(returnValues, 1, returnValues.n)
1334+
end
1335+
end
1336+
1337+
local function processFile(params)
1338+
return processFileOrString(params, true)
1339+
end
1340+
1341+
local function processString(params)
1342+
return processFileOrString(params, false)
13041343
end
13051344

13061345

@@ -1319,19 +1358,37 @@ local lib = {
13191358
-- error: Error message, or nil if no error happened.
13201359
--
13211360
-- params: Table with these fields:
1322-
-- pathIn = pathToInputFile -- [Required]
1323-
-- pathMeta = pathForMetaprogram -- [Optional] You can inspect this temporary output file if an error ocurrs in the metaprogram.
1324-
-- pathOut = pathToOutputFile -- [Required]
1361+
-- pathIn = pathToInputFile -- [Required]
1362+
-- pathMeta = pathForMetaprogram -- [Optional] You can inspect this temporary output file if an error ocurrs in the metaprogram.
1363+
-- pathOut = pathToOutputFile -- [Required]
13251364
--
1326-
-- addLineNumbers = boolean -- [Optional] Add comments with line numbers to the output.
1327-
-- debug = boolean -- [Optional] Debug mode. The metaprogram file is formatted more nicely and does not get deleted automatically.
1365+
-- addLineNumbers = boolean -- [Optional] Add comments with line numbers to the output.
1366+
-- debug = boolean -- [Optional] Debug mode. The metaprogram file is formatted more nicely and does not get deleted automatically.
13281367
--
1329-
-- onAfterMeta = function( lua ) -- [Optional] Here you can modify and return the Lua code before it's written to 'pathOut'.
1330-
-- onBeforeMeta = function( ) -- [Optional] Called before the metaprogram runs.
1331-
-- onError = function( error ) -- [Optional] You can use this to get traceback information. 'error' is the same value as what is returned from processFile().
1368+
-- onAfterMeta = function( luaCode ) -- [Optional] Here you can modify and return the Lua code before it's written to 'pathOut'.
1369+
-- onBeforeMeta = function( ) -- [Optional] Called before the metaprogram runs.
1370+
-- onError = function( error ) -- [Optional] You can use this to get traceback information. 'error' is the same value as what is returned from processFile().
13321371
--
13331372
processFile = processFile,
13341373

1374+
-- processString()
1375+
-- Process Lua code.
1376+
--
1377+
-- code, info = processString( params )
1378+
-- info: Table with various information, or a message if an error happened. See 'ProcessInfo' for more info.
1379+
--
1380+
-- params: Table with these fields:
1381+
-- code = luaCode -- [Required]
1382+
-- pathMeta = pathForMetaprogram -- [Optional] You can inspect this temporary output file if an error ocurrs in the metaprogram.
1383+
--
1384+
-- addLineNumbers = boolean -- [Optional] Add comments with line numbers to the output.
1385+
-- debug = boolean -- [Optional] Debug mode. The metaprogram file is formatted more nicely and does not get deleted automatically.
1386+
--
1387+
-- onBeforeMeta = function( ) -- [Optional] Called before the metaprogram runs.
1388+
-- onError = function( error ) -- [Optional] You can use this to get traceback information. 'error' is the same value as the second returned value from processString().
1389+
--
1390+
processString = processString,
1391+
13351392
-- Values.
13361393
----------------------------------------------------------------
13371394

0 commit comments

Comments
 (0)