From 83f34e7fa4975e9646e118b61226e17cebc5db14 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Tue, 30 Aug 2016 22:37:51 +0200 Subject: [PATCH] Retain comments on blocks on first inner statement --- grammar/php5.y | 2 +- grammar/php7.y | 2 +- grammar/rebuildParsers.php | 9 +++++++++ lib/PhpParser/Parser/Php5.php | 2 +- lib/PhpParser/Parser/Php7.php | 2 +- test/code/parser/blockComments.test | 23 +++++++++++++++++++++++ 6 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 test/code/parser/blockComments.test diff --git a/grammar/php5.y b/grammar/php5.y index 250ad9b..d40d093 100644 --- a/grammar/php5.y +++ b/grammar/php5.y @@ -150,7 +150,7 @@ inner_statement: ; non_empty_statement: - '{' inner_statement_list '}' { $$ = $2; } + '{' inner_statement_list '}' { $$ = $2; prependLeadingComments($$); } | T_IF parentheses_expr statement elseif_list else_single { $$ = Stmt\If_[$2, ['stmts' => toArray($3), 'elseifs' => $4, 'else' => $5]]; } | T_IF parentheses_expr ':' inner_statement_list new_elseif_list new_else_single T_ENDIF ';' diff --git a/grammar/php7.y b/grammar/php7.y index edd3b53..cf5f841 100644 --- a/grammar/php7.y +++ b/grammar/php7.y @@ -150,7 +150,7 @@ inner_statement: ; non_empty_statement: - '{' inner_statement_list '}' { $$ = $2; } + '{' inner_statement_list '}' { $$ = $2; prependLeadingComments($$); } | T_IF '(' expr ')' statement elseif_list else_single { $$ = Stmt\If_[$3, ['stmts' => toArray($5), 'elseifs' => $6, 'else' => $7]]; } | T_IF '(' expr ')' ':' inner_statement_list new_elseif_list new_else_single T_ENDIF ';' diff --git a/grammar/rebuildParsers.php b/grammar/rebuildParsers.php index d7ecbe4..5b538ce 100644 --- a/grammar/rebuildParsers.php +++ b/grammar/rebuildParsers.php @@ -195,6 +195,15 @@ function resolveMacros($code) { . $args[0] . '[\'docLabel\'] = $matches[1];'; } + if ('prependLeadingComments' == $name) { + assertArgs(1, $args, $name); + + return '$attrs = $this->startAttributeStack[#1]; $stmts = ' . $args[0] . '; ' + . 'if (!empty($attrs[\'comments\']) && isset($stmts[0])) {' + . '$stmts[0]->setAttribute(\'comments\', ' + . 'array_merge($attrs[\'comments\'], $stmts[0]->getAttribute(\'comments\', []))); }'; + } + return $matches[0]; }, $code diff --git a/lib/PhpParser/Parser/Php5.php b/lib/PhpParser/Parser/Php5.php index 47e706f..fe40052 100644 --- a/lib/PhpParser/Parser/Php5.php +++ b/lib/PhpParser/Parser/Php5.php @@ -1420,7 +1420,7 @@ class Php5 extends \PhpParser\ParserAbstract } protected function reduceRule127() { - $this->semValue = $this->semStack[$this->stackPos-(3-2)]; + $this->semValue = $this->semStack[$this->stackPos-(3-2)]; $attrs = $this->startAttributeStack[$this->stackPos-(3-1)]; $stmts = $this->semValue; if (!empty($attrs['comments']) && isset($stmts[0])) {$stmts[0]->setAttribute('comments', array_merge($attrs['comments'], $stmts[0]->getAttribute('comments', []))); }; } protected function reduceRule128() { diff --git a/lib/PhpParser/Parser/Php7.php b/lib/PhpParser/Parser/Php7.php index 5d1be42..87d0bac 100644 --- a/lib/PhpParser/Parser/Php7.php +++ b/lib/PhpParser/Parser/Php7.php @@ -1300,7 +1300,7 @@ class Php7 extends \PhpParser\ParserAbstract } protected function reduceRule127() { - $this->semValue = $this->semStack[$this->stackPos-(3-2)]; + $this->semValue = $this->semStack[$this->stackPos-(3-2)]; $attrs = $this->startAttributeStack[$this->stackPos-(3-1)]; $stmts = $this->semValue; if (!empty($attrs['comments']) && isset($stmts[0])) {$stmts[0]->setAttribute('comments', array_merge($attrs['comments'], $stmts[0]->getAttribute('comments', []))); }; } protected function reduceRule128() { diff --git a/test/code/parser/blockComments.test b/test/code/parser/blockComments.test new file mode 100644 index 0000000..0d12326 --- /dev/null +++ b/test/code/parser/blockComments.test @@ -0,0 +1,23 @@ +Comments on blocks +----- +