Chris@13: tokens = $tokens; Chris@13: $this->indentMap = $this->calcIndentMap(); Chris@13: } Chris@13: Chris@13: /** Chris@13: * Whether the given position is immediately surrounded by parenthesis. Chris@13: * Chris@13: * @param int $startPos Start position Chris@13: * @param int $endPos End position Chris@13: * Chris@13: * @return bool Chris@13: */ Chris@13: public function haveParens(int $startPos, int $endPos) : bool { Chris@13: return $this->haveTokenImmediativelyBefore($startPos, '(') Chris@13: && $this->haveTokenImmediatelyAfter($endPos, ')'); Chris@13: } Chris@13: Chris@13: /** Chris@13: * Whether the given position is immediately surrounded by braces. Chris@13: * Chris@13: * @param int $startPos Start position Chris@13: * @param int $endPos End position Chris@13: * Chris@13: * @return bool Chris@13: */ Chris@13: public function haveBraces(int $startPos, int $endPos) : bool { Chris@13: return $this->haveTokenImmediativelyBefore($startPos, '{') Chris@13: && $this->haveTokenImmediatelyAfter($endPos, '}'); Chris@13: } Chris@13: Chris@13: /** Chris@13: * Check whether the position is directly preceded by a certain token type. Chris@13: * Chris@13: * During this check whitespace and comments are skipped. Chris@13: * Chris@13: * @param int $pos Position before which the token should occur Chris@13: * @param int|string $expectedTokenType Token to check for Chris@13: * Chris@13: * @return bool Whether the expected token was found Chris@13: */ Chris@13: public function haveTokenImmediativelyBefore(int $pos, $expectedTokenType) : bool { Chris@13: $tokens = $this->tokens; Chris@13: $pos--; Chris@13: for (; $pos >= 0; $pos--) { Chris@13: $tokenType = $tokens[$pos][0]; Chris@13: if ($tokenType === $expectedTokenType) { Chris@13: return true; Chris@13: } Chris@13: if ($tokenType !== \T_WHITESPACE Chris@13: && $tokenType !== \T_COMMENT && $tokenType !== \T_DOC_COMMENT) { Chris@13: break; Chris@13: } Chris@13: } Chris@13: return false; Chris@13: } Chris@13: Chris@13: /** Chris@13: * Check whether the position is directly followed by a certain token type. Chris@13: * Chris@13: * During this check whitespace and comments are skipped. Chris@13: * Chris@13: * @param int $pos Position after which the token should occur Chris@13: * @param int|string $expectedTokenType Token to check for Chris@13: * Chris@13: * @return bool Whether the expected token was found Chris@13: */ Chris@13: public function haveTokenImmediatelyAfter(int $pos, $expectedTokenType) : bool { Chris@13: $tokens = $this->tokens; Chris@13: $pos++; Chris@13: for (; $pos < \count($tokens); $pos++) { Chris@13: $tokenType = $tokens[$pos][0]; Chris@13: if ($tokenType === $expectedTokenType) { Chris@13: return true; Chris@13: } Chris@13: if ($tokenType !== \T_WHITESPACE Chris@13: && $tokenType !== \T_COMMENT && $tokenType !== \T_DOC_COMMENT) { Chris@13: break; Chris@13: } Chris@13: } Chris@13: return false; Chris@13: } Chris@13: Chris@13: public function skipLeft(int $pos, $skipTokenType) { Chris@13: $tokens = $this->tokens; Chris@13: Chris@13: $pos = $this->skipLeftWhitespace($pos); Chris@13: if ($skipTokenType === \T_WHITESPACE) { Chris@13: return $pos; Chris@13: } Chris@13: Chris@13: if ($tokens[$pos][0] !== $skipTokenType) { Chris@13: // Shouldn't happen. The skip token MUST be there Chris@13: throw new \Exception('Encountered unexpected token'); Chris@13: } Chris@13: $pos--; Chris@13: Chris@13: return $this->skipLeftWhitespace($pos); Chris@13: } Chris@13: Chris@13: public function skipRight(int $pos, $skipTokenType) { Chris@13: $tokens = $this->tokens; Chris@13: Chris@13: $pos = $this->skipRightWhitespace($pos); Chris@13: if ($skipTokenType === \T_WHITESPACE) { Chris@13: return $pos; Chris@13: } Chris@13: Chris@13: if ($tokens[$pos][0] !== $skipTokenType) { Chris@13: // Shouldn't happen. The skip token MUST be there Chris@13: throw new \Exception('Encountered unexpected token'); Chris@13: } Chris@13: $pos++; Chris@13: Chris@13: return $this->skipRightWhitespace($pos); Chris@13: } Chris@13: Chris@13: /** Chris@13: * Return first non-whitespace token position smaller or equal to passed position. Chris@13: * Chris@13: * @param int $pos Token position Chris@13: * @return int Non-whitespace token position Chris@13: */ Chris@13: public function skipLeftWhitespace(int $pos) { Chris@13: $tokens = $this->tokens; Chris@13: for (; $pos >= 0; $pos--) { Chris@13: $type = $tokens[$pos][0]; Chris@13: if ($type !== \T_WHITESPACE && $type !== \T_COMMENT && $type !== \T_DOC_COMMENT) { Chris@13: break; Chris@13: } Chris@13: } Chris@13: return $pos; Chris@13: } Chris@13: Chris@13: /** Chris@13: * Return first non-whitespace position greater or equal to passed position. Chris@13: * Chris@13: * @param int $pos Token position Chris@13: * @return int Non-whitespace token position Chris@13: */ Chris@13: public function skipRightWhitespace(int $pos) { Chris@13: $tokens = $this->tokens; Chris@13: for ($count = \count($tokens); $pos < $count; $pos++) { Chris@13: $type = $tokens[$pos][0]; Chris@13: if ($type !== \T_WHITESPACE && $type !== \T_COMMENT && $type !== \T_DOC_COMMENT) { Chris@13: break; Chris@13: } Chris@13: } Chris@13: return $pos; Chris@13: } Chris@13: Chris@13: public function findRight($pos, $findTokenType) { Chris@13: $tokens = $this->tokens; Chris@13: for ($count = \count($tokens); $pos < $count; $pos++) { Chris@13: $type = $tokens[$pos][0]; Chris@13: if ($type === $findTokenType) { Chris@13: return $pos; Chris@13: } Chris@13: } Chris@13: return -1; Chris@13: } Chris@13: Chris@13: /** Chris@13: * Get indentation before token position. Chris@13: * Chris@13: * @param int $pos Token position Chris@13: * Chris@13: * @return int Indentation depth (in spaces) Chris@13: */ Chris@13: public function getIndentationBefore(int $pos) : int { Chris@13: return $this->indentMap[$pos]; Chris@13: } Chris@13: Chris@13: /** Chris@13: * Get the code corresponding to a token offset range, optionally adjusted for indentation. Chris@13: * Chris@13: * @param int $from Token start position (inclusive) Chris@13: * @param int $to Token end position (exclusive) Chris@13: * @param int $indent By how much the code should be indented (can be negative as well) Chris@13: * Chris@13: * @return string Code corresponding to token range, adjusted for indentation Chris@13: */ Chris@13: public function getTokenCode(int $from, int $to, int $indent) : string { Chris@13: $tokens = $this->tokens; Chris@13: $result = ''; Chris@13: for ($pos = $from; $pos < $to; $pos++) { Chris@13: $token = $tokens[$pos]; Chris@13: if (\is_array($token)) { Chris@13: $type = $token[0]; Chris@13: $content = $token[1]; Chris@13: if ($type === \T_CONSTANT_ENCAPSED_STRING || $type === \T_ENCAPSED_AND_WHITESPACE) { Chris@13: $result .= $content; Chris@13: } else { Chris@13: // TODO Handle non-space indentation Chris@13: if ($indent < 0) { Chris@13: $result .= str_replace("\n" . str_repeat(" ", -$indent), "\n", $content); Chris@13: } elseif ($indent > 0) { Chris@13: $result .= str_replace("\n", "\n" . str_repeat(" ", $indent), $content); Chris@13: } else { Chris@13: $result .= $content; Chris@13: } Chris@13: } Chris@13: } else { Chris@13: $result .= $token; Chris@13: } Chris@13: } Chris@13: return $result; Chris@13: } Chris@13: Chris@13: /** Chris@13: * Precalculate the indentation at every token position. Chris@13: * Chris@13: * @return int[] Token position to indentation map Chris@13: */ Chris@13: private function calcIndentMap() { Chris@13: $indentMap = []; Chris@13: $indent = 0; Chris@13: foreach ($this->tokens as $token) { Chris@13: $indentMap[] = $indent; Chris@13: Chris@13: if ($token[0] === \T_WHITESPACE) { Chris@13: $content = $token[1]; Chris@13: $newlinePos = \strrpos($content, "\n"); Chris@13: if (false !== $newlinePos) { Chris@13: $indent = \strlen($content) - $newlinePos - 1; Chris@13: } Chris@13: } Chris@13: } Chris@13: Chris@13: // Add a sentinel for one past end of the file Chris@13: $indentMap[] = $indent; Chris@13: Chris@13: return $indentMap; Chris@13: } Chris@13: }