fix(parser): canonicalize unexpected-token error shape across five sites#241
Merged
Conversation
Five sites in lib/lua/parser.ex emitted a 3-tuple
`{:unexpected_token, peek(tokens), msg}` instead of the canonical
4-tuple `{:unexpected_token, type, pos, msg}` the renderer
pattern-matches on. They fell through the catch-all converter,
producing user-visible output like:
Parse Error
(no position information)
Parse error: {:unexpected_token, {:operator, :assign, %{...}}, ...}
Reported by a user running `return { "ok" = 5 }`.
Introduce a private helper `unexpected_token_error/2` that
destructures the head token (real / EOF / empty) into the canonical
shape, and route all five sites plus the existing inline case in
`parse_local/1` (A36) through it.
Also extend `suggest_for_token_error/2` with a tailored hint for the
common `{ "key" = value }` mistake (users reaching for JSON/map
syntax), pointing them at the bracketed form `{ ["key"] = value }`.
Test count: 1705 -> 1715 (+10), 0 failures.
Plan: A37
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a user-reported parser error rendering bug. The Lua snippet
…previously produced:
Now produces:
Root cause
Lua.Parser.convert_error/2pattern-matches the canonical 4-tuple{:unexpected_token, type, pos, message}and routes it through thebeautified
Lua.Parser.Error.format/2pipeline. Five sites inlib/lua/parser.exinstead built a malformed 3-tuple where the secondelement was the entire peeked token (itself a 3-tuple):
That falls through to the catch-all converter
(
defp convert_error(other, _code)), whichinspect/1-dumps the tupleinto the user-facing message with
position: nil.This is the same class of bug A36 (#222) fixed for the lone
localkeyword path; five siblings were missed.
Changes
lib/lua/parser.exunexpected_token_error/2that destructuresthe head token (real / EOF / empty token list) into the canonical
error shape.
parse_for/1—for x ?with neither=norinparse_generic_for/3— variable list not terminated byinparse_assignment_targets/2— multi-target assignment with a bad continuationparse_table_fields/2— table field followed by unexpected token (the reported case)parse_param_list_acc/2— parameter list with a non-identifier tokenparse_local/1(introduced by A36) migrated to the helper for consistency.
suggest_for_token_error/2extended with a tailored hint for the{ "key" = value }mistake.test/lua/parser/error_test.exs— newdescribe "unexpected-token sites carry position"block with 9 tests covering the five sites,the suggestion content, a multi-line case (line 3), and a negative
test asserting the table-specific suggestion doesn't leak into
unrelated errors.
test/lua/compiler_exception_test.exs— 1 regression test at thepublic API surface pinning the user's reported reproducer.
Verification
Manual smoke at IEx:
All render with position, source echo, caret, and a suggestion. None
contain
(no position information)or:unexpected_tokenas raw text.Out of scope
Lua.Parser.Recoverystill not wired in —same boundary as A36).
metaonExpr.Property/Expr.Indexpostfix nodes.Lua.CompilerException's public struct fields.Plan:
.agents/plans/A37-parser-error-tuple-shapes.md