From d06362acb64a9cd2ffcd09a9cc89dd8df9e45405 Mon Sep 17 00:00:00 2001 From: Dave Lucia Date: Mon, 15 Jun 2026 10:19:39 -0400 Subject: [PATCH 1/3] fix(parser): propagate deep errors past function-call argument lists When an argument sub-parse failed with a non-:unexpected_token error (e.g. a bare-expression statement deep inside a `function` argument), parse_expr_list_acc/2 treated it as a clean list terminator because error_position/1 only understood :unexpected_token. The list "recovered" and expect_closing emitted a misleading "Expected rparen, got function" pinned at the enclosing call's opener line instead of the real error. Teach error_position/1 to read the position out of :bare_expression, :invalid_assign_target, and :unclosed_delimiter errors so committed?/2 classifies them correctly and the genuine deep error propagates. --- lib/lua/parser.ex | 3 +++ test/lua/parser/error_position_test.exs | 8 +++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/lua/parser.ex b/lib/lua/parser.ex index 738e7c9..e1e555e 100644 --- a/lib/lua/parser.ex +++ b/lib/lua/parser.ex @@ -1254,6 +1254,9 @@ defmodule Lua.Parser do end defp error_position({:unexpected_token, _type, pos, _message}), do: pos + defp error_position({:bare_expression, pos, _expr_struct}), do: pos + defp error_position({:invalid_assign_target, pos, _expr_struct}), do: pos + defp error_position({:unclosed_delimiter, _delimiter, open_pos}), do: open_pos defp error_position(_), do: nil defp parse_expr_list_until(tokens, terminator, delimiter, open_pos) do diff --git a/test/lua/parser/error_position_test.exs b/test/lua/parser/error_position_test.exs index f1a9dfa..1f9f8a9 100644 --- a/test/lua/parser/error_position_test.exs +++ b/test/lua/parser/error_position_test.exs @@ -99,7 +99,13 @@ defmodule Lua.Parser.ErrorPositionTest do {"bad token in function inside function inside call", "outer(function()\n inner(function()\n x = +\n end)\nend)\n", 3, 9, "Expected expression"}, {"deep error in a return list", "function f()\n return 1, 2, g(\nend\n", 3, 1, "Expected expression"}, - {"stray statement after nested calls", "outer(mid(inner(\n)))\n.. bad\n", 3, 1, "bare"} + {"stray statement after nested calls", "outer(mid(inner(\n)))\n.. bad\n", 3, 1, "bare"}, + # A bare-expression error deep inside a function argument must blame the + # inner statement, not the `function` keyword at the enclosing call's + # opener. Regression for a stray `)` reported against the wrong line. + {"bare expression in nested function argument", + ~s|w:step("a", "b", function(output)\n device.doesntExist)\nend)\n|, 2, 3, + "bare table-access expression"} ] @statement_errors [ From 5e73e779b0cd894c08e0f5e866c14438dd545d78 Mon Sep 17 00:00:00 2001 From: Dave Lucia Date: Mon, 15 Jun 2026 10:34:39 -0400 Subject: [PATCH 2/3] fix(parser): treat bare-expr/invalid-assign sub-parse failures as committed Stabilization follow-up from adversarial review of the deep-error-position fix. Three refinements: - :bare_expression and :invalid_assign_target are now classified as always-committed in committed?/2 rather than position-based. They are statement-level errors only reachable through a function-literal body, so they always represent consumed input. This closes a nil-position fall-through: an Expr.Call/Expr.Paren assignment target (built with meta: nil) yielded a nil position, so the old position-based check still swallowed the deep error and blamed the enclosing call opener. - validate_assign_targets/2 falls back to the `=` operator position when the offending target node carries no position, so the propagated error points at the right line instead of degrading to nil. - error_position/1 keeps the :unclosed_delimiter clause (position-based via the opener) so a delimiter that begins an argument still recovers at the outer boundary. Adds regression rows locking the invalid-assign and non-first-argument unclosed-delimiter paths, which were previously untested. --- lib/lua/parser.ex | 31 ++++++++++++++++++------- test/lua/parser/error_position_test.exs | 13 ++++++++--- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/lib/lua/parser.ex b/lib/lua/parser.ex index e1e555e..a3b6e96 100644 --- a/lib/lua/parser.ex +++ b/lib/lua/parser.ex @@ -642,7 +642,7 @@ defmodule Lua.Parser do end defp parse_assignment(targets, [{:operator, :assign, pos} | rest]) do - case validate_assign_targets(targets) do + case validate_assign_targets(targets, pos) do :ok -> case parse_expr_list(rest) do {:ok, values, rest2} -> @@ -669,13 +669,17 @@ defmodule Lua.Parser do # Name | prefixexp '[' exp ']' | prefixexp '.' Name. Anything else # (number, string, function call, parenthesised expression) is a # syntax error. - defp validate_assign_targets(targets) do + # `assign_pos` is the position of the `=` operator, used as a fallback when + # the offending target node carries no position (e.g. an `Expr.Call` LHS, + # built with `meta: nil`) so the error still points at the right line rather + # than degrading to a nil position. + defp validate_assign_targets(targets, assign_pos) do Enum.reduce_while(targets, :ok, fn target, :ok -> case target do %Expr.Var{} -> {:cont, :ok} %Expr.Property{} -> {:cont, :ok} %Expr.Index{} -> {:cont, :ok} - other -> {:halt, {:error, {:invalid_assign_target, target_position(other), other.__struct__}}} + other -> {:halt, {:error, {:invalid_assign_target, target_position(other) || assign_pos, other.__struct__}}} end end) end @@ -1238,11 +1242,20 @@ defmodule Lua.Parser do # A sub-parse "committed" if it consumed input before failing. We detect # this by comparing the error's byte offset against the offset of the # token the sub-parse started on: an error positioned strictly past the - # start means tokens were consumed. `:unexpected_end` (EOF) is always - # committed — running out of input mid-list is a real error, never a - # clean list terminator. A missing position falls back to "not + # start means tokens were consumed. A missing position falls back to "not # committed", preserving the previous recovery behaviour. + # + # Some error shapes are always committed regardless of their reported + # position: `:unexpected_end` (EOF) means we ran out of input mid-list, + # and `:bare_expression` / `:invalid_assign_target` are statement-level + # errors only reachable through a `function() ... end` body — i.e. only + # after a full prefix expression has been consumed. None of these can ever + # represent a clean list terminator, so they propagate even when their + # position is absent (e.g. an invalid-assign target on a `meta: nil` + # postfix node, whose `target_position/1` is `nil`). defp committed?({:unexpected_end, _message, _pos}, _tokens), do: true + defp committed?({:bare_expression, _pos, _expr_struct}, _tokens), do: true + defp committed?({:invalid_assign_target, _pos, _expr_struct}, _tokens), do: true defp committed?(reason, tokens) do with %{byte_offset: error_offset} <- error_position(reason), @@ -1253,9 +1266,11 @@ defmodule Lua.Parser do end end + # `:unclosed_delimiter` stays position-based (via its opener): a delimiter + # that *begins* an argument has `open_pos == start_offset`, so the list + # still recovers at the outer boundary, while a delimiter opened mid-element + # propagates as the genuine deep error. defp error_position({:unexpected_token, _type, pos, _message}), do: pos - defp error_position({:bare_expression, pos, _expr_struct}), do: pos - defp error_position({:invalid_assign_target, pos, _expr_struct}), do: pos defp error_position({:unclosed_delimiter, _delimiter, open_pos}), do: open_pos defp error_position(_), do: nil diff --git a/test/lua/parser/error_position_test.exs b/test/lua/parser/error_position_test.exs index 1f9f8a9..1b81cd9 100644 --- a/test/lua/parser/error_position_test.exs +++ b/test/lua/parser/error_position_test.exs @@ -103,9 +103,16 @@ defmodule Lua.Parser.ErrorPositionTest do # A bare-expression error deep inside a function argument must blame the # inner statement, not the `function` keyword at the enclosing call's # opener. Regression for a stray `)` reported against the wrong line. - {"bare expression in nested function argument", - ~s|w:step("a", "b", function(output)\n device.doesntExist)\nend)\n|, 2, 3, - "bare table-access expression"} + {"bare expression in nested function argument", ~s|w:step("a", "b", function(output)\n device.doesntExist)\nend)\n|, + 2, 3, "bare table-access expression"}, + # An invalid assignment target deep inside a function argument must blame + # the inner statement even though the offending node (`f()`, an Expr.Call) + # carries no position — the error is structurally committed. + {"invalid assign target in nested function argument", ~s|outer("ok", function()\n f() = 1\nend)\n|, 2, 7, + "syntax error near '='"}, + # An unclosed delimiter opened inside a non-first argument propagates as + # the genuine deep error rather than being swallowed at the outer boundary. + {"unclosed delimiter in non-first call argument", "outer(1, inner(\n", 1, 15, "Unclosed opening parenthesis"} ] @statement_errors [ From 65a7c042990a95e733190a29d8ac47988bc07c36 Mon Sep 17 00:00:00 2001 From: Dave Lucia Date: Mon, 15 Jun 2026 11:30:40 -0400 Subject: [PATCH 3/3] fix(parser): blame opener for unclosed delimiter beginning a non-first argument An unclosed (/{/[ that begins a non-first call argument was reported as a misleading "Expected )" at the outer call boundary, while the same delimiter as the first argument or opened mid-element was correctly blamed at its opener. The begins-a-non-first-argument case fell through because its open_pos equals the sub-parse start offset, so committed?/2's strict position test (error_offset > start_offset) read it as "not committed" and the list recovered. Make :unclosed_delimiter unconditionally committed, joining :unexpected_end, :bare_expression, and :invalid_assign_target. An opened-but-unclosed delimiter has by construction consumed an opener and content, so it can never be a clean list terminator and must always propagate as the genuine deep error. The position-gated path is left for :unexpected_token, which still relies on it for clean trailing-comma/stray-closer recovery. --- lib/lua/parser.ex | 13 ++++++++----- test/lua/parser/error_position_test.exs | 7 ++++++- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/lua/parser.ex b/lib/lua/parser.ex index a3b6e96..51671cc 100644 --- a/lib/lua/parser.ex +++ b/lib/lua/parser.ex @@ -1253,9 +1253,17 @@ defmodule Lua.Parser do # represent a clean list terminator, so they propagate even when their # position is absent (e.g. an invalid-assign target on a `meta: nil` # postfix node, whose `target_position/1` is `nil`). + # + # `:unclosed_delimiter` joins them: an opened-but-unclosed delimiter has by + # construction consumed an opener and its content, so it can never be a clean + # list terminator. Gating it on position would make blame depend on whether + # the opener happens to be the argument's first token (`open_pos == + # start_offset`) — the begins-a-non-first-argument case — versus opened + # mid-element. Always committing it blames the inner opener uniformly. defp committed?({:unexpected_end, _message, _pos}, _tokens), do: true defp committed?({:bare_expression, _pos, _expr_struct}, _tokens), do: true defp committed?({:invalid_assign_target, _pos, _expr_struct}, _tokens), do: true + defp committed?({:unclosed_delimiter, _delimiter, _open_pos}, _tokens), do: true defp committed?(reason, tokens) do with %{byte_offset: error_offset} <- error_position(reason), @@ -1266,12 +1274,7 @@ defmodule Lua.Parser do end end - # `:unclosed_delimiter` stays position-based (via its opener): a delimiter - # that *begins* an argument has `open_pos == start_offset`, so the list - # still recovers at the outer boundary, while a delimiter opened mid-element - # propagates as the genuine deep error. defp error_position({:unexpected_token, _type, pos, _message}), do: pos - defp error_position({:unclosed_delimiter, _delimiter, open_pos}), do: open_pos defp error_position(_), do: nil defp parse_expr_list_until(tokens, terminator, delimiter, open_pos) do diff --git a/test/lua/parser/error_position_test.exs b/test/lua/parser/error_position_test.exs index 1b81cd9..7139914 100644 --- a/test/lua/parser/error_position_test.exs +++ b/test/lua/parser/error_position_test.exs @@ -112,7 +112,12 @@ defmodule Lua.Parser.ErrorPositionTest do "syntax error near '='"}, # An unclosed delimiter opened inside a non-first argument propagates as # the genuine deep error rather than being swallowed at the outer boundary. - {"unclosed delimiter in non-first call argument", "outer(1, inner(\n", 1, 15, "Unclosed opening parenthesis"} + {"unclosed delimiter in non-first call argument", "outer(1, inner(\n", 1, 15, "Unclosed opening parenthesis"}, + # An unclosed delimiter that *begins* a non-first argument is blamed at + # that opener too, matching the first-argument and mid-element cases — + # not at a misleading "Expected )" on the outer call boundary. + {"unclosed brace beginning a non-first call argument", "outer(1, {a = 1\n", 1, 10, "Unclosed opening brace"}, + {"unclosed paren beginning a non-first call argument", "outer(1, (2 + 3\n", 1, 10, "Unclosed opening parenthesis"} ] @statement_errors [