Skip to content

Commit 4b123b8

Browse files
efredrikssonhishamhm
authored andcommitted
fix: preserve line comment at end of file without trailing newline
1 parent ed9ff68 commit 4b123b8

4 files changed

Lines changed: 39 additions & 0 deletions

File tree

spec/lang/lexer/comments_spec.lua

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,31 @@ describe("lexer", function()
122122
}, tokens[11].comments)
123123
end)
124124

125+
it("preserves line comment at end of file without trailing newline", function()
126+
local tokens = tl.lex("-- a comment")
127+
assert.same(1, #tokens)
128+
assert.same("$EOF$", tokens[1].tk)
129+
assert.same({
130+
{text = "-- a comment", x = 1, y = 1}
131+
}, tokens[1].comments)
132+
end)
133+
134+
it("preserves empty line comment at end of file without trailing newline", function()
135+
local tokens = tl.lex("--")
136+
assert.same(1, #tokens)
137+
assert.same("$EOF$", tokens[1].tk)
138+
assert.same({
139+
{text = "--", x = 1, y = 1}
140+
}, tokens[1].comments)
141+
end)
142+
143+
it("preserves line comment starting with [ at end of file without trailing newline", function()
144+
local tokens = tl.lex("--[")
145+
assert.same(1, #tokens)
146+
assert.same("$EOF$", tokens[1].tk)
147+
assert.same({
148+
{text = "--[", x = 1, y = 1}
149+
}, tokens[1].comments)
150+
end)
151+
125152
end)

teal/lexer.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,10 @@ do
805805
end
806806
end
807807

808+
if state == "comment short" or state == "got --" or state == "got --[" then
809+
add_comment(input:sub(ti, #input))
810+
end
811+
808812
table.insert(tokens, { x = x + 1, y = y, tk = "$EOF$", kind = "$EOF$", comments = comments })
809813

810814
return tokens, errs

teal/lexer.tl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,10 @@ do
805805
end
806806
end
807807

808+
if state == "comment short" or state == "got --" or state == "got --[" then
809+
add_comment(input:sub(ti, #input))
810+
end
811+
808812
table.insert(tokens, { x = x + 1, y = y, tk = "$EOF$", kind = "$EOF$", comments = comments })
809813

810814
return tokens, errs

tl.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13302,6 +13302,10 @@ do
1330213302
end
1330313303
end
1330413304

13305+
if state == "comment short" or state == "got --" or state == "got --[" then
13306+
add_comment(input:sub(ti, #input))
13307+
end
13308+
1330513309
table.insert(tokens, { x = x + 1, y = y, tk = "$EOF$", kind = "$EOF$", comments = comments })
1330613310

1330713311
return tokens, errs

0 commit comments

Comments
 (0)