TokenStream.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php declare(strict_types=1);
  2. namespace PhpParser\Internal;
  3. use PhpParser\Token;
  4. /**
  5. * Provides operations on token streams, for use by pretty printer.
  6. *
  7. * @internal
  8. */
  9. class TokenStream {
  10. /** @var Token[] Tokens (in PhpToken::tokenize() format) */
  11. private array $tokens;
  12. /** @var int[] Map from position to indentation */
  13. private array $indentMap;
  14. /**
  15. * Create token stream instance.
  16. *
  17. * @param Token[] $tokens Tokens in PhpToken::tokenize() format
  18. */
  19. public function __construct(array $tokens) {
  20. $this->tokens = $tokens;
  21. $this->indentMap = $this->calcIndentMap();
  22. }
  23. /**
  24. * Whether the given position is immediately surrounded by parenthesis.
  25. *
  26. * @param int $startPos Start position
  27. * @param int $endPos End position
  28. */
  29. public function haveParens(int $startPos, int $endPos): bool {
  30. return $this->haveTokenImmediatelyBefore($startPos, '(')
  31. && $this->haveTokenImmediatelyAfter($endPos, ')');
  32. }
  33. /**
  34. * Whether the given position is immediately surrounded by braces.
  35. *
  36. * @param int $startPos Start position
  37. * @param int $endPos End position
  38. */
  39. public function haveBraces(int $startPos, int $endPos): bool {
  40. return ($this->haveTokenImmediatelyBefore($startPos, '{')
  41. || $this->haveTokenImmediatelyBefore($startPos, T_CURLY_OPEN))
  42. && $this->haveTokenImmediatelyAfter($endPos, '}');
  43. }
  44. /**
  45. * Check whether the position is directly preceded by a certain token type.
  46. *
  47. * During this check whitespace and comments are skipped.
  48. *
  49. * @param int $pos Position before which the token should occur
  50. * @param int|string $expectedTokenType Token to check for
  51. *
  52. * @return bool Whether the expected token was found
  53. */
  54. public function haveTokenImmediatelyBefore(int $pos, $expectedTokenType): bool {
  55. $tokens = $this->tokens;
  56. $pos--;
  57. for (; $pos >= 0; $pos--) {
  58. $token = $tokens[$pos];
  59. if ($token->is($expectedTokenType)) {
  60. return true;
  61. }
  62. if (!$token->isIgnorable()) {
  63. break;
  64. }
  65. }
  66. return false;
  67. }
  68. /**
  69. * Check whether the position is directly followed by a certain token type.
  70. *
  71. * During this check whitespace and comments are skipped.
  72. *
  73. * @param int $pos Position after which the token should occur
  74. * @param int|string $expectedTokenType Token to check for
  75. *
  76. * @return bool Whether the expected token was found
  77. */
  78. public function haveTokenImmediatelyAfter(int $pos, $expectedTokenType): bool {
  79. $tokens = $this->tokens;
  80. $pos++;
  81. for ($c = \count($tokens); $pos < $c; $pos++) {
  82. $token = $tokens[$pos];
  83. if ($token->is($expectedTokenType)) {
  84. return true;
  85. }
  86. if (!$token->isIgnorable()) {
  87. break;
  88. }
  89. }
  90. return false;
  91. }
  92. /** @param int|string|(int|string)[] $skipTokenType */
  93. public function skipLeft(int $pos, $skipTokenType): int {
  94. $tokens = $this->tokens;
  95. $pos = $this->skipLeftWhitespace($pos);
  96. if ($skipTokenType === \T_WHITESPACE) {
  97. return $pos;
  98. }
  99. if (!$tokens[$pos]->is($skipTokenType)) {
  100. // Shouldn't happen. The skip token MUST be there
  101. throw new \Exception('Encountered unexpected token');
  102. }
  103. $pos--;
  104. return $this->skipLeftWhitespace($pos);
  105. }
  106. /** @param int|string|(int|string)[] $skipTokenType */
  107. public function skipRight(int $pos, $skipTokenType): int {
  108. $tokens = $this->tokens;
  109. $pos = $this->skipRightWhitespace($pos);
  110. if ($skipTokenType === \T_WHITESPACE) {
  111. return $pos;
  112. }
  113. if (!$tokens[$pos]->is($skipTokenType)) {
  114. // Shouldn't happen. The skip token MUST be there
  115. throw new \Exception('Encountered unexpected token');
  116. }
  117. $pos++;
  118. return $this->skipRightWhitespace($pos);
  119. }
  120. /**
  121. * Return first non-whitespace token position smaller or equal to passed position.
  122. *
  123. * @param int $pos Token position
  124. * @return int Non-whitespace token position
  125. */
  126. public function skipLeftWhitespace(int $pos): int {
  127. $tokens = $this->tokens;
  128. for (; $pos >= 0; $pos--) {
  129. if (!$tokens[$pos]->isIgnorable()) {
  130. break;
  131. }
  132. }
  133. return $pos;
  134. }
  135. /**
  136. * Return first non-whitespace position greater or equal to passed position.
  137. *
  138. * @param int $pos Token position
  139. * @return int Non-whitespace token position
  140. */
  141. public function skipRightWhitespace(int $pos): int {
  142. $tokens = $this->tokens;
  143. for ($count = \count($tokens); $pos < $count; $pos++) {
  144. if (!$tokens[$pos]->isIgnorable()) {
  145. break;
  146. }
  147. }
  148. return $pos;
  149. }
  150. /** @param int|string|(int|string)[] $findTokenType */
  151. public function findRight(int $pos, $findTokenType): int {
  152. $tokens = $this->tokens;
  153. for ($count = \count($tokens); $pos < $count; $pos++) {
  154. if ($tokens[$pos]->is($findTokenType)) {
  155. return $pos;
  156. }
  157. }
  158. return -1;
  159. }
  160. /**
  161. * Whether the given position range contains a certain token type.
  162. *
  163. * @param int $startPos Starting position (inclusive)
  164. * @param int $endPos Ending position (exclusive)
  165. * @param int|string $tokenType Token type to look for
  166. * @return bool Whether the token occurs in the given range
  167. */
  168. public function haveTokenInRange(int $startPos, int $endPos, $tokenType): bool {
  169. $tokens = $this->tokens;
  170. for ($pos = $startPos; $pos < $endPos; $pos++) {
  171. if ($tokens[$pos]->is($tokenType)) {
  172. return true;
  173. }
  174. }
  175. return false;
  176. }
  177. public function haveTagInRange(int $startPos, int $endPos): bool {
  178. return $this->haveTokenInRange($startPos, $endPos, \T_OPEN_TAG)
  179. || $this->haveTokenInRange($startPos, $endPos, \T_CLOSE_TAG);
  180. }
  181. /**
  182. * Get indentation before token position.
  183. *
  184. * @param int $pos Token position
  185. *
  186. * @return int Indentation depth (in spaces)
  187. */
  188. public function getIndentationBefore(int $pos): int {
  189. return $this->indentMap[$pos];
  190. }
  191. /**
  192. * Get the code corresponding to a token offset range, optionally adjusted for indentation.
  193. *
  194. * @param int $from Token start position (inclusive)
  195. * @param int $to Token end position (exclusive)
  196. * @param int $indent By how much the code should be indented (can be negative as well)
  197. *
  198. * @return string Code corresponding to token range, adjusted for indentation
  199. */
  200. public function getTokenCode(int $from, int $to, int $indent): string {
  201. $tokens = $this->tokens;
  202. $result = '';
  203. for ($pos = $from; $pos < $to; $pos++) {
  204. $token = $tokens[$pos];
  205. $id = $token->id;
  206. $text = $token->text;
  207. if ($id === \T_CONSTANT_ENCAPSED_STRING || $id === \T_ENCAPSED_AND_WHITESPACE) {
  208. $result .= $text;
  209. } else {
  210. // TODO Handle non-space indentation
  211. if ($indent < 0) {
  212. $result .= str_replace("\n" . str_repeat(" ", -$indent), "\n", $text);
  213. } elseif ($indent > 0) {
  214. $result .= str_replace("\n", "\n" . str_repeat(" ", $indent), $text);
  215. } else {
  216. $result .= $text;
  217. }
  218. }
  219. }
  220. return $result;
  221. }
  222. /**
  223. * Precalculate the indentation at every token position.
  224. *
  225. * @return int[] Token position to indentation map
  226. */
  227. private function calcIndentMap(): array {
  228. $indentMap = [];
  229. $indent = 0;
  230. foreach ($this->tokens as $token) {
  231. $indentMap[] = $indent;
  232. if ($token->id === \T_WHITESPACE) {
  233. $content = $token->text;
  234. $newlinePos = \strrpos($content, "\n");
  235. if (false !== $newlinePos) {
  236. $indent = \strlen($content) - $newlinePos - 1;
  237. }
  238. }
  239. }
  240. // Add a sentinel for one past end of the file
  241. $indentMap[] = $indent;
  242. return $indentMap;
  243. }
  244. }