diff --git a/internal/php8/lexer.go b/internal/php8/lexer.go index 8bb9d89..386402c 100644 --- a/internal/php8/lexer.go +++ b/internal/php8/lexer.go @@ -206,7 +206,9 @@ func (lex *Lexer) call(state int, fnext int) { func (lex *Lexer) ret(count int) { lex.top = max(lex.top-count, 0) - lex.cs = lex.stack[lex.top] + if lex.top < len(lex.stack) { + lex.cs = lex.stack[lex.top] + } lex.p++ } diff --git a/internal/scanner/lexer.go b/internal/scanner/lexer.go index aa54b5e..ca29a55 100644 --- a/internal/scanner/lexer.go +++ b/internal/scanner/lexer.go @@ -201,7 +201,9 @@ func (lex *Lexer) call(state int, fnext int) { func (lex *Lexer) ret(levels int) { lex.top = max(lex.top-levels, 0) - lex.cs = lex.stack[lex.top] + if lex.top < len(lex.stack) { + lex.cs = lex.stack[lex.top] + } lex.p++ } diff --git a/pkg/parser/unmatched_brace_test.go b/pkg/parser/unmatched_brace_test.go new file mode 100644 index 0000000..77dfa40 --- /dev/null +++ b/pkg/parser/unmatched_brace_test.go @@ -0,0 +1,31 @@ +package parser_test + +import ( + "testing" + + "github.com/rectorphp/php-parser-in-go/pkg/conf" + "github.com/rectorphp/php-parser-in-go/pkg/errors" + "github.com/rectorphp/php-parser-in-go/pkg/parser" + "github.com/rectorphp/php-parser-in-go/pkg/version" +) + +// A closing brace with nothing open is a syntax error, not a lexer panic. +func TestParse_UnmatchedClosingBrace(test *testing.T) { + for _, grammar := range []string{"8.4", "7.4", "5.6"} { + for _, source := range []string{"