Highlight single-line comments, finish step 171

This commit is contained in:
Timothy Warren 2019-10-25 12:08:21 -04:00
parent 3dc3fc01aa
commit cf0436dc7d
5 changed files with 29 additions and 5 deletions

View File

@ -4,7 +4,8 @@ namespace Kilo;
class Highlight { class Highlight {
public const NORMAL = 0; public const NORMAL = 0;
public const STRING = 1; public const COMMENT = 1;
public const NUMBER = 2; public const STRING = 2;
public const MATCH = 3; public const NUMBER = 3;
public const MATCH = 4;
} }

View File

@ -114,6 +114,8 @@ class Row {
{ {
$this->hl = array_fill(0, $this->rsize, Highlight::NORMAL); $this->hl = array_fill(0, $this->rsize, Highlight::NORMAL);
$scs = $this->parent->syntax->singleLineCommentStart;
$prevSep = TRUE; $prevSep = TRUE;
$inString = ''; $inString = '';
@ -129,6 +131,14 @@ class Row {
return; return;
} }
// Single-line comments
if ($scs !== '' && empty($inString) && substr($this->render, $i, strlen($scs)) === $scs)// strncmp($char, $scs, strlen($scs)) === 0)
{
array_replace_range($this->hl, $i, $this->rsize - $i, Highlight::COMMENT);
break;
}
// String/Char literals
if ($this->parent->syntax->flags & Syntax::HIGHLIGHT_STRINGS) if ($this->parent->syntax->flags & Syntax::HIGHLIGHT_STRINGS)
{ {
if ($inString !== '') if ($inString !== '')
@ -161,9 +171,9 @@ class Row {
} }
} }
// Numbers, including decimal points
if ($this->parent->syntax->flags & Syntax::HIGHLIGHT_NUMBERS) if ($this->parent->syntax->flags & Syntax::HIGHLIGHT_NUMBERS)
{ {
// Numbers, including decimal points
if ( if (
($char === '.' && $prevHl === Highlight::NUMBER) || ($char === '.' && $prevHl === Highlight::NUMBER) ||
(($prevSep || $prevHl === Highlight::NUMBER) && is_digit($char)) (($prevSep || $prevHl === Highlight::NUMBER) && is_digit($char))

View File

@ -8,14 +8,19 @@ class Syntax {
public string $filetype = ''; public string $filetype = '';
public array $filematch = []; public array $filematch = [];
public string $singleLineCommentStart = '//';
public int $flags = 0; public int $flags = 0;
public static function new(string $name, array $extList, int $flags): self public static function new(string $name, array $extList, string $slcs, int $flags): self
{ {
$self = new self(); $self = new self();
$self->filetype = $name; $self->filetype = $name;
$self->filematch = $extList; $self->filematch = $extList;
$self->singleLineCommentStart = '//';
$self->flags = $flags; $self->flags = $flags;
return $self; return $self;

View File

@ -280,6 +280,9 @@ function syntax_to_color(int $hl): int
{ {
switch ($hl) switch ($hl)
{ {
case Highlight::COMMENT:
return 36; // Foreground Cyan
case Highlight::STRING: case Highlight::STRING:
return 35; // Foreground Magenta return 35; // Foreground Magenta

View File

@ -12,26 +12,31 @@ function get_hldb(): array
Syntax::new( Syntax::new(
'C', 'C',
['.c', '.h', '.cpp'], ['.c', '.h', '.cpp'],
'//',
Syntax::HIGHLIGHT_NUMBERS | Syntax::HIGHLIGHT_STRINGS, Syntax::HIGHLIGHT_NUMBERS | Syntax::HIGHLIGHT_STRINGS,
), ),
Syntax::new( Syntax::new(
'CSS', 'CSS',
['.css', '.less', '.sass', 'scss'], ['.css', '.less', '.sass', 'scss'],
'',
Syntax::HIGHLIGHT_NUMBERS | Syntax::HIGHLIGHT_STRINGS, Syntax::HIGHLIGHT_NUMBERS | Syntax::HIGHLIGHT_STRINGS,
), ),
Syntax::new( Syntax::new(
'JavaScript', 'JavaScript',
['.js', '.jsx', '.ts', '.tsx', '.jsm', '.es'], ['.js', '.jsx', '.ts', '.tsx', '.jsm', '.es'],
'//',
Syntax::HIGHLIGHT_NUMBERS | Syntax::HIGHLIGHT_STRINGS, Syntax::HIGHLIGHT_NUMBERS | Syntax::HIGHLIGHT_STRINGS,
), ),
Syntax::new( Syntax::new(
'PHP', 'PHP',
['.php'], ['.php'],
'//',
Syntax::HIGHLIGHT_NUMBERS | Syntax::HIGHLIGHT_STRINGS, Syntax::HIGHLIGHT_NUMBERS | Syntax::HIGHLIGHT_STRINGS,
), ),
Syntax::new( Syntax::new(
'Rust', 'Rust',
['.rs'], ['.rs'],
'//',
Syntax::HIGHLIGHT_NUMBERS | Syntax::HIGHLIGHT_STRINGS, Syntax::HIGHLIGHT_NUMBERS | Syntax::HIGHLIGHT_STRINGS,
), ),
]; ];