Skip to content

Commit

Permalink
Add get{Start,End}{Line,TokenPos,FilePos}() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nikic committed Sep 29, 2017
1 parent 3d4621b commit cc328a4
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 14 deletions.
62 changes: 60 additions & 2 deletions lib/PhpParser/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,70 @@ public function getType() : string;
public function getSubNodeNames() : array;

/**
* Gets line the node started in.
* Gets line the node started in (alias of getStartLine).
*
* @return int Line
* @return int Start line (or -1 if not available)
*/
public function getLine() : int;

/**
* Gets line the node started in.
*
* Requires the 'startLine' attribute to be enabled in the lexer (enabled by default).
*
* @return int Start line (or -1 if not available)
*/
public function getStartLine() : int;

/**
* Gets the line the node ended in.
*
* Requires the 'endLine' attribute to be enabled in the lexer (enabled by default).
*
* @return int End line (or -1 if not available)
*/
public function getEndLine() : int;

/**
* Gets the token offset of the first token that is part of this node.
*
* The offset is an index into the array returned by Lexer::getTokens().
*
* Requires the 'startTokenPos' attribute to be enabled in the lexer (DISABLED by default).
*
* @return int Token start position (or -1 if not available)
*/
public function getStartTokenPos() : int;

/**
* Gets the token offset of the last token that is part of this node.
*
* The offset is an index into the array returned by Lexer::getTokens().
*
* Requires the 'endTokenPos' attribute to be enabled in the lexer (DISABLED by default).
*
* @return int Token end position (or -1 if not available)
*/
public function getEndTokenPos() : int;

/**
* Gets the file offset of the first character that is part of this node.
*
* Requires the 'startFilePos' attribute to be enabled in the lexer (DISABLED by default).
*
* @return int File start position (or -1 if not available)
*/
public function getStartFilePos() : int;

/**
* Gets the file offset of the last character that is part of this node.
*
* Requires the 'endFilePos' attribute to be enabled in the lexer (DISABLED by default).
*
* @return int File end position (or -1 if not available)
*/
public function getEndFilePos() : int;

/**
* Gets all comments directly preceding this node.
*
Expand Down
74 changes: 72 additions & 2 deletions lib/PhpParser/NodeAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,84 @@ public function getType() : string {
}

/**
* Gets line the node started in.
* Gets line the node started in (alias of getStartLine).
*
* @return int Line
* @return int Start line (or -1 if not available)
*/
public function getLine() : int {
return $this->attributes['startLine'] ?? -1;
}

/**
* Gets line the node started in.
*
* Requires the 'startLine' attribute to be enabled in the lexer (enabled by default).
*
* @return int Start line (or -1 if not available)
*/
public function getStartLine() : int {
return $this->attributes['startLine'] ?? -1;
}

/**
* Gets the line the node ended in.
*
* Requires the 'endLine' attribute to be enabled in the lexer (enabled by default).
*
* @return int End line (or -1 if not available)
*/
public function getEndLine() : int {
return $this->attributes['endLine'] ?? -1;
}

/**
* Gets the token offset of the first token that is part of this node.
*
* The offset is an index into the array returned by Lexer::getTokens().
*
* Requires the 'startTokenPos' attribute to be enabled in the lexer (DISABLED by default).
*
* @return int Token start position (or -1 if not available)
*/
public function getStartTokenPos() : int {
return $this->attributes['startTokenPos'] ?? -1;
}

/**
* Gets the token offset of the last token that is part of this node.
*
* The offset is an index into the array returned by Lexer::getTokens().
*
* Requires the 'endTokenPos' attribute to be enabled in the lexer (DISABLED by default).
*
* @return int Token end position (or -1 if not available)
*/
public function getEndTokenPos() : int {
return $this->attributes['endTokenPos'] ?? -1;
}

/**
* Gets the file offset of the first character that is part of this node.
*
* Requires the 'startFilePos' attribute to be enabled in the lexer (DISABLED by default).
*
* @return int File start position (or -1 if not available)
*/
public function getStartFilePos() : int {
return $this->attributes['startFilePos'] ?? -1;
}

