2012-02-21 18:45:07 +01:00
|
|
|
<?php
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser\Lexer;
|
|
|
|
|
2014-11-27 20:57:38 +01:00
|
|
|
use PhpParser\LexerTest;
|
2014-02-06 14:44:16 +01:00
|
|
|
use PhpParser\Parser;
|
|
|
|
|
2014-11-27 21:01:56 +01:00
|
|
|
require_once __DIR__ . '/../LexerTest.php';
|
|
|
|
|
2014-11-27 20:57:38 +01:00
|
|
|
class EmulativeTest extends LexerTest
|
2012-02-21 18:45:07 +01:00
|
|
|
{
|
2014-11-27 20:57:38 +01:00
|
|
|
protected function getLexer(array $options = array()) {
|
|
|
|
return new Emulative($options);
|
2012-04-25 20:04:46 +02:00
|
|
|
}
|
|
|
|
|
2012-02-21 18:45:07 +01:00
|
|
|
/**
|
|
|
|
* @dataProvider provideTestReplaceKeywords
|
|
|
|
*/
|
|
|
|
public function testReplaceKeywords($keyword, $expectedToken) {
|
2014-11-27 20:57:38 +01:00
|
|
|
$lexer = $this->getLexer();
|
|
|
|
$lexer->startLexing('<?php ' . $keyword);
|
2012-02-21 18:45:07 +01:00
|
|
|
|
2014-11-27 20:57:38 +01:00
|
|
|
$this->assertSame($expectedToken, $lexer->getNextToken());
|
|
|
|
$this->assertSame(0, $lexer->getNextToken());
|
2012-02-21 18:45:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideTestReplaceKeywords
|
|
|
|
*/
|
|
|
|
public function testNoReplaceKeywordsAfterObjectOperator($keyword) {
|
2014-11-27 20:57:38 +01:00
|
|
|
$lexer = $this->getLexer();
|
|
|
|
$lexer->startLexing('<?php ->' . $keyword);
|
2012-02-21 18:45:07 +01:00
|
|
|
|
2014-11-27 20:57:38 +01:00
|
|
|
$this->assertSame(Parser::T_OBJECT_OPERATOR, $lexer->getNextToken());
|
|
|
|
$this->assertSame(Parser::T_STRING, $lexer->getNextToken());
|
|
|
|
$this->assertSame(0, $lexer->getNextToken());
|
2012-02-21 18:45:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideTestReplaceKeywords() {
|
|
|
|
return array(
|
2012-09-07 19:24:44 +02:00
|
|
|
// PHP 5.5
|
2014-02-06 14:44:16 +01:00
|
|
|
array('finally', Parser::T_FINALLY),
|
|
|
|
array('yield', Parser::T_YIELD),
|
2012-09-07 19:24:44 +02:00
|
|
|
|
|
|
|
// PHP 5.4
|
2014-02-06 14:44:16 +01:00
|
|
|
array('callable', Parser::T_CALLABLE),
|
|
|
|
array('insteadof', Parser::T_INSTEADOF),
|
|
|
|
array('trait', Parser::T_TRAIT),
|
|
|
|
array('__TRAIT__', Parser::T_TRAIT_C),
|
2012-09-07 19:24:44 +02:00
|
|
|
|
|
|
|
// PHP 5.3
|
2014-02-06 14:44:16 +01:00
|
|
|
array('__DIR__', Parser::T_DIR),
|
|
|
|
array('goto', Parser::T_GOTO),
|
|
|
|
array('namespace', Parser::T_NAMESPACE),
|
|
|
|
array('__NAMESPACE__', Parser::T_NS_C),
|
2012-02-21 18:45:07 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideTestLexNewFeatures
|
|
|
|
*/
|
|
|
|
public function testLexNewFeatures($code, array $expectedTokens) {
|
2014-11-27 20:57:38 +01:00
|
|
|
$lexer = $this->getLexer();
|
|
|
|
$lexer->startLexing('<?php ' . $code);
|
2012-02-21 18:45:07 +01:00
|
|
|
|
|
|
|
foreach ($expectedTokens as $expectedToken) {
|
|
|
|
list($expectedTokenType, $expectedTokenText) = $expectedToken;
|
2014-11-27 20:57:38 +01:00
|
|
|
$this->assertSame($expectedTokenType, $lexer->getNextToken($text));
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame($expectedTokenText, $text);
|
2012-02-21 18:45:07 +01:00
|
|
|
}
|
2014-11-27 20:57:38 +01:00
|
|
|
$this->assertSame(0, $lexer->getNextToken());
|
2012-02-21 18:45:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideTestLexNewFeatures
|
|
|
|
*/
|
|
|
|
public function testLeaveStuffAloneInStrings($code) {
|
|
|
|
$stringifiedToken = '"' . addcslashes($code, '"\\') . '"';
|
|
|
|
|
2014-11-27 20:57:38 +01:00
|
|
|
$lexer = $this->getLexer();
|
|
|
|
$lexer->startLexing('<?php ' . $stringifiedToken);
|
|
|
|
|
|
|
|
$this->assertSame(Parser::T_CONSTANT_ENCAPSED_STRING, $lexer->getNextToken($text));
|
2014-09-30 20:38:09 +02:00
|
|
|
$this->assertSame($stringifiedToken, $text);
|
2014-11-27 20:57:38 +01:00
|
|
|
$this->assertSame(0, $lexer->getNextToken());
|
2012-02-21 18:45:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function provideTestLexNewFeatures() {
|
|
|
|
return array(
|
2015-04-26 11:46:23 +02:00
|
|
|
array('yield from', array(
|
|
|
|
array(Parser::T_YIELD_FROM, 'yield from'),
|
|
|
|
)),
|
|
|
|
array("yield\r\nfrom", array(
|
|
|
|
array(Parser::T_YIELD_FROM, "yield\r\nfrom"),
|
|
|
|
)),
|
2014-03-26 18:23:30 +01:00
|
|
|
array('...', array(
|
|
|
|
array(Parser::T_ELLIPSIS, '...'),
|
|
|
|
)),
|
2014-03-26 19:18:16 +01:00
|
|
|
array('**', array(
|
|
|
|
array(Parser::T_POW, '**'),
|
|
|
|
)),
|
|
|
|
array('**=', array(
|
|
|
|
array(Parser::T_POW_EQUAL, '**='),
|
|
|
|
)),
|
2015-03-12 22:45:38 +01:00
|
|
|
array('??', array(
|
|
|
|
array(Parser::T_COALESCE, '??'),
|
|
|
|
)),
|
2015-03-12 23:13:31 +01:00
|
|
|
array('<=>', array(
|
|
|
|
array(Parser::T_SPACESHIP, '<=>'),
|
|
|
|
)),
|
2012-02-21 18:45:07 +01:00
|
|
|
array('0b1010110', array(
|
2014-02-06 14:44:16 +01:00
|
|
|
array(Parser::T_LNUMBER, '0b1010110'),
|
2012-02-21 18:45:07 +01:00
|
|
|
)),
|
2012-06-08 18:09:42 +02:00
|
|
|
array('0b1011010101001010110101010010101011010101010101101011001110111100', array(
|
2014-02-06 14:44:16 +01:00
|
|
|
array(Parser::T_DNUMBER, '0b1011010101001010110101010010101011010101010101101011001110111100'),
|
2012-02-21 18:45:07 +01:00
|
|
|
)),
|
|
|
|
array('\\', array(
|
2014-02-06 14:44:16 +01:00
|
|
|
array(Parser::T_NS_SEPARATOR, '\\'),
|
2012-02-21 18:45:07 +01:00
|
|
|
)),
|
|
|
|
array("<<<'NOWDOC'\nNOWDOC;\n", array(
|
2014-02-06 14:44:16 +01:00
|
|
|
array(Parser::T_START_HEREDOC, "<<<'NOWDOC'\n"),
|
|
|
|
array(Parser::T_END_HEREDOC, 'NOWDOC'),
|
2012-02-21 18:45:07 +01:00
|
|
|
array(ord(';'), ';'),
|
|
|
|
)),
|
|
|
|
array("<<<'NOWDOC'\nFoobar\nNOWDOC;\n", array(
|
2014-02-06 14:44:16 +01:00
|
|
|
array(Parser::T_START_HEREDOC, "<<<'NOWDOC'\n"),
|
|
|
|
array(Parser::T_ENCAPSED_AND_WHITESPACE, "Foobar\n"),
|
|
|
|
array(Parser::T_END_HEREDOC, 'NOWDOC'),
|
2012-02-21 18:45:07 +01:00
|
|
|
array(ord(';'), ';'),
|
|
|
|
)),
|
|
|
|
);
|
|
|
|
}
|
2014-11-27 20:57:38 +01:00
|
|
|
}
|