diff --git a/grammar/php5.y b/grammar/php5.y index 5f03bf6..fcd1686 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 107a092..47fe2e7 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 d1d17e1..aea69ad 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 8345a02..c9d40c4 100644 --- a/lib/PhpParser/Parser/Php7.php +++ b/lib/PhpParser/Parser/Php7.php @@ -1305,7 +1305,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 +----- +