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