mirror of
https://github.com/phabelio/PHP-Parser.git
synced 2024-11-30 04:29:15 +01:00
Add start file offset to Comments
This commit is contained in:
parent
68b4c0388a
commit
15a2388d75
@ -6,16 +6,19 @@ class Comment
|
||||
{
|
||||
protected $text;
|
||||
protected $line;
|
||||
protected $filePos;
|
||||
|
||||
/**
|
||||
* Constructs a comment node.
|
||||
*
|
||||
* @param string $text Comment text (including comment delimiters like /*)
|
||||
* @param int $line Line number the comment started on
|
||||
* @param string $text Comment text (including comment delimiters like /*)
|
||||
* @param int $startLine Line number the comment started on
|
||||
* @param int $startFilePos File offset the comment started on
|
||||
*/
|
||||
public function __construct($text, $line = -1) {
|
||||
public function __construct($text, $startLine = -1, $startFilePos = -1) {
|
||||
$this->text = $text;
|
||||
$this->line = $line;
|
||||
$this->line = $startLine;
|
||||
$this->filePos = $startFilePos;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -54,6 +57,15 @@ class Comment
|
||||
$this->line = $line;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the file offset the comment started on.
|
||||
*
|
||||
* @return int File offset
|
||||
*/
|
||||
public function getFilePos() {
|
||||
return $this->filePos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the comment text.
|
||||
*
|
||||
|
@ -33,7 +33,9 @@ class Lexer
|
||||
|
||||
// map of tokens to drop while lexing (the map is only used for isset lookup,
|
||||
// that's why the value is simply set to 1; the value is never actually used.)
|
||||
$this->dropTokens = array_fill_keys(array(T_WHITESPACE, T_OPEN_TAG), 1);
|
||||
$this->dropTokens = array_fill_keys(
|
||||
array(T_WHITESPACE, T_OPEN_TAG, T_COMMENT, T_DOC_COMMENT), 1
|
||||
);
|
||||
|
||||
// the usedAttributes member is a map of the used attribute names to a dummy
|
||||
// value (here "true")
|
||||
@ -174,36 +176,38 @@ class Lexer
|
||||
}
|
||||
|
||||
return $id;
|
||||
} elseif (!isset($this->dropTokens[$token[0]])) {
|
||||
$value = $token[1];
|
||||
|
||||
$this->line += substr_count($value, "\n");
|
||||
$this->filePos += strlen($value);
|
||||
|
||||
if (isset($this->usedAttributes['startLine'])) {
|
||||
$startAttributes['startLine'] = $token[2];
|
||||
}
|
||||
if (isset($this->usedAttributes['endLine'])) {
|
||||
$endAttributes['endLine'] = $this->line;
|
||||
}
|
||||
if (isset($this->usedAttributes['endTokenPos'])) {
|
||||
$endAttributes['endTokenPos'] = $this->pos;
|
||||
}
|
||||
if (isset($this->usedAttributes['endFilePos'])) {
|
||||
$endAttributes['endFilePos'] = $this->filePos - 1;
|
||||
}
|
||||
|
||||
return $this->tokenMap[$token[0]];
|
||||
} else {
|
||||
if (T_COMMENT === $token[0] || T_DOC_COMMENT === $token[0]) {
|
||||
if (isset($this->usedAttributes['comments'])) {
|
||||
$comment = T_DOC_COMMENT === $token[0]
|
||||
? new Comment\Doc($token[1], $this->line, $this->filePos)
|
||||
: new Comment($token[1], $this->line, $this->filePos);
|
||||
$startAttributes['comments'][] = $comment;
|
||||
}
|
||||
}
|
||||
|
||||
$this->line += substr_count($token[1], "\n");
|
||||
$this->filePos += strlen($token[1]);
|
||||
|
||||
if (T_COMMENT === $token[0]) {
|
||||
if (isset($this->usedAttributes['comments'])) {
|
||||
$startAttributes['comments'][] = new Comment($token[1], $token[2]);
|
||||
}
|
||||
} elseif (T_DOC_COMMENT === $token[0]) {
|
||||
if (isset($this->usedAttributes['comments'])) {
|
||||
$startAttributes['comments'][] = new Comment\Doc($token[1], $token[2]);
|
||||
}
|
||||
} elseif (!isset($this->dropTokens[$token[0]])) {
|
||||
$value = $token[1];
|
||||
|
||||
if (isset($this->usedAttributes['startLine'])) {
|
||||
$startAttributes['startLine'] = $token[2];
|
||||
}
|
||||
if (isset($this->usedAttributes['endLine'])) {
|
||||
$endAttributes['endLine'] = $this->line;
|
||||
}
|
||||
if (isset($this->usedAttributes['endTokenPos'])) {
|
||||
$endAttributes['endTokenPos'] = $this->pos;
|
||||
}
|
||||
if (isset($this->usedAttributes['endFilePos'])) {
|
||||
$endAttributes['endFilePos'] = $this->filePos - 1;
|
||||
}
|
||||
|
||||
return $this->tokenMap[$token[0]];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -5,11 +5,12 @@ namespace PhpParser;
|
||||
class CommentTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testGetSet() {
|
||||
$comment = new Comment('/* Some comment */', 1);
|
||||
$comment = new Comment('/* Some comment */', 1, 10);
|
||||
|
||||
$this->assertSame('/* Some comment */', $comment->getText());
|
||||
$this->assertSame('/* Some comment */', (string) $comment);
|
||||
$this->assertSame(1, $comment->getLine());
|
||||
$this->assertSame(10, $comment->getFilePos());
|
||||
|
||||
$comment->setText('/* Some other comment */');
|
||||
$comment->setLine(10);
|
||||
|
@ -93,7 +93,9 @@ class LexerTest extends \PHPUnit_Framework_TestCase
|
||||
ord('$'), '$',
|
||||
array(
|
||||
'startLine' => 3,
|
||||
'comments' => array(new Comment\Doc('/** doc' . "\n" . 'comment */', 2))
|
||||
'comments' => array(
|
||||
new Comment\Doc('/** doc' . "\n" . 'comment */', 2, 14),
|
||||
)
|
||||
),
|
||||
array('endLine' => 3)
|
||||
),
|
||||
@ -109,10 +111,10 @@ class LexerTest extends \PHPUnit_Framework_TestCase
|
||||
array(
|
||||
'startLine' => 2,
|
||||
'comments' => array(
|
||||
new Comment('/* comment */', 1),
|
||||
new Comment('// comment' . "\n", 1),
|
||||
new Comment\Doc('/** docComment 1 */', 2),
|
||||
new Comment\Doc('/** docComment 2 */', 2),
|
||||
new Comment('/* comment */', 1, 6),
|
||||
new Comment('// comment' . "\n", 1, 20),
|
||||
new Comment\Doc('/** docComment 1 */', 2, 31),
|
||||
new Comment\Doc('/** docComment 2 */', 2, 50),
|
||||
),
|
||||
),
|
||||
array('endLine' => 2)
|
||||
|
@ -54,7 +54,7 @@ EOC;
|
||||
$this->assertInstanceOf('PhpParser\Node\Stmt\Function_', $fn);
|
||||
$this->assertEquals(array(
|
||||
'comments' => array(
|
||||
new Comment\Doc('/** Doc comment */', 2),
|
||||
new Comment\Doc('/** Doc comment */', 2, 6),
|
||||
),
|
||||
'startLine' => 3,
|
||||
'endLine' => 7,
|
||||
@ -76,8 +76,8 @@ EOC;
|
||||
$this->assertInstanceOf('PhpParser\Node\Stmt\Echo_', $echo);
|
||||
$this->assertEquals(array(
|
||||
'comments' => array(
|
||||
new Comment("// Line\n", 4),
|
||||
new Comment("// Comments\n", 5),
|
||||
new Comment("// Line\n", 4, 49),
|
||||
new Comment("// Comments\n", 5, 61),
|
||||
),
|
||||
'startLine' => 6,
|
||||
'endLine' => 6,
|
||||
|
Loading…
Reference in New Issue
Block a user