Fix incorrect line number extraction

This commit is contained in:
nikic 2011-07-13 13:27:14 +02:00
parent a6f97681f6
commit cc80385aae
2 changed files with 23 additions and 8 deletions

View File

@ -27,7 +27,7 @@ class PHPParser_Lexer
$this->code = $code;
$this->tokens = @token_get_all($code);
$this->pos = -1;
$this->line = -1;
$this->line = 1;
$error = error_get_last();
@ -77,12 +77,16 @@ class PHPParser_Lexer
$value = $token;
$line = $this->line;
return ord($token);
} elseif (T_DOC_COMMENT === $token[0]) {
$docComment = $token[1];
} elseif (!isset(self::$dropTokens[$token[0]])) {
$value = $token[1];
$line = $this->line = $token[2];
return self::$tokenMap[$token[0]];
} else {
$this->line += substr_count($token[1], "\n");
if (T_DOC_COMMENT === $token[0]) {
$docComment = $token[1];
} elseif (!isset(self::$dropTokens[$token[0]])) {
$value = $token[1];
$line = $token[2];
return self::$tokenMap[$token[0]];
}
}
}

View File

@ -43,14 +43,25 @@ class Unit_LexerTest extends PHPUnit_Framework_TestCase
public function provideTestLex() {
return array(
// tests conversion of closing PHP tag and drop of whitespace, comments and opening tags
array(
'<?php tokens ?>plaintext',
'<?php tokens // ?>plaintext',
array(
array(PHPParser_Parser::T_STRING, 'tokens', 1, null),
array(ord(';'), '?>', 1, null),
array(PHPParser_Parser::T_INLINE_HTML, 'plaintext', 1, null),
)
),
// tests line numbers
array(
'<?php' . "\n" . '$ token /** doc' . "\n" . 'comment */ $',
array(
array(ord('$'), '$', 2, null),
array(PHPParser_Parser::T_STRING, 'token', 2, null),
array(ord('$'), '$', 3, '/** doc' . "\n" . 'comment */')
)
),
// tests doccomment extraction
array(
'<?php /** docComment 1 *//** docComment 2 */ token',
array(