Emulative.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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\AttributeEmulator;
  7. use PhpParser\Lexer\TokenEmulator\CoaleseEqualTokenEmulator;
  8. use PhpParser\Lexer\TokenEmulator\FlexibleDocStringEmulator;
  9. use PhpParser\Lexer\TokenEmulator\FnTokenEmulator;
  10. use PhpParser\Lexer\TokenEmulator\MatchTokenEmulator;
  11. use PhpParser\Lexer\TokenEmulator\NullsafeTokenEmulator;
  12. use PhpParser\Lexer\TokenEmulator\NumericLiteralSeparatorEmulator;
  13. use PhpParser\Lexer\TokenEmulator\ReverseEmulator;
  14. use PhpParser\Lexer\TokenEmulator\TokenEmulator;
  15. use PhpParser\Parser\Tokens;
  16. class Emulative extends Lexer
  17. {
  18. const PHP_7_3 = '7.3dev';
  19. const PHP_7_4 = '7.4dev';
  20. const PHP_8_0 = '8.0dev';
  21. /** @var mixed[] Patches used to reverse changes introduced in the code */
  22. private $patches = [];
  23. /** @var TokenEmulator[] */
  24. private $emulators = [];
  25. /** @var string */
  26. private $targetPhpVersion;
  27. /**
  28. * @param mixed[] $options Lexer options. In addition to the usual options,
  29. * accepts a 'phpVersion' string that specifies the
  30. * version to emulated. Defaults to newest supported.
  31. */
  32. public function __construct(array $options = [])
  33. {
  34. $this->targetPhpVersion = $options['phpVersion'] ?? Emulative::PHP_8_0;
  35. unset($options['phpVersion']);
  36. parent::__construct($options);
  37. $emulators = [
  38. new FlexibleDocStringEmulator(),
  39. new FnTokenEmulator(),
  40. new MatchTokenEmulator(),
  41. new CoaleseEqualTokenEmulator(),
  42. new NumericLiteralSeparatorEmulator(),
  43. new NullsafeTokenEmulator(),
  44. new AttributeEmulator(),
  45. ];
  46. // Collect emulators that are relevant for the PHP version we're running
  47. // and the PHP version we're targeting for emulation.
  48. foreach ($emulators as $emulator) {
  49. $emulatorPhpVersion = $emulator->getPhpVersion();
  50. if ($this->isForwardEmulationNeeded($emulatorPhpVersion)) {
  51. $this->emulators[] = $emulator;
  52. } else if ($this->isReverseEmulationNeeded($emulatorPhpVersion)) {
  53. $this->emulators[] = new ReverseEmulator($emulator);
  54. }
  55. }
  56. }
  57. public function startLexing(string $code, ErrorHandler $errorHandler = null) {
  58. $emulators = array_filter($this->emulators, function($emulator) use($code) {
  59. return $emulator->isEmulationNeeded($code);
  60. });
  61. if (empty($emulators)) {
  62. // Nothing to emulate, yay
  63. parent::startLexing($code, $errorHandler);
  64. return;
  65. }
  66. $this->patches = [];
  67. foreach ($emulators as $emulator) {
  68. $code = $emulator->preprocessCode($code, $this->patches);
  69. }
  70. $collector = new ErrorHandler\Collecting();
  71. parent::startLexing($code, $collector);
  72. $this->sortPatches();
  73. $this->fixupTokens();
  74. $errors = $collector->getErrors();
  75. if (!empty($errors)) {
  76. $this->fixupErrors($errors);
  77. foreach ($errors as $error) {
  78. $errorHandler->handleError($error);
  79. }
  80. }
  81. foreach ($emulators as $emulator) {
  82. $this->tokens = $emulator->emulate($code, $this->tokens);
  83. }
  84. }
  85. private function isForwardEmulationNeeded(string $emulatorPhpVersion): bool {
  86. return version_compare(\PHP_VERSION, $emulatorPhpVersion, '<')
  87. && version_compare($this->targetPhpVersion, $emulatorPhpVersion, '>=');
  88. }
  89. private function isReverseEmulationNeeded(string $emulatorPhpVersion): bool {
  90. return version_compare(\PHP_VERSION, $emulatorPhpVersion, '>=')
  91. && version_compare($this->targetPhpVersion, $emulatorPhpVersion, '<');
  92. }
  93. private function sortPatches()
  94. {
  95. // Patches may be contributed by different emulators.
  96. // Make sure they are sorted by increasing patch position.
  97. usort($this->patches, function($p1, $p2) {
  98. return $p1[0] <=> $p2[0];
  99. });
  100. }
  101. private function fixupTokens()
  102. {
  103. if (\count($this->patches) === 0) {
  104. return;
  105. }
  106. // Load first patch
  107. $patchIdx = 0;
  108. list($patchPos, $patchType, $patchText) = $this->patches[$patchIdx];
  109. // We use a manual loop over the tokens, because we modify the array on the fly
  110. $pos = 0;
  111. for ($i = 0, $c = \count($this->tokens); $i < $c; $i++) {
  112. $token = $this->tokens[$i];
  113. if (\is_string($token)) {
  114. if ($patchPos === $pos) {
  115. // Only support replacement for string tokens.
  116. assert($patchType === 'replace');
  117. $this->tokens[$i] = $patchText;
  118. // Fetch the next patch
  119. $patchIdx++;
  120. if ($patchIdx >= \count($this->patches)) {
  121. // No more patches, we're done
  122. return;
  123. }
  124. list($patchPos, $patchType, $patchText) = $this->patches[$patchIdx];
  125. }
  126. $pos += \strlen($token);
  127. continue;
  128. }
  129. $len = \strlen($token[1]);
  130. $posDelta = 0;
  131. while ($patchPos >= $pos && $patchPos < $pos + $len) {
  132. $patchTextLen = \strlen($patchText);
  133. if ($patchType === 'remove') {
  134. if ($patchPos === $pos && $patchTextLen === $len) {
  135. // Remove token entirely
  136. array_splice($this->tokens, $i, 1, []);
  137. $i--;
  138. $c--;
  139. } else {
  140. // Remove from token string
  141. $this->tokens[$i][1] = substr_replace(
  142. $token[1], '', $patchPos - $pos + $posDelta, $patchTextLen
  143. );
  144. $posDelta -= $patchTextLen;
  145. }
  146. } elseif ($patchType === 'add') {
  147. // Insert into the token string
  148. $this->tokens[$i][1] = substr_replace(
  149. $token[1], $patchText, $patchPos - $pos + $posDelta, 0
  150. );
  151. $posDelta += $patchTextLen;
  152. } else if ($patchType === 'replace') {
  153. // Replace inside the token string
  154. $this->tokens[$i][1] = substr_replace(
  155. $token[1], $patchText, $patchPos - $pos + $posDelta, $patchTextLen
  156. );
  157. } else {
  158. assert(false);
  159. }
  160. // Fetch the next patch
  161. $patchIdx++;
  162. if ($patchIdx >= \count($this->patches)) {
  163. // No more patches, we're done
  164. return;
  165. }
  166. list($patchPos, $patchType, $patchText) = $this->patches[$patchIdx];
  167. // Multiple patches may apply to the same token. Reload the current one to check
  168. // If the new patch applies
  169. $token = $this->tokens[$i];
  170. }
  171. $pos += $len;
  172. }
  173. // A patch did not apply
  174. assert(false);
  175. }
  176. /**
  177. * Fixup line and position information in errors.
  178. *
  179. * @param Error[] $errors
  180. */
  181. private function fixupErrors(array $errors) {
  182. foreach ($errors as $error) {
  183. $attrs = $error->getAttributes();
  184. $posDelta = 0;
  185. $lineDelta = 0;
  186. foreach ($this->patches as $patch) {
  187. list($patchPos, $patchType, $patchText) = $patch;
  188. if ($patchPos >= $attrs['startFilePos']) {
  189. // No longer relevant
  190. break;
  191. }
  192. if ($patchType === 'add') {
  193. $posDelta += strlen($patchText);
  194. $lineDelta += substr_count($patchText, "\n");
  195. } else if ($patchType === 'remove') {
  196. $posDelta -= strlen($patchText);
  197. $lineDelta -= substr_count($patchText, "\n");
  198. }
  199. }
  200. $attrs['startFilePos'] += $posDelta;
  201. $attrs['endFilePos'] += $posDelta;
  202. $attrs['startLine'] += $lineDelta;
  203. $attrs['endLine'] += $lineDelta;
  204. $error->setAttributes($attrs);
  205. }
  206. }
  207. }