Some more code cleanup
All checks were successful
timw4mail/php-kilo/pipeline/head This commit looks good

This commit is contained in:
Timothy Warren 2021-03-10 11:10:52 -05:00
parent 99685f3fcb
commit 31144fb2e3
6 changed files with 191 additions and 216 deletions

View File

@ -23,4 +23,188 @@ class Highlight {
public const INVALID = 10; public const INVALID = 10;
public const MATCH = 11; public const MATCH = 11;
public const IDENTIFIER = 12; public const IDENTIFIER = 12;
/**
* Map a PHP syntax token to its associated highlighting type
*
* @param int $token
* @return int
*/
public static function fromPHPToken(int $token): int
{
return match($token) {
// Delimiters
T_ARRAY,
T_CURLY_OPEN,
T_DOLLAR_OPEN_CURLY_BRACES,
T_OPEN_TAG,
T_OPEN_TAG_WITH_ECHO,
T_CLOSE_TAG,
T_START_HEREDOC,
T_END_HEREDOC => Highlight::DELIMITER,
// Number literals and magic constants
T_CLASS_C,
T_DIR,
T_DNUMBER,
T_LNUMBER,
T_FILE,
T_FUNC_C,
T_LINE,
T_METHOD_C,
T_NS_C,
T_NUM_STRING,
T_TRAIT_C => Highlight::NUMBER,
// String literals
T_CONSTANT_ENCAPSED_STRING, T_ENCAPSED_AND_WHITESPACE => Highlight::STRING,
// Simple variables
T_VARIABLE, T_STRING_VARNAME => Highlight::VARIABLE,
// Operators
T_AS,
T_AND_EQUAL,
T_BOOLEAN_AND,
T_BOOLEAN_OR,
T_COALESCE,
T_COALESCE_EQUAL,
T_CONCAT_EQUAL,
T_DEC,
T_DIV_EQUAL,
T_DOUBLE_ARROW,
T_DOUBLE_COLON,
T_ELLIPSIS,
T_INC,
T_INSTANCEOF,
T_INSTEADOF,
T_IS_EQUAL,
T_IS_GREATER_OR_EQUAL,
T_IS_IDENTICAL,
T_IS_NOT_EQUAL,
T_IS_NOT_IDENTICAL,
T_IS_SMALLER_OR_EQUAL,
T_SPACESHIP,
T_LOGICAL_AND,
T_LOGICAL_OR,
T_LOGICAL_XOR,
T_MINUS_EQUAL,
T_MOD_EQUAL,
T_MUL_EQUAL,
T_NS_SEPARATOR,
T_NULLSAFE_OBJECT_OPERATOR,
T_OBJECT_OPERATOR,
T_OR_EQUAL,
T_PLUS_EQUAL,
T_POW,
T_POW_EQUAL,
T_SL,
T_SL_EQUAL,
T_SR,
T_SR_EQUAL,
T_XOR_EQUAL => Highlight::OPERATOR,
// Keywords1
T_ABSTRACT,
T_BREAK,
T_CASE,
T_CATCH,
T_CLASS,
T_CLONE,
T_CONST,
T_CONTINUE,
T_DECLARE,
T_DEFAULT,
T_DO,
T_ECHO,
T_ELSE,
T_ELSEIF,
T_EMPTY,
T_ENDDECLARE,
T_ENDFOR,
T_ENDFOREACH,
T_ENDIF,
T_ENDSWITCH,
T_ENDWHILE,
T_EVAL,
T_EXIT,
T_EXTENDS,
T_FINAL,
T_FINALLY,
T_FN,
T_FOR,
T_FOREACH,
T_FUNCTION,
T_GLOBAL,
T_GOTO,
T_HALT_COMPILER,
T_IF,
T_IMPLEMENTS,
T_INCLUDE,
T_INCLUDE_ONCE,
T_INTERFACE,
T_ISSET,
T_LIST,
T_MATCH,
T_NAMESPACE,
T_NEW,
T_PRINT,
T_PRIVATE,
T_PUBLIC,
T_PROTECTED,
T_REQUIRE,
T_REQUIRE_ONCE,
T_RETURN,
T_STATIC,
T_SWITCH,
T_THROW,
T_TRAIT,
T_TRY,
T_UNSET,
T_USE,
T_VAR,
T_WHILE,
T_YIELD,
T_YIELD_FROM => Highlight::KEYWORD1,
// Not string literals, but identifiers, keywords, etc.
T_STRING => Highlight::IDENTIFIER,
// Types and casts
T_ARRAY_CAST,
T_BOOL_CAST,
T_CALLABLE,
T_DOUBLE_CAST,
T_INT_CAST,
T_OBJECT_CAST,
T_STRING_CAST,
T_UNSET_CAST => Highlight::KEYWORD2,
// Invalid syntax
T_BAD_CHARACTER => Highlight::INVALID,
default => Highlight::NORMAL,
};
}
/**
* Map a single character PHP delimiter or operator to its associated
* highlighting type
*
* @param string $char
* @return int
*/
public static function fromPHPChar(string $char): int
{
return match ($char) {
// Delimiter characters
'[', ']', '{', '}', '(', ')', '"', "'" => Highlight::DELIMITER,
// Single character operators
'?', ',', ';', ':', '^', '%', '+', '-',
'*', '/', '.', '|', '~', '>', '<', '=', '!' => Highlight::OPERATOR,
default => Highlight::NORMAL,
};
}
} }

