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;
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
class LexerTest extends \PHPUnit_Framework_TestCase
|
2011-07-13 12:24:10 +02:00
|
|
|
{
|
2014-11-27 20:57:38 +01:00
|
|
|
/* To allow overwriting in parent class */
|
|
|
|
protected function getLexer(array $options = array()) {
|
|
|
|
return new Lexer($options);
|
|
|
|
}
|
2012-04-25 20:04:46 +02:00
|
|
|
|
2011-07-13 12:24:10 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider provideTestError
|
|
|
|
*/
|
|
|
|
public function testError($code, $message) {
|
2015-02-26 19:19:40 +01:00
|
|
|
if (defined('HHVM_VERSION')) {
|
|
|
|
$this->markTestSkipped('HHVM does not throw warnings from token_get_all()');
|
|
|
|
}
|
|
|
|
|
2014-11-27 20:57:38 +01:00
|
|
|
$lexer = $this->getLexer();
|
2011-07-13 12:24:10 +02:00
|
|
|
try {
|
2014-11-27 20:38:14 +01:00
|
|
|
$lexer->startLexing($code);
|
2014-02-06 14:44:16 +01:00
|
|
|
} catch (Error $e) {
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame($message, $e->getMessage());
|
2011-07-13 12:24:10 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
$this->fail('Expected PhpParser\Error');
|
2011-07-13 12:24:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideTestError() {
|
|
|
|
return array(
|
|
|
|
array('<?php /*', 'Unterminated comment on line 1'),
|
|
|
|
array('<?php ' . "\1", 'Unexpected character "' . "\1" . '" (ASCII 1) on unknown line'),
|
|
|
|
array('<?php ' . "\0", 'Unexpected null byte on unknown line'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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() {
|
|
|
|
return array(
|
2012-05-06 17:49:04 +02:00
|
|
|
// tests conversion of closing PHP tag and drop of whitespace and opening tags
|
2011-07-13 12:24:10 +02:00
|
|
|
array(
|
2012-05-06 17:49:04 +02:00
|
|
|
'<?php tokens ?>plaintext',
|
2014-11-27 20:38:14 +01:00
|
|
|
array(),
|
2011-07-13 12:24:10 +02:00
|
|
|
array(
|
2012-05-05 17:34:27 +02:00
|
|
|
array(
|
2015-06-13 18:39:55 +02:00
|
|
|
Tokens::T_STRING, 'tokens',
|
2012-05-05 17:34:27 +02:00
|
|
|
array('startLine' => 1), array('endLine' => 1)
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
ord(';'), '?>',
|
|
|
|
array('startLine' => 1), array('endLine' => 1)
|
|
|
|
),
|
|
|
|
array(
|
2015-06-13 18:39:55 +02:00
|
|
|
Tokens::T_INLINE_HTML, 'plaintext',
|
2016-07-25 16:42:42 +02:00
|
|
|
array('startLine' => 1, 'hasLeadingNewline' => false),
|
|
|
|
array('endLine' => 1)
|
2012-05-05 17:34:27 +02:00
|
|
|
),
|
2011-07-13 12:24:10 +02:00
|
|
|
)
|
|
|
|
),
|
2011-07-13 13:27:14 +02:00
|
|
|
// tests line numbers
|
|
|
|
array(
|
|
|
|
'<?php' . "\n" . '$ token /** doc' . "\n" . 'comment */ $',
|
2014-11-27 20:38:14 +01:00
|
|
|
array(),
|
2011-07-13 13:27:14 +02:00
|
|
|
array(
|
2012-05-05 17:34:27 +02:00
|
|
|
array(
|
|
|
|
ord('$'), '$',
|
|
|
|
array('startLine' => 2), array('endLine' => 2)
|
|
|
|
),
|
|
|
|
array(
|
2015-06-13 18:39:55 +02:00
|
|
|
Tokens::T_STRING, 'token',
|
2012-05-05 17:34:27 +02:00
|
|
|
array('startLine' => 2), array('endLine' => 2)
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
ord('$'), '$',
|
2012-05-06 17:49:04 +02:00
|
|
|
array(
|
|
|
|
'startLine' => 3,
|
2016-04-02 00:54:01 +02:00
|
|
|
'comments' => array(
|
|
|
|
new Comment\Doc('/** doc' . "\n" . 'comment */', 2, 14),
|
|
|
|
)
|
2012-05-06 17:49:04 +02:00
|
|
|
),
|
|
|
|
array('endLine' => 3)
|
2012-05-05 17:34:27 +02:00
|
|
|
),
|
2011-07-13 13:27:14 +02:00
|
|
|
)
|
|
|
|
),
|
2012-05-06 17:49:04 +02:00
|
|
|
// tests comment extraction
|
2011-07-13 12:24:10 +02:00
|
|
|
array(
|
2012-05-06 17:49:04 +02:00
|
|
|
'<?php /* comment */ // comment' . "\n" . '/** docComment 1 *//** docComment 2 */ token',
|
2014-11-27 20:38:14 +01:00
|
|
|
array(),
|
2011-07-13 12:24:10 +02:00
|
|
|
array(
|
2012-05-05 17:34:27 +02:00
|
|
|
array(
|
2015-06-13 18:39:55 +02:00
|
|
|
Tokens::T_STRING, 'token',
|
2012-05-06 17:49:04 +02:00
|
|
|
array(
|
|
|
|
'startLine' => 2,
|
|
|
|
'comments' => array(
|
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),
|
2012-05-06 17:49:04 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
array('endLine' => 2)
|
2012-05-05 17:34:27 +02:00
|
|
|
),
|
|
|
|
)
|
|
|
|
),
|
|
|
|
// tests differing start and end line
|
|
|
|
array(
|
|
|
|
'<?php "foo' . "\n" . 'bar"',
|
2014-11-27 20:38:14 +01:00
|
|
|
array(),
|
2012-05-05 17:34:27 +02:00
|
|
|
array(
|
|
|
|
array(
|
2015-06-13 18:39:55 +02:00
|
|
|
Tokens::T_CONSTANT_ENCAPSED_STRING, '"foo' . "\n" . 'bar"',
|
2012-05-05 17:34:27 +02:00
|
|
|
array('startLine' => 1), array('endLine' => 2)
|
|
|
|
),
|
2011-07-13 12:24:10 +02:00
|
|
|
)
|
|
|
|
),
|
2014-11-27 20:38:14 +01:00
|
|
|
// tests exact file offsets
|
|
|
|
array(
|
|
|
|
'<?php "a";' . "\n" . '// foo' . "\n" . '"b";',
|
|
|
|
array('usedAttributes' => array('startFilePos', 'endFilePos')),
|
|
|
|
array(
|
|
|
|
array(
|
2015-06-13 18:39:55 +02:00
|
|
|
Tokens::T_CONSTANT_ENCAPSED_STRING, '"a"',
|
2014-12-18 23:26:17 +01:00
|
|
|
array('startFilePos' => 6), array('endFilePos' => 8)
|
2014-11-27 20:38:14 +01:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
ord(';'), ';',
|
2014-12-18 23:26:17 +01:00
|
|
|
array('startFilePos' => 9), array('endFilePos' => 9)
|
2014-11-27 20:38:14 +01:00
|
|
|
),
|
|
|
|
array(
|
2015-06-13 18:39:55 +02:00
|
|
|
Tokens::T_CONSTANT_ENCAPSED_STRING, '"b"',
|
2014-12-18 23:26:17 +01:00
|
|
|
array('startFilePos' => 18), array('endFilePos' => 20)
|
2014-11-27 20:38:14 +01:00
|
|
|
),
|
|
|
|
array(
|
|
|
|
ord(';'), ';',
|
2014-12-18 23:26:17 +01:00
|
|
|
array('startFilePos' => 21), array('endFilePos' => 21)
|
|
|
|
),
|
|
|
|
)
|
|
|
|
),
|
|
|
|
// tests token offsets
|
|
|
|
array(
|
|
|
|
'<?php "a";' . "\n" . '// foo' . "\n" . '"b";',
|
|
|
|
array('usedAttributes' => array('startTokenPos', 'endTokenPos')),
|
|
|
|
array(
|
|
|
|
array(
|
2015-06-13 18:39:55 +02:00
|
|
|
Tokens::T_CONSTANT_ENCAPSED_STRING, '"a"',
|
2014-12-18 23:26:17 +01:00
|
|
|
array('startTokenPos' => 1), array('endTokenPos' => 1)
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
ord(';'), ';',
|
|
|
|
array('startTokenPos' => 2), array('endTokenPos' => 2)
|
|
|
|
),
|
|
|
|
array(
|
2015-06-13 18:39:55 +02:00
|
|
|
Tokens::T_CONSTANT_ENCAPSED_STRING, '"b"',
|
2014-12-18 23:26:17 +01:00
|
|
|
array('startTokenPos' => 5), array('endTokenPos' => 5)
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
ord(';'), ';',
|
|
|
|
array('startTokenPos' => 6), array('endTokenPos' => 6)
|
2014-11-27 20:38:14 +01:00
|
|
|
),
|
|
|
|
)
|
|
|
|
),
|
|
|
|
// tests all attributes being disabled
|
|
|
|
array(
|
|
|
|
'<?php /* foo */ $bar;',
|
|
|
|
array('usedAttributes' => array()),
|
|
|
|
array(
|
|
|
|
array(
|
2015-06-13 18:39:55 +02:00
|
|
|
Tokens::T_VARIABLE, '$bar',
|
2014-11-27 20:38:14 +01:00
|
|
|
array(), array()
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
ord(';'), ';',
|
|
|
|
array(), array()
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
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() {
|
|
|
|
return array(
|
|
|
|
array('<?php ... __halt_compiler();Remaining Text', 'Remaining Text'),
|
|
|
|
array('<?php ... __halt_compiler ( ) ;Remaining Text', 'Remaining Text'),
|
2011-10-18 18:03:28 +02:00
|
|
|
array('<?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'),
|
|
|
|
);
|
|
|
|
}
|
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";';
|
|
|
|
$expectedTokens = array(
|
|
|
|
array(T_OPEN_TAG, '<?php ', 1),
|
|
|
|
array(T_CONSTANT_ENCAPSED_STRING, '"a"', 1),
|
|
|
|
';',
|
|
|
|
array(T_WHITESPACE, "\n", 1),
|
|
|
|
array(T_COMMENT, '// foo' . "\n", 2),
|
|
|
|
array(T_CONSTANT_ENCAPSED_STRING, '"b"', 3),
|
|
|
|
';',
|
|
|
|
);
|
|
|
|
|
|
|
|
$lexer = $this->getLexer();
|
|
|
|
$lexer->startLexing($code);
|
|
|
|
$this->assertSame($expectedTokens, $lexer->getTokens());
|
|
|
|
}
|
2014-11-27 20:38:14 +01:00
|
|
|
}
|