2011-12-18 13:04:27 +01:00
|
|
|
<?php
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser\Lexer;
|
|
|
|
|
2015-06-13 18:39:55 +02:00
|
|
|
use PhpParser\Parser\Tokens;
|
2014-02-06 14:44:16 +01:00
|
|
|
|
2011-12-18 13:04:27 +01:00
|
|
|
/**
|
|
|
|
* ATTENTION: This code is WRITE-ONLY. Do not try to read it.
|
|
|
|
*/
|
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_ELLIPSIS = 1001;
|
|
|
|
const T_POW = 1002;
|
|
|
|
const T_POW_EQUAL = 1003;
|
|
|
|
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-08-31 16:33:41 +02:00
|
|
|
const PHP_5_6 = '5.6.0rc1';
|
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;
|
2015-03-12 22:45:38 +01:00
|
|
|
|
|
|
|
if (version_compare(PHP_VERSION, self::PHP_5_6, '>=')) {
|
|
|
|
return;
|
|
|
|
}
|
2015-06-13 18:39:55 +02:00
|
|
|
$this->tokenMap[self::T_ELLIPSIS] = Tokens::T_ELLIPSIS;
|
|
|
|
$this->tokenMap[self::T_POW] = Tokens::T_POW;
|
|
|
|
$this->tokenMap[self::T_POW_EQUAL] = Tokens::T_POW_EQUAL;
|
2012-04-25 20:04:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function startLexing($code) {
|
2011-12-18 13:04:27 +01:00
|
|
|
$this->inObjectAccess = false;
|
|
|
|
|
2014-03-26 23:50:15 +01:00
|
|
|
$preprocessedCode = $this->preprocessCode($code);
|
|
|
|
parent::startLexing($preprocessedCode);
|
|
|
|
if ($preprocessedCode !== $code) {
|
2012-02-21 17:56:07 +01:00
|
|
|
$this->postprocessTokens();
|
2011-12-18 13:04:27 +01:00
|
|
|
}
|
2014-11-27 20:57:38 +01:00
|
|
|
|
|
|
|
// Set code property back to the original code, so __halt_compiler()
|
|
|
|
// handling and (start|end)FilePos attributes use the correct offsets
|
|
|
|
$this->code = $code;
|
2012-02-21 17:56:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Replaces new features in the code by ~__EMU__{NAME}__{DATA}__~ sequences.
|
|
|
|
* ~LABEL~ is never valid PHP code, that's why we can (to some degree) safely
|
|
|
|
* use it here.
|
|
|
|
* Later when preprocessing the tokens these sequences will either be replaced
|
2014-03-26 23:50:15 +01:00
|
|
|
* by real tokens or replaced with their original content (e.g. if they occurred
|
2012-02-21 17:56:07 +01:00
|
|
|
* inside a string, i.e. a place where they don't have a special meaning).
|
|
|
|
*/
|
|
|
|
protected function preprocessCode($code) {
|
2015-03-12 22:45:38 +01:00
|
|
|
if (version_compare(PHP_VERSION, self::PHP_7_0, '>=')) {
|
|
|
|
return $code;
|
|
|
|
}
|
|
|
|
|
|
|
|
$code = str_replace('??', '~__EMU__COALESCE__~', $code);
|
2015-03-12 23:13:31 +01:00
|
|
|
$code = str_replace('<=>', '~__EMU__SPACESHIP__~', $code);
|
2015-04-26 11:46:23 +02:00
|
|
|
$code = preg_replace_callback('(yield[ \n\r\t]+from)', function($matches) {
|
|
|
|
// Encoding $0 in order to preserve exact whitespace
|
|
|
|
return '~__EMU__YIELDFROM__' . bin2hex($matches[0]) . '__~';
|
|
|
|
}, $code);
|
2015-03-12 22:45:38 +01:00
|
|
|
|
2014-03-26 23:50:15 +01:00
|
|
|
if (version_compare(PHP_VERSION, self::PHP_5_6, '>=')) {
|
|
|
|
return $code;
|
|
|
|
}
|
|
|
|
|
2014-03-26 18:23:30 +01:00
|
|
|
$code = str_replace('...', '~__EMU__ELLIPSIS__~', $code);
|
2014-03-26 19:18:16 +01:00
|
|
|
$code = preg_replace('((?<!/)\*\*=)', '~__EMU__POWEQUAL__~', $code);
|
2014-03-26 23:41:06 +01:00
|
|
|
$code = preg_replace('((?<!/)\*\*(?!/))', '~__EMU__POW__~', $code);
|
2014-03-26 18:23:30 +01:00
|
|
|
|
2015-05-02 22:13:55 +02:00
|
|
|
return $code;
|
2012-02-21 17:56:07 +01:00
|
|
|
}
|
2011-12-18 13:04:27 +01:00
|
|
|
|
2012-02-21 17:56:07 +01:00
|
|
|
/*
|
|
|
|
* Replaces the ~__EMU__...~ sequences with real tokens or their original
|
|
|
|
* value.
|
|
|
|
*/
|
|
|
|
protected function postprocessTokens() {
|
|
|
|
// we need to manually iterate and manage a count because we'll change
|
|
|
|
// the tokens array on the way
|
2011-12-18 13:04:27 +01:00
|
|
|
for ($i = 0, $c = count($this->tokens); $i < $c; ++$i) {
|
2014-02-05 22:37:07 +01:00
|
|
|
// first check that the following tokens are of form ~LABEL~,
|
2012-02-21 17:56:07 +01:00
|
|
|
// then match the __EMU__... sequence.
|
2011-12-18 13:04:27 +01:00
|
|
|
if ('~' === $this->tokens[$i]
|
|
|
|
&& isset($this->tokens[$i + 2])
|
|
|
|
&& '~' === $this->tokens[$i + 2]
|
|
|
|
&& T_STRING === $this->tokens[$i + 1][0]
|
|
|
|
&& preg_match('(^__EMU__([A-Z]++)__(?:([A-Za-z0-9]++)__)?$)', $this->tokens[$i + 1][1], $matches)
|
|
|
|
) {
|
2015-05-02 22:13:55 +02:00
|
|
|
if ('ELLIPSIS' === $matches[1]) {
|
2014-03-26 18:23:30 +01:00
|
|
|
$replace = array(
|
|
|
|
array(self::T_ELLIPSIS, '...', $this->tokens[$i + 1][2])
|
|
|
|
);
|
2014-03-26 19:18:16 +01:00
|
|
|
} else if ('POW' === $matches[1]) {
|
|
|
|
$replace = array(
|
|
|
|
array(self::T_POW, '**', $this->tokens[$i + 1][2])
|
|
|
|
);
|
|
|
|
} else if ('POWEQUAL' === $matches[1]) {
|
|
|
|
$replace = array(
|
|
|
|
array(self::T_POW_EQUAL, '**=', $this->tokens[$i + 1][2])
|
|
|
|
);
|
2015-03-12 22:45:38 +01:00
|
|
|
} else if ('COALESCE' === $matches[1]) {
|
|
|
|
$replace = array(
|
|
|
|
array(self::T_COALESCE, '??', $this->tokens[$i + 1][2])
|
|
|
|
);
|
2015-03-12 23:13:31 +01:00
|
|
|
} else if ('SPACESHIP' === $matches[1]) {
|
|
|
|
$replace = array(
|
|
|
|
array(self::T_SPACESHIP, '<=>', $this->tokens[$i + 1][2]),
|
|
|
|
);
|
2015-04-26 11:46:23 +02:00
|
|
|
} else if ('YIELDFROM' === $matches[1]) {
|
2015-05-02 22:13:55 +02:00
|
|
|
$content = hex2bin($matches[2]);
|
2015-04-26 11:46:23 +02:00
|
|
|
$replace = array(
|
|
|
|
array(self::T_YIELD_FROM, $content, $this->tokens[$i + 1][2] - substr_count($content, "\n"))
|
|
|
|
);
|
2011-12-18 13:04:27 +01:00
|
|
|
} else {
|
2015-04-26 11:46:23 +02:00
|
|
|
throw new \RuntimeException('Invalid __EMU__ sequence');
|
2011-12-18 13:04:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
array_splice($this->tokens, $i, 3, $replace);
|
|
|
|
$c -= 3 - count($replace);
|
2012-02-21 17:56:07 +01:00
|
|
|
// for multichar tokens (e.g. strings) replace any ~__EMU__...~ sequences
|
|
|
|
// in their content with the original character sequence
|
2011-12-18 13:04:27 +01:00
|
|
|
} elseif (is_array($this->tokens[$i])
|
|
|
|
&& 0 !== strpos($this->tokens[$i][1], '__EMU__')
|
|
|
|
) {
|
|
|
|
$this->tokens[$i][1] = preg_replace_callback(
|
|
|
|
'(~__EMU__([A-Z]++)__(?:([A-Za-z0-9]++)__)?~)',
|
|
|
|
array($this, 'restoreContentCallback'),
|
|
|
|
$this->tokens[$i][1]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-21 17:56:07 +01:00
|
|
|
/*
|
|
|
|
* This method is a callback for restoring EMU sequences in
|
|
|
|
* multichar tokens (like strings) to their original value.
|
|
|
|
*/
|
2011-12-18 13:04:27 +01:00
|
|
|
public function restoreContentCallback(array $matches) {
|
2015-05-02 22:13:55 +02:00
|
|
|
if ('ELLIPSIS' === $matches[1]) {
|
2014-03-26 18:23:30 +01:00
|
|
|
return '...';
|
2014-03-26 19:18:16 +01:00
|
|
|
} else if ('POW' === $matches[1]) {
|
|
|
|
return '**';
|
|
|
|
} else if ('POWEQUAL' === $matches[1]) {
|
|
|
|
return '**=';
|
2015-03-12 22:45:38 +01:00
|
|
|
} else if ('COALESCE' === $matches[1]) {
|
|
|
|
return '??';
|
2015-03-12 23:13:31 +01:00
|
|
|
} else if ('SPACESHIP' === $matches[1]) {
|
|
|
|
return '<=>';
|
2015-04-26 11:46:23 +02:00
|
|
|
} else if ('YIELDFROM' === $matches[1]) {
|
2015-05-02 22:13:55 +02:00
|
|
|
return hex2bin($matches[2]);
|
2011-12-18 13:04:27 +01:00
|
|
|
} else {
|
|
|
|
return $matches[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-05 17:34:27 +02:00
|
|
|
public function getNextToken(&$value = null, &$startAttributes = null, &$endAttributes = null) {
|
|
|
|
$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;
|
|
|
|
}
|
2014-11-27 20:57:38 +01:00
|
|
|
}
|