View File

@ -21,7 +21,7 @@ final class Point {
} }
/** /**
* Create a new Point from another position * Create a new Point from another Point
* *
* @param Point $pos * @param Point $pos
* @return Point * @return Point

View File

@ -389,8 +389,8 @@ class Row {
} }
} }
$tokenHighlight = php_token_to_highlight($token['type']); $tokenHighlight = Highlight::fromPHPToken($token['type']);
$charHighlight = php_char_to_highlight(trim($token['char'])); $charHighlight = Highlight::fromPHPChar(trim($token['char']));
$hl = match(true) { $hl = match(true) {
// Matches a predefined PHP token // Matches a predefined PHP token

View File

@ -247,13 +247,8 @@ function get_file_syntax_map(): array
Syntax::new( Syntax::new(
'PHP', 'PHP',
['.php', 'kilo'], ['.php', 'kilo'],
[ [],
'?php', '$this', '__halt_compiler' [],
],
[
'int', 'float', 'bool', 'string', 'true', 'TRUE', 'false', 'FALSE', 'null', 'NULL',
'void', 'iterable', 'object', 'strict_types'
],
'//', '//',
'/*', '/*',
'*/', '*/',
@ -286,174 +281,3 @@ function get_file_syntax_map(): array
return $db; return $db;
} }
function php_token_to_highlight(int $token): int
{
return match($token) {
// Delimiters
T_ARRAY,
T_CURLY_OPEN,
T_DOLLAR_OPEN_CURLY_BRACES,
T_OPEN_TAG,
T_OPEN_TAG_WITH_ECHO,
T_CLOSE_TAG,
T_START_HEREDOC,
T_END_HEREDOC => Highlight::DELIMITER,
// Number literals and magic constants
T_CLASS_C,
T_DIR,
T_DNUMBER,
T_LNUMBER,
T_FILE,
T_FUNC_C,
T_LINE,
T_METHOD_C,
T_NS_C,
T_NUM_STRING,
T_TRAIT_C => Highlight::NUMBER,
// String literals
T_CONSTANT_ENCAPSED_STRING, T_ENCAPSED_AND_WHITESPACE => Highlight::STRING,
// Simple variables
T_VARIABLE, T_STRING_VARNAME => Highlight::VARIABLE,
// Operators
T_AS,
T_AND_EQUAL,
T_BOOLEAN_AND,
T_BOOLEAN_OR,
T_COALESCE,
T_COALESCE_EQUAL,
T_CONCAT_EQUAL,
T_DEC,
T_DIV_EQUAL,
T_DOUBLE_ARROW,
T_DOUBLE_COLON,
T_ELLIPSIS,
T_INC,
T_INSTANCEOF,
T_INSTEADOF,
T_IS_EQUAL,
T_IS_GREATER_OR_EQUAL,
T_IS_IDENTICAL,
T_IS_NOT_EQUAL,
T_IS_NOT_IDENTICAL,
T_IS_SMALLER_OR_EQUAL,
T_SPACESHIP,
T_LOGICAL_AND,
T_LOGICAL_OR,
T_LOGICAL_XOR,
T_MINUS_EQUAL,
T_MOD_EQUAL,
T_MUL_EQUAL,
T_NS_SEPARATOR,
T_NULLSAFE_OBJECT_OPERATOR,
T_OBJECT_OPERATOR,
T_OR_EQUAL,
T_PLUS_EQUAL,
T_POW,
T_POW_EQUAL,
T_SL,
T_SL_EQUAL,
T_SR,
T_SR_EQUAL,
T_XOR_EQUAL => Highlight::OPERATOR,
// Keywords1
T_ABSTRACT,
T_BREAK,
T_CASE,
T_CATCH,
T_CLASS,
T_CLONE,
T_CONST,
T_CONTINUE,
T_DECLARE,
T_DEFAULT,
T_DO,
T_ECHO,
T_ELSE,
T_ELSEIF,
T_EMPTY,
T_ENDDECLARE,
T_ENDFOR,
T_ENDFOREACH,
T_ENDIF,
T_ENDSWITCH,
T_ENDWHILE,
T_EVAL,
T_EXIT,
T_EXTENDS,
T_FINAL,
T_FINALLY,
T_FN,
T_FOR,
T_FOREACH,
T_FUNCTION,
T_GLOBAL,
T_GOTO,
T_HALT_COMPILER,
T_IF,
T_IMPLEMENTS,
T_INCLUDE,
T_INCLUDE_ONCE,
T_INTERFACE,
T_ISSET,
T_LIST,
T_MATCH,
T_NAMESPACE,
T_NEW,
T_PRINT,
T_PRIVATE,
T_PUBLIC,
T_PROTECTED,
T_REQUIRE,
T_REQUIRE_ONCE,
T_RETURN,
T_STATIC,
T_SWITCH,
T_THROW,
T_TRAIT,
T_TRY,
T_UNSET,
T_USE,
T_VAR,
T_WHILE,
T_YIELD,
T_YIELD_FROM => Highlight::KEYWORD1,
// Not string literals, but identifiers, keywords, etc.
T_STRING => Highlight::IDENTIFIER,
// Types and casts
T_ARRAY_CAST,
T_BOOL_CAST,
T_CALLABLE,
T_DOUBLE_CAST,
T_INT_CAST,
T_OBJECT_CAST,
T_STRING_CAST,
T_UNSET_CAST => Highlight::KEYWORD2,
// Invalid syntax
T_BAD_CHARACTER => Highlight::INVALID,
default => Highlight::NORMAL,
};
}
function php_char_to_highlight(string $char): int
{
return match ($char) {
// Delimiter characters
'[', ']', '{', '}', '(', ')', '"', "'" => Highlight::DELIMITER,
// Single character operators
'?', ',', ';', ':', '^', '%', '+', '-',
'*', '/', '.', '|', '~', '>', '<', '=', '!' => Highlight::OPERATOR,
default => Highlight::NORMAL,
};
}

View File

@ -2714,27 +2714,8 @@
".php", ".php",
"kilo" "kilo"
], ],
"keywords1": [ "keywords1": [],
"?php", "keywords2": [],
"$this",
"__halt_compiler"
],
"keywords2": [
"int",
"float",
"bool",
"string",
"true",
"TRUE",
"false",
"FALSE",
"null",
"NULL",
"void",
"iterable",
"object",
"strict_types"
],
"singleLineCommentStart": "\/\/", "singleLineCommentStart": "\/\/",
"multiLineCommentStart": "\/*", "multiLineCommentStart": "\/*",
"multiLineCommentEnd": "*\/", "multiLineCommentEnd": "*\/",

View File

@ -1,14 +0,0 @@
<?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);