1 <?php declare(strict_types=1);
3 namespace PhpParser\Internal;
6 * Provides operations on token streams, for use by pretty printer.
12 /** @var array Tokens (in token_get_all format) */
14 /** @var int[] Map from position to indentation */
18 * Create token stream instance.
20 * @param array $tokens Tokens in token_get_all() format
22 public function __construct(array $tokens) {
23 $this->tokens = $tokens;
24 $this->indentMap = $this->calcIndentMap();
28 * Whether the given position is immediately surrounded by parenthesis.
30 * @param int $startPos Start position
31 * @param int $endPos End position
35 public function haveParens(int $startPos, int $endPos) : bool {
36 return $this->haveTokenImmediativelyBefore($startPos, '(')
37 && $this->haveTokenImmediatelyAfter($endPos, ')');
41 * Whether the given position is immediately surrounded by braces.
43 * @param int $startPos Start position
44 * @param int $endPos End position
48 public function haveBraces(int $startPos, int $endPos) : bool {
49 return $this->haveTokenImmediativelyBefore($startPos, '{')
50 && $this->haveTokenImmediatelyAfter($endPos, '}');
54 * Check whether the position is directly preceded by a certain token type.
56 * During this check whitespace and comments are skipped.
58 * @param int $pos Position before which the token should occur
59 * @param int|string $expectedTokenType Token to check for
61 * @return bool Whether the expected token was found
63 public function haveTokenImmediativelyBefore(int $pos, $expectedTokenType) : bool {
64 $tokens = $this->tokens;
66 for (; $pos >= 0; $pos--) {
67 $tokenType = $tokens[$pos][0];
68 if ($tokenType === $expectedTokenType) {
71 if ($tokenType !== \T_WHITESPACE
72 && $tokenType !== \T_COMMENT && $tokenType !== \T_DOC_COMMENT) {
80 * Check whether the position is directly followed by a certain token type.
82 * During this check whitespace and comments are skipped.
84 * @param int $pos Position after which the token should occur
85 * @param int|string $expectedTokenType Token to check for
87 * @return bool Whether the expected token was found
89 public function haveTokenImmediatelyAfter(int $pos, $expectedTokenType) : bool {
90 $tokens = $this->tokens;
92 for (; $pos < \count($tokens); $pos++) {
93 $tokenType = $tokens[$pos][0];
94 if ($tokenType === $expectedTokenType) {
97 if ($tokenType !== \T_WHITESPACE
98 && $tokenType !== \T_COMMENT && $tokenType !== \T_DOC_COMMENT) {
105 public function skipLeft(int $pos, $skipTokenType) {
106 $tokens = $this->tokens;
108 $pos = $this->skipLeftWhitespace($pos);
109 if ($skipTokenType === \T_WHITESPACE) {
113 if ($tokens[$pos][0] !== $skipTokenType) {
114 // Shouldn't happen. The skip token MUST be there
115 throw new \Exception('Encountered unexpected token');
119 return $this->skipLeftWhitespace($pos);
122 public function skipRight(int $pos, $skipTokenType) {
123 $tokens = $this->tokens;
125 $pos = $this->skipRightWhitespace($pos);
126 if ($skipTokenType === \T_WHITESPACE) {
130 if ($tokens[$pos][0] !== $skipTokenType) {
131 // Shouldn't happen. The skip token MUST be there
132 throw new \Exception('Encountered unexpected token');
136 return $this->skipRightWhitespace($pos);
140 * Return first non-whitespace token position smaller or equal to passed position.
142 * @param int $pos Token position
143 * @return int Non-whitespace token position
145 public function skipLeftWhitespace(int $pos) {
146 $tokens = $this->tokens;
147 for (; $pos >= 0; $pos--) {
148 $type = $tokens[$pos][0];
149 if ($type !== \T_WHITESPACE && $type !== \T_COMMENT && $type !== \T_DOC_COMMENT) {
157 * Return first non-whitespace position greater or equal to passed position.
159 * @param int $pos Token position
160 * @return int Non-whitespace token position
162 public function skipRightWhitespace(int $pos) {
163 $tokens = $this->tokens;
164 for ($count = \count($tokens); $pos < $count; $pos++) {
165 $type = $tokens[$pos][0];
166 if ($type !== \T_WHITESPACE && $type !== \T_COMMENT && $type !== \T_DOC_COMMENT) {
173 public function findRight($pos, $findTokenType) {
174 $tokens = $this->tokens;
175 for ($count = \count($tokens); $pos < $count; $pos++) {
176 $type = $tokens[$pos][0];
177 if ($type === $findTokenType) {
185 * Get indentation before token position.
187 * @param int $pos Token position
189 * @return int Indentation depth (in spaces)
191 public function getIndentationBefore(int $pos) : int {
192 return $this->indentMap[$pos];
196 * Get the code corresponding to a token offset range, optionally adjusted for indentation.
198 * @param int $from Token start position (inclusive)
199 * @param int $to Token end position (exclusive)
200 * @param int $indent By how much the code should be indented (can be negative as well)
202 * @return string Code corresponding to token range, adjusted for indentation
204 public function getTokenCode(int $from, int $to, int $indent) : string {
205 $tokens = $this->tokens;
207 for ($pos = $from; $pos < $to; $pos++) {
208 $token = $tokens[$pos];
209 if (\is_array($token)) {
211 $content = $token[1];
212 if ($type === \T_CONSTANT_ENCAPSED_STRING || $type === \T_ENCAPSED_AND_WHITESPACE) {
215 // TODO Handle non-space indentation
217 $result .= str_replace("\n" . str_repeat(" ", -$indent), "\n", $content);
218 } elseif ($indent > 0) {
219 $result .= str_replace("\n", "\n" . str_repeat(" ", $indent), $content);
232 * Precalculate the indentation at every token position.
234 * @return int[] Token position to indentation map
236 private function calcIndentMap() {
239 foreach ($this->tokens as $token) {
240 $indentMap[] = $indent;
242 if ($token[0] === \T_WHITESPACE) {
243 $content = $token[1];
244 $newlinePos = \strrpos($content, "\n");
245 if (false !== $newlinePos) {
246 $indent = \strlen($content) - $newlinePos - 1;
251 // Add a sentinel for one past end of the file
252 $indentMap[] = $indent;