Give PHP enums a go
This commit is contained in:
parent
eb3f7f848d
commit
9214c16595
@ -28,6 +28,6 @@
|
|||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
"ext-ffi": "*",
|
"ext-ffi": "*",
|
||||||
"php": ">= 8.0.0"
|
"php": ">= 8.1.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,44 +8,44 @@ use Aviat\Kilo\Traits;
|
|||||||
* ANSI Color escape sequences
|
* ANSI Color escape sequences
|
||||||
* @enum
|
* @enum
|
||||||
*/
|
*/
|
||||||
class Color {
|
enum Color: int {
|
||||||
use Traits\ConstList;
|
use Traits\ConstList;
|
||||||
|
|
||||||
// Foreground colors
|
// Foreground colors
|
||||||
public const FG_BLACK = 30;
|
case FG_BLACK = 30;
|
||||||
public const FG_RED = 31;
|
case FG_RED = 31;
|
||||||
public const FG_GREEN = 32;
|
case FG_GREEN = 32;
|
||||||
public const FG_YELLOW = 33;
|
case FG_YELLOW = 33;
|
||||||
public const FG_BLUE = 34;
|
case FG_BLUE = 34;
|
||||||
public const FG_MAGENTA = 35;
|
case FG_MAGENTA = 35;
|
||||||
public const FG_CYAN = 36;
|
case FG_CYAN = 36;
|
||||||
public const FG_WHITE = 37;
|
case FG_WHITE = 37;
|
||||||
public const FG_BRIGHT_BLACK = 90;
|
case FG_BRIGHT_BLACK = 90;
|
||||||
public const FG_BRIGHT_RED = 91;
|
case FG_BRIGHT_RED = 91;
|
||||||
public const FG_BRIGHT_GREEN = 92;
|
case FG_BRIGHT_GREEN = 92;
|
||||||
public const FG_BRIGHT_YELLOW = 93;
|
case FG_BRIGHT_YELLOW = 93;
|
||||||
public const FG_BRIGHT_BLUE = 94;
|
case FG_BRIGHT_BLUE = 94;
|
||||||
public const FG_BRIGHT_MAGENTA = 95;
|
case FG_BRIGHT_MAGENTA = 95;
|
||||||
public const FG_BRIGHT_CYAN = 96;
|
case FG_BRIGHT_CYAN = 96;
|
||||||
public const FG_BRIGHT_WHITE = 97;
|
case FG_BRIGHT_WHITE = 97;
|
||||||
|
|
||||||
// Background colors
|
// Background colors
|
||||||
public const BG_BLACK = 40;
|
case BG_BLACK = 40;
|
||||||
public const BG_RED = 41;
|
case BG_RED = 41;
|
||||||
public const BG_GREEN = 42;
|
case BG_GREEN = 42;
|
||||||
public const BG_YELLOW = 43;
|
case BG_YELLOW = 43;
|
||||||
public const BG_BLUE = 44;
|
case BG_BLUE = 44;
|
||||||
public const BG_MAGENTA = 45;
|
case BG_MAGENTA = 45;
|
||||||
public const BG_CYAN = 46;
|
case BG_CYAN = 46;
|
||||||
public const BG_WHITE = 47;
|
case BG_WHITE = 47;
|
||||||
public const BG_BRIGHT_BLACK = 100;
|
case BG_BRIGHT_BLACK = 100;
|
||||||
public const BG_BRIGHT_RED = 101;
|
case BG_BRIGHT_RED = 101;
|
||||||
public const BG_BRIGHT_GREEN = 102;
|
case BG_BRIGHT_GREEN = 102;
|
||||||
public const BG_BRIGHT_YELLOW = 103;
|
case BG_BRIGHT_YELLOW = 103;
|
||||||
public const BG_BRIGHT_BLUE = 104;
|
case BG_BRIGHT_BLUE = 104;
|
||||||
public const BG_BRIGHT_MAGENTA = 105;
|
case BG_BRIGHT_MAGENTA = 105;
|
||||||
public const BG_BRIGHT_CYAN = 106;
|
case BG_BRIGHT_CYAN = 106;
|
||||||
public const BG_BRIGHT_WHITE = 107;
|
case BG_BRIGHT_WHITE = 107;
|
||||||
|
|
||||||
public const INVERT = 7;
|
case INVERT = 7;
|
||||||
}
|
}
|
@ -3,35 +3,36 @@
|
|||||||
namespace Aviat\Kilo\Enum;
|
namespace Aviat\Kilo\Enum;
|
||||||
|
|
||||||
use Aviat\Kilo\Traits;
|
use Aviat\Kilo\Traits;
|
||||||
|
use JsonSerializable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @enum
|
* @enum
|
||||||
*/
|
*/
|
||||||
class Highlight {
|
enum Highlight implements JsonSerializable {
|
||||||
use Traits\ConstList;
|
use Traits\EnumTrait;
|
||||||
|
|
||||||
public const NORMAL = 0;
|
case Normal;
|
||||||
public const COMMENT = 1;
|
case Comment;
|
||||||
public const ML_COMMENT= 2;
|
case MultiLineComment;
|
||||||
public const KEYWORD1 = 3;
|
case Keyword1;
|
||||||
public const KEYWORD2 = 4;
|
case Keyword2;
|
||||||
public const STRING = 5;
|
case String;
|
||||||
public const NUMBER = 6;
|
case Number;
|
||||||
public const OPERATOR = 7;
|
case Operator;
|
||||||
public const VARIABLE = 8;
|
case Variable;
|
||||||
public const DELIMITER = 9;
|
case Delimiter;
|
||||||
public const INVALID = 10;
|
case Invalid;
|
||||||
public const MATCH = 11;
|
case SearchMatch;
|
||||||
public const IDENTIFIER = 12;
|
case Identifier;
|
||||||
public const CHARACTER = 13;
|
case Character;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Map a PHP syntax token to its associated highlighting type
|
* Map a PHP syntax token to its associated highlighting type
|
||||||
*
|
*
|
||||||
* @param int $token
|
* @param int $token
|
||||||
* @return int
|
* @return Highlight
|
||||||
*/
|
*/
|
||||||
public static function fromPHPToken(int $token): int
|
public static function fromPHPToken(int $token): self
|
||||||
{
|
{
|
||||||
return match($token) {
|
return match($token) {
|
||||||
// Delimiters
|
// Delimiters
|
||||||
@ -42,7 +43,7 @@ class Highlight {
|
|||||||
T_OPEN_TAG_WITH_ECHO,
|
T_OPEN_TAG_WITH_ECHO,
|
||||||
T_CLOSE_TAG,
|
T_CLOSE_TAG,
|
||||||
T_START_HEREDOC,
|
T_START_HEREDOC,
|
||||||
T_END_HEREDOC => Highlight::DELIMITER,
|
T_END_HEREDOC => Highlight::Delimiter,
|
||||||
|
|
||||||
// Number literals and magic constants
|
// Number literals and magic constants
|
||||||
T_CLASS_C,
|
T_CLASS_C,
|
||||||
@ -55,13 +56,13 @@ class Highlight {
|
|||||||
T_METHOD_C,
|
T_METHOD_C,
|
||||||
T_NS_C,
|
T_NS_C,
|
||||||
T_NUM_STRING,
|
T_NUM_STRING,
|
||||||
T_TRAIT_C => Highlight::NUMBER,
|
T_TRAIT_C => Highlight::Number,
|
||||||
|
|
||||||
// String literals
|
// String literals
|
||||||
T_CONSTANT_ENCAPSED_STRING, T_ENCAPSED_AND_WHITESPACE => Highlight::STRING,
|
T_CONSTANT_ENCAPSED_STRING, T_ENCAPSED_AND_WHITESPACE => Highlight::String,
|
||||||
|
|
||||||
// Simple variables
|
// Simple variables
|
||||||
T_VARIABLE, T_STRING_VARNAME => Highlight::VARIABLE,
|
T_VARIABLE, T_STRING_VARNAME => Highlight::Variable,
|
||||||
|
|
||||||
// Operators
|
// Operators
|
||||||
T_AS,
|
T_AS,
|
||||||
@ -103,7 +104,7 @@ class Highlight {
|
|||||||
T_SL_EQUAL,
|
T_SL_EQUAL,
|
||||||
T_SR,
|
T_SR,
|
||||||
T_SR_EQUAL,
|
T_SR_EQUAL,
|
||||||
T_XOR_EQUAL => Highlight::OPERATOR,
|
T_XOR_EQUAL => Highlight::Operator,
|
||||||
|
|
||||||
// Keywords1
|
// Keywords1
|
||||||
T_ABSTRACT,
|
T_ABSTRACT,
|
||||||
@ -166,10 +167,10 @@ class Highlight {
|
|||||||
T_VAR,
|
T_VAR,
|
||||||
T_WHILE,
|
T_WHILE,
|
||||||
T_YIELD,
|
T_YIELD,
|
||||||
T_YIELD_FROM => Highlight::KEYWORD1,
|
T_YIELD_FROM => Highlight::Keyword1,
|
||||||
|
|
||||||
// Not string literals, but identifiers, keywords, etc.
|
// Not string literals, but identifiers, keywords, etc.
|
||||||
T_STRING => Highlight::IDENTIFIER,
|
T_STRING => Highlight::Identifier,
|
||||||
|
|
||||||
// Types and casts
|
// Types and casts
|
||||||
T_ARRAY_CAST,
|
T_ARRAY_CAST,
|
||||||
@ -179,12 +180,12 @@ class Highlight {
|
|||||||
T_INT_CAST,
|
T_INT_CAST,
|
||||||
T_OBJECT_CAST,
|
T_OBJECT_CAST,
|
||||||
T_STRING_CAST,
|
T_STRING_CAST,
|
||||||
T_UNSET_CAST => Highlight::KEYWORD2,
|
T_UNSET_CAST => Highlight::Keyword2,
|
||||||
|
|
||||||
// Invalid syntax
|
// Invalid syntax
|
||||||
T_BAD_CHARACTER => Highlight::INVALID,
|
T_BAD_CHARACTER => Highlight::Invalid,
|
||||||
|
|
||||||
default => Highlight::NORMAL,
|
default => Highlight::Normal,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -193,19 +194,19 @@ class Highlight {
|
|||||||
* highlighting type
|
* highlighting type
|
||||||
*
|
*
|
||||||
* @param string $char
|
* @param string $char
|
||||||
* @return int
|
* @return Highlight
|
||||||
*/
|
*/
|
||||||
public static function fromPHPChar(string $char): int
|
public static function fromPHPChar(string $char): self
|
||||||
{
|
{
|
||||||
return match ($char) {
|
return match ($char) {
|
||||||
// Delimiter characters
|
// Delimiter characters
|
||||||
'[', ']', '{', '}', '(', ')', '"', "'" => Highlight::DELIMITER,
|
'[', ']', '{', '}', '(', ')', '"', "'" => Highlight::Delimiter,
|
||||||
|
|
||||||
// Single character operators
|
// Single character operators
|
||||||
'?', ',', ';', ':', '^', '%', '+', '-',
|
'?', ',', ';', ':', '^', '%', '+', '-',
|
||||||
'*', '/', '.', '|', '~', '>', '<', '=', '!' => Highlight::OPERATOR,
|
'*', '/', '.', '|', '~', '>', '<', '=', '!' => Highlight::Operator,
|
||||||
|
|
||||||
default => Highlight::NORMAL,
|
default => Highlight::Normal,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -3,24 +3,24 @@
|
|||||||
namespace Aviat\Kilo\Enum;
|
namespace Aviat\Kilo\Enum;
|
||||||
|
|
||||||
use Aviat\Kilo\Traits;
|
use Aviat\Kilo\Traits;
|
||||||
|
use JsonSerializable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constants representing various control keys
|
* Enum representing various control keys
|
||||||
* @enum
|
|
||||||
*/
|
*/
|
||||||
class KeyType {
|
enum KeyType implements JsonSerializable {
|
||||||
use Traits\ConstList;
|
use Traits\EnumTrait;
|
||||||
|
|
||||||
public const ARROW_DOWN = 'KEY_ARROW_DOWN';
|
case ArrowDown;
|
||||||
public const ARROW_LEFT = 'KEY_ARROW_LEFT';
|
case ArrowLeft;
|
||||||
public const ARROW_RIGHT = 'KEY_ARROW_RIGHT';
|
case ArrowRight;
|
||||||
public const ARROW_UP = 'KEY_ARROW_UP';
|
case ArrowUp;
|
||||||
public const BACKSPACE = 'KEY_BACKSPACE';
|
case Backspace;
|
||||||
public const DELETE = 'KEY_DELETE';
|
case Delete;
|
||||||
public const END = 'KEY_END';
|
case End;
|
||||||
public const ENTER = 'KEY_ENTER';
|
case Enter;
|
||||||
public const ESCAPE = 'KEY_ESCAPE';
|
case Escape;
|
||||||
public const HOME = 'KEY_HOME';
|
case Home;
|
||||||
public const PAGE_DOWN = 'KEY_PAGE_DOWN';
|
case PageDown;
|
||||||
public const PAGE_UP = 'KEY_PAGE_UP';
|
case PageUp;
|
||||||
}
|
}
|
@ -7,9 +7,9 @@ use Aviat\Kilo\Traits;
|
|||||||
/**
|
/**
|
||||||
* @enum
|
* @enum
|
||||||
*/
|
*/
|
||||||
class SearchDirection {
|
enum SearchDirection: int {
|
||||||
use Traits\ConstList;
|
use Traits\ConstList;
|
||||||
|
|
||||||
public const FORWARD = 1;
|
case FORWARD = 1;
|
||||||
public const BACKWARD = -1;
|
case BACKWARD = -1;
|
||||||
}
|
}
|
@ -3,14 +3,11 @@
|
|||||||
namespace Aviat\Kilo\Enum;
|
namespace Aviat\Kilo\Enum;
|
||||||
|
|
||||||
use Aviat\Kilo\Traits;
|
use Aviat\Kilo\Traits;
|
||||||
|
use JsonSerializable;
|
||||||
|
|
||||||
/**
|
enum SyntaxFamily implements JsonSerializable {
|
||||||
*
|
use Traits\EnumTrait;
|
||||||
*/
|
|
||||||
class SyntaxFamily {
|
|
||||||
use Traits\ConstList;
|
|
||||||
|
|
||||||
public const C = 'C';
|
case C;
|
||||||
|
case XML;
|
||||||
public const XML = 'XML';
|
|
||||||
}
|
}
|
20
src/Traits/EnumTrait.php
Normal file
20
src/Traits/EnumTrait.php
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Aviat\Kilo\Traits;
|
||||||
|
|
||||||
|
trait EnumTrait {
|
||||||
|
public function jsonSerialize(): mixed
|
||||||
|
{
|
||||||
|
if (property_exists($this, 'value'))
|
||||||
|
{
|
||||||
|
return $this->value;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (property_exists($this, 'name'))
|
||||||
|
{
|
||||||
|
return $this->name;
|
||||||
|
}
|
||||||
|
|
||||||
|
return print_r($this, true);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user