Skip to content

Commit

Permalink
Don't drop class statements before error (#952)
Browse files Browse the repository at this point in the history
When encountering a null statement (indicating that an error occurred),
retain the preceding statements. These were accidentally dropped
previously.

(cherry picked from commit 54103d8)
  • Loading branch information
xjaja authored and nikic committed Oct 7, 2023
1 parent 481fec4 commit 8d50e9d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion grammar/php.y
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ static_var:
;

class_statement_list_ex:
class_statement_list_ex class_statement { if ($2 !== null) { push($1, $2); } }
class_statement_list_ex class_statement { if ($2 !== null) { push($1, $2); } else { $$ = $1; } }
| /* empty */ { init(); }
;

Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Parser/Php7.php
Original file line number Diff line number Diff line change
Expand Up @@ -1935,7 +1935,7 @@ protected function initReduceCallbacks(): void {
$this->semValue = new Node\StaticVar($this->semStack[$stackPos-(3-1)], $this->semStack[$stackPos-(3-3)], $this->getAttributes($this->tokenStartStack[$stackPos-(3-1)], $this->tokenEndStack[$stackPos]));
},
340 => function ($stackPos) {
if ($this->semStack[$stackPos-(2-2)] !== null) { $this->semStack[$stackPos-(2-1)][] = $this->semStack[$stackPos-(2-2)]; $this->semValue = $this->semStack[$stackPos-(2-1)]; }
if ($this->semStack[$stackPos-(2-2)] !== null) { $this->semStack[$stackPos-(2-1)][] = $this->semStack[$stackPos-(2-2)]; $this->semValue = $this->semStack[$stackPos-(2-1)]; } else { $this->semValue = $this->semStack[$stackPos-(2-1)]; }
},
341 => function ($stackPos) {
$this->semValue = array();
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Parser/Php8.php
Original file line number Diff line number Diff line change
Expand Up @@ -1953,7 +1953,7 @@ protected function initReduceCallbacks(): void {
$this->semValue = new Node\StaticVar($this->semStack[$stackPos-(3-1)], $this->semStack[$stackPos-(3-3)], $this->getAttributes($this->tokenStartStack[$stackPos-(3-1)], $this->tokenEndStack[$stackPos]));
},
340 => function ($stackPos) {
if ($this->semStack[$stackPos-(2-2)] !== null) { $this->semStack[$stackPos-(2-1)][] = $this->semStack[$stackPos-(2-2)]; $this->semValue = $this->semStack[$stackPos-(2-1)]; }
if ($this->semStack[$stackPos-(2-2)] !== null) { $this->semStack[$stackPos-(2-1)][] = $this->semStack[$stackPos-(2-2)]; $this->semValue = $this->semStack[$stackPos-(2-1)]; } else { $this->semValue = $this->semStack[$stackPos-(2-1)]; }
},
341 => function ($stackPos) {
$this->semValue = array();
Expand Down
17 changes: 16 additions & 1 deletion test/code/parser/errorHandling/recovery.test
Original file line number Diff line number Diff line change
Expand Up @@ -838,11 +838,12 @@ array(
<?php

class Foo {
public $bar1;
publi $foo;
public $bar;
}
-----
Syntax error, unexpected T_STRING from 4:5 to 4:9
Syntax error, unexpected T_STRING from 5:5 to 5:9
array(
0: Stmt_Class(
attrGroups: array(
Expand All @@ -856,6 +857,20 @@ array(
)
stmts: array(
0: Stmt_Property(
attrGroups: array(
)
flags: PUBLIC (1)
type: null
props: array(
0: PropertyItem(
name: VarLikeIdentifier(
name: bar1
)
default: null
)
)
)
1: Stmt_Property(
attrGroups: array(
)
flags: PUBLIC (1)
Expand Down

0 comments on commit 8d50e9d

Please sign in to comment.