From 8d50e9d066fd857dc4d6354047bda9111f179b46 Mon Sep 17 00:00:00 2001 From: xjaja <5057757+xjaja@users.noreply.github.com> Date: Wed, 4 Oct 2023 05:00:18 +0800 Subject: [PATCH] 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. (cherry picked from commit 54103d838734be0499172026525e38cbf6af2711) --- grammar/php.y | 2 +- lib/PhpParser/Parser/Php7.php | 2 +- lib/PhpParser/Parser/Php8.php | 2 +- test/code/parser/errorHandling/recovery.test | 17 ++++++++++++++++- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/grammar/php.y b/grammar/php.y index 05ebd13e7b..580c4f8095 100644 --- a/grammar/php.y +++ b/grammar/php.y @@ -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(); } ; diff --git a/lib/PhpParser/Parser/Php7.php b/lib/PhpParser/Parser/Php7.php index e172902772..7d8cb10744 100644 --- a/lib/PhpParser/Parser/Php7.php +++ b/lib/PhpParser/Parser/Php7.php @@ -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(); diff --git a/lib/PhpParser/Parser/Php8.php b/lib/PhpParser/Parser/Php8.php index 718af9cde9..76d52a8c77 100644 --- a/lib/PhpParser/Parser/Php8.php +++ b/lib/PhpParser/Parser/Php8.php @@ -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(); diff --git a/test/code/parser/errorHandling/recovery.test b/test/code/parser/errorHandling/recovery.test index 1293ba551e..f6a016eb4d 100644 --- a/test/code/parser/errorHandling/recovery.test +++ b/test/code/parser/errorHandling/recovery.test @@ -838,11 +838,12 @@ array(