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

Place Nop nodes more precisely

If the Nop is created to collect comments, place it directly after
the comment, instead of at the next non-whitespace character.
This commit is contained in:
Nikita Popov 2020-02-09 17:10:33 +01:00
parent 602af9060d
commit 46cbd9393a
6 changed files with 59 additions and 18 deletions

View File

@ -178,7 +178,7 @@ function resolveMacros($code) {
return '$startAttributes = ' . $args[1] . ';'
. ' if (isset($startAttributes[\'comments\']))'
. ' { ' . $args[0] . ' = new Stmt\Nop($this->createZeroLengthAttributes($startAttributes)); }'
. ' { ' . $args[0] . ' = new Stmt\Nop($this->createCommentNopAttributes($startAttributes[\'comments\'])); }'
. ' else { ' . $args[0] . ' = null; }';
}

View File

@ -944,7 +944,7 @@ class Php5 extends \PhpParser\ParserAbstract
$this->semValue = array();
},
4 => function ($stackPos) {
$startAttributes = $this->lookaheadStartAttributes; if (isset($startAttributes['comments'])) { $nop = new Stmt\Nop($this->createZeroLengthAttributes($startAttributes)); } else { $nop = null; };
$startAttributes = $this->lookaheadStartAttributes; if (isset($startAttributes['comments'])) { $nop = new Stmt\Nop($this->createCommentNopAttributes($startAttributes['comments'])); } else { $nop = null; };
if ($nop !== null) { $this->semStack[$stackPos-(1-1)][] = $nop; } $this->semValue = $this->semStack[$stackPos-(1-1)];
},
5 => function ($stackPos) {
@ -1317,7 +1317,7 @@ class Php5 extends \PhpParser\ParserAbstract
$this->semValue = array();
},
126 => function ($stackPos) {
$startAttributes = $this->lookaheadStartAttributes; if (isset($startAttributes['comments'])) { $nop = new Stmt\Nop($this->createZeroLengthAttributes($startAttributes)); } else { $nop = null; };
$startAttributes = $this->lookaheadStartAttributes; if (isset($startAttributes['comments'])) { $nop = new Stmt\Nop($this->createCommentNopAttributes($startAttributes['comments'])); } else { $nop = null; };
if ($nop !== null) { $this->semStack[$stackPos-(1-1)][] = $nop; } $this->semValue = $this->semStack[$stackPos-(1-1)];
},
127 => function ($stackPos) {
@ -1715,7 +1715,7 @@ class Php5 extends \PhpParser\ParserAbstract
$this->semValue = array();
},
255 => function ($stackPos) {
$startAttributes = $this->lookaheadStartAttributes; if (isset($startAttributes['comments'])) { $nop = new Stmt\Nop($this->createZeroLengthAttributes($startAttributes)); } else { $nop = null; };
$startAttributes = $this->lookaheadStartAttributes; if (isset($startAttributes['comments'])) { $nop = new Stmt\Nop($this->createCommentNopAttributes($startAttributes['comments'])); } else { $nop = null; };
if ($nop !== null) { $this->semStack[$stackPos-(1-1)][] = $nop; } $this->semValue = $this->semStack[$stackPos-(1-1)];
},
256 => function ($stackPos) {

View File

@ -876,7 +876,7 @@ class Php7 extends \PhpParser\ParserAbstract
$this->semValue = array();
},
4 => function ($stackPos) {
$startAttributes = $this->lookaheadStartAttributes; if (isset($startAttributes['comments'])) { $nop = new Stmt\Nop($this->createZeroLengthAttributes($startAttributes)); } else { $nop = null; };
$startAttributes = $this->lookaheadStartAttributes; if (isset($startAttributes['comments'])) { $nop = new Stmt\Nop($this->createCommentNopAttributes($startAttributes['comments'])); } else { $nop = null; };
if ($nop !== null) { $this->semStack[$stackPos-(1-1)][] = $nop; } $this->semValue = $this->semStack[$stackPos-(1-1)];
},
5 => function ($stackPos) {
@ -1282,7 +1282,7 @@ class Php7 extends \PhpParser\ParserAbstract
$this->semValue = array();
},
137 => function ($stackPos) {
$startAttributes = $this->lookaheadStartAttributes; if (isset($startAttributes['comments'])) { $nop = new Stmt\Nop($this->createZeroLengthAttributes($startAttributes)); } else { $nop = null; };
$startAttributes = $this->lookaheadStartAttributes; if (isset($startAttributes['comments'])) { $nop = new Stmt\Nop($this->createCommentNopAttributes($startAttributes['comments'])); } else { $nop = null; };
if ($nop !== null) { $this->semStack[$stackPos-(1-1)][] = $nop; } $this->semValue = $this->semStack[$stackPos-(1-1)];
},
138 => function ($stackPos) {
@ -1710,7 +1710,7 @@ class Php7 extends \PhpParser\ParserAbstract
$this->semValue = array();
},
276 => function ($stackPos) {
$startAttributes = $this->lookaheadStartAttributes; if (isset($startAttributes['comments'])) { $nop = new Stmt\Nop($this->createZeroLengthAttributes($startAttributes)); } else { $nop = null; };
$startAttributes = $this->lookaheadStartAttributes; if (isset($startAttributes['comments'])) { $nop = new Stmt\Nop($this->createCommentNopAttributes($startAttributes['comments'])); } else { $nop = null; };
if ($nop !== null) { $this->semStack[$stackPos-(1-1)][] = $nop; } $this->semValue = $this->semStack[$stackPos-(1-1)];
},
277 => function ($stackPos) {

View File

@ -842,21 +842,29 @@ abstract class ParserAbstract implements Parser
}
/**
* Create attributes for a zero-length node with the given start attributes.
* Create attributes for a zero-length common-capturing nop.
*
* @param array $startAttributes
* @param Comment[] $comments
* @return array
*/
protected function createZeroLengthAttributes(array $startAttributes) {
$attributes = $startAttributes;
if (isset($startAttributes['startLine'])) {
$attributes['endLine'] = $startAttributes['startLine'];
protected function createCommentNopAttributes(array $comments) {
$comment = $comments[count($comments) - 1];
$commentEndLine = $comment->getEndLine();
$commentEndFilePos = $comment->getEndFilePos();
$commentEndTokenPos = $comment->getEndTokenPos();
$attributes = ['comments' => $comments];
if (-1 !== $commentEndLine) {
$attributes['startLine'] = $commentEndLine;
$attributes['endLine'] = $commentEndLine;
}
if (isset($startAttributes['startTokenPos'])) {
$attributes['endTokenPos'] = $startAttributes['startTokenPos'] - 1;
if (-1 !== $commentEndFilePos) {
$attributes['startFilePos'] = $commentEndFilePos + 1;
$attributes['endFilePos'] = $commentEndFilePos;
}
if (isset($startAttributes['startFilePos'])) {
$attributes['endFilePos'] = $startAttributes['startFilePos'] - 1;
if (-1 !== $commentEndTokenPos) {
$attributes['startTokenPos'] = $commentEndTokenPos + 1;
$attributes['endTokenPos'] = $commentEndTokenPos;
}
return $attributes;
}

View File

@ -16,5 +16,24 @@ class Foo {
{
// I'm just a comment
$foo;}
$foo; }
}
-----
<?php
class Foo {
public function __construct()
{
/* I'm just a comment */
}
}
-----
$stmts[0]->stmts[0]->stmts[] = new Stmt\Expression(new Node\Expr\Variable('foo'));
-----
<?php
class Foo {
public function __construct()
{
/* I'm just a comment */
$foo;
}
}

View File

@ -10,4 +10,18 @@ array(
0: /* Comment */
)
)
)
-----
<?php
{
/* comment */
}
-----
!!positions
array(
0: Stmt_Nop[3:0 - 3:17](
comments: array(
0: /* comment */
)
)
)