Highlight strings, complete step 168

This commit is contained in:
Timothy Warren 2019-10-25 11:49:04 -04:00
parent 17283f5011
commit 3dc3fc01aa
5 changed files with 45 additions and 7 deletions

View File

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

View File

@ -115,6 +115,7 @@ class Row {
$this->hl = array_fill(0, $this->rsize, Highlight::NORMAL); $this->hl = array_fill(0, $this->rsize, Highlight::NORMAL);
$prevSep = TRUE; $prevSep = TRUE;
$inString = '';
$i = 0; $i = 0;
@ -128,6 +129,38 @@ class Row {
return; return;
} }
if ($this->parent->syntax->flags & Syntax::HIGHLIGHT_STRINGS)
{
if ($inString !== '')
{
$this->hl[$i] = Highlight::STRING;
// Check for escaped character
if ($char === '\\' && $i+1 < $this->rsize)
{
$this->hl[$i + 1] = Highlight::STRING;
$i += 2;
continue;
}
if ($char === $inString)
{
$inString = '';
}
$i++;
$prevSep = 1;
continue;
}
if ( $char === '""' || $char === '\'')
{
$inString = $char;
$this->hl[$i] = Highlight::STRING;
$i++;
continue;
}
}
if ($this->parent->syntax->flags & Syntax::HIGHLIGHT_NUMBERS) if ($this->parent->syntax->flags & Syntax::HIGHLIGHT_NUMBERS)
{ {
// Numbers, including decimal points // Numbers, including decimal points

View File

@ -4,6 +4,7 @@ namespace Kilo;
class Syntax { class Syntax {
public const HIGHLIGHT_NUMBERS = (1 << 0); public const HIGHLIGHT_NUMBERS = (1 << 0);
public const HIGHLIGHT_STRINGS = (1 << 1);
public string $filetype = ''; public string $filetype = '';
public array $filematch = []; public array $filematch = [];

View File

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

View File

@ -12,27 +12,27 @@ function get_hldb(): array
Syntax::new( Syntax::new(
'C', 'C',
['.c', '.h', '.cpp'], ['.c', '.h', '.cpp'],
Syntax::HIGHLIGHT_NUMBERS, 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_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_NUMBERS | Syntax::HIGHLIGHT_STRINGS,
), ),
Syntax::new( Syntax::new(
'PHP', 'PHP',
['.php'], ['.php'],
Syntax::HIGHLIGHT_NUMBERS, Syntax::HIGHLIGHT_NUMBERS | Syntax::HIGHLIGHT_STRINGS,
), ),
Syntax::new( Syntax::new(
'Rust', 'Rust',
['.rs'], ['.rs'],
Syntax::HIGHLIGHT_NUMBERS, Syntax::HIGHLIGHT_NUMBERS | Syntax::HIGHLIGHT_STRINGS,
), ),
]; ];
} }