1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-11-26 20:04:48 +01:00

Fix nop statement comment assignment

Keep around the start attributes on the lookahead token around in
a separate parser property.
This commit is contained in:
Nikita Popov 2016-04-05 18:53:50 +09:00
parent 5fa8493675
commit 14de71898e
8 changed files with 83 additions and 34 deletions

View File

@ -16,7 +16,8 @@ top_statement_list_ex:
top_statement_list:
top_statement_list_ex
{ makeNop($nop, #1+1); if ($nop !== null) { $1[] = $nop; } $$ = $1; }
{ makeNop($nop, $this->lookaheadStartAttributes);
if ($nop !== null) { $1[] = $nop; } $$ = $1; }
;
reserved_non_modifiers:
@ -136,7 +137,8 @@ inner_statement_list_ex:
inner_statement_list:
inner_statement_list_ex
{ makeNop($nop, #1+1); if ($nop !== null) { $1[] = $nop; } $$ = $1; }
{ makeNop($nop, $this->lookaheadStartAttributes);
if ($nop !== null) { $1[] = $nop; } $$ = $1; }
;
inner_statement:
@ -187,7 +189,8 @@ non_empty_statement:
statement:
non_empty_statement { $$ = $1; }
| ';'
{ makeNop($$, #1); if ($$ === null) $$ = array(); /* means: no statement */ }
{ makeNop($$, $this->startAttributeStack[#1]);
if ($$ === null) $$ = array(); /* means: no statement */ }
;
catches:

View File

@ -16,7 +16,8 @@ top_statement_list_ex:
top_statement_list:
top_statement_list_ex
{ makeNop($nop, #1+1); if ($nop !== null) { $1[] = $nop; } $$ = $1; }
{ makeNop($nop, $this->lookaheadStartAttributes);
if ($nop !== null) { $1[] = $nop; } $$ = $1; }
;
reserved_non_modifiers:
@ -136,7 +137,8 @@ inner_statement_list_ex:
inner_statement_list:
inner_statement_list_ex
{ makeNop($nop, #1+1); if ($nop !== null) { $1[] = $nop; } $$ = $1; }
{ makeNop($nop, $this->lookaheadStartAttributes);
if ($nop !== null) { $1[] = $nop; } $$ = $1; }
;
inner_statement:
@ -183,7 +185,8 @@ non_empty_statement:
statement:
non_empty_statement { $$ = $1; }
| ';'
{ makeNop($$, #1); if ($$ === null) $$ = array(); /* means: no statement */ }
{ makeNop($$, $this->startAttributeStack[#1]);
if ($$ === null) $$ = array(); /* means: no statement */ }
;
catches:

View File

@ -85,7 +85,7 @@ foreach ($grammarFileToName as $grammarFile => $name) {
function resolveNodes($code) {
return preg_replace_callback(
'~(?<name>[A-Z][a-zA-Z_\\\\]++)\s*' . PARAMS . '~',
'~\b(?<name>[A-Z][a-zA-Z_\\\\]++)\s*' . PARAMS . '~',
function($matches) {
// recurse
$matches['params'] = resolveNodes($matches['params']);
@ -172,7 +172,7 @@ function resolveMacros($code) {
if ('makeNop' == $name) {
assertArgs(2, $args, $name);
return '$startAttributes = $this->startAttributeStack[' . $args[1] . '];'
return '$startAttributes = ' . $args[1] . ';'
. ' if (isset($startAttributes[\'comments\']))'
. ' { ' . $args[0] . ' = new Stmt\Nop([\'comments\' => $startAttributes[\'comments\']]); }'
. ' else { ' . $args[0] . ' = null; }';

View File

@ -924,7 +924,8 @@ class Php5 extends \PhpParser\ParserAbstract
}
protected function reduceRule4() {
$startAttributes = $this->startAttributeStack[$this->stackPos-(1-1)+1]; if (isset($startAttributes['comments'])) { $nop = new Stmt\Nop(['comments' => $startAttributes['comments']]); } else { $nop = null; }; if ($nop !== null) { $this->semStack[$this->stackPos-(1-1)][] = $nop; } $this->semValue = $this->semStack[$this->stackPos-(1-1)];
$startAttributes = $this->lookaheadStartAttributes; if (isset($startAttributes['comments'])) { $nop = new Stmt\Nop(['comments' => $startAttributes['comments']]); } else { $nop = null; };
if ($nop !== null) { $this->semStack[$this->stackPos-(1-1)][] = $nop; } $this->semValue = $this->semStack[$this->stackPos-(1-1)];
}
protected function reduceRule5() {
@ -1396,7 +1397,8 @@ class Php5 extends \PhpParser\ParserAbstract
}
protected function reduceRule122() {
$startAttributes = $this->startAttributeStack[$this->stackPos-(1-1)+1]; if (isset($startAttributes['comments'])) { $nop = new Stmt\Nop(['comments' => $startAttributes['comments']]); } else { $nop = null; }; if ($nop !== null) { $this->semStack[$this->stackPos-(1-1)][] = $nop; } $this->semValue = $this->semStack[$this->stackPos-(1-1)];
$startAttributes = $this->lookaheadStartAttributes; if (isset($startAttributes['comments'])) { $nop = new Stmt\Nop(['comments' => $startAttributes['comments']]); } else { $nop = null; };
if ($nop !== null) { $this->semStack[$this->stackPos-(1-1)][] = $nop; } $this->semValue = $this->semStack[$this->stackPos-(1-1)];
}
protected function reduceRule123() {
@ -1532,7 +1534,8 @@ class Php5 extends \PhpParser\ParserAbstract
}
protected function reduceRule156() {
$startAttributes = $this->startAttributeStack[$this->stackPos-(1-1)]; if (isset($startAttributes['comments'])) { $this->semValue = new Stmt\Nop(['comments' => $startAttributes['comments']]); } else { $this->semValue = null; }; if ($this->semValue === null) $this->semValue = array(); /* means: no statement */
$startAttributes = $this->startAttributeStack[$this->stackPos-(1-1)]; if (isset($startAttributes['comments'])) { $this->semValue = new Stmt\Nop(['comments' => $startAttributes['comments']]); } else { $this->semValue = null; };
if ($this->semValue === null) $this->semValue = array(); /* means: no statement */
}
protected function reduceRule157() {

View File

@ -806,7 +806,8 @@ class Php7 extends \PhpParser\ParserAbstract
}
protected function reduceRule4() {
$startAttributes = $this->startAttributeStack[$this->stackPos-(1-1)+1]; if (isset($startAttributes['comments'])) { $nop = new Stmt\Nop(['comments' => $startAttributes['comments']]); } else { $nop = null; }; if ($nop !== null) { $this->semStack[$this->stackPos-(1-1)][] = $nop; } $this->semValue = $this->semStack[$this->stackPos-(1-1)];
$startAttributes = $this->lookaheadStartAttributes; if (isset($startAttributes['comments'])) { $nop = new Stmt\Nop(['comments' => $startAttributes['comments']]); } else { $nop = null; };
if ($nop !== null) { $this->semStack[$this->stackPos-(1-1)][] = $nop; } $this->semValue = $this->semStack[$this->stackPos-(1-1)];
}
protected function reduceRule5() {
@ -1278,7 +1279,8 @@ class Php7 extends \PhpParser\ParserAbstract
}
protected function reduceRule122() {
$startAttributes = $this->startAttributeStack[$this->stackPos-(1-1)+1]; if (isset($startAttributes['comments'])) { $nop = new Stmt\Nop(['comments' => $startAttributes['comments']]); } else { $nop = null; }; if ($nop !== null) { $this->semStack[$this->stackPos-(1-1)][] = $nop; } $this->semValue = $this->semStack[$this->stackPos-(1-1)];
$startAttributes = $this->lookaheadStartAttributes; if (isset($startAttributes['comments'])) { $nop = new Stmt\Nop(['comments' => $startAttributes['comments']]); } else { $nop = null; };
if ($nop !== null) { $this->semStack[$this->stackPos-(1-1)][] = $nop; } $this->semValue = $this->semStack[$this->stackPos-(1-1)];
}
protected function reduceRule123() {
@ -1398,7 +1400,8 @@ class Php7 extends \PhpParser\ParserAbstract
}
protected function reduceRule152() {
$startAttributes = $this->startAttributeStack[$this->stackPos-(1-1)]; if (isset($startAttributes['comments'])) { $this->semValue = new Stmt\Nop(['comments' => $startAttributes['comments']]); } else { $this->semValue = null; }; if ($this->semValue === null) $this->semValue = array(); /* means: no statement */
$startAttributes = $this->startAttributeStack[$this->stackPos-(1-1)]; if (isset($startAttributes['comments'])) { $this->semValue = new Stmt\Nop(['comments' => $startAttributes['comments']]); } else { $this->semValue = null; };
if ($this->semValue === null) $this->semValue = array(); /* means: no statement */
}
protected function reduceRule153() {

View File

@ -88,6 +88,8 @@ abstract class ParserAbstract implements Parser
protected $startAttributeStack;
/** @var array End attributes of last *shifted* token */
protected $endAttributes;
/** @var array Start attributes of last *read* token */
protected $lookaheadStartAttributes;
/** @var bool Whether to throw on first error */
protected $throwOnError;
@ -185,6 +187,7 @@ abstract class ParserAbstract implements Parser
// This is necessary to assign some meaningful attributes to /* empty */ productions. They'll get
// the attributes of the next token, even though they don't contain it themselves.
$this->startAttributeStack[$this->stackPos+1] = $startAttributes;
$this->lookaheadStartAttributes = $startAttributes;
//$this->traceRead($symbol);
}

View File

@ -2,32 +2,32 @@ Comments
-----
<?php
/** doc */
/* foobar */
// foo
// bar
/** doc 1 */
/* foobar 1 */
// foo 1
// bar 1
$var;
if ($cond) {
/** doc */
/* foobar */
// foo
// bar
/** doc 2 */
/* foobar 2 */
// foo 2
// bar 2
}
/** doc */
/* foobar */
// foo
// bar
/** doc 3 */
/* foobar 3 */
// foo 3
// bar 3
-----
array(
0: Expr_Variable(
name: var
comments: array(
0: /** doc */
1: /* foobar */
2: // foo
3: // bar
0: /** doc 1 */
1: /* foobar 1 */
2: // foo 1
3: // bar 1
)
)
1: Stmt_If(
@ -37,10 +37,10 @@ array(
stmts: array(
0: Stmt_Nop(
comments: array(
0: /** doc */
1: /* foobar */
2: // foo
3: // bar
0: /** doc 2 */
1: /* foobar 2 */
2: // foo 2
3: // bar 2
)
)
)
@ -48,6 +48,14 @@ array(
)
else: null
)
2: Stmt_Nop(
comments: array(
0: /** doc 3 */
1: /* foobar 3 */
2: // foo 3
3: // bar 3
)
)
)
-----
<?php
@ -68,4 +76,25 @@ array(
3: // bar
)
)
)
-----
<?php
// comment
if (42) {}
-----
array(
0: Stmt_If(
cond: Scalar_LNumber(
value: 42
)
stmts: array(
)
elseifs: array(
)
else: null
comments: array(
0: // comment
)
)
)

View File

@ -10,4 +10,9 @@ array(
-----
Syntax error, unexpected EOF from 1:20 to 1:20
array(
0: Stmt_Nop(
comments: array(
0: /* bar */
)
)
)