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