2017-11-02 18:56:01 +01:00
|
|
|
<?php declare(strict_types=1);
|
2012-02-21 18:45:07 +01:00
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser\Lexer;
|
|
|
|
|
2018-04-28 22:14:16 +02:00
|
|
|
use PhpParser\ErrorHandler;
|
2020-08-01 22:34:36 +02:00
|
|
|
use PhpParser\Lexer;
|
2014-11-27 20:57:38 +01:00
|
|
|
use PhpParser\LexerTest;
|
2015-06-13 18:39:55 +02:00
|
|
|
use PhpParser\Parser\Tokens;
|
2014-02-06 14:44:16 +01:00
|
|
|
|
2014-11-27 20:57:38 +01:00
|
|
|
class EmulativeTest extends LexerTest
|
2012-02-21 18:45:07 +01:00
|
|
|
{
|
2017-08-13 14:35:03 +02:00
|
|
|
protected function getLexer(array $options = []) {
|
2014-11-27 20:57:38 +01:00
|
|
|
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
|
|
|
|
*/
|
2019-05-09 14:17:28 +02:00
|
|
|
public function testNoReplaceKeywordsAfterObjectOperator(string $keyword) {
|
2014-11-27 20:57:38 +01:00
|
|
|
$lexer = $this->getLexer();
|
|
|
|
$lexer->startLexing('<?php ->' . $keyword);
|
2012-02-21 18:45:07 +01:00
|
|
|
|
2015-06-13 18:39:55 +02:00
|
|
|
$this->assertSame(Tokens::T_OBJECT_OPERATOR, $lexer->getNextToken());
|
|
|
|
$this->assertSame(Tokens::T_STRING, $lexer->getNextToken());
|
2014-11-27 20:57:38 +01:00
|
|
|
$this->assertSame(0, $lexer->getNextToken());
|
2012-02-21 18:45:07 +01:00
|
|
|
}
|
|
|
|
|
2019-05-09 14:17:28 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider provideTestReplaceKeywords
|
|
|
|
*/
|
|
|
|
public function testNoReplaceKeywordsAfterObjectOperatorWithSpaces(string $keyword) {
|
|
|
|
$lexer = $this->getLexer();
|
|
|
|
$lexer->startLexing('<?php -> ' . $keyword);
|
|
|
|
|
|
|
|
$this->assertSame(Tokens::T_OBJECT_OPERATOR, $lexer->getNextToken());
|
|
|
|
$this->assertSame(Tokens::T_STRING, $lexer->getNextToken());
|
|
|
|
$this->assertSame(0, $lexer->getNextToken());
|
|
|
|
}
|
|
|
|
|
2020-08-02 10:30:44 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider provideTestReplaceKeywords
|
|
|
|
*/
|
|
|
|
public function testNoReplaceKeywordsAfterNullsafeObjectOperator(string $keyword) {
|
|
|
|
$lexer = $this->getLexer();
|
|
|
|
$lexer->startLexing('<?php ?->' . $keyword);
|
|
|
|
|
|
|
|
$this->assertSame(Tokens::T_NULLSAFE_OBJECT_OPERATOR, $lexer->getNextToken());
|
|
|
|
$this->assertSame(Tokens::T_STRING, $lexer->getNextToken());
|
|
|
|
$this->assertSame(0, $lexer->getNextToken());
|
|
|
|
}
|
|
|
|
|
2012-02-21 18:45:07 +01:00
|
|
|
public function provideTestReplaceKeywords() {
|
2017-08-13 14:35:03 +02:00
|
|
|
return [
|
2020-08-01 22:34:36 +02:00
|
|
|
// PHP 8.0
|
|
|
|
['match', Tokens::T_MATCH],
|
|
|
|
|
2019-05-09 14:17:28 +02:00
|
|
|
// PHP 7.4
|
|
|
|
['fn', Tokens::T_FN],
|
|
|
|
|
2012-09-07 19:24:44 +02:00
|
|
|
// PHP 5.5
|
2017-08-13 14:35:03 +02:00
|
|
|
['finally', Tokens::T_FINALLY],
|
|
|
|
['yield', Tokens::T_YIELD],
|
2012-09-07 19:24:44 +02:00
|
|
|
|
|
|
|
// PHP 5.4
|
2017-08-13 14:35:03 +02:00
|
|
|
['callable', Tokens::T_CALLABLE],
|
|
|
|
['insteadof', Tokens::T_INSTEADOF],
|
|
|
|
['trait', Tokens::T_TRAIT],
|
|
|
|
['__TRAIT__', Tokens::T_TRAIT_C],
|
2012-09-07 19:24:44 +02:00
|
|
|
|
|
|
|
// PHP 5.3
|
2017-08-13 14:35:03 +02:00
|
|
|
['__DIR__', Tokens::T_DIR],
|
|
|
|
['goto', Tokens::T_GOTO],
|
|
|
|
['namespace', Tokens::T_NAMESPACE],
|
|
|
|
['__NAMESPACE__', Tokens::T_NS_C],
|
|
|
|
];
|
2012-02-21 18:45:07 +01:00
|
|
|
}
|
|
|
|
|
2020-08-01 22:34:36 +02:00
|
|
|
private function assertSameTokens(array $expectedTokens, Lexer $lexer) {
|
|
|
|
$tokens = [];
|
|
|
|
while (0 !== $token = $lexer->getNextToken($text)) {
|
|
|
|
$tokens[] = [$token, $text];
|
|
|
|
}
|
|
|
|
$this->assertSame($expectedTokens, $tokens);
|
|
|
|
}
|
|
|
|
|
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);
|
2020-08-01 22:34:36 +02:00
|
|
|
$this->assertSameTokens($expectedTokens, $lexer);
|
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);
|
|
|
|
|
2015-06-13 18:39:55 +02:00
|
|
|
$this->assertSame(Tokens::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
|
|
|
}
|
|
|
|
|
2018-04-28 22:14:16 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider provideTestLexNewFeatures
|
|
|
|
*/
|
|
|
|
public function testErrorAfterEmulation($code) {
|
|
|
|
$errorHandler = new ErrorHandler\Collecting;
|
2019-05-09 14:17:28 +02:00
|
|
|
$lexer = $this->getLexer();
|
2018-04-28 22:14:16 +02:00
|
|
|
$lexer->startLexing('<?php ' . $code . "\0", $errorHandler);
|
|
|
|
|
|
|
|
$errors = $errorHandler->getErrors();
|
|
|
|
$this->assertCount(1, $errors);
|
|
|
|
|
|
|
|
$error = $errors[0];
|
|
|
|
$this->assertSame('Unexpected null byte', $error->getRawMessage());
|
|
|
|
|
|
|
|
$attrs = $error->getAttributes();
|
|
|
|
$expPos = strlen('<?php ' . $code);
|
|
|
|
$expLine = 1 + substr_count('<?php ' . $code, "\n");
|
|
|
|
$this->assertSame($expPos, $attrs['startFilePos']);
|
|
|
|
$this->assertSame($expPos, $attrs['endFilePos']);
|
|
|
|
$this->assertSame($expLine, $attrs['startLine']);
|
|
|
|
$this->assertSame($expLine, $attrs['endLine']);
|
|
|
|
}
|
|
|
|
|
2012-02-21 18:45:07 +01:00
|
|
|
public function provideTestLexNewFeatures() {
|
2017-08-13 14:35:03 +02:00
|
|
|
return [
|
|
|
|
['yield from', [
|
|
|
|
[Tokens::T_YIELD_FROM, 'yield from'],
|
|
|
|
]],
|
|
|
|
["yield\r\nfrom", [
|
|
|
|
[Tokens::T_YIELD_FROM, "yield\r\nfrom"],
|
|
|
|
]],
|
|
|
|
['...', [
|
|
|
|
[Tokens::T_ELLIPSIS, '...'],
|
|
|
|
]],
|
|
|
|
['**', [
|
|
|
|
[Tokens::T_POW, '**'],
|
|
|
|
]],
|
|
|
|
['**=', [
|
|
|
|
[Tokens::T_POW_EQUAL, '**='],
|
|
|
|
]],
|
|
|
|
['??', [
|
|
|
|
[Tokens::T_COALESCE, '??'],
|
|
|
|
]],
|
|
|
|
['<=>', [
|
|
|
|
[Tokens::T_SPACESHIP, '<=>'],
|
|
|
|
]],
|
|
|
|
['0b1010110', [
|
|
|
|
[Tokens::T_LNUMBER, '0b1010110'],
|
|
|
|
]],
|
|
|
|
['0b1011010101001010110101010010101011010101010101101011001110111100', [
|
|
|
|
[Tokens::T_DNUMBER, '0b1011010101001010110101010010101011010101010101101011001110111100'],
|
|
|
|
]],
|
|
|
|
['\\', [
|
|
|
|
[Tokens::T_NS_SEPARATOR, '\\'],
|
|
|
|
]],
|
|
|
|
["<<<'NOWDOC'\nNOWDOC;\n", [
|
|
|
|
[Tokens::T_START_HEREDOC, "<<<'NOWDOC'\n"],
|
|
|
|
[Tokens::T_END_HEREDOC, 'NOWDOC'],
|
|
|
|
[ord(';'), ';'],
|
|
|
|
]],
|
|
|
|
["<<<'NOWDOC'\nFoobar\nNOWDOC;\n", [
|
|
|
|
[Tokens::T_START_HEREDOC, "<<<'NOWDOC'\n"],
|
|
|
|
[Tokens::T_ENCAPSED_AND_WHITESPACE, "Foobar\n"],
|
|
|
|
[Tokens::T_END_HEREDOC, 'NOWDOC'],
|
|
|
|
[ord(';'), ';'],
|
|
|
|
]],
|
2018-04-28 22:14:16 +02:00
|
|
|
|
2019-06-30 12:13:28 +02:00
|
|
|
// PHP 7.3: Flexible heredoc/nowdoc
|
2018-04-28 22:14:16 +02:00
|
|
|
["<<<LABEL\nLABEL,", [
|
|
|
|
[Tokens::T_START_HEREDOC, "<<<LABEL\n"],
|
|
|
|
[Tokens::T_END_HEREDOC, "LABEL"],
|
|
|
|
[ord(','), ','],
|
|
|
|
]],
|
|
|
|
["<<<LABEL\n LABEL,", [
|
|
|
|
[Tokens::T_START_HEREDOC, "<<<LABEL\n"],
|
|
|
|
[Tokens::T_END_HEREDOC, " LABEL"],
|
|
|
|
[ord(','), ','],
|
|
|
|
]],
|
|
|
|
["<<<LABEL\n Foo\n LABEL;", [
|
|
|
|
[Tokens::T_START_HEREDOC, "<<<LABEL\n"],
|
|
|
|
[Tokens::T_ENCAPSED_AND_WHITESPACE, " Foo\n"],
|
|
|
|
[Tokens::T_END_HEREDOC, " LABEL"],
|
|
|
|
[ord(';'), ';'],
|
|
|
|
]],
|
|
|
|
["<<<A\n A,<<<A\n A,", [
|
|
|
|
[Tokens::T_START_HEREDOC, "<<<A\n"],
|
|
|
|
[Tokens::T_END_HEREDOC, " A"],
|
|
|
|
[ord(','), ','],
|
|
|
|
[Tokens::T_START_HEREDOC, "<<<A\n"],
|
|
|
|
[Tokens::T_END_HEREDOC, " A"],
|
|
|
|
[ord(','), ','],
|
|
|
|
]],
|
2018-09-21 11:44:39 +02:00
|
|
|
["<<<LABEL\nLABELNOPE\nLABEL\n", [
|
|
|
|
[Tokens::T_START_HEREDOC, "<<<LABEL\n"],
|
|
|
|
[Tokens::T_ENCAPSED_AND_WHITESPACE, "LABELNOPE\n"],
|
|
|
|
[Tokens::T_END_HEREDOC, "LABEL"],
|
|
|
|
]],
|
2018-04-28 22:14:16 +02:00
|
|
|
// Interpretation changed
|
|
|
|
["<<<LABEL\n LABEL\nLABEL\n", [
|
|
|
|
[Tokens::T_START_HEREDOC, "<<<LABEL\n"],
|
|
|
|
[Tokens::T_END_HEREDOC, " LABEL"],
|
|
|
|
[Tokens::T_STRING, "LABEL"],
|
|
|
|
]],
|
2019-06-30 12:13:28 +02:00
|
|
|
|
|
|
|
// PHP 7.4: Null coalesce equal
|
|
|
|
['??=', [
|
|
|
|
[Tokens::T_COALESCE_EQUAL, '??='],
|
|
|
|
]],
|
|
|
|
|
|
|
|
// PHP 7.4: Number literal separator
|
|
|
|
['1_000', [
|
|
|
|
[Tokens::T_LNUMBER, '1_000'],
|
|
|
|
]],
|
|
|
|
['0xCAFE_F00D', [
|
|
|
|
[Tokens::T_LNUMBER, '0xCAFE_F00D'],
|
|
|
|
]],
|
|
|
|
['0b0101_1111', [
|
|
|
|
[Tokens::T_LNUMBER, '0b0101_1111'],
|
|
|
|
]],
|
|
|
|
['0137_041', [
|
|
|
|
[Tokens::T_LNUMBER, '0137_041'],
|
|
|
|
]],
|
|
|
|
['1_000.0', [
|
|
|
|
[Tokens::T_DNUMBER, '1_000.0'],
|
|
|
|
]],
|
|
|
|
['1_0.0', [
|
|
|
|
[Tokens::T_DNUMBER, '1_0.0']
|
|
|
|
]],
|
|
|
|
['1_000_000_000.0', [
|
|
|
|
[Tokens::T_DNUMBER, '1_000_000_000.0']
|
|
|
|
]],
|
|
|
|
['0e1_0', [
|
|
|
|
[Tokens::T_DNUMBER, '0e1_0']
|
|
|
|
]],
|
|
|
|
['1_0e+10', [
|
|
|
|
[Tokens::T_DNUMBER, '1_0e+10']
|
|
|
|
]],
|
|
|
|
['1_0e-10', [
|
|
|
|
[Tokens::T_DNUMBER, '1_0e-10']
|
|
|
|
]],
|
|
|
|
['0b1011010101001010_110101010010_10101101010101_0101101011001_110111100', [
|
|
|
|
[Tokens::T_DNUMBER, '0b1011010101001010_110101010010_10101101010101_0101101011001_110111100'],
|
|
|
|
]],
|
|
|
|
['0xFFFF_FFFF_FFFF_FFFF', [
|
|
|
|
[Tokens::T_DNUMBER, '0xFFFF_FFFF_FFFF_FFFF'],
|
|
|
|
]],
|
|
|
|
['1_000+1', [
|
|
|
|
[Tokens::T_LNUMBER, '1_000'],
|
|
|
|
[ord('+'), '+'],
|
|
|
|
[Tokens::T_LNUMBER, '1'],
|
|
|
|
]],
|
|
|
|
['1_0abc', [
|
|
|
|
[Tokens::T_LNUMBER, '1_0'],
|
|
|
|
[Tokens::T_STRING, 'abc'],
|
|
|
|
]],
|
2020-08-02 10:30:44 +02:00
|
|
|
['?->', [
|
|
|
|
[Tokens::T_NULLSAFE_OBJECT_OPERATOR, '?->'],
|
|
|
|
]],
|
2020-09-06 17:04:33 +02:00
|
|
|
['#[Attr]', [
|
|
|
|
[Tokens::T_ATTRIBUTE, '#['],
|
|
|
|
[Tokens::T_STRING, 'Attr'],
|
|
|
|
[ord(']'), ']'],
|
|
|
|
]],
|
|
|
|
["#[\nAttr\n]", [
|
|
|
|
[Tokens::T_ATTRIBUTE, '#['],
|
|
|
|
[Tokens::T_STRING, 'Attr'],
|
|
|
|
[ord(']'), ']'],
|
|
|
|
]],
|
|
|
|
// Test interaction of two patch-based emulators
|
|
|
|
["<<<LABEL\n LABEL, #[Attr]", [
|
|
|
|
[Tokens::T_START_HEREDOC, "<<<LABEL\n"],
|
|
|
|
[Tokens::T_END_HEREDOC, " LABEL"],
|
|
|
|
[ord(','), ','],
|
|
|
|
[Tokens::T_ATTRIBUTE, '#['],
|
|
|
|
[Tokens::T_STRING, 'Attr'],
|
|
|
|
[ord(']'), ']'],
|
|
|
|
]],
|
|
|
|
["#[Attr] <<<LABEL\n LABEL,", [
|
|
|
|
[Tokens::T_ATTRIBUTE, '#['],
|
|
|
|
[Tokens::T_STRING, 'Attr'],
|
|
|
|
[ord(']'), ']'],
|
|
|
|
[Tokens::T_START_HEREDOC, "<<<LABEL\n"],
|
|
|
|
[Tokens::T_END_HEREDOC, " LABEL"],
|
|
|
|
[ord(','), ','],
|
|
|
|
]],
|
2017-08-13 14:35:03 +02:00
|
|
|
];
|
2012-02-21 18:45:07 +01:00
|
|
|
}
|
2020-08-01 22:34:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideTestTargetVersion
|
|
|
|
*/
|
|
|
|
public function testTargetVersion(string $phpVersion, string $code, array $expectedTokens) {
|
|
|
|
$lexer = $this->getLexer(['phpVersion' => $phpVersion]);
|
|
|
|
$lexer->startLexing('<?php ' . $code);
|
|
|
|
$this->assertSameTokens($expectedTokens, $lexer);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideTestTargetVersion() {
|
|
|
|
return [
|
|
|
|
['8.0', 'match', [[Tokens::T_MATCH, 'match']]],
|
|
|
|
['7.4', 'match', [[Tokens::T_STRING, 'match']]],
|
|
|
|
['7.4', 'fn', [[Tokens::T_FN, 'fn']]],
|
|
|
|
['7.3', 'fn', [[Tokens::T_STRING, 'fn']]],
|
|
|
|
];
|
|
|
|
}
|
2014-11-27 20:57:38 +01:00
|
|
|
}
|