Skip to content

Commit

Permalink
pragma: ignores other --# lines
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamhm committed Sep 2, 2024
1 parent e49ac8b commit d04fa0f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
14 changes: 9 additions & 5 deletions spec/pragma/invalid_spec.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
local util = require("spec.util")

describe("invalid pragma", function()
it("rejects invalid pragma", util.check_syntax_error([[
--#invalid_pragma on
it("ignores other --# lines", util.check([[
--#invalid on
]]))

it("rejects invalid pragma", util.check_type_error([[
--#pragma invalid_foo on
]], {
{ y = 1, msg = "invalid token '--#invalid_pragma'" }
{ y = 1, msg = "invalid pragma: invalid_foo" }
}))

it("pragmas currently do not accept punctuation", util.check_syntax_error([[
--#pragma something(other)
]], {
{ y = 1, msg = "invalid token '('" },
{ y = 1, msg = "invalid token ')'" },
{ y = 1, x = 26, msg = "invalid token '('" },
{ y = 1, x = 32, msg = "invalid token ')'" },
}))

it("pragma arguments need to be in a single line", util.check_syntax_error([[
Expand Down
12 changes: 8 additions & 4 deletions tl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ do
["number hexfloat"] = "number",
["number power"] = "number",
["number powersign"] = "$ERR invalid_number$",
["pragma"] = "pragma",
["pragma"] = nil,
["pragma any"] = nil,
["pragma word"] = "pragma_identifier",
}
Expand Down Expand Up @@ -1285,11 +1285,15 @@ do
elseif state == "pragma" then
if not lex_word[c] then
end_token_prev("pragma")
if tokens[nt].tk ~= "--#pragma" then
add_syntax_error()
if tokens[nt].tk == "--#pragma" then
state = "pragma any"
else
state = "comment short"
table.remove(tokens)
nt = nt - 1
drop_token()
end
fwd = false
state = "pragma any"
end
elseif state == "pragma any" then
if c == "\n" then
Expand Down
12 changes: 8 additions & 4 deletions tl.tl
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ do
["number hexfloat"] = "number",
["number power"] = "number",
["number powersign"] = "$ERR invalid_number$",
["pragma"] = "pragma",
["pragma"] = nil, -- drop comment
["pragma any"] = nil, -- never in a token
["pragma word"] = "pragma_identifier", -- never in a token
}
Expand Down Expand Up @@ -1285,11 +1285,15 @@ do
elseif state == "pragma" then
if not lex_word[c] then
end_token_prev("pragma")
if tokens[nt].tk ~= "--#pragma" then
add_syntax_error()
if tokens[nt].tk == "--#pragma" then
state = "pragma any"
else
state = "comment short"
table.remove(tokens)
nt = nt - 1
drop_token()
end
fwd = false
state = "pragma any"
end
elseif state == "pragma any" then
if c == "\n" then
Expand Down

0 comments on commit d04fa0f

Please sign in to comment.