Skip to content

Commit bbf9de6

Browse files
committed
Leave the lexer state alone on an unmatched closing brace, as Zend does
1 parent d61a9b2 commit bbf9de6

4 files changed

Lines changed: 50 additions & 6 deletions

File tree

‎internal/php8/lexer.go‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,13 @@ func (lex *Lexer) call(state int, fnext int) {
205205
}
206206

207207
func (lex *Lexer) ret(count int) {
208-
lex.top = max(lex.top-count, 0)
209-
if lex.top < len(lex.stack) {
210-
lex.cs = lex.stack[lex.top]
208+
// an unmatched closing brace leaves the state alone, as Zend does
209+
if lex.top == 0 {
210+
lex.p++
211+
return
211212
}
213+
lex.top = max(lex.top-count, 0)
214+
lex.cs = lex.stack[lex.top]
212215
lex.p++
213216
}
214217

‎internal/php8/scanner_test.go‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2046,3 +2046,22 @@ func TestTripleDollar(test *testing.T) {
20462046
actual = string(tkn.Value)
20472047
assert.DeepEqual(test, expected, actual)
20482048
}
2049+
2050+
func TestUnmatchedClosingBraceKeepsState(test *testing.T) {
2051+
src := `<?php "{$a}"; } echo`
2052+
2053+
config := conf.Config{
2054+
Version: &version.Version{
2055+
Major: 7,
2056+
Minor: 4,
2057+
},
2058+
}
2059+
lexer := NewLexer([]byte(src), config)
2060+
2061+
var last *token.Token
2062+
for tkn := lexer.Lex(); tkn.ID != 0; tkn = lexer.Lex() {
2063+
last = tkn
2064+
}
2065+
2066+
assert.DeepEqual(test, token.T_ECHO, last.ID)
2067+
}

‎internal/scanner/lexer.go‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,13 @@ func (lex *Lexer) call(state int, fnext int) {
200200
}
201201

202202
func (lex *Lexer) ret(levels int) {
203-
lex.top = max(lex.top-levels, 0)
204-
if lex.top < len(lex.stack) {
205-
lex.cs = lex.stack[lex.top]
203+
// an unmatched closing brace leaves the state alone, as Zend does
204+
if lex.top == 0 {
205+
lex.p++
206+
return
206207
}
208+
lex.top = max(lex.top-levels, 0)
209+
lex.cs = lex.stack[lex.top]
207210
lex.p++
208211
}
209212

‎internal/scanner/scanner_test.go‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1898,3 +1898,22 @@ func TestTripleDollar(test *testing.T) {
18981898
actual = string(tkn.Value)
18991899
assert.DeepEqual(test, expected, actual)
19001900
}
1901+
1902+
func TestUnmatchedClosingBraceKeepsState(test *testing.T) {
1903+
src := `<?php "{$a}"; } echo`
1904+
1905+
config := conf.Config{
1906+
Version: &version.Version{
1907+
Major: 7,
1908+
Minor: 4,
1909+
},
1910+
}
1911+
lexer := NewLexer([]byte(src), config)
1912+
1913+
var last *token.Token
1914+
for tkn := lexer.Lex(); tkn.ID != 0; tkn = lexer.Lex() {
1915+
last = tkn
1916+
}
1917+
1918+
assert.DeepEqual(test, token.T_ECHO, last.ID)
1919+
}

0 commit comments

Comments
 (0)