Skip to content

fix: raise ParseError instead of AssertionError on unmatched '}' - #604

Merged
eliben merged 1 commit into
eliben:mainfrom
patchwright:fix/unmatched-rbrace-parse-error
Jul 6, 2026
Merged

fix: raise ParseError instead of AssertionError on unmatched '}'#604
eliben merged 1 commit into
eliben:mainfrom
patchwright:fix/unmatched-rbrace-parse-error

Conversation

@patchwright

Copy link
Copy Markdown
Contributor

Problem

parser.parse("}") — an unmatched } — raises AssertionError from parser internals instead of the documented ParseError:

CLexer tokenizes RBRACE
  -> on_rbrace_func()
  -> CParser._lex_on_rbrace_func()
  -> CParser._pop_scope()
  -> assert len(self._scope_stack) > 1   # AssertionError

Callers that catch ParseError to handle parse failures cannot catch the AssertionError.

Cause

_pop_scope (c_parser.py:127) uses a bare assert to guard against an empty scope stack. Assertions are for invariant checks, not for user-input validation — and they vanish under python -O.

Fix

Replace the bare assert with an explicit ParseError:

def _pop_scope(self) -> None:
    if len(self._scope_stack) <= 1:
        raise ParseError("Unmatched '}'")
    self._scope_stack.pop()

Test

Added TestUnmatchedRbrace::test_unmatched_rbrace_raises_parse_error — fails on unpatched (AssertionError), passes with the fix. Full suite: 135 passed, 0 regressions (the 1 pre-existing test_examples failure is unrelated — AttributeError: Coord on master HEAD).

Fixes #603.

Assisted-by: Claude (Anthropic).

An unmatched '}' (e.g. parser.parse('}')) reached an internal assertion
(c_parser.py:127: assert len(self._scope_stack) > 1) rather than returning
through the usual ParseError path. Callers that catch ParseError to handle
parse failures would not catch the AssertionError.

Replace the bare assert with an explicit ParseError so malformed input
travels the documented error path. Fixes eliben#603.

Co-Authored-By: claude-flow <ruv@ruv.net>
@eliben
eliben merged commit 313d292 into eliben:main Jul 6, 2026
15 checks passed
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.

Unmatched } raises AssertionError instead of ParseError

2 participants