Retain comments on blocks on first inner statement

This commit is contained in:
Nikita Popov 2016-08-30 22:37:51 +02:00
parent 13f7321def
commit 83f34e7fa4
6 changed files with 36 additions and 4 deletions

View File

@ -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 ';'

View File

@ -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 ';'

View File

@ -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

View File

@ -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() {

View File

@ -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() {

View File

@ -0,0 +1,23 @@
Comments on blocks
-----
<?php
// foo
{
// bar
{
// baz
$a;
}
}
-----
array(
0: Expr_Variable(
name: a
comments: array(
0: // foo
1: // bar
2: // baz
)
)
)