Skip to content

Commit 2d5ecf3

Browse files
committed
Added support for using stin/stdout as input/output.
1 parent 11cfa9e commit 2d5ecf3

File tree

4 files changed

+64
-28
lines changed

4 files changed

+64
-28
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ local/
44
/*.sublime-*
55
/.run
66

7+
/temp/
8+
79
/tests/quickTest.lua
810
/tests/quickTest.meta.lua
911

preprocess-cl.lua

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ exec lua "$0" "$@"
2121
OR
2222
lua preprocess-cl.lua --outputpaths [options] [--] inputpath1 outputpath1 [inputpath2 outputpath2 ...]
2323
24+
File paths can be "-" for usage of stdin/stdout.
25+
2426
Examples:
2527
lua preprocess-cl.lua --saveinfo=logs/info.lua --silent src/main.lua2p src/network.lua2p
2628
lua preprocess-cl.lua --debug src/main.lua2p src/network.lua2p
@@ -97,16 +99,17 @@ exec lua "$0" "$@"
9799
to see what information is saved.
98100
99101
--silent
100-
Only print errors to the console.
102+
Only print errors to the console. (This flag is automatically
103+
enabled if an output path is stdout.)
101104
102105
--debug
103106
Enable some preprocessing debug features. Useful if you want
104107
to inspect the generated metaprogram (*.meta.lua). (This also
105108
enables the --meta option.)
106109
107110
--
108-
Stop options from being parsed further. Needed if you have
109-
paths starting with "-".
111+
Stop options from being parsed further. Needed if you have paths
112+
starting with "-" (except for usage of stdin/stdout).
110113
111114
----------------------------------------------------------------
112115
@@ -157,7 +160,7 @@ local args = arg
157160

158161
local major, minor = _VERSION:match"Lua (%d+)%.(%d+)"
159162
if not major then
160-
io.stderr:write("[LuaPreprocess] Warning: Could not detect Lua version.\n") -- Note: This line does not obey the --silent option.
163+
io.stderr:write("[LuaPreprocess] Warning: Could not detect Lua version.\n")
161164
else
162165
major = tonumber(major)
163166
minor = tonumber(minor)
@@ -269,10 +272,14 @@ local pathsIn = {}
269272
local pathsOut = {}
270273

271274
for _, arg in ipairs(args) do
272-
if not (processOptions and arg:find"^%-") then
273-
local paths = (hasOutputPaths and #pathsOut < #pathsIn and pathsOut or pathsIn)
275+
if not (processOptions and arg:find"^%-.") then
276+
local paths = (hasOutputPaths and #pathsOut < #pathsIn) and pathsOut or pathsIn
274277
table.insert(paths, arg)
275278

279+
if arg == "-" and (not hasOutputPaths or paths == pathsOut) then
280+
silent = true
281+
end
282+
276283
elseif arg == "--" then
277284
processOptions = false
278285

@@ -432,7 +439,7 @@ sendMessage("init", pathsIn, (hasOutputPaths and pathsOut or nil))
432439

433440
if not hasOutputPaths then
434441
for i, pathIn in ipairs(pathsIn) do
435-
pathsOut[i] = pathIn:gsub("%.%w+$", "").."."..outputExtension
442+
pathsOut[i] = (pathIn == "-") and "-" or pathIn:gsub("%.%w+$", "").."."..outputExtension
436443
end
437444
end
438445

@@ -444,13 +451,16 @@ end
444451

445452
local pathsSetIn = {}
446453
local pathsSetOut = {}
454+
447455
for i = 1, #pathsIn do
448456
if pathsSetIn [pathsIn [i]] then errorLine("Duplicate input path: " ..pathsIn [i]) end
449457
if pathsSetOut[pathsOut[i]] then errorLine("Duplicate output path: "..pathsOut[i]) end
458+
450459
pathsSetIn [pathsIn [i]] = true
451460
pathsSetOut[pathsOut[i]] = true
452-
if pathsSetOut[pathsIn [i]] then errorLine("Path is both input and output: "..pathsIn [i]) end
453-
if pathsSetIn [pathsOut[i]] then errorLine("Path is both input and output: "..pathsOut[i]) end
461+
462+
if pathsIn [i] ~= "-" and pathsSetOut[pathsIn [i]] then errorLine("Path is both input and output: "..pathsIn [i]) end
463+
if pathsOut[i] ~= "-" and pathsSetIn [pathsOut[i]] then errorLine("Path is both input and output: "..pathsOut[i]) end
454464
end
455465

456466

@@ -475,7 +485,7 @@ for i, pathIn in ipairs(pathsIn) do
475485
local pathOut = pathsOut[i]
476486
local pathMeta = pathOut:gsub("%.%w+$", "")..".meta.lua"
477487

478-
if not outputMeta then
488+
if not outputMeta or pathOut == "-" then
479489
pathMeta = nil
480490
end
481491

@@ -582,10 +592,10 @@ end
582592
printfNoise(
583593
"All done! (%.3fs, %.0f file%s, %.0f LOC, %.0f line%s, %.0f token%s, %s)",
584594
os.clock()-startClock,
585-
#pathsIn, #pathsIn == 1 and "" or "s",
595+
#pathsIn, (#pathsIn == 1) and "" or "s",
586596
lineCountCode,
587-
lineCount, lineCount == 1 and "" or "s",
588-
tokenCount, tokenCount == 1 and "" or "s",
597+
lineCount, (lineCount == 1) and "" or "s",
598+
tokenCount, (tokenCount == 1) and "" or "s",
589599
formatBytes(byteCount)
590600
)
591601

preprocess.lua

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ end
251251

252252

253253
function printf(s, ...)
254-
print(s:format(...))
254+
print(F(s, ...))
255255
end
256256

257257
-- printTokens( tokens [, filterUselessTokens ] )
@@ -764,7 +764,7 @@ function _tokenize(s, path, allowPpTokens, allowBacktickStrings, allowJitSyntax)
764764
tok.lineEnd = ln
765765

766766
tableInsert(tokens, tok)
767-
-- print(#tokens, tok.type, tok.representation)
767+
-- print(#tokens, tok.type, tok.representation) -- DEBUG
768768
end
769769

770770
return tokens
@@ -1334,7 +1334,7 @@ local metaFuncs = {}
13341334

13351335
-- printf()
13361336
-- printf( format, value1, ... )
1337-
-- Print a formatted string.
1337+
-- Print a formatted string to stdout.
13381338
metaFuncs.printf = printf
13391339

13401340
-- getFileContents()
@@ -2616,7 +2616,9 @@ local function _processFileOrString(params, isFile)
26162616
if not params.pathIn then error("Missing 'pathIn' in params.", 2) end
26172617
if not params.pathOut then error("Missing 'pathOut' in params.", 2) end
26182618

2619-
if params.pathOut == params.pathIn then error("'pathIn' and 'pathOut' are the same in params.", 2) end
2619+
if params.pathOut == params.pathIn and params.pathOut ~= "-" then
2620+
error("'pathIn' and 'pathOut' are the same in params.", 2)
2621+
end
26202622

26212623
else
26222624
if not params.code then error("Missing 'code' in params.", 2) end
@@ -2625,10 +2627,14 @@ local function _processFileOrString(params, isFile)
26252627
local luaUnprocessed, pathIn
26262628

26272629
if isFile then
2630+
pathIn = params.pathIn
26282631
local err
26292632

2630-
pathIn = params.pathIn
2631-
luaUnprocessed, err = getFileContents(pathIn, true)
2633+
if pathIn == "-" then
2634+
luaUnprocessed, err = io.stdin:read"*a"
2635+
else
2636+
luaUnprocessed, err = getFileContents(pathIn, true)
2637+
end
26322638

26332639
if not luaUnprocessed then
26342640
errorf("Could not read file '%s'. (%s)", pathIn, err)
@@ -2650,7 +2656,7 @@ local function _processFileOrString(params, isFile)
26502656
end
26512657

26522658
local tokens = _tokenize(luaUnprocessed, pathIn, true, params.backtickStrings, params.jitSyntax)
2653-
-- printTokens(tokens)
2659+
-- printTokens(tokens) -- DEBUG
26542660

26552661
-- Info variables.
26562662
local lastTok = tokens[#tokens]
@@ -3029,7 +3035,7 @@ local function _processFileOrString(params, isFile)
30293035
--==============================================================
30303036

30313037
local luaMeta = table.concat(metaParts)
3032-
--[[ :PrintCode
3038+
--[[ DEBUG :PrintCode
30333039
print("=META===============================")
30343040
print(luaMeta)
30353041
print("====================================")
@@ -3068,7 +3074,7 @@ local function _processFileOrString(params, isFile)
30683074
end
30693075

30703076
local lua = table.concat(outputFromMeta)
3071-
--[[ :PrintCode
3077+
--[[ DEBUG :PrintCode
30723078
print("=OUTPUT=============================")
30733079
print(lua)
30743080
print("====================================")
@@ -3095,10 +3101,15 @@ local function _processFileOrString(params, isFile)
30953101
local pathOut = isFile and params.pathOut or "<output>"
30963102

30973103
if isFile then
3098-
local file = assert(io.open(pathOut, "wb"))
3099-
file:write(specialFirstLine or "")
3100-
file:write(lua)
3101-
file:close()
3104+
if pathOut == "-" then
3105+
io.stdout:write(specialFirstLine or "")
3106+
io.stdout:write(lua)
3107+
else
3108+
local file = assert(io.open(pathOut, "wb"))
3109+
file:write(specialFirstLine or "")
3110+
file:write(lua)
3111+
file:close()
3112+
end
31023113
end
31033114

31043115
-- Check if the output is valid Lua.
@@ -3218,8 +3229,8 @@ local pp = {
32183229
-- info: Table with various information. (See 'ProcessInfo' for more info.)
32193230
--
32203231
-- params: Table with these fields:
3221-
-- pathIn = pathToInputFile -- [Required]
3222-
-- pathOut = pathToOutputFile -- [Required]
3232+
-- pathIn = pathToInputFile -- [Required] Specify "-" to use stdin.
3233+
-- pathOut = pathToOutputFile -- [Required] Specify "-" to use stdout. (Note that if stdout is used then anything you print() in the metaprogram will end up there.)
32233234
-- pathMeta = pathForMetaprogram -- [Optional] You can inspect this temporary output file if an error occurs in the metaprogram.
32243235
--
32253236
-- debug = boolean -- [Optional] Debug mode. The metaprogram file is formatted more nicely and does not get deleted automatically.

tests/runQuickTestUsingStd.cmd

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@ECHO OFF
2+
CD /D "%~dp0.."
3+
4+
IF NOT EXIST temp MD temp
5+
IF EXIST temp\stdout.txt DEL temp\stdout.txt
6+
IF EXIST temp\stderr.txt DEL temp\stderr.txt
7+
8+
lua ./preprocess-cl.lua --debug --data="Hello, world!" - <tests\quickTest.lua2p 1>temp\stdout.txt 2>temp\stderr.txt
9+
REM lua ./preprocess-cl.lua --debug --data="Hello, world!" - --backtickstrings <tests\quickTest.lua2p 1>temp\stdout.txt 2>temp\stderr.txt
10+
REM lua ./preprocess-cl.lua --debug --data="Hello, world!" - --linenumbers <tests\quickTest.lua2p 1>temp\stdout.txt 2>temp\stderr.txt
11+
12+
REM lua ./preprocess-cl.lua --debug --data="Hello, world!" --outputpaths - - <tests\quickTest.lua2p 1>temp\stdout.txt 2>temp\stderr.txt
13+
REM lua ./preprocess-cl.lua --debug --data="Hello, world!" --outputpaths - - --linenumbers <tests\quickTest.lua2p 1>temp\stdout.txt 2>temp\stderr.txt

0 commit comments

Comments
 (0)