Skip to content

Commit 10eab1a

Browse files
committed
Fixed issue when files don't end in a newline.
CL: Many more tests!
1 parent 5ab5d7c commit 10eab1a

File tree

6 files changed

+293
-86
lines changed

6 files changed

+293
-86
lines changed

preprocess-cl.lua

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ exec lua "$0" "$@"
1111
--=
1212
--= License: MIT (see the bottom of this file)
1313
--= Website: http://refreezed.com/luapreprocess/
14+
--= Documentation: http://refreezed.com/luapreprocess/docs/command-line/
1415
--=
15-
--= Tested with Lua 5.1, 5.2, 5.3 and 5.4.
16+
--= Tested with Lua 5.1, 5.2, 5.3, 5.4 and LuaJIT.
1617
--=
1718
--==============================================================
1819
@@ -111,7 +112,7 @@ exec lua "$0" "$@"
111112
enabled if an output path is stdout.)
112113
113114
--version
114-
Print the version of LuaPreprocess and exit.
115+
Print the version of LuaPreprocess to stdout and exit.
115116
116117
--debug
117118
Enable some preprocessing debug features. Useful if you want
@@ -335,7 +336,7 @@ for _, arg in ipairs(args) do
335336
if hasOutputExtension then
336337
errorLine("Cannot specify both --outputpaths and --outputextension")
337338
elseif pathsIn[1] then
338-
errorLine(arg.." must appear before any path.")
339+
errorLine(arg.." must appear before any input path.")
339340
end
340341
hasOutputPaths = true
341342

@@ -364,7 +365,7 @@ for _, arg in ipairs(args) do
364365
maxLogLevel = arg:gsub("^.-=", "")
365366

366367
elseif arg == "--version" then
367-
print(pp.VERSION)
368+
io.stdout:write(pp.VERSION)
368369
os.exit()
369370

370371
-- elseif arg == "/?" or arg:find"^%-%-?help" or arg:lower() == "/help" then

preprocess.lua

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -432,36 +432,36 @@ local function parseStringlikeToken(s, ptr)
432432
local valueEnd
433433

434434
local longEqualSigns = s:match("^%[(=*)%[", ptr)
435-
local isLong = (longEqualSigns ~= nil)
435+
local isLong = longEqualSigns ~= nil
436436

437437
-- Single line.
438438
if not isLong then
439439
valueStart = ptr
440440

441-
local i1, i2 = s:find("\r?\n", ptr)
442-
if not i1 then
441+
local i = s:find("\n", ptr, true)
442+
if not i then
443443
reprEnd = #s
444444
valueEnd = #s
445-
ptr = reprEnd+1
445+
ptr = reprEnd + 1
446446
else
447-
reprEnd = i2
448-
valueEnd = i1-1
449-
ptr = reprEnd+1
447+
reprEnd = i
448+
valueEnd = i - 1
449+
ptr = reprEnd + 1
450450
end
451451

452452
-- Multiline.
453453
else
454-
ptr = ptr+1+#longEqualSigns+1
454+
ptr = ptr + 1 + #longEqualSigns + 1
455455
valueStart = ptr
456456

457-
local i1, i2 = s:find("%]"..longEqualSigns.."%]", ptr)
457+
local i1, i2 = s:find("]"..longEqualSigns.."]", ptr, true)
458458
if not i1 then
459459
return nil, ERROR_UNFINISHED_STRINGLIKE
460460
end
461461

462462
reprEnd = i2
463-
valueEnd = i1-1
464-
ptr = reprEnd+1
463+
valueEnd = i1 - 1
464+
ptr = reprEnd + 1
465465
end
466466

467467
local repr = s:sub(reprStart, reprEnd)
@@ -484,6 +484,8 @@ local NUM_DEC = ("^( %d+ %.? )"):gsub(" +",
484484

485485
-- tokens = _tokenize( luaString, path, allowPreprocessorTokens, allowBacktickStrings, allowJitSyntax )
486486
local function _tokenize(s, path, allowPpTokens, allowBacktickStrings, allowJitSyntax)
487+
s = s:gsub("\r", "") -- Normalize line breaks. (Assume the input is either "\n" or "\r\n".)
488+
487489
local tokens = {}
488490
local ptr = 1
489491
local ln = 1
@@ -2400,8 +2402,8 @@ local function doEarlyExpansions(tokensToExpand, stats)
24002402

24012403
-- Backtick string.
24022404
elseif isToken(tok, "string") and tok.representation:find"^`" then
2403-
local stringTok = tok
2404-
stringTok.representation = F("%q", stringTok.value)
2405+
local stringTok = tok
2406+
stringTok.representation = toLua(stringTok.value)--F("%q", stringTok.value)
24052407

24062408
tableInsert(outTokens, stringTok)
24072409
tableRemove(tokenStack) -- the string
@@ -3290,7 +3292,7 @@ local function astNodeToMetaprogram(buffer, ast, ln, lnMeta, asMacroArgExpr)
32903292
tableInsert(buffer, ")")
32913293
end
32923294

3293-
-- Use trailing comma if the user does.
3295+
-- Use trailing semicolon if the user does.
32943296
for i = #ast.valueTokens, 1, -1 do
32953297
if isToken(ast.valueTokens[i], "punctuation", ";") then
32963298
if current_parsingAndMeta_isDebug
@@ -3378,7 +3380,13 @@ local function _processFileOrString(params, isFile)
33783380

33793381
local specialFirstLine, rest = luaUnprocessed:match"^(#[^\r\n]*\r?\n?)(.*)$"
33803382
if specialFirstLine then
3381-
luaUnprocessed = rest
3383+
specialFirstLine = specialFirstLine:gsub("\r", "") -- Normalize line breaks. (Assume the input is either "\n" or "\r\n".)
3384+
luaUnprocessed = rest
3385+
end
3386+
3387+
-- Ensure there's a newline at the end of the code, otherwise there will be problems down the line.
3388+
if not (luaUnprocessed == "" or luaUnprocessed:find"\n%s*$") then
3389+
luaUnprocessed = luaUnprocessed .. "\n"
33823390
end
33833391

33843392
local tokens = _tokenize(luaUnprocessed, virtualPathIn, true, params.backtickStrings, params.jitSyntax)
@@ -3509,7 +3517,7 @@ local function _processFileOrString(params, isFile)
35093517

35103518
if not chunk then
35113519
local ln, _err = err:match"^.-:(%d+): (.*)"
3512-
errorOnLine(pathOut, (tonumber(ln) or 0), nil, "%s", (_err or err))
3520+
errorOnLine(pathOut, (tonumber(ln) or 0), nil, "Output is invalid Lua. (%s)", (_err or err))
35133521
end
35143522
end
35153523

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
--= Test message handler
44
--= for use with preprocess-cl.lua
55
--=
6+
--= Alternative #2: A single catch-all message handler.
7+
--=
68
--============================================================]]
79

