Emulative.php 8.5 KB

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