Skip to content

Commit 637c274

Browse files
committed
Fixed error for '@line..""'. (It resulted in something like '1..""'.)
Fixed confusing error message for @@func(@bad). Invalid preprocessor keywords are reported earlier.
1 parent 26ab70f commit 637c274

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

preprocess.lua

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,8 @@ function _tokenize(s, path, allowPpTokens, allowBacktickStrings, allowJitSyntax)
720720
local i1, i2, repr, word = s:find("^(@([%a_][%w_]*))", ptr)
721721
if not i1 then
722722
errorInFile(s, path, ptr+1, "Tokenizer", "Expected an identifier.")
723+
elseif not PREPROCESSOR_KEYWORDS[word] then
724+
errorInFile(s, path, ptr+1, "Tokenizer", "Invalid preprocessor keyword '%s'.", word)
723725
end
724726
ptr = i2+1
725727
tok = {type="pp_keyword", representation=repr, value=word}
@@ -1806,7 +1808,7 @@ local function doEarlyExpansions(tokensToExpand, fileBuffers, params, stats)
18061808
tableInsert(tokens, newTokenAt({type="string", value=ppKeywordTok.file, representation=F("%q",ppKeywordTok.file)}, ppKeywordTok))
18071809
elseif ppKeywordTok.value == "line" then
18081810
table.remove(tokenStack) -- "@line"
1809-
tableInsert(tokens, newTokenAt({type="number", value=ppKeywordTok.line, representation=F("%d",ppKeywordTok.line)}, ppKeywordTok))
1811+
tableInsert(tokens, newTokenAt({type="number", value=ppKeywordTok.line, representation=F(" %d ",ppKeywordTok.line)}, ppKeywordTok)) -- Is it fine for the representation to have spaces? Probably.
18101812

18111813
else
18121814
-- Expand later.
@@ -1969,7 +1971,7 @@ local function doLateExpansions(tokensToExpand, fileBuffers, params, stats)
19691971
errorAtToken(fileBuffers, tableStartTok, nil, "Macro", "Syntax error: Could not find end of table constructor before EOF.")
19701972

19711973
elseif tok.type:find"^pp_" then
1972-
errorAtToken(fileBuffers, tok, nil, "Macro", "Preprocessor code not supported in macros. (Macro starts %s)", getRelativeLocationText(ppKeywordTok, tok))
1974+
errorAtToken(fileBuffers, tok, nil, "Macro", "Non-simple preprocessor code not supported in macros. (Macro starts %s)", getRelativeLocationText(ppKeywordTok, tok))
19731975

19741976
elseif bracketDepth == 1 and isToken(tok, "punctuation", "}") then
19751977
tableInsert(argTokens, table.remove(tokenStack))
@@ -2047,7 +2049,7 @@ local function doLateExpansions(tokensToExpand, fileBuffers, params, stats)
20472049
errorAtToken(fileBuffers, parensStartTok, nil, "Macro", "Syntax error: Could not find end of argument list before EOF.")
20482050

20492051
elseif tok.type:find"^pp_" then
2050-
errorAtToken(fileBuffers, tok, nil, "Macro", "Preprocessor code not supported in macros. (Macro starts %s)", getRelativeLocationText(ppKeywordTok, tok))
2052+
errorAtToken(fileBuffers, tok, nil, "Macro", "Non-simple preprocessor code not supported in macros. (Macro starts %s)", getRelativeLocationText(ppKeywordTok, tok))
20512053

20522054
elseif not depthStack[1] and (isToken(tok, "punctuation", ",") or isToken(tok, "punctuation", ")")) then
20532055
break
@@ -2138,7 +2140,7 @@ local function doLateExpansions(tokensToExpand, fileBuffers, params, stats)
21382140
end
21392141

21402142
else
2141-
errorAtToken(fileBuffers, ppKeywordTok, ppKeywordTok.position+1, "Parser", "Unknown preprocessor keyword '%s'.", ppKeywordTok.value)
2143+
errorAtToken(fileBuffers, ppKeywordTok, nil, "Macro", "Internal error. (%s)", ppKeywordTok.value)
21422144
end
21432145

21442146
-- Anything else.

tests/suite.lua

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,9 @@ doTest("Output values of different types", function()
189189

190190
-- Invalid: Functions, userdata, coroutines.
191191

192-
local luaOut = pp.processString{ code=[[ func = !(function()end) ]]}
193-
assert(not luaOut)
194-
195-
local luaOut = pp.processString{ code=[[ file = !(io.stdout) ]]}
196-
assert(not luaOut)
197-
198-
local luaOut = pp.processString{ code=[[ co = !(coroutine.create(function()end)) ]]}
199-
assert(not luaOut)
192+
assert(not pp.processString{ code=[[ func = !(function()end) ]]})
193+
assert(not pp.processString{ code=[[ file = !(io.stdout) ]]})
194+
assert(not pp.processString{ code=[[ co = !(coroutine.create(function()end)) ]]})
200195
end)
201196

202197
doTest("Preprocessor keywords", function()
@@ -206,7 +201,10 @@ doTest("Preprocessor keywords", function()
206201
assertCodeOutput(luaOut, [[filename = "<code>"]]) -- Note: The dummy value when we have no real path may change in the future.
207202

208203
local luaOut = assert(pp.processString{ code=[[ ln = @line ]]})
209-
assertCodeOutput(luaOut, [[ln = 1]])
204+
assertCodeOutput(luaOut, [[ln = 1]])
205+
206+
local luaOut = assert(pp.processString{ code=[[ lnStr = @file..@line..@line..@file ]]})
207+
assertCodeOutput(luaOut, [[lnStr = "<code>".. 1 .. 1 .."<code>"]])
210208

211209
local luaOut = assert(pp.processString{
212210
code = [[
@@ -254,13 +252,15 @@ doTest("Preprocessor keywords", function()
254252
]]})
255253
assertCodeOutput(luaOut, [[f = function() return a,"b" end]])
256254

255+
-- Invalid: Bad keyword.
256+
assert(not pp.processString{ code=[[ @bad ]]})
257+
257258
-- Invalid: Ambiguous syntax.
258-
local luaOut = pp.processString{ code=[[
259+
assert(not pp.processString{ code=[[
259260
!function void() return "" end
260261
v = @insert void
261262
()
262-
]]}
263-
assert(not luaOut)
263+
]]})
264264
end)
265265

266266

0 commit comments

Comments
 (0)