PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/backend-blogtamsu/vendor/nikic/php-parser/lib/PhpParser/Lexer/Emulative.php

https://gitlab.com/ntphuc/FoodyBackend
PHP | 205 lines | 148 code | 25 blank | 32 comment | 41 complexity | 905483559e90e63286443cd775c3da18 MD5 | raw file
  1. <?php
  2. namespace PhpParser\Lexer;
  3. use PhpParser\Parser\Tokens;
  4. /**
  5. * ATTENTION: This code is WRITE-ONLY. Do not try to read it.
  6. */
  7. class Emulative extends \PhpParser\Lexer
  8. {
  9. protected $newKeywords;
  10. protected $inObjectAccess;
  11. const T_ELLIPSIS = 1001;
  12. const T_POW = 1002;
  13. const T_POW_EQUAL = 1003;
  14. const T_COALESCE = 1004;
  15. const T_SPACESHIP = 1005;
  16. const T_YIELD_FROM = 1006;
  17. const PHP_7_0 = '7.0.0dev';
  18. const PHP_5_6 = '5.6.0rc1';
  19. const PHP_5_5 = '5.5.0beta1';
  20. public function __construct(array $options = array()) {
  21. parent::__construct($options);
  22. $newKeywordsPerVersion = array(
  23. self::PHP_5_5 => array(
  24. 'finally' => Tokens::T_FINALLY,
  25. 'yield' => Tokens::T_YIELD,
  26. ),
  27. );
  28. $this->newKeywords = array();
  29. foreach ($newKeywordsPerVersion as $version => $newKeywords) {
  30. if (version_compare(PHP_VERSION, $version, '>=')) {
  31. break;
  32. }
  33. $this->newKeywords += $newKeywords;
  34. }
  35. if (version_compare(PHP_VERSION, self::PHP_7_0, '>=')) {
  36. return;
  37. }
  38. $this->tokenMap[self::T_COALESCE] = Tokens::T_COALESCE;
  39. $this->tokenMap[self::T_SPACESHIP] = Tokens::T_SPACESHIP;
  40. $this->tokenMap[self::T_YIELD_FROM] = Tokens::T_YIELD_FROM;
  41. if (version_compare(PHP_VERSION, self::PHP_5_6, '>=')) {
  42. return;
  43. }
  44. $this->tokenMap[self::T_ELLIPSIS] = Tokens::T_ELLIPSIS;
  45. $this->tokenMap[self::T_POW] = Tokens::T_POW;
  46. $this->tokenMap[self::T_POW_EQUAL] = Tokens::T_POW_EQUAL;
  47. }
  48. public function startLexing($code) {
  49. $this->inObjectAccess = false;
  50. $preprocessedCode = $this->preprocessCode($code);
  51. parent::startLexing($preprocessedCode);
  52. if ($preprocessedCode !== $code) {
  53. $this->postprocessTokens();
  54. }
  55. // Set code property back to the original code, so __halt_compiler()
  56. // handling and (start|end)FilePos attributes use the correct offsets
  57. $this->code = $code;
  58. }
  59. /*
  60. * Replaces new features in the code by ~__EMU__{NAME}__{DATA}__~ sequences.
  61. * ~LABEL~ is never valid PHP code, that's why we can (to some degree) safely
  62. * use it here.
  63. * Later when preprocessing the tokens these sequences will either be replaced
  64. * by real tokens or replaced with their original content (e.g. if they occurred
  65. * inside a string, i.e. a place where they don't have a special meaning).
  66. */
  67. protected function preprocessCode($code) {
  68. if (version_compare(PHP_VERSION, self::PHP_7_0, '>=')) {
  69. return $code;
  70. }
  71. $code = str_replace('??', '~__EMU__COALESCE__~', $code);
  72. $code = str_replace('<=>', '~__EMU__SPACESHIP__~', $code);
  73. $code = preg_replace_callback('(yield[ \n\r\t]+from)', function($matches) {
  74. // Encoding $0 in order to preserve exact whitespace
  75. return '~__EMU__YIELDFROM__' . bin2hex($matches[0]) . '__~';
  76. }, $code);
  77. if (version_compare(PHP_VERSION, self::PHP_5_6, '>=')) {
  78. return $code;
  79. }
  80. $code = str_replace('...', '~__EMU__ELLIPSIS__~', $code);
  81. $code = preg_replace('((?<!/)\*\*=)', '~__EMU__POWEQUAL__~', $code);
  82. $code = preg_replace('((?<!/)\*\*(?!/))', '~__EMU__POW__~', $code);
  83. return $code;
  84. }
  85. /*
  86. * Replaces the ~__EMU__...~ sequences with real tokens or their original
  87. * value.
  88. */
  89. protected function postprocessTokens() {
  90. // we need to manually iterate and manage a count because we'll change
  91. // the tokens array on the way
  92. for ($i = 0, $c = count($this->tokens); $i < $c; ++$i) {
  93. // first check that the following tokens are of form ~LABEL~,
  94. // then match the __EMU__... sequence.
  95. if ('~' === $this->tokens[$i]
  96. && isset($this->tokens[$i + 2])
  97. && '~' === $this->tokens[$i + 2]
  98. && T_STRING === $this->tokens[$i + 1][0]
  99. && preg_match('(^__EMU__([A-Z]++)__(?:([A-Za-z0-9]++)__)?$)', $this->tokens[$i + 1][1], $matches)
  100. ) {
  101. if ('ELLIPSIS' === $matches[1]) {
  102. $replace = array(
  103. array(self::T_ELLIPSIS, '...', $this->tokens[$i + 1][2])
  104. );
  105. } else if ('POW' === $matches[1]) {
  106. $replace = array(
  107. array(self::T_POW, '**', $this->tokens[$i + 1][2])
  108. );
  109. } else if ('POWEQUAL' === $matches[1]) {
  110. $replace = array(
  111. array(self::T_POW_EQUAL, '**=', $this->tokens[$i + 1][2])
  112. );
  113. } else if ('COALESCE' === $matches[1]) {
  114. $replace = array(
  115. array(self::T_COALESCE, '??', $this->tokens[$i + 1][2])
  116. );
  117. } else if ('SPACESHIP' === $matches[1]) {
  118. $replace = array(
  119. array(self::T_SPACESHIP, '<=>', $this->tokens[$i + 1][2]),
  120. );
  121. } else if ('YIELDFROM' === $matches[1]) {
  122. $content = hex2bin($matches[2]);
  123. $replace = array(
  124. array(self::T_YIELD_FROM, $content, $this->tokens[$i + 1][2] - substr_count($content, "\n"))
  125. );
  126. } else {
  127. throw new \RuntimeException('Invalid __EMU__ sequence');
  128. }
  129. array_splice($this->tokens, $i, 3, $replace);
  130. $c -= 3 - count($replace);
  131. // for multichar tokens (e.g. strings) replace any ~__EMU__...~ sequences
  132. // in their content with the original character sequence
  133. } elseif (is_array($this->tokens[$i])
  134. && 0 !== strpos($this->tokens[$i][1], '__EMU__')
  135. ) {
  136. $this->tokens[$i][1] = preg_replace_callback(
  137. '(~__EMU__([A-Z]++)__(?:([A-Za-z0-9]++)__)?~)',
  138. array($this, 'restoreContentCallback'),
  139. $this->tokens[$i][1]
  140. );
  141. }
  142. }
  143. }
  144. /*
  145. * This method is a callback for restoring EMU sequences in
  146. * multichar tokens (like strings) to their original value.
  147. */
  148. public function restoreContentCallback(array $matches) {
  149. if ('ELLIPSIS' === $matches[1]) {
  150. return '...';
  151. } else if ('POW' === $matches[1]) {
  152. return '**';
  153. } else if ('POWEQUAL' === $matches[1]) {
  154. return '**=';
  155. } else if ('COALESCE' === $matches[1]) {
  156. return '??';
  157. } else if ('SPACESHIP' === $matches[1]) {
  158. return '<=>';
  159. } else if ('YIELDFROM' === $matches[1]) {
  160. return hex2bin($matches[2]);
  161. } else {
  162. return $matches[0];
  163. }
  164. }
  165. public function getNextToken(&$value = null, &$startAttributes = null, &$endAttributes = null) {
  166. $token = parent::getNextToken($value, $startAttributes, $endAttributes);
  167. // replace new keywords by their respective tokens. This is not done
  168. // if we currently are in an object access (e.g. in $obj->namespace
  169. // "namespace" stays a T_STRING tokens and isn't converted to T_NAMESPACE)
  170. if (Tokens::T_STRING === $token && !$this->inObjectAccess) {
  171. if (isset($this->newKeywords[strtolower($value)])) {
  172. return $this->newKeywords[strtolower($value)];
  173. }
  174. } else {
  175. // keep track of whether we currently are in an object access (after ->)
  176. $this->inObjectAccess = Tokens::T_OBJECT_OPERATOR === $token;
  177. }
  178. return $token;
  179. }
  180. }