/**
* Gets the file offset of the last character that is part of this node.
*
* Requires the 'endFilePos' attribute to be enabled in the lexer (DISABLED by default).
*
* @return int File end position (or -1 if not available)
*/
public function getEndFilePos() : int {
return $this->attributes['endFilePos'] ?? -1;
}

/**
* Gets all comments directly preceding this node.
*
Expand Down
8 changes: 4 additions & 4 deletions lib/PhpParser/NodeDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,13 @@ protected function dumpPosition(Node $node) {
return null;
}

$start = $node->getAttribute('startLine');
$end = $node->getAttribute('endLine');
$start = $node->getStartLine();
$end = $node->getEndLine();
if ($node->hasAttribute('startFilePos') && $node->hasAttribute('endFilePos')
&& null !== $this->code
) {
$start .= ':' . $this->toColumn($this->code, $node->getAttribute('startFilePos'));
$end .= ':' . $this->toColumn($this->code, $node->getAttribute('endFilePos'));
$start .= ':' . $this->toColumn($this->code, $node->getStartFilePos());
$end .= ':' . $this->toColumn($this->code, $node->getEndFilePos());
}
return "[$start - $end]";
}
Expand Down
12 changes: 6 additions & 6 deletions lib/PhpParser/PrettyPrinterAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -495,8 +495,8 @@ protected function p(Node $node) : string {
return $this->pFallback($node);
}

$startPos = $origNode->getAttribute('startTokenPos', -1);
$endPos = $origNode->getAttribute('endTokenPos', -1);
$startPos = $origNode->getStartTokenPos();
$endPos = $origNode->getEndTokenPos();
if ($startPos < 0 || $endPos < 0) {
// Shouldn't happen
return $this->pFallback($node);
Expand Down Expand Up @@ -550,8 +550,8 @@ protected function p(Node $node) : string {
$extraLeft = '';
$extraRight = '';
if ($origSubNode !== null) {
$subStartPos = $origSubNode->getAttribute('startTokenPos', -1);
$subEndPos = $origSubNode->getAttribute('endTokenPos', -1);
$subStartPos = $origSubNode->getStartTokenPos();
$subEndPos = $origSubNode->getEndTokenPos();
if ($subStartPos < 0 || $subEndPos < 0) {
// Shouldn't happen
return $this->pFallback($node);
Expand Down Expand Up @@ -663,8 +663,8 @@ protected function pArray(array $nodes, array $origNodes, int &$pos, int $indent
return null;
}

$itemStartPos = $origArrItem->getAttribute('startTokenPos', -1);
$itemEndPos = $origArrItem->getAttribute('endTokenPos', -1);
$itemStartPos = $origArrItem->getStartTokenPos();
$itemEndPos = $origArrItem->getEndTokenPos();
if ($itemStartPos < 0 || $itemEndPos < 0) {
// Shouldn't happen
return null;
Expand Down
11 changes: 11 additions & 0 deletions test/PhpParser/NodeAbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class NodeAbstractTest extends TestCase
public function provideNodes() {
$attributes = [
'startLine' => 10,
'endLine' => 11,
'startTokenPos' => 12,
'endTokenPos' => 13,
'startFilePos' => 14,
'endFilePos' => 15,
'comments' => [
new Comment('// Comment' . "\n"),
new Comment\Doc('/** doc comment */'),
Expand All @@ -50,6 +55,12 @@ public function testConstruct(array $attributes, Node $node) {
$this->assertSame('Dummy', $node->getType());
$this->assertSame(['subNode1', 'subNode2'], $node->getSubNodeNames());
$this->assertSame(10, $node->getLine());
$this->assertSame(10, $node->getStartLine());
$this->assertSame(11, $node->getEndLine());
$this->assertSame(12, $node->getStartTokenPos());
$this->assertSame(13, $node->getEndTokenPos());
$this->assertSame(14, $node->getStartFilePos());
$this->assertSame(15, $node->getEndFilePos());
$this->assertSame('/** doc comment */', $node->getDocComment()->getText());
$this->assertSame('value1', $node->subNode1);
$this->assertSame('value2', $node->subNode2);
Expand Down

0 comments on commit cc328a4

Please sign in to comment.