Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Emulative.php 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\Lexer;
  3. use PhpParser\Error;
  4. use PhpParser\ErrorHandler;
  5. use PhpParser\Lexer;
  6. use PhpParser\Lexer\TokenEmulator\CoaleseEqualTokenEmulator;
  7. use PhpParser\Lexer\TokenEmulator\FnTokenEmulator;
  8. use PhpParser\Lexer\TokenEmulator\NumericLiteralSeparatorEmulator;
  9. use PhpParser\Lexer\TokenEmulator\TokenEmulatorInterface;
  10. use PhpParser\Parser\Tokens;
  11. class Emulative extends Lexer
  12. {
  13. const PHP_7_3 = '7.3.0dev';
  14. const PHP_7_4 = '7.4.0dev';
  15. const T_COALESCE_EQUAL = 1007;
  16. const T_FN = 1008;
  17. const FLEXIBLE_DOC_STRING_REGEX = <<<'REGEX'
  18. /<<<[ \t]*(['"]?)([a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)\1\r?\n
  19. (?:.*\r?\n)*?
  20. (?<indentation>\h*)\2(?![a-zA-Z_\x80-\xff])(?<separator>(?:;?[\r\n])?)/x
  21. REGEX;
  22. /** @var mixed[] Patches used to reverse changes introduced in the code */
  23. private $patches = [];
  24. /** @var TokenEmulatorInterface[] */
  25. private $tokenEmulators = [];
  26. /**
  27. * @param mixed[] $options
  28. */
  29. public function __construct(array $options = [])
  30. {
  31. parent::__construct($options);
  32. $this->tokenEmulators[] = new FnTokenEmulator();
  33. $this->tokenEmulators[] = new CoaleseEqualTokenEmulator();
  34. $this->tokenEmulators[] = new NumericLiteralSeparatorEmulator();
  35. $this->tokenMap[self::T_COALESCE_EQUAL] = Tokens::T_COALESCE_EQUAL;
  36. $this->tokenMap[self::T_FN] = Tokens::T_FN;
  37. }
  38. public function startLexing(string $code, ErrorHandler $errorHandler = null) {
  39. $this->patches = [];
  40. if ($this->isEmulationNeeded($code) === false) {
  41. // Nothing to emulate, yay
  42. parent::startLexing($code, $errorHandler);
  43. return;
  44. }
  45. $collector = new ErrorHandler\Collecting();
  46. // 1. emulation of heredoc and nowdoc new syntax
  47. $preparedCode = $this->processHeredocNowdoc($code);
  48. parent::startLexing($preparedCode, $collector);
  49. $this->fixupTokens();
  50. $errors = $collector->getErrors();
  51. if (!empty($errors)) {
  52. $this->fixupErrors($errors);
  53. foreach ($errors as $error) {
  54. $errorHandler->handleError($error);
  55. }
  56. }
  57. // add token emulation
  58. foreach ($this->tokenEmulators as $emulativeToken) {
  59. if ($emulativeToken->isEmulationNeeded($code)) {
  60. $this->tokens = $emulativeToken->emulate($code, $this->tokens);
  61. }
  62. }
  63. }
  64. private function isHeredocNowdocEmulationNeeded(string $code): bool
  65. {
  66. // skip version where this works without emulation
  67. if (version_compare(\PHP_VERSION, self::PHP_7_3, '>=')) {
  68. return false;
  69. }
  70. return strpos($code, '<<<') !== false;
  71. }
  72. private function processHeredocNowdoc(string $code): string
  73. {
  74. if ($this->isHeredocNowdocEmulationNeeded($code) === false) {
  75. return $code;
  76. }
  77. if (!preg_match_all(self::FLEXIBLE_DOC_STRING_REGEX, $code, $matches, PREG_SET_ORDER|PREG_OFFSET_CAPTURE)) {
  78. // No heredoc/nowdoc found
  79. return $code;
  80. }
  81. // Keep track of how much we need to adjust string offsets due to the modifications we
  82. // already made
  83. $posDelta = 0;
  84. foreach ($matches as $match) {
  85. $indentation = $match['indentation'][0];
  86. $indentationStart = $match['indentation'][1];
  87. $separator = $match['separator'][0];
  88. $separatorStart = $match['separator'][1];
  89. if ($indentation === '' && $separator !== '') {
  90. // Ordinary heredoc/nowdoc
  91. continue;
  92. }
  93. if ($indentation !== '') {
  94. // Remove indentation
  95. $indentationLen = strlen($indentation);
  96. $code = substr_replace($code, '', $indentationStart + $posDelta, $indentationLen);
  97. $this->patches[] = [$indentationStart + $posDelta, 'add', $indentation];
  98. $posDelta -= $indentationLen;
  99. }
  100. if ($separator === '') {
  101. // Insert newline as separator
  102. $code = substr_replace($code, "\n", $separatorStart + $posDelta, 0);
  103. $this->patches[] = [$separatorStart + $posDelta, 'remove', "\n"];
  104. $posDelta += 1;
  105. }
  106. }
  107. return $code;
  108. }
  109. private function isEmulationNeeded(string $code): bool
  110. {
  111. foreach ($this->tokenEmulators as $emulativeToken) {
  112. if ($emulativeToken->isEmulationNeeded($code)) {
  113. return true;
  114. }
  115. }
  116. return $this->isHeredocNowdocEmulationNeeded($code);
  117. }
  118. private function fixupTokens()
  119. {
  120. if (\count($this->patches) === 0) {
  121. return;
  122. }
  123. // Load first patch
  124. $patchIdx = 0;
  125. list($patchPos, $patchType, $patchText) = $this->patches[$patchIdx];
  126. // We use a manual loop over the tokens, because we modify the array on the fly
  127. $pos = 0;
  128. for ($i = 0, $c = \count($this->tokens); $i < $c; $i++) {
  129. $token = $this->tokens[$i];
  130. if (\is_string($token)) {
  131. // We assume that patches don't apply to string tokens
  132. $pos += \strlen($token);
  133. continue;
  134. }
  135. $len = \strlen($token[1]);
  136. $posDelta = 0;
  137. while ($patchPos >= $pos && $patchPos < $pos + $len) {
  138. $patchTextLen = \strlen($patchText);
  139. if ($patchType === 'remove') {
  140. if ($patchPos === $pos && $patchTextLen === $len) {
  141. // Remove token entirely
  142. array_splice($this->tokens, $i, 1, []);
  143. $i--;
  144. $c--;
  145. } else {
  146. // Remove from token string
  147. $this->tokens[$i][1] = substr_replace(
  148. $token[1], '', $patchPos - $pos + $posDelta, $patchTextLen
  149. );
  150. $posDelta -= $patchTextLen;
  151. }
  152. } elseif ($patchType === 'add') {
  153. // Insert into the token string
  154. $this->tokens[$i][1] = substr_replace(
  155. $token[1], $patchText, $patchPos - $pos + $posDelta, 0
  156. );
  157. $posDelta += $patchTextLen;
  158. } else {
  159. assert(false);
  160. }
  161. // Fetch the next patch
  162. $patchIdx++;
  163. if ($patchIdx >= \count($this->patches)) {
  164. // No more patches, we're done
  165. return;
  166. }
  167. list($patchPos, $patchType, $patchText) = $this->patches[$patchIdx];
  168. // Multiple patches may apply to the same token. Reload the current one to check
  169. // If the new patch applies
  170. $token = $this->tokens[$i];
  171. }
  172. $pos += $len;
  173. }
  174. // A patch did not apply
  175. assert(false);
  176. }
  177. /**
  178. * Fixup line and position information in errors.
  179. *
  180. * @param Error[] $errors
  181. */
  182. private function fixupErrors(array $errors) {
  183. foreach ($errors as $error) {
  184. $attrs = $error->getAttributes();
  185. $posDelta = 0;
  186. $lineDelta = 0;
  187. foreach ($this->patches as $patch) {
  188. list($patchPos, $patchType, $patchText) = $patch;
  189. if ($patchPos >= $attrs['startFilePos']) {
  190. // No longer relevant
  191. break;
  192. }
  193. if ($patchType === 'add') {
  194. $posDelta += strlen($patchText);
  195. $lineDelta += substr_count($patchText, "\n");
  196. } else {
  197. $posDelta -= strlen($patchText);
  198. $lineDelta -= substr_count($patchText, "\n");
  199. }
  200. }
  201. $attrs['startFilePos'] += $posDelta;
  202. $attrs['endFilePos'] += $posDelta;
  203. $attrs['startLine'] += $lineDelta;
  204. $attrs['endLine'] += $lineDelta;
  205. $error->setAttributes($attrs);
  206. }
  207. }
  208. }