810
--
@@ -26,28 +28,6 @@ end
2628
-- The module is expected to return one or multiple message handlers.
2729
--
2830

29-
-- [[ Alternative #1: Multiple specific message handlers.
30-
return {
31-
beforemeta = function(path)
32-
print("... Now processing "..path)
33-
end,
34-
35-
aftermeta = function(path, luaString)
36-
-- Remove comments (quick and dirty).
37-
luaString = luaString
38-
:gsub("%-%-%[%[.-%]%]", "") -- Multi-line.
39-
:gsub("%-%-[^\n]*", "") -- Single line.
40-
41-
return luaString
42-
end,
43-
44-
filedone = function(path, outputPath)
45-
print("... Done with "..path.." (writing to "..outputPath..")")
46-
end,
47-
}
48-
--]]
49-
50-
--[[ Alternative #2: A single catch-all message handler.
5131
return function(message, ...)
5232
if message == "beforemeta" then
5333
local path = ...
@@ -68,4 +48,3 @@ return function(message, ...)
6848
print("... Done with "..path.." (writing to "..outputPath..")")
6949
end
7050
end
71-
--]]

tests/quickTestHandlerTable.lua

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
--[[============================================================
2+
--=
3+
--= Test message handler
4+
--= for use with preprocess-cl.lua
5+
--=
6+
--= Alternative #1: Multiple specific message handlers
7+
--=
8+
--============================================================]]
9+
10+
--
11+
-- This module shares environment with the processed files,
12+
-- making it a good place to put things all files use/share.
13+
--
14+
15+
_G.IS_DEVELOPER = true
16+
17+
math.tau = 2*math.pi
18+
19+
function _G.flipTable(t)
20+
local flipped = {}
21+
for k, v in pairs(t) do
22+
flipped[v] = k
23+
end
24+
return flipped
25+
end
26+
27+
--
28+
-- The module is expected to return one or multiple message handlers.
29+
--
30+
31+
return {
32+
beforemeta = function(path)
33+
print("... Now processing "..path)
34+
end,
35+
36+
aftermeta = function(path, luaString)
37+
-- Remove comments (quick and dirty).
38+
luaString = luaString
39+
:gsub("%-%-%[%[.-%]%]", "") -- Multi-line.
40+
:gsub("%-%-[^\n]*", "") -- Single line.
41+
42+
return luaString
43+
end,
44+
45+
filedone = function(path, outputPath)
46+
print("... Done with "..path.." (writing to "..outputPath..")")
47+
end,
48+
}

tests/runQuickTestUsingHandler.cmd

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ IF NOT EXIST temp MD temp
1919

2020

2121

22-
%_lua% ./preprocess-cl.lua --debug --saveinfo=temp/info.lua --handler=tests/quickTestHandler.lua tests/quickTest.lua2p || EXIT /B 1
23-
REM %_lua% ./preprocess-cl.lua --debug --saveinfo=temp/info.lua --handler=tests/quickTestHandler.lua --outputpaths tests/quickTest.lua2p temp/quickTest.output.lua || EXIT /B 1
22+
%_lua% ./preprocess-cl.lua --debug --saveinfo=temp/info.lua --handler=tests/quickTestHandlerTable.lua tests/quickTest.lua2p || EXIT /B 1
23+
REM %_lua% ./preprocess-cl.lua --debug --saveinfo=temp/info.lua --handler=tests/quickTestHandlerTable.lua --outputpaths tests/quickTest.lua2p temp/quickTest.output.lua || EXIT /B 1
2424

25+
%_lua% ./preprocess-cl.lua --debug --saveinfo=temp/info.lua --handler=tests/quickTestHandlerFunction.lua tests/quickTest.lua2p || EXIT /B 1
26+
REM %_lua% ./preprocess-cl.lua --debug --saveinfo=temp/info.lua --handler=tests/quickTestHandlerFunction.lua --outputpaths tests/quickTest.lua2p temp/quickTest.output.lua || EXIT /B 1
27+
28+
ECHO. & ECHO Running quickTest.lua...
2529
%_lua% -e"io.stdout:setvbuf'no' io.stderr:setvbuf'no'" tests/quickTest.lua || EXIT /B 1

0 commit comments

Comments
 (0)