Skip to content

fix(parser): blame the opener for unclosed tables and empty bracket lists - #365

Merged
davydog187 merged 2 commits into
mainfrom
parser/error-position-corpus
Jun 15, 2026
Merged

davydog187 merged 2 commits into
mainfrom
parser/error-position-corpus

Conversation

@davydog187

Copy link
Copy Markdown
Contributor

Context

Parser error positions were sometimes reported at the wrong place,
especially in nested constructs. The motivating report was:

function() do
  foo.bar(
end

This one turns out to be reported correctly — function used as a
statement requires a name, so the genuine first syntax error is the (
on line 1 (col 9), not the foo.bar( on line 2. Rather than chase a
non-bug, this PR builds a broad golden corpus that pins line and
column for many error shapes and fixes the cases that genuinely diverge
from our position convention.

We do not use luac as the bar — its messages are poor and we want
our own opinionated positions/messages. The corpus encodes that convention.

The convention

  • Concrete offending token — blamed exactly at that token, never at a
    shallower recovery boundary. A deep error inside the Nth call argument
    wins over the outer call terminator.
  • Runs off the end of input ((, [, {, string, long string/comment)
    — blamed at the opening delimiter, with an "add a closing X" suggestion.
  • Block body missing end (function/if/while/for/do) —
    reported where end was expected.

Fixes (lib/lua/parser.ex)

Two delimiter constructs didn't follow the opener convention that calls,
indexes, and parenthesised lists already used:

  1. Unclosed table { at EOF emitted a bare "Expected ',' or '}', got :eof" pinned to the end of the file instead of pointing at the opening
    brace. → now Unclosed opening brace '{' at the opener.
  2. A bracket opened immediately before <eof> (e.g. g( at end of
    input) reported "Expected expression" at the file end. → now blames
    the opener, matching the args-then-EOF path. The innermost opener wins
    in nested constructs (table -> function -> call).

The nested concrete-token propagation cases were already correct; the
corpus now locks them in.

Tests (test/lua/parser/error_position_test.exs)

Expanded from 8 to 39 golden cases, grouped by failure shape (openers,
lexer-origin, missing-end, concrete offending tokens, deep nested
propagation, statement-level). Each case asserts the exact {line, column}
via parse_structured/1 and the rendered at line L, column C banner
plus a message substring via parse/1. Dropped the luac differential block.

Verification

  • New corpus: 39 passed
  • Full suite: 2474 passed, 7 skipped, 30 excluded — no regressions
  • mix format --check-formatted: clean

…ists

Two delimiter constructs did not follow the opener convention that calls,
indexes and parenthesised lists already use:

  * An unclosed table `{` that ran off the end of input emitted a bare
    "Expected ',' or '}', got :eof" pinned to the end of the file instead
    of pointing at the opening brace.
  * A bracket delimiter opened immediately before <eof> (e.g. `g(` at the
    end of input) reported "Expected expression" at the file end rather
    than blaming the opener.

Both now report an unclosed-delimiter error at the opening token, so the
innermost opener wins in nested constructs (table -> function -> call).

Expand test/lua/parser/error_position_test.exs into a golden corpus that
pins both line and column for every error shape — openers, lexer-origin,
missing-end, concrete offending tokens, deep nested propagation, and
statement-level. Each case asserts the structured position and the
rendered banner/message together. Drop the luac differential block: our
convention, not a reference compiler, is the bar.
The lexer always terminates the token stream with {:eof, pos}, so
peek/1 returns {:eof, _} (never nil) inside an unclosed table or
bracket list. Drop the dead nil branches; the {:eof, _} clause is
the only reachable one.
@davydog187
davydog187 merged commit 26f8fb5 into main Jun 15, 2026
5 checks passed
@davydog187
davydog187 deleted the parser/error-position-corpus branch June 15, 2026 12:44
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.

1 participant