Skip to content

Commit 54103d8

Browse files
authored
Don't drop class statements before error (#952)
When encountering a null statement (indicating that an error occurred), retain the preceding statements. These were accidentally dropped previously.
1 parent a6303e5 commit 54103d8

File tree

5 files changed

+21
-6
lines changed

5 files changed

+21
-6
lines changed

grammar/php5.y

+1-1
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ static_var:
469469
;
470470

471471
class_statement_list_ex:
472-
class_statement_list_ex class_statement { if ($2 !== null) { push($1, $2); } }
472+
class_statement_list_ex class_statement { if ($2 !== null) { push($1, $2); } else { $$ = $1; } }
473473
| /* empty */ { init(); }
474474
;
475475

grammar/php7.y

+1-1
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ static_var:
708708
;
709709

710710
class_statement_list_ex:
711-
class_statement_list_ex class_statement { if ($2 !== null) { push($1, $2); } }
711+
class_statement_list_ex class_statement { if ($2 !== null) { push($1, $2); } else { $$ = $1; } }
712712
| /* empty */ { init(); }
713713
;
714714

lib/PhpParser/Parser/Php5.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1738,7 +1738,7 @@ protected function initReduceCallbacks() {
17381738
$this->semValue = new Stmt\StaticVar($this->semStack[$stackPos-(3-1)], $this->semStack[$stackPos-(3-3)], $this->startAttributeStack[$stackPos-(3-1)] + $this->endAttributes);
17391739
},
17401740
259 => function ($stackPos) {
1741-
if ($this->semStack[$stackPos-(2-2)] !== null) { $this->semStack[$stackPos-(2-1)][] = $this->semStack[$stackPos-(2-2)]; $this->semValue = $this->semStack[$stackPos-(2-1)]; }
1741+
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)]; }
17421742
},
17431743
260 => function ($stackPos) {
17441744
$this->semValue = array();

lib/PhpParser/Parser/Php7.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2056,7 +2056,7 @@ protected function initReduceCallbacks() {
20562056
$this->semValue = new Stmt\StaticVar($this->semStack[$stackPos-(3-1)], $this->semStack[$stackPos-(3-3)], $this->startAttributeStack[$stackPos-(3-1)] + $this->endAttributes);
20572057
},
20582058
340 => function ($stackPos) {
2059-
if ($this->semStack[$stackPos-(2-2)] !== null) { $this->semStack[$stackPos-(2-1)][] = $this->semStack[$stackPos-(2-2)]; $this->semValue = $this->semStack[$stackPos-(2-1)]; }
2059+
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)]; }
20602060
},
20612061
341 => function ($stackPos) {
20622062
$this->semValue = array();

test/code/parser/errorHandling/recovery.test

+17-2
Original file line numberDiff line numberDiff line change
@@ -900,12 +900,13 @@ array(
900900
<?php
901901

902902
class Foo {
903+
public $bar1;
903904
publi $foo;
904905
public $bar;
905906
}
906907
-----
907908
!!php7
908-
Syntax error, unexpected T_STRING from 4:5 to 4:9
909+
Syntax error, unexpected T_STRING from 5:5 to 5:9
909910
array(
910911
0: Stmt_Class(
911912
attrGroups: array(
@@ -919,6 +920,20 @@ array(
919920
)
920921
stmts: array(
921922
0: Stmt_Property(
923+
attrGroups: array(
924+
)
925+
flags: MODIFIER_PUBLIC (1)
926+
type: null
927+
props: array(
928+
0: Stmt_PropertyProperty(
929+
name: VarLikeIdentifier(
930+
name: bar1
931+
)
932+
default: null
933+
)
934+
)
935+
)
936+
1: Stmt_Property(
922937
attrGroups: array(
923938
)
924939
flags: MODIFIER_PUBLIC (1)
@@ -1523,4 +1538,4 @@ array(
15231538
)
15241539
)
15251540
)
1526-
)
1541+
)

0 commit comments

Comments
 (0)