Highlight more token types
Some checks failed
timw4mail/php-kilo/pipeline/head There was a failure building this commit

This commit is contained in:
Timothy Warren 2021-03-03 13:14:44 -05:00
parent 03fed4d667
commit 9280b77d1e
6 changed files with 77 additions and 28 deletions

View File

@ -16,10 +16,12 @@
"require-dev": {
"ext-json": "*",
"phpunit/phpunit": "^9.5.0",
"phpstan/phpstan": "^0.12.19",
"spatie/phpunit-snapshot-assertions": "^4.2.0"
},
"scripts": {
"coverage": "phpdbg -qrr -- vendor/bin/phpunit -c phpunit.xml tests",
"phpstan": "phpstan analyse -c phpstan.neon",
"test": "vendor/bin/phpunit -c phpunit.xml --no-coverage tests",
"test-update": "vendor/bin/phpunit -c phpunit.xml --no-coverage -d --update-snapshots tests"
},

8
phpstan.neon Normal file
View File

@ -0,0 +1,8 @@
parameters:
checkGenericClassInNonGenericObjectType: false
checkMissingIterableValueType: false
inferPrivatePropertyTypeFromConstructor: true
level: 8
paths:
- src
- ./kilo

View File

@ -36,7 +36,9 @@ class Row {
T_START_HEREDOC => Highlight::DELIMITER,
T_END_HEREDOC => Highlight::DELIMITER,
// Number literals
// Number literals and magic constants
T_DIR => Highlight::NUMBER,
T_TRAIT_C => Highlight::NUMBER,
T_DNUMBER => Highlight::NUMBER,
T_LNUMBER => Highlight::NUMBER,
@ -49,6 +51,7 @@ class Row {
T_STRING_VARNAME => Highlight::VARIABLE,
// Operators
T_AS => Highlight::OPERATOR,
T_AND_EQUAL => Highlight::OPERATOR,
T_BOOLEAN_AND => Highlight::OPERATOR,
T_BOOLEAN_OR => Highlight::OPERATOR,
@ -107,9 +110,11 @@ class Row {
T_ENDIF => Highlight::KEYWORD1,
T_ENDSWITCH => Highlight::KEYWORD1,
T_ENDWHILE => Highlight::KEYWORD1,
T_EXIT => Highlight::KEYWORD1,
T_EXTENDS => Highlight::KEYWORD1,
T_FINAL => Highlight::KEYWORD1,
T_FINALLY => Highlight::KEYWORD1,
T_FN => Highlight::KEYWORD1,
T_FOR => Highlight::KEYWORD1,
T_FOREACH => Highlight::KEYWORD1,
T_FUNCTION => Highlight::KEYWORD1,
@ -122,6 +127,7 @@ class Row {
T_INSTEADOF => Highlight::KEYWORD1,
T_INTERFACE => Highlight::KEYWORD1,
T_NAMESPACE => Highlight::KEYWORD1,
T_MATCH => Highlight::KEYWORD1,
T_NEW => Highlight::KEYWORD1,
T_PRIVATE => Highlight::KEYWORD1,
T_PUBLIC => Highlight::KEYWORD1,
@ -141,9 +147,10 @@ class Row {
// Not string literals, but identifiers, keywords, etc.
// T_STRING => Highlight::KEYWORD2,
// Type casts
// Types and casts
T_ARRAY_CAST => Highlight::KEYWORD2,
T_BOOL_CAST => Highlight::KEYWORD2,
T_CALLABLE => Highlight::KEYWORD2,
T_DOUBLE_CAST => Highlight::KEYWORD2,
T_INT_CAST => Highlight::KEYWORD2,
T_OBJECT_CAST => Highlight::KEYWORD2,
@ -493,7 +500,7 @@ class Row {
break;
}
$char = $token['char'] ?? '';
$char = $token['char']; // ?? '';
$charLen = strlen($char);
if ($charLen === 0 || $offset >= $this->rsize)
{
@ -546,9 +553,6 @@ class Row {
}
}
// Highlight specific tokens
if ($token['typeName'] !== 'RAW')
{
if (array_key_exists($token['type'], $this->phpTokenHighlightMap))
{
$hl = $this->phpTokenHighlightMap[$token['type']];
@ -558,17 +562,15 @@ class Row {
}
// Types/identifiers/keywords that don't have their own token
if ($token['type'] === T_STRING &&
in_array($token['char'], $this->parent->syntax->keywords2, TRUE))
if (in_array($token['char'], $this->parent->syntax->keywords2, TRUE))
{
array_replace_range($this->hl, $charStart, $charLen, Highlight::KEYWORD2);
$offset = $charEnd;
continue;
}
}
// Highlight raw characters
if (($token['type'] === self::T_RAW) && array_key_exists(trim($token['char']), $this->phpCharacterHighlightMap))
if (array_key_exists(trim($token['char']), $this->phpCharacterHighlightMap))
{
$hl = $this->phpCharacterHighlightMap[trim($token['char'])];
array_replace_range($this->hl, $charStart, $charLen, $hl);

View File

@ -249,7 +249,7 @@ function str_contains(string $haystack, string $str, ?int $offset = NULL): bool
return ($offset !== NULL)
? strpos($haystack, $str, $offset) !== FALSE
: strpos($haystack, $str) !== FALSE;
: \str_contains($haystack, $str);
}
/**
@ -260,7 +260,8 @@ function str_contains(string $haystack, string $str, ?int $offset = NULL): bool
*/
function syntax_to_color(int $hl): int
{
$map = [
return match ($hl)
{
Highlight::COMMENT => Color::FG_CYAN,
Highlight::ML_COMMENT => Color::FG_BRIGHT_BLACK,
Highlight::KEYWORD1 => Color::FG_YELLOW,
@ -272,11 +273,8 @@ function syntax_to_color(int $hl): int
Highlight::DELIMITER => Color::FG_BLUE,
Highlight::INVALID => Color::BG_BRIGHT_RED,
Highlight::MATCH => Color::INVERT,
];
return (array_key_exists($hl, $map))
? $map[$hl]
: Color::FG_WHITE;
default => Color::FG_WHITE,
};
}
/**

View File

@ -44,8 +44,33 @@ class FooBar extends Foo implements Ifoo {
}
}
trait Baz {
public function about(): array
{
return [
'__CLASS__' => __CLASS__,
'__DIR__' => __DIR__,
'__FILE__' => __FILE__,
'__FUNCTION__' => __FUNCTION__,
'__LINE__' => __LINE__,
'__METHOD__' => __METHOD__,
'__NAMESPACE__' => __NAMESPACE__,
'__TRAIT__' => __TRAIT__,
];
}
}
$square = fn (int $x) => $x ** 2;
foreach ([-1, 0, 1, 2] as $x)
{
$not = match ($x) {
0 => 1,
1,-1 => 0,
default => 0,
};
}
/*
* Multi-line comment
*/

14
tokens.php Normal file
View File

@ -0,0 +1,14 @@
<?php declare(strict_types=1);
$file = file_get_contents('test.php');
$tokens = PhpToken::tokenize($file);
function addName (PhpToken $token): PhpToken
{
$token->type = $token->getTokenName();
return $token;
}
$tokens = array_map('addName', $tokens);
var_export($tokens);