123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- <?php declare(strict_types=1);
- namespace PhpParser\Internal;
- use PhpParser\Token;
- class TokenStream {
-
- private array $tokens;
-
- private array $indentMap;
-
- public function __construct(array $tokens) {
- $this->tokens = $tokens;
- $this->indentMap = $this->calcIndentMap();
- }
-
- public function haveParens(int $startPos, int $endPos): bool {
- return $this->haveTokenImmediatelyBefore($startPos, '(')
- && $this->haveTokenImmediatelyAfter($endPos, ')');
- }
-
- public function haveBraces(int $startPos, int $endPos): bool {
- return ($this->haveTokenImmediatelyBefore($startPos, '{')
- || $this->haveTokenImmediatelyBefore($startPos, T_CURLY_OPEN))
- && $this->haveTokenImmediatelyAfter($endPos, '}');
- }
-
- public function haveTokenImmediatelyBefore(int $pos, $expectedTokenType): bool {
- $tokens = $this->tokens;
- $pos--;
- for (; $pos >= 0; $pos--) {
- $token = $tokens[$pos];
- if ($token->is($expectedTokenType)) {
- return true;
- }
- if (!$token->isIgnorable()) {
- break;
- }
- }
- return false;
- }
-
- public function haveTokenImmediatelyAfter(int $pos, $expectedTokenType): bool {
- $tokens = $this->tokens;
- $pos++;
- for ($c = \count($tokens); $pos < $c; $pos++) {
- $token = $tokens[$pos];
- if ($token->is($expectedTokenType)) {
- return true;
- }
- if (!$token->isIgnorable()) {
- break;
- }
- }
- return false;
- }
-
- public function skipLeft(int $pos, $skipTokenType): int {
- $tokens = $this->tokens;
- $pos = $this->skipLeftWhitespace($pos);
- if ($skipTokenType === \T_WHITESPACE) {
- return $pos;
- }
- if (!$tokens[$pos]->is($skipTokenType)) {
-
- throw new \Exception('Encountered unexpected token');
- }
- $pos--;
- return $this->skipLeftWhitespace($pos);
- }
-
- public function skipRight(int $pos, $skipTokenType): int {
- $tokens = $this->tokens;
- $pos = $this->skipRightWhitespace($pos);
- if ($skipTokenType === \T_WHITESPACE) {
- return $pos;
- }
- if (!$tokens[$pos]->is($skipTokenType)) {
-
- throw new \Exception('Encountered unexpected token');
- }
- $pos++;
- return $this->skipRightWhitespace($pos);
- }
-
- public function skipLeftWhitespace(int $pos): int {
- $tokens = $this->tokens;
- for (; $pos >= 0; $pos--) {
- if (!$tokens[$pos]->isIgnorable()) {
- break;
- }
- }
- return $pos;
- }
-
- public function skipRightWhitespace(int $pos): int {
- $tokens = $this->tokens;
- for ($count = \count($tokens); $pos < $count; $pos++) {
- if (!$tokens[$pos]->isIgnorable()) {
- break;
- }
- }
- return $pos;
- }
-
- public function findRight(int $pos, $findTokenType): int {
- $tokens = $this->tokens;
- for ($count = \count($tokens); $pos < $count; $pos++) {
- if ($tokens[$pos]->is($findTokenType)) {
- return $pos;
- }
- }
- return -1;
- }
-
- public function haveTokenInRange(int $startPos, int $endPos, $tokenType): bool {
- $tokens = $this->tokens;
- for ($pos = $startPos; $pos < $endPos; $pos++) {
- if ($tokens[$pos]->is($tokenType)) {
- return true;
- }
- }
- return false;
- }
- public function haveTagInRange(int $startPos, int $endPos): bool {
- return $this->haveTokenInRange($startPos, $endPos, \T_OPEN_TAG)
- || $this->haveTokenInRange($startPos, $endPos, \T_CLOSE_TAG);
- }
-
- public function getIndentationBefore(int $pos): int {
- return $this->indentMap[$pos];
- }
-
- public function getTokenCode(int $from, int $to, int $indent): string {
- $tokens = $this->tokens;
- $result = '';
- for ($pos = $from; $pos < $to; $pos++) {
- $token = $tokens[$pos];
- $id = $token->id;
- $text = $token->text;
- if ($id === \T_CONSTANT_ENCAPSED_STRING || $id === \T_ENCAPSED_AND_WHITESPACE) {
- $result .= $text;
- } else {
-
- if ($indent < 0) {
- $result .= str_replace("\n" . str_repeat(" ", -$indent), "\n", $text);
- } elseif ($indent > 0) {
- $result .= str_replace("\n", "\n" . str_repeat(" ", $indent), $text);
- } else {
- $result .= $text;
- }
- }
- }
- return $result;
- }
-
- private function calcIndentMap(): array {
- $indentMap = [];
- $indent = 0;
- foreach ($this->tokens as $token) {
- $indentMap[] = $indent;
- if ($token->id === \T_WHITESPACE) {
- $content = $token->text;
- $newlinePos = \strrpos($content, "\n");
- if (false !== $newlinePos) {
- $indent = \strlen($content) - $newlinePos - 1;
- }
- }
- }
-
- $indentMap[] = $indent;
- return $indentMap;
- }
- }
|