diff --git a/lib/lua/parser.ex b/lib/lua/parser.ex index 738e7c9..51671cc 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,28 @@ 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`). + # + # `: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), diff --git a/test/lua/parser/error_position_test.exs b/test/lua/parser/error_position_test.exs index f1a9dfa..7139914 100644 --- a/test/lua/parser/error_position_test.exs +++ b/test/lua/parser/error_position_test.exs @@ -99,7 +99,25 @@ 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"}, + # 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"}, + # 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 [