Refactor syntax class to use boolean flags instead of bitflags, allow conditional rendering of characters

This commit is contained in:
Timothy Warren 2021-04-09 19:40:28 -04:00
parent 30b4c3818a
commit ead70c33ac
5 changed files with 75 additions and 9 deletions

View File

@ -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";

View File

@ -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

View File

@ -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(),
};

View File

@ -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;
}
}

View File

@ -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,