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.
  • Loading branch information
xjaja committed Oct 3, 2023
1 parent a6303e5 commit 54103d8
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion grammar/php5.y
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,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 grammar/php7.y
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,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/Php5.php
Original file line number Diff line number Diff line change
Expand Up @@ -1738,7 +1738,7 @@ protected function initReduceCallbacks() {
$this->semValue = new Stmt\StaticVar($this->semStack[$stackPos-(3-1)], $this->semStack[$stackPos-(3-3)], $this->startAttributeStack[$stackPos-(3-1)] + $this->endAttributes);
},
259 => 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)]; }
},
260 => function ($stackPos) {
$this->semValue = array();
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 @@ -2056,7 +2056,7 @@ protected function initReduceCallbacks() {
$this->semValue = new Stmt\StaticVar($this->semStack[$stackPos-(3-1)], $this->semStack[$stackPos-(3-3)], $this->startAttributeStack[$stackPos-(3-1)] + $this->endAttributes);
},
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
19 changes: 17 additions & 2 deletions test/code/parser/errorHandling/recovery.test
Original file line number Diff line number Diff line change
Expand Up @@ -900,12 +900,13 @@ array(
<?php

class Foo {
public $bar1;
publi $foo;
public $bar;
}
-----
!!php7
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 @@ -919,6 +920,20 @@ array(
)
stmts: array(
0: Stmt_Property(
attrGroups: array(
)
flags: MODIFIER_PUBLIC (1)
type: null
props: array(
0: Stmt_PropertyProperty(
name: VarLikeIdentifier(
name: bar1
)
default: null
)
)
)
1: Stmt_Property(
attrGroups: array(
)
flags: MODIFIER_PUBLIC (1)
Expand Down Expand Up @@ -1523,4 +1538,4 @@ array(
)
)
)
)
)

0 comments on commit 54103d8

Please sign in to comment.