2011-12-18 13:04:27 +01:00
|
|
|
<?php
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser\Lexer;
|
|
|
|
|
2016-10-09 13:15:24 +02:00
|
|
|
use PhpParser\ErrorHandler;
|
2015-06-13 18:39:55 +02:00
|
|
|
use PhpParser\Parser\Tokens;
|
2014-02-06 14:44:16 +01:00
|
|
|
|
|
|
|
class Emulative extends \PhpParser\Lexer
|
2011-12-18 13:04:27 +01:00
|
|
|
{
|
2012-04-25 20:04:46 +02:00
|
|
|
protected $newKeywords;
|
2011-12-18 13:04:27 +01:00
|
|
|
protected $inObjectAccess;
|
|
|
|
|
2015-04-26 11:46:23 +02:00
|
|
|
const T_COALESCE = 1004;
|
|
|
|
const T_SPACESHIP = 1005;
|
|
|
|
const T_YIELD_FROM = 1006;
|
2014-03-26 18:23:30 +01:00
|
|
|
|
2015-03-12 22:45:38 +01:00
|
|
|
const PHP_7_0 = '7.0.0dev';
|
2014-03-26 23:50:15 +01:00
|
|
|
|
2014-11-27 20:57:38 +01:00
|
|
|
public function __construct(array $options = array()) {
|
|
|
|
parent::__construct($options);
|
2012-04-25 20:04:46 +02:00
|
|
|
|
2012-09-07 18:06:11 +02:00
|
|
|
$newKeywordsPerVersion = array(
|
2016-07-05 22:29:41 +02:00
|
|
|
// No new keywords since PHP 5.5
|
2012-04-25 20:04:46 +02:00
|
|
|
);
|
|
|
|
|
2012-09-07 18:06:11 +02:00
|
|
|
$this->newKeywords = array();
|
|
|
|
foreach ($newKeywordsPerVersion as $version => $newKeywords) {
|
|
|
|
if (version_compare(PHP_VERSION, $version, '>=')) {
|
|
|
|
break;
|
|
|
|
}
|
2012-04-25 20:04:46 +02:00
|
|
|
|
2012-09-07 18:06:11 +02:00
|
|
|
$this->newKeywords += $newKeywords;
|
|
|
|
}
|
2014-03-26 18:23:30 +01:00
|
|
|
|
2015-03-12 22:45:38 +01:00
|
|
|
if (version_compare(PHP_VERSION, self::PHP_7_0, '>=')) {
|
|
|
|
return;
|
2014-03-26 18:23:30 +01:00
|
|
|
}
|
2015-06-13 18:39:55 +02:00
|
|
|
$this->tokenMap[self::T_COALESCE] = Tokens::T_COALESCE;
|
|
|
|
$this->tokenMap[self::T_SPACESHIP] = Tokens::T_SPACESHIP;
|
|
|
|
$this->tokenMap[self::T_YIELD_FROM] = Tokens::T_YIELD_FROM;
|
2012-04-25 20:04:46 +02:00
|
|
|
}
|
|
|
|
|
2016-10-09 13:15:24 +02:00
|
|
|
public function startLexing($code, ErrorHandler $errorHandler = null) {
|
2011-12-18 13:04:27 +01:00
|
|
|
$this->inObjectAccess = false;
|
|
|
|
|
2016-10-09 13:15:24 +02:00
|
|
|
parent::startLexing($code, $errorHandler);
|
2016-09-30 19:05:49 +02:00
|
|
|
if ($this->requiresEmulation($code)) {
|
|
|
|
$this->emulateTokens();
|
2011-12-18 13:04:27 +01:00
|
|
|
}
|
2012-02-21 17:56:07 +01:00
|
|
|
}
|
|
|
|
|
2017-01-24 08:38:55 +01:00
|
|
|
/**
|
2017-01-26 00:16:54 +01:00
|
|
|
* Checks if the code is potentially using features that require emulation.
|
|
|
|
*
|
|
|
|
* @param string $code Code to check
|
|
|
|
*
|
|
|
|
* @return bool
|
2017-01-24 08:38:55 +01:00
|
|
|
*/
|
2016-09-30 19:05:49 +02:00
|
|
|
protected function requiresEmulation($code) {
|
2015-03-12 22:45:38 +01:00
|
|
|
if (version_compare(PHP_VERSION, self::PHP_7_0, '>=')) {
|
2016-09-30 19:05:49 +02:00
|
|
|
return false;
|
2015-03-12 22:45:38 +01:00
|
|
|
}
|
|
|
|
|
2017-01-26 00:16:54 +01:00
|
|
|
return (bool) preg_match('(\?\?|<=>|yield[ \n\r\t]+from)', $code);
|
2012-02-21 17:56:07 +01:00
|
|
|
}
|
2011-12-18 13:04:27 +01:00
|
|
|
|
2017-01-26 00:16:54 +01:00
|
|
|
/**
|
2016-09-30 19:05:49 +02:00
|
|
|
* Emulates tokens for newer PHP versions.
|
2012-02-21 17:56:07 +01:00
|
|
|
*/
|
2016-09-30 19:05:49 +02:00
|
|
|
protected function emulateTokens() {
|
|
|
|
// We need to manually iterate and manage a count because we'll change
|
2012-02-21 17:56:07 +01:00
|
|
|
// the tokens array on the way
|
2016-09-30 19:05:49 +02:00
|
|
|
$line = 1;
|
2011-12-18 13:04:27 +01:00
|
|
|
for ($i = 0, $c = count($this->tokens); $i < $c; ++$i) {
|
2016-09-30 19:05:49 +02:00
|
|
|
if (isset($this->tokens[$i + 1])) {
|
|
|
|
if ($this->tokens[$i] === '?' && $this->tokens[$i + 1] === '?') {
|
|
|
|
array_splice($this->tokens, $i, 2, array(
|
|
|
|
array(self::T_COALESCE, '??', $line)
|
|
|
|
));
|
|
|
|
$c--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($this->tokens[$i][0] === T_IS_SMALLER_OR_EQUAL
|
|
|
|
&& $this->tokens[$i + 1] === '>'
|
|
|
|
) {
|
|
|
|
array_splice($this->tokens, $i, 2, array(
|
|
|
|
array(self::T_SPACESHIP, '<=>', $line)
|
|
|
|
));
|
|
|
|
$c--;
|
|
|
|
continue;
|
2011-12-18 13:04:27 +01:00
|
|
|
}
|
2016-09-30 19:05:49 +02:00
|
|
|
}
|
2011-12-18 13:04:27 +01:00
|
|
|
|
2016-09-30 19:05:49 +02:00
|
|
|
if (isset($this->tokens[$i + 2])) {
|
|
|
|
if ($this->tokens[$i][0] === T_YIELD && $this->tokens[$i + 1][0] === T_WHITESPACE
|
|
|
|
&& $this->tokens[$i + 2][0] === T_STRING
|
|
|
|
&& !strcasecmp($this->tokens[$i + 2][1], 'from')
|
|
|
|
) {
|
|
|
|
array_splice($this->tokens, $i, 3, array(
|
|
|
|
array(
|
|
|
|
self::T_YIELD_FROM,
|
|
|
|
$this->tokens[$i][1] . $this->tokens[$i + 1][1] . $this->tokens[$i + 2][1],
|
|
|
|
$line
|
|
|
|
)
|
|
|
|
));
|
|
|
|
$c -= 2;
|
|
|
|
$line += substr_count($this->tokens[$i][1], "\n");
|
|
|
|
continue;
|
|
|
|
}
|
2011-12-18 13:04:27 +01:00
|
|
|
}
|
|
|
|
|
2016-09-30 19:05:49 +02:00
|
|
|
if (\is_array($this->tokens[$i])) {
|
|
|
|
$line += substr_count($this->tokens[$i][1], "\n");
|
|
|
|
}
|
2011-12-18 13:04:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-19 20:58:45 +01:00
|
|
|
// This method is currently commented out, because there are no keywords to be emulated.
|
|
|
|
// Add it back once new keywords are added.
|
|
|
|
/*public function getNextToken(&$value = null, &$startAttributes = null, &$endAttributes = null) {
|
2012-05-05 17:34:27 +02:00
|
|
|
$token = parent::getNextToken($value, $startAttributes, $endAttributes);
|
2011-12-18 13:04:27 +01:00
|
|
|
|
2012-02-21 17:56:07 +01:00
|
|
|
// replace new keywords by their respective tokens. This is not done
|
|
|
|
// if we currently are in an object access (e.g. in $obj->namespace
|
|
|
|
// "namespace" stays a T_STRING tokens and isn't converted to T_NAMESPACE)
|
2015-06-13 18:39:55 +02:00
|
|
|
if (Tokens::T_STRING === $token && !$this->inObjectAccess) {
|
2012-04-25 20:04:46 +02:00
|
|
|
if (isset($this->newKeywords[strtolower($value)])) {
|
|
|
|
return $this->newKeywords[strtolower($value)];
|
2011-12-18 13:04:27 +01:00
|
|
|
}
|
|
|
|
} else {
|
2015-04-26 11:46:23 +02:00
|
|
|
// keep track of whether we currently are in an object access (after ->)
|
2015-06-13 18:39:55 +02:00
|
|
|
$this->inObjectAccess = Tokens::T_OBJECT_OPERATOR === $token;
|
2011-12-18 13:04:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $token;
|
2017-01-19 20:58:45 +01:00
|
|
|
}*/
|
2014-11-27 20:57:38 +01:00
|
|
|
}
|