1 | <?php |
||
10 | class Emulative extends \PhpParser\Lexer |
||
11 | { |
||
12 | protected $newKeywords; |
||
13 | protected $inObjectAccess; |
||
14 | |||
15 | const T_ELLIPSIS = 1001; |
||
16 | const T_POW = 1002; |
||
17 | const T_POW_EQUAL = 1003; |
||
18 | const T_COALESCE = 1004; |
||
19 | const T_SPACESHIP = 1005; |
||
20 | const T_YIELD_FROM = 1006; |
||
21 | |||
22 | const PHP_7_0 = '7.0.0dev'; |
||
23 | const PHP_5_6 = '5.6.0rc1'; |
||
24 | const PHP_5_5 = '5.5.0beta1'; |
||
25 | |||
26 | public function __construct(array $options = array()) { |
||
27 | parent::__construct($options); |
||
28 | |||
29 | $newKeywordsPerVersion = array( |
||
30 | self::PHP_5_5 => array( |
||
31 | 'finally' => Tokens::T_FINALLY, |
||
32 | 'yield' => Tokens::T_YIELD, |
||
33 | ), |
||
34 | ); |
||
35 | |||
36 | $this->newKeywords = array(); |
||
37 | foreach ($newKeywordsPerVersion as $version => $newKeywords) { |
||
38 | if (version_compare(PHP_VERSION, $version, '>=')) { |
||
39 | break; |
||
40 | } |
||
41 | |||
42 | $this->newKeywords += $newKeywords; |
||
43 | } |
||
44 | |||
45 | if (version_compare(PHP_VERSION, self::PHP_7_0, '>=')) { |
||
46 | return; |
||
47 | } |
||
48 | $this->tokenMap[self::T_COALESCE] = Tokens::T_COALESCE; |
||
49 | $this->tokenMap[self::T_SPACESHIP] = Tokens::T_SPACESHIP; |
||
50 | $this->tokenMap[self::T_YIELD_FROM] = Tokens::T_YIELD_FROM; |
||
51 | |||
52 | if (version_compare(PHP_VERSION, self::PHP_5_6, '>=')) { |
||
53 | return; |
||
54 | } |
||
55 | $this->tokenMap[self::T_ELLIPSIS] = Tokens::T_ELLIPSIS; |
||
56 | $this->tokenMap[self::T_POW] = Tokens::T_POW; |
||
57 | $this->tokenMap[self::T_POW_EQUAL] = Tokens::T_POW_EQUAL; |
||
58 | } |
||
59 | |||
60 | public function startLexing($code) { |
||
61 | $this->inObjectAccess = false; |
||
62 | |||
63 | $preprocessedCode = $this->preprocessCode($code); |
||
64 | parent::startLexing($preprocessedCode); |
||
65 | if ($preprocessedCode !== $code) { |
||
66 | $this->postprocessTokens(); |
||
67 | } |
||
68 | |||
69 | // Set code property back to the original code, so __halt_compiler() |
||
70 | // handling and (start|end)FilePos attributes use the correct offsets |
||
71 | $this->code = $code; |
||
72 | } |
||
73 | |||
74 | /* |
||
75 | * Replaces new features in the code by ~__EMU__{NAME}__{DATA}__~ sequences. |
||
76 | * ~LABEL~ is never valid PHP code, that's why we can (to some degree) safely |
||
77 | * use it here. |
||
78 | * Later when preprocessing the tokens these sequences will either be replaced |
||
79 | * by real tokens or replaced with their original content (e.g. if they occurred |
||
80 | * inside a string, i.e. a place where they don't have a special meaning). |
||
81 | */ |
||
82 | protected function preprocessCode($code) { |
||
104 | |||
105 | /* |
||
106 | * Replaces the ~__EMU__...~ sequences with real tokens or their original |
||
107 | * value. |
||
108 | */ |
||
109 | protected function postprocessTokens() { |
||
165 | |||
166 | /* |
||
167 | * This method is a callback for restoring EMU sequences in |
||
168 | * multichar tokens (like strings) to their original value. |
||
169 | */ |
||
170 | public function restoreContentCallback(array $matches) { |
||
187 | |||
188 | public function getNextToken(&$value = null, &$startAttributes = null, &$endAttributes = null) { |
||
189 | $token = parent::getNextToken($value, $startAttributes, $endAttributes); |
||
205 | } |
||
206 |