From bbb85dbbc45bd3d331bff5e2367b167ea5a305ef Mon Sep 17 00:00:00 2001 From: Emil Fredriksson Date: Sun, 7 Jun 2026 11:23:06 +0200 Subject: [PATCH] fix: preserve line comment at end of file without trailing newline --- spec/lang/lexer/comments_spec.lua | 27 +++++++++++++++++++++++++++ teal/lexer.lua | 4 ++++ teal/lexer.tl | 4 ++++ tl.lua | 4 ++++ 4 files changed, 39 insertions(+) diff --git a/spec/lang/lexer/comments_spec.lua b/spec/lang/lexer/comments_spec.lua index 5899f269e..e9bfbe0ca 100644 --- a/spec/lang/lexer/comments_spec.lua +++ b/spec/lang/lexer/comments_spec.lua @@ -122,4 +122,31 @@ describe("lexer", function() }, tokens[11].comments) end) + it("preserves line comment at end of file without trailing newline", function() + local tokens = tl.lex("-- a comment") + assert.same(1, #tokens) + assert.same("$EOF$", tokens[1].tk) + assert.same({ + {text = "-- a comment", x = 1, y = 1} + }, tokens[1].comments) + end) + + it("preserves empty line comment at end of file without trailing newline", function() + local tokens = tl.lex("--") + assert.same(1, #tokens) + assert.same("$EOF$", tokens[1].tk) + assert.same({ + {text = "--", x = 1, y = 1} + }, tokens[1].comments) + end) + + it("preserves line comment starting with [ at end of file without trailing newline", function() + local tokens = tl.lex("--[") + assert.same(1, #tokens) + assert.same("$EOF$", tokens[1].tk) + assert.same({ + {text = "--[", x = 1, y = 1} + }, tokens[1].comments) + end) + end) diff --git a/teal/lexer.lua b/teal/lexer.lua index 8c58a62f9..528113c99 100644 --- a/teal/lexer.lua +++ b/teal/lexer.lua @@ -805,6 +805,10 @@ do end end + if state == "comment short" or state == "got --" or state == "got --[" then + add_comment(input:sub(ti, #input)) + end + table.insert(tokens, { x = x + 1, y = y, tk = "$EOF$", kind = "$EOF$", comments = comments }) return tokens, errs diff --git a/teal/lexer.tl b/teal/lexer.tl index c586c485b..4522a58bc 100644 --- a/teal/lexer.tl +++ b/teal/lexer.tl @@ -805,6 +805,10 @@ do end end + if state == "comment short" or state == "got --" or state == "got --[" then + add_comment(input:sub(ti, #input)) + end + table.insert(tokens, { x = x + 1, y = y, tk = "$EOF$", kind = "$EOF$", comments = comments }) return tokens, errs diff --git a/tl.lua b/tl.lua index 552921030..a9272314a 100644 --- a/tl.lua +++ b/tl.lua @@ -13302,6 +13302,10 @@ do end end + if state == "comment short" or state == "got --" or state == "got --[" then + add_comment(input:sub(ti, #input)) + end + table.insert(tokens, { x = x + 1, y = y, tk = "$EOF$", kind = "$EOF$", comments = comments }) return tokens, errs