Skip to content

Commit a712011

Browse files
committed
Merge branch '2.x'
2 parents d0cfb98 + 83f34e7 commit a712011

File tree

6 files changed

+36
-4
lines changed

6 files changed

+36
-4
lines changed

grammar/php5.y

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ inner_statement:
150150
;
151151

152152
non_empty_statement:
153-
'{' inner_statement_list '}' { $$ = $2; }
153+
'{' inner_statement_list '}' { $$ = $2; prependLeadingComments($$); }
154154
| T_IF parentheses_expr statement elseif_list else_single
155155
{ $$ = Stmt\If_[$2, ['stmts' => toArray($3), 'elseifs' => $4, 'else' => $5]]; }
156156
| T_IF parentheses_expr ':' inner_statement_list new_elseif_list new_else_single T_ENDIF ';'

grammar/php7.y

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ inner_statement:
150150
;
151151

152152
non_empty_statement:
153-
'{' inner_statement_list '}' { $$ = $2; }
153+
'{' inner_statement_list '}' { $$ = $2; prependLeadingComments($$); }
154154
| T_IF '(' expr ')' statement elseif_list else_single
155155
{ $$ = Stmt\If_[$3, ['stmts' => toArray($5), 'elseifs' => $6, 'else' => $7]]; }
156156
| T_IF '(' expr ')' ':' inner_statement_list new_elseif_list new_else_single T_ENDIF ';'

grammar/rebuildParsers.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,15 @@ function($matches) {
195195
. $args[0] . '[\'docLabel\'] = $matches[1];';
196196
}
197197

198+
if ('prependLeadingComments' == $name) {
199+
assertArgs(1, $args, $name);
200+
201+
return '$attrs = $this->startAttributeStack[#1]; $stmts = ' . $args[0] . '; '
202+
. 'if (!empty($attrs[\'comments\']) && isset($stmts[0])) {'
203+
. '$stmts[0]->setAttribute(\'comments\', '
204+
. 'array_merge($attrs[\'comments\'], $stmts[0]->getAttribute(\'comments\', []))); }';
205+
}
206+
198207
return $matches[0];
199208
},
200209
$code

lib/PhpParser/Parser/Php5.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1420,7 +1420,7 @@ protected function reduceRule126() {
14201420
}
14211421

14221422
protected function reduceRule127() {
1423-
$this->semValue = $this->semStack[$this->stackPos-(3-2)];
1423+
$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', []))); };
14241424
}
14251425

14261426
protected function reduceRule128() {

lib/PhpParser/Parser/Php7.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1305,7 +1305,7 @@ protected function reduceRule126() {
13051305
}
13061306

13071307
protected function reduceRule127() {
1308-
$this->semValue = $this->semStack[$this->stackPos-(3-2)];
1308+
$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', []))); };
13091309
}
13101310

13111311
protected function reduceRule128() {

test/code/parser/blockComments.test

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Comments on blocks
2+
-----
3+
<?php
4+
5+
// foo
6+
{
7+
// bar
8+
{
9+
// baz
10+
$a;
11+
}
12+
}
13+
-----
14+
array(
15+
0: Expr_Variable(
16+
name: a
17+
comments: array(
18+
0: // foo
19+
1: // bar
20+
2: // baz
21+
)
22+
)
23+
)

0 commit comments

Comments
 (0)