83 lines
1.5 KiB
PHP
83 lines
1.5 KiB
PHP
|
<?php declare(strict_types=1);
|
||
|
|
||
|
namespace Aviat\Kilo\Tokens;
|
||
|
|
||
|
use PhpToken;
|
||
|
|
||
|
use function Aviat\Kilo\str_contains;
|
||
|
use function Aviat\Kilo\tabs_to_spaces;
|
||
|
|
||
|
class PHP8 extends PhpToken {
|
||
|
private array $rawLines = [];
|
||
|
|
||
|
private array $tokens = [];
|
||
|
|
||
|
/**
|
||
|
* Use 'token_get_all' to get the tokens for a file,
|
||
|
* organized by row number
|
||
|
*
|
||
|
* @param string $code
|
||
|
* @return array
|
||
|
*/
|
||
|
public static function getTokens(string $code): array
|
||
|
{
|
||
|
$lines = explode("\n", $code);
|
||
|
array_unshift($lines, '');
|
||
|
unset($lines[0]);
|
||
|
|
||
|
$self = (new self(0, $code));
|
||
|
|
||
|
// $->code = $code;
|
||
|
$self->rawLines = $lines;
|
||
|
$self->tokens = array_fill(1, count($lines), []);
|
||
|
|
||
|
return $self->organizeTokens($code);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Return tokens for the current $filename, organized
|
||
|
* by row number
|
||
|
*
|
||
|
* @param string $filename
|
||
|
* @return array
|
||
|
*/
|
||
|
public static function getFileTokens(string $filename): array
|
||
|
{
|
||
|
$code = @file_get_contents($filename);
|
||
|
|
||
|
if ($code === FALSE)
|
||
|
{
|
||
|
return [];
|
||
|
}
|
||
|
|
||
|
return self::getTokens($code);
|
||
|
}
|
||
|
|
||
|
protected function organizeTokens(string $code): array
|
||
|
{
|
||
|
$rawTokens = self::tokenize($code);
|
||
|
foreach ($rawTokens as $t)
|
||
|
{
|
||
|
$this->processObjectToken($t);
|
||
|
}
|
||
|
|
||
|
ksort($this->tokens);
|
||
|
|
||
|
return $this->tokens;
|
||
|
}
|
||
|
|
||
|
protected function processObjectToken(\PhpToken $token): void
|
||
|
{
|
||
|
$currentLine = $token->line;
|
||
|
$char = tabs_to_spaces($token->text);
|
||
|
|
||
|
$current = [
|
||
|
'type' => $token->id,
|
||
|
'typeName' => $token->getTokenName(),
|
||
|
'char' => $char,
|
||
|
'line' => $currentLine,
|
||
|
];
|
||
|
|
||
|
$this->tokens[$currentLine][] = $current;
|
||
|
}
|
||
|
}
|