Skip to content

fix(parser): allow comments between a bare return and its terminator - #418

Merged
davydog187 merged 1 commit into
tv-labs:mainfrom
turnhub:upstream-return-comment-fix
Jul 28, 2026
Merged

fix(parser): allow comments between a bare return and its terminator#418
davydog187 merged 1 commit into
tv-labs:mainfrom
turnhub:upstream-return-comment-fix

Conversation

@smn

@smn smn commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Problem

A valueless return followed by a comment before the block terminator (elseif / else / end / until) fails to parse:

local function f(method)
  if method == "GET" then
    return
  -- handle other methods
  elseif method == "POST" then
    return
  end
end
Parse Error: Expected expression

Comments are whitespace in Lua, so the return here is simply a bare (valueless) return and the branch continues normally. Reference Lua 5.3 accepts this, and it's a common idiom (then return -- note \n elseif ...).

Root cause

Lua.Parser.parse_return/1 peeked at the raw next token to decide whether the return was valueless. A comment token is not one of the terminators (end/else/elseif/until), nor EOF/;, so it fell through to parse_expr_list, which then choked on the comment → "Expected expression".

Fix

Peek past comment tokens (via the existing skip_comments/1) when detecting the terminator. The comment tokens are left in the stream so the block parser still collects them as orphaned/trailing comments — no comment metadata is lost.

case peek(skip_comments(rest)) do

The semicolon branch consumes past comments as well, so return -- note followed by ; is handled too.

Tests

Added a regression test in test/lua/parser/statement_test.exs covering a bare return with a comment before elseif/else/end — fails before, passes after.

  • mix test test/lua/parser/ — all green
  • mix test (full suite) — all green

🤖 Generated with Claude Code

A valueless `return` followed by a comment before the block terminator
(`elseif`/`else`/`end`/`until`) failed to parse with "Expected expression".
`parse_return/1` peeked at the raw next token to detect an empty return; a
comment token is not a terminator, so it fell through to `parse_expr_list`
and choked on the comment.

Comments are whitespace in Lua, so peek past them (`skip_comments/1`) when
deciding whether the return is bare. The comment tokens are left in the
stream so the block parser still collects them as orphaned/trailing
comments. Real Lua 5.3 and Luerl both accept this; the idiom shows up in
`if ... then return -- note \n elseif ...` style branches.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@davydog187
davydog187 merged commit 637f604 into tv-labs:main Jul 28, 2026
5 checks passed
@davydog187

Copy link
Copy Markdown
Contributor

Thanks!

🍍 🍍 🍍 🍍 🍍

@davydog187 davydog187 mentioned this pull request Jul 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants