Skip to content

Report an unmatched closing brace as a syntax error instead of panicking - #25

Merged
TomasVotruba merged 2 commits into
rectorphp:mainfrom
danslo:fix-unmatched-brace
Nov 22, 2026
Merged

TomasVotruba merged 2 commits into
rectorphp:mainfrom
danslo:fix-unmatched-brace

Conversation

@danslo

@danslo danslo commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

The lexer pops its state stack on every }. With nothing pushed, it indexed an empty stack, so a closing brace with nothing open panicked:

<?php }
panic: runtime error: index out of range [0] with length 0

This happens in both internal/php8/lexer.go and internal/scanner/lexer.go. The lexer now keeps its current state when there is nothing to pop, and the parser reports the syntax error. Added a parser test for }, } } and { } } with the 8.4, 7.4 and 5.6 grammars.

@TomasVotruba

Copy link
Copy Markdown
Member

Thank you! I pushed a small follow-up: an unmatched closing brace now leaves the lexer state alone (as Zend does) instead of restoring a stale stack entry, with a lexer test for it.

@TomasVotruba
TomasVotruba merged commit f0dead4 into rectorphp:main Nov 22, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants