From ead70c33ac82ece7b961404219d73fb6af39b0e6 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 9 Apr 2021 19:40:28 -0400 Subject: [PATCH] Refactor syntax class to use boolean flags instead of bitflags, allow conditional rendering of characters --- src/ANSI.php | 2 +- src/Enum/Highlight.php | 1 + src/FileType.php | 18 ++++++++++++ src/Syntax.php | 62 ++++++++++++++++++++++++++++++++++++------ src/functions.php | 1 + 5 files changed, 75 insertions(+), 9 deletions(-) diff --git a/src/ANSI.php b/src/ANSI.php index b2ea018..9d96e04 100644 --- a/src/ANSI.php +++ b/src/ANSI.php @@ -27,7 +27,7 @@ class ANSI { /** * Removes text attributes, such as bold, underline, blink, inverted colors */ - public const RESET_TEXT = "\e[0m"; + public const RESET_TEXT = "\e[m"; public const BOLD_TEXT = "\e[1m"; diff --git a/src/Enum/Highlight.php b/src/Enum/Highlight.php index 80cd0f3..3bd1c54 100644 --- a/src/Enum/Highlight.php +++ b/src/Enum/Highlight.php @@ -23,6 +23,7 @@ class Highlight { public const INVALID = 10; public const MATCH = 11; public const IDENTIFIER = 12; + public const CHARACTER = 13; /** * Map a PHP syntax token to its associated highlighting type diff --git a/src/FileType.php b/src/FileType.php index 2436f44..337656f 100644 --- a/src/FileType.php +++ b/src/FileType.php @@ -29,11 +29,27 @@ class FileType { '#include', 'unsigned', '#define', '#ifndef', 'double', 'signed', '#endif', '#ifdef', 'float', '#error', '#undef', 'long', 'char', 'int', 'void', '#if', ], + hasCharType: true, + highlightCharacters: true, ), '.css', '.less', '.sass', '.scss' => Syntax::new( 'CSS', slcs: '', ), + '.go' => Syntax::new( + 'Go', + [ + 'break', 'case', 'chan', 'const', 'continue', 'default', 'defer', 'else', + 'fallthrough', 'for', 'func', 'go', 'goto', 'if', 'import', 'interface', + 'map', 'package', 'range', 'return', 'select', 'struct', 'switch', 'type', 'var', + ], + [ + 'uint8', 'uint16', 'uint32', 'uint64', 'int8', 'int16', 'int32', 'int64', 'float32', 'float64', + 'uint', 'int', 'uintptr', 'complex64', 'complex128', 'byte', 'rune', '[]', + ], + hasCharType: true, + highlightCharacters: true, + ), '.js', '.jsx', '.ts', '.tsx', '.jsm', '.mjs', '.es' => Syntax::new( 'JavaScript', [ @@ -63,6 +79,8 @@ class FileType { 'Sync', 'bool', 'char', 'i128', 'u128', 'Box', 'Err', 'Ord', 'Vec', 'dyn', 'f32', 'f64', 'i16', 'i32', 'i64', 'str', 'u16', 'u32', 'u64', 'Eq', 'Fn', 'Ok', 'i8', 'u8', ], + hasCharType: true, + highlightCharacters: true, ), default => Syntax::default(), }; diff --git a/src/Syntax.php b/src/Syntax.php index 5f2f933..2c01aff 100644 --- a/src/Syntax.php +++ b/src/Syntax.php @@ -3,9 +3,6 @@ namespace Aviat\Kilo; class Syntax { - public const HIGHLIGHT_NUMBERS = (1 << 0); - public const HIGHLIGHT_STRINGS = (1 << 1); - // Tokens for PHP files public array $tokens = []; @@ -16,15 +13,31 @@ class Syntax { string $slcs = '//', string $mcs = '/*', string $mce = '*/', - int $flags = self::HIGHLIGHT_NUMBERS | self::HIGHLIGHT_STRINGS, + bool $highlightNumbers = true, + bool $highlightStrings = true, + bool $highlightComments = true, + bool $hasCharType = false, + bool $highlightCharacters = false, ): self { - return new self($name, $keywords1, $keywords2, $slcs, $mcs, $mce, $flags); + return new self( + $name, + $keywords1, + $keywords2, + $slcs, + $mcs, + $mce, + $highlightNumbers, + $hasCharType, + $highlightCharacters, + $highlightStrings, + $highlightComments, + ); } public static function default(): self { - return self::new('No filetype', slcs: '', mcs: '', mce: '', flags: 0); + return self::new('No filetype', slcs: '', mcs: '', mce: '', highlightNumbers: false, highlightStrings: false); } private function __construct( @@ -40,7 +53,40 @@ class Syntax { public string $multiLineCommentStart, /** Syntax to end a multi-line commment */ public string $multiLineCommentEnd, - /** Bitflags configuring the specified language syntax */ - public int $flags, + /** Should we highlight numbers? */ + private bool $highlightNumbers, + /** Does this language have a character type, separate from strings? */ + private bool $hasCharType, + /** Should we highlight chars? */ + private bool $highlightCharacters, + /** should we highlight Strings? */ + private bool $highlightStrings, + /** should we highlight comments? */ + private bool $highlightComments, ) {} + + public function numbers(): bool + { + return $this->highlightNumbers; + } + + public function strings(): bool + { + return $this->highlightStrings; + } + + public function hasChar(): bool + { + return $this->hasCharType; + } + + public function characters(): bool + { + return $this->hasCharType && $this->highlightCharacters; + } + + public function comments(): bool + { + return $this->highlightComments; + } } \ No newline at end of file diff --git a/src/functions.php b/src/functions.php index 4a559f4..bd1f0a5 100644 --- a/src/functions.php +++ b/src/functions.php @@ -165,6 +165,7 @@ function syntax_to_color(int $hl): int Highlight::KEYWORD1 => Color::FG_YELLOW, Highlight::KEYWORD2 => Color::FG_GREEN, Highlight::STRING => Color::FG_MAGENTA, + Highlight::CHARACTER => Color::FG_BRIGHT_MAGENTA, Highlight::NUMBER => Color::FG_BRIGHT_RED, Highlight::OPERATOR => Color::FG_BRIGHT_GREEN, Highlight::VARIABLE => Color::FG_BRIGHT_CYAN,