3 namespace PhpParser\Lexer;
5 use PhpParser\ErrorHandler;
6 use PhpParser\Parser\Tokens;
8 class Emulative extends \PhpParser\Lexer
10 protected $newKeywords;
11 protected $inObjectAccess;
13 const T_ELLIPSIS = 1001;
15 const T_POW_EQUAL = 1003;
16 const T_COALESCE = 1004;
17 const T_SPACESHIP = 1005;
18 const T_YIELD_FROM = 1006;
20 const PHP_7_0 = '7.0.0dev';
21 const PHP_5_6 = '5.6.0rc1';
23 public function __construct(array $options = array()) {
24 parent::__construct($options);
26 $newKeywordsPerVersion = array(
27 // No new keywords since PHP 5.5
30 $this->newKeywords = array();
31 foreach ($newKeywordsPerVersion as $version => $newKeywords) {
32 if (version_compare(PHP_VERSION, $version, '>=')) {
36 $this->newKeywords += $newKeywords;
39 if (version_compare(PHP_VERSION, self::PHP_7_0, '>=')) {
42 $this->tokenMap[self::T_COALESCE] = Tokens::T_COALESCE;
43 $this->tokenMap[self::T_SPACESHIP] = Tokens::T_SPACESHIP;
44 $this->tokenMap[self::T_YIELD_FROM] = Tokens::T_YIELD_FROM;
46 if (version_compare(PHP_VERSION, self::PHP_5_6, '>=')) {
49 $this->tokenMap[self::T_ELLIPSIS] = Tokens::T_ELLIPSIS;
50 $this->tokenMap[self::T_POW] = Tokens::T_POW;
51 $this->tokenMap[self::T_POW_EQUAL] = Tokens::T_POW_EQUAL;
54 public function startLexing($code, ErrorHandler $errorHandler = null) {
55 $this->inObjectAccess = false;
57 parent::startLexing($code, $errorHandler);
58 if ($this->requiresEmulation($code)) {
59 $this->emulateTokens();
64 * Checks if the code is potentially using features that require emulation.
66 protected function requiresEmulation($code) {
67 if (version_compare(PHP_VERSION, self::PHP_7_0, '>=')) {
71 if (preg_match('(\?\?|<=>|yield[ \n\r\t]+from)', $code)) {
75 if (version_compare(PHP_VERSION, self::PHP_5_6, '>=')) {
79 return preg_match('(\.\.\.|(?<!/)\*\*(?!/))', $code);
83 * Emulates tokens for newer PHP versions.
85 protected function emulateTokens() {
86 // We need to manually iterate and manage a count because we'll change
87 // the tokens array on the way
89 for ($i = 0, $c = count($this->tokens); $i < $c; ++$i) {
91 if (isset($this->tokens[$i + 1])) {
92 if ($this->tokens[$i] === '?' && $this->tokens[$i + 1] === '?') {
93 array_splice($this->tokens, $i, 2, array(
94 array(self::T_COALESCE, '??', $line)
99 if ($this->tokens[$i][0] === T_IS_SMALLER_OR_EQUAL
100 && $this->tokens[$i + 1] === '>'
102 array_splice($this->tokens, $i, 2, array(
103 array(self::T_SPACESHIP, '<=>', $line)
108 if ($this->tokens[$i] === '*' && $this->tokens[$i + 1] === '*') {
109 array_splice($this->tokens, $i, 2, array(
110 array(self::T_POW, '**', $line)
115 if ($this->tokens[$i] === '*' && $this->tokens[$i + 1][0] === T_MUL_EQUAL) {
116 array_splice($this->tokens, $i, 2, array(
117 array(self::T_POW_EQUAL, '**=', $line)
124 if (isset($this->tokens[$i + 2])) {
125 if ($this->tokens[$i][0] === T_YIELD && $this->tokens[$i + 1][0] === T_WHITESPACE
126 && $this->tokens[$i + 2][0] === T_STRING
127 && !strcasecmp($this->tokens[$i + 2][1], 'from')
129 array_splice($this->tokens, $i, 3, array(
132 $this->tokens[$i][1] . $this->tokens[$i + 1][1] . $this->tokens[$i + 2][1],
137 $line += substr_count($this->tokens[$i][1], "\n");
140 if ($this->tokens[$i] === '.' && $this->tokens[$i + 1] === '.'
141 && $this->tokens[$i + 2] === '.'
143 array_splice($this->tokens, $i, 3, array(
144 array(self::T_ELLIPSIS, '...', $line)
151 if (\is_array($this->tokens[$i])) {
152 $line += substr_count($this->tokens[$i][1], "\n");
157 public function getNextToken(&$value = null, &$startAttributes = null, &$endAttributes = null) {
158 $token = parent::getNextToken($value, $startAttributes, $endAttributes);
160 // replace new keywords by their respective tokens. This is not done
161 // if we currently are in an object access (e.g. in $obj->namespace
162 // "namespace" stays a T_STRING tokens and isn't converted to T_NAMESPACE)
163 if (Tokens::T_STRING === $token && !$this->inObjectAccess) {
164 if (isset($this->newKeywords[strtolower($value)])) {
165 return $this->newKeywords[strtolower($value)];
168 // keep track of whether we currently are in an object access (after ->)
169 $this->inObjectAccess = Tokens::T_OBJECT_OPERATOR === $token;