2011-07-13 12:24:10 +02:00
|
|
|
<?php
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser;
|
|
|
|
|
2015-06-13 18:39:55 +02:00
|
|
|
use PhpParser\Parser\Tokens;
|
2017-04-27 18:14:07 +02:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2015-06-13 18:39:55 +02:00
|
|
|
|
2017-04-27 18:14:07 +02:00
|
|
|
class LexerTest extends TestCase
|
2011-07-13 12:24:10 +02:00
|
|
|
{
|
2014-11-27 20:57:38 +01:00
|
|
|
/* To allow overwriting in parent class */
|
2017-08-13 14:35:03 +02:00
|
|
|
protected function getLexer(array $options = []) {
|
2014-11-27 20:57:38 +01:00
|
|
|
return new Lexer($options);
|
|
|
|
}
|
2012-04-25 20:04:46 +02:00
|
|
|
|
2011-07-13 12:24:10 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider provideTestError
|
|
|
|
*/
|
2016-09-30 18:28:35 +02:00
|
|
|
public function testError($code, $messages) {
|
2015-02-26 19:19:40 +01:00
|
|
|
if (defined('HHVM_VERSION')) {
|
|
|
|
$this->markTestSkipped('HHVM does not throw warnings from token_get_all()');
|
|
|
|
}
|
|
|
|
|
2016-10-09 13:15:24 +02:00
|
|
|
$errorHandler = new ErrorHandler\Collecting();
|
2016-09-30 18:28:35 +02:00
|
|
|
$lexer = $this->getLexer(['usedAttributes' => [
|
|
|
|
'comments', 'startLine', 'endLine', 'startFilePos', 'endFilePos'
|
|
|
|
]]);
|
2016-10-09 13:15:24 +02:00
|
|
|
$lexer->startLexing($code, $errorHandler);
|
|
|
|
$errors = $errorHandler->getErrors();
|
2011-07-13 12:24:10 +02:00
|
|
|
|
2016-09-30 18:28:35 +02:00
|
|
|
$this->assertSame(count($messages), count($errors));
|
|
|
|
for ($i = 0; $i < count($messages); $i++) {
|
|
|
|
$this->assertSame($messages[$i], $errors[$i]->getMessageWithColumnInfo($code));
|
2011-07-13 12:24:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideTestError() {
|
2017-08-13 14:35:03 +02:00
|
|
|
return [
|
|
|
|
["<?php /*", ["Unterminated comment from 1:7 to 1:9"]],
|
|
|
|
["<?php \1", ["Unexpected character \"\1\" (ASCII 1) from 1:7 to 1:7"]],
|
|
|
|
["<?php \0", ["Unexpected null byte from 1:7 to 1:7"]],
|
2016-09-30 18:28:35 +02:00
|
|
|
// Error with potentially emulated token
|
2017-08-13 14:35:03 +02:00
|
|
|
["<?php ?? \0", ["Unexpected null byte from 1:10 to 1:10"]],
|
|
|
|
["<?php\n\0\1 foo /* bar", [
|
2016-09-30 18:28:35 +02:00
|
|
|
"Unexpected null byte from 2:1 to 2:1",
|
|
|
|
"Unexpected character \"\1\" (ASCII 1) from 2:2 to 2:2",
|
|
|
|
"Unterminated comment from 2:8 to 2:14"
|
2017-08-13 14:35:03 +02:00
|
|
|
]],
|
|
|
|
];
|
2011-07-13 12:24:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideTestLex
|
|
|
|
*/
|
2014-11-27 20:38:14 +01:00
|
|
|
public function testLex($code, $options, $tokens) {
|
2014-11-27 20:57:38 +01:00
|
|
|
$lexer = $this->getLexer($options);
|
2014-11-27 20:38:14 +01:00
|
|
|
$lexer->startLexing($code);
|
|
|
|
while ($id = $lexer->getNextToken($value, $startAttributes, $endAttributes)) {
|
2011-07-13 12:24:10 +02:00
|
|
|
$token = array_shift($tokens);
|
|
|
|
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame($token[0], $id);
|
|
|
|
$this->assertSame($token[1], $value);
|
2012-05-05 17:34:27 +02:00
|
|
|
$this->assertEquals($token[2], $startAttributes);
|
|
|
|
$this->assertEquals($token[3], $endAttributes);
|
2011-07-13 12:24:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideTestLex() {
|
2017-08-13 14:35:03 +02:00
|
|
|
return [
|
2012-05-06 17:49:04 +02:00
|
|
|
// tests conversion of closing PHP tag and drop of whitespace and opening tags
|
2017-08-13 14:35:03 +02:00
|
|
|
[
|
2012-05-06 17:49:04 +02:00
|
|
|
'<?php tokens ?>plaintext',
|
2017-08-13 14:35:03 +02:00
|
|
|
[],
|
|
|
|
[
|
|
|
|
[
|
2015-06-13 18:39:55 +02:00
|
|
|
Tokens::T_STRING, 'tokens',
|
2017-08-13 14:35:03 +02:00
|
|
|
['startLine' => 1], ['endLine' => 1]
|
|
|
|
],
|
|
|
|
[
|
2012-05-05 17:34:27 +02:00
|
|
|
ord(';'), '?>',
|
2017-08-13 14:35:03 +02:00
|
|
|
['startLine' => 1], ['endLine' => 1]
|
|
|
|
],
|
|
|
|
[
|
2015-06-13 18:39:55 +02:00
|
|
|
Tokens::T_INLINE_HTML, 'plaintext',
|
2017-08-13 14:35:03 +02:00
|
|
|
['startLine' => 1, 'hasLeadingNewline' => false],
|
|
|
|
['endLine' => 1]
|
|
|
|
],
|
|
|
|
]
|
|
|
|
],
|
2011-07-13 13:27:14 +02:00
|
|
|
// tests line numbers
|
2017-08-13 14:35:03 +02:00
|
|
|
[
|
2011-07-13 13:27:14 +02:00
|
|
|
'<?php' . "\n" . '$ token /** doc' . "\n" . 'comment */ $',
|
2017-08-13 14:35:03 +02:00
|
|
|
[],
|
|
|
|
[
|
|
|
|
[
|
2012-05-05 17:34:27 +02:00
|
|
|
ord('$'), '$',
|
2017-08-13 14:35:03 +02:00
|
|
|
['startLine' => 2], ['endLine' => 2]
|
|
|
|
],
|
|
|
|
[
|
2015-06-13 18:39:55 +02:00
|
|
|
Tokens::T_STRING, 'token',
|
2017-08-13 14:35:03 +02:00
|
|
|
['startLine' => 2], ['endLine' => 2]
|
|
|
|
],
|
|
|
|
[
|
2012-05-05 17:34:27 +02:00
|
|
|
ord('$'), '$',
|
2017-08-13 14:35:03 +02:00
|
|
|
[
|
2012-05-06 17:49:04 +02:00
|
|
|
'startLine' => 3,
|
2017-08-13 14:35:03 +02:00
|
|
|
'comments' => [
|
2016-04-02 00:54:01 +02:00
|
|
|
new Comment\Doc('/** doc' . "\n" . 'comment */', 2, 14),
|
2017-08-13 14:35:03 +02:00
|
|
|
]
|
|
|
|
],
|
|
|
|
['endLine' => 3]
|
|
|
|
],
|
|
|
|
]
|
|
|
|
],
|
2012-05-06 17:49:04 +02:00
|
|
|
// tests comment extraction
|
2017-08-13 14:35:03 +02:00
|
|
|
[
|
2012-05-06 17:49:04 +02:00
|
|
|
'<?php /* comment */ // comment' . "\n" . '/** docComment 1 *//** docComment 2 */ token',
|
2017-08-13 14:35:03 +02:00
|
|
|
[],
|
|
|
|
[
|
|
|
|
[
|
2015-06-13 18:39:55 +02:00
|
|
|
Tokens::T_STRING, 'token',
|
2017-08-13 14:35:03 +02:00
|
|
|
[
|
2012-05-06 17:49:04 +02:00
|
|
|
'startLine' => 2,
|
2017-08-13 14:35:03 +02:00
|
|
|
'comments' => [
|
2016-04-02 00:54:01 +02:00
|
|
|
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),
|
2017-08-13 14:35:03 +02:00
|
|
|
],
|
|
|
|
],
|
|
|
|
['endLine' => 2]
|
|
|
|
],
|
|
|
|
]
|
|
|
|
],
|
2012-05-05 17:34:27 +02:00
|
|
|
// tests differing start and end line
|
2017-08-13 14:35:03 +02:00
|
|
|
[
|
2012-05-05 17:34:27 +02:00
|
|
|
'<?php "foo' . "\n" . 'bar"',
|
2017-08-13 14:35:03 +02:00
|
|
|
[],
|
|
|
|
[
|
|
|
|
[
|
2015-06-13 18:39:55 +02:00
|
|
|
Tokens::T_CONSTANT_ENCAPSED_STRING, '"foo' . "\n" . 'bar"',
|
2017-08-13 14:35:03 +02:00
|
|
|
['startLine' => 1], ['endLine' => 2]
|
|
|
|
],
|
|
|
|
]
|
|
|
|
],
|
2014-11-27 20:38:14 +01:00
|
|
|
// tests exact file offsets
|
2017-08-13 14:35:03 +02:00
|
|
|
[
|
2014-11-27 20:38:14 +01:00
|
|
|
'<?php "a";' . "\n" . '// foo' . "\n" . '"b";',
|
2017-08-13 14:35:03 +02:00
|
|
|
['usedAttributes' => ['startFilePos', 'endFilePos']],
|
|
|
|
[
|
|
|
|
[
|
2015-06-13 18:39:55 +02:00
|
|
|
Tokens::T_CONSTANT_ENCAPSED_STRING, '"a"',
|
2017-08-13 14:35:03 +02:00
|
|
|
['startFilePos' => 6], ['endFilePos' => 8]
|
|
|
|
],
|
|
|
|
[
|
2014-11-27 20:38:14 +01:00
|
|
|
ord(';'), ';',
|
2017-08-13 14:35:03 +02:00
|
|
|
['startFilePos' => 9], ['endFilePos' => 9]
|
|
|
|
],
|
|
|
|
[
|
2015-06-13 18:39:55 +02:00
|
|
|
Tokens::T_CONSTANT_ENCAPSED_STRING, '"b"',
|
2017-08-13 14:35:03 +02:00
|
|
|
['startFilePos' => 18], ['endFilePos' => 20]
|
|
|
|
],
|
|
|
|
[
|
2014-11-27 20:38:14 +01:00
|
|
|
ord(';'), ';',
|
2017-08-13 14:35:03 +02:00
|
|
|
['startFilePos' => 21], ['endFilePos' => 21]
|
|
|
|
],
|
|
|
|
]
|
|
|
|
],
|
2014-12-18 23:26:17 +01:00
|
|
|
// tests token offsets
|
2017-08-13 14:35:03 +02:00
|
|
|
[
|
2014-12-18 23:26:17 +01:00
|
|
|
'<?php "a";' . "\n" . '// foo' . "\n" . '"b";',
|
2017-08-13 14:35:03 +02:00
|
|
|
['usedAttributes' => ['startTokenPos', 'endTokenPos']],
|
|
|
|
[
|
|
|
|
[
|
2015-06-13 18:39:55 +02:00
|
|
|
Tokens::T_CONSTANT_ENCAPSED_STRING, '"a"',
|
2017-08-13 14:35:03 +02:00
|
|
|
['startTokenPos' => 1], ['endTokenPos' => 1]
|
|
|
|
],
|
|
|
|
[
|
2014-12-18 23:26:17 +01:00
|
|
|
ord(';'), ';',
|
2017-08-13 14:35:03 +02:00
|
|
|
['startTokenPos' => 2], ['endTokenPos' => 2]
|
|
|
|
],
|
|
|
|
[
|
2015-06-13 18:39:55 +02:00
|
|
|
Tokens::T_CONSTANT_ENCAPSED_STRING, '"b"',
|
2017-08-13 14:35:03 +02:00
|
|
|
['startTokenPos' => 5], ['endTokenPos' => 5]
|
|
|
|
],
|
|
|
|
[
|
2014-12-18 23:26:17 +01:00
|
|
|
ord(';'), ';',
|
2017-08-13 14:35:03 +02:00
|
|
|
['startTokenPos' => 6], ['endTokenPos' => 6]
|
|
|
|
],
|
|
|
|
]
|
|
|
|
],
|
2014-11-27 20:38:14 +01:00
|
|
|
// tests all attributes being disabled
|
2017-08-13 14:35:03 +02:00
|
|
|
[
|
2014-11-27 20:38:14 +01:00
|
|
|
'<?php /* foo */ $bar;',
|
2017-08-13 14:35:03 +02:00
|
|
|
['usedAttributes' => []],
|
|
|
|
[
|
|
|
|
[
|
2015-06-13 18:39:55 +02:00
|
|
|
Tokens::T_VARIABLE, '$bar',
|
2017-08-13 14:35:03 +02:00
|
|
|
[], []
|
|
|
|
],
|
|
|
|
[
|
2014-11-27 20:38:14 +01:00
|
|
|
ord(';'), ';',
|
2017-08-13 14:35:03 +02:00
|
|
|
[], []
|
|
|
|
]
|
|
|
|
]
|
|
|
|
],
|
2017-05-31 23:54:26 +02:00
|
|
|
// tests no tokens
|
2017-08-13 14:35:03 +02:00
|
|
|
[
|
2017-05-31 23:54:26 +02:00
|
|
|
'',
|
2017-08-13 14:35:03 +02:00
|
|
|
[],
|
|
|
|
[]
|
|
|
|
],
|
|
|
|
];
|
2011-07-13 12:24:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideTestHaltCompiler
|
|
|
|
*/
|
|
|
|
public function testHandleHaltCompiler($code, $remaining) {
|
2014-11-27 20:57:38 +01:00
|
|
|
$lexer = $this->getLexer();
|
2014-11-27 20:38:14 +01:00
|
|
|
$lexer->startLexing($code);
|
2011-07-13 12:24:10 +02:00
|
|
|
|
2015-06-13 18:39:55 +02:00
|
|
|
while (Tokens::T_HALT_COMPILER !== $lexer->getNextToken());
|
2011-07-13 12:24:10 +02:00
|
|
|
|
2014-11-27 20:57:38 +01:00
|
|
|
$this->assertSame($remaining, $lexer->handleHaltCompiler());
|
2014-11-27 20:38:14 +01:00
|
|
|
$this->assertSame(0, $lexer->getNextToken());
|
2011-07-13 12:24:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideTestHaltCompiler() {
|
2017-08-13 14:35:03 +02:00
|
|
|
return [
|
|
|
|
['<?php ... __halt_compiler();Remaining Text', 'Remaining Text'],
|
|
|
|
['<?php ... __halt_compiler ( ) ;Remaining Text', 'Remaining Text'],
|
|
|
|
['<?php ... __halt_compiler() ?>Remaining Text', 'Remaining Text'],
|
2011-07-13 12:24:10 +02:00
|
|
|
//array('<?php ... __halt_compiler();' . "\0", "\0"),
|
|
|
|
//array('<?php ... __halt_compiler /* */ ( ) ;Remaining Text', 'Remaining Text'),
|
2017-08-13 14:35:03 +02:00
|
|
|
];
|
2011-07-13 12:24:10 +02:00
|
|
|
}
|
2014-12-18 23:26:17 +01:00
|
|
|
|
2015-06-20 12:09:03 +02:00
|
|
|
/**
|
|
|
|
* @expectedException \PhpParser\Error
|
|
|
|
* @expectedExceptionMessage __HALT_COMPILER must be followed by "();"
|
|
|
|
*/
|
|
|
|
public function testHandleHaltCompilerError() {
|
|
|
|
$lexer = $this->getLexer();
|
|
|
|
$lexer->startLexing('<?php ... __halt_compiler invalid ();');
|
|
|
|
|
2015-06-20 12:34:20 +02:00
|
|
|
while (Tokens::T_HALT_COMPILER !== $lexer->getNextToken());
|
2015-06-20 12:09:03 +02:00
|
|
|
$lexer->handleHaltCompiler();
|
|
|
|
}
|
|
|
|
|
2014-12-18 23:26:17 +01:00
|
|
|
public function testGetTokens() {
|
|
|
|
$code = '<?php "a";' . "\n" . '// foo' . "\n" . '"b";';
|
2017-08-13 14:35:03 +02:00
|
|
|
$expectedTokens = [
|
|
|
|
[T_OPEN_TAG, '<?php ', 1],
|
|
|
|
[T_CONSTANT_ENCAPSED_STRING, '"a"', 1],
|
2014-12-18 23:26:17 +01:00
|
|
|
';',
|
2017-08-13 14:35:03 +02:00
|
|
|
[T_WHITESPACE, "\n", 1],
|
|
|
|
[T_COMMENT, '// foo' . "\n", 2],
|
|
|
|
[T_CONSTANT_ENCAPSED_STRING, '"b"', 3],
|
2014-12-18 23:26:17 +01:00
|
|
|
';',
|
2017-08-13 14:35:03 +02:00
|
|
|
];
|
2014-12-18 23:26:17 +01:00
|
|
|
|
|
|
|
$lexer = $this->getLexer();
|
|
|
|
$lexer->startLexing($code);
|
|
|
|
$this->assertSame($expectedTokens, $lexer->getTokens());
|
|
|
|
}
|
2014-11-27 20:38:14 +01:00
|
|
|
}
|