Keyword highlighting, completing step 174

This commit is contained in:
Timothy Warren 2019-10-25 14:49:34 -04:00
parent cf0436dc7d
commit 2bc98b0308
5 changed files with 188 additions and 6 deletions

View File

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

View File

@ -114,6 +114,9 @@ class Row {
{
$this->hl = array_fill(0, $this->rsize, Highlight::NORMAL);
$keywords1 = $this->parent->syntax->keywords1;
$keywords2 = $this->parent->syntax->keywords2;
$scs = $this->parent->syntax->singleLineCommentStart;
$prevSep = TRUE;
@ -186,6 +189,40 @@ class Row {
}
}
// Keywords
if ($prevSep)
{
foreach ($keywords1 as $k)
{
$klen = strlen($k);
$nextCharOffset = $i + $klen;
$isEndOfLine = $nextCharOffset >= $this->rsize;
$nextChar = ($isEndOfLine) ? "\0" : $this->render[$nextCharOffset];
if (substr($this->render, $i, $klen) === $k && is_separator($nextChar))
{
array_replace_range($this->hl, $i, $klen, Highlight::KEYWORD1);
$i += $klen - 1;
break;
}
}
foreach ($keywords2 as $k)
{
$klen = strlen($k);
$nextCharOffset = $i + $klen;
$isEndOfLine = $nextCharOffset >= $this->rsize;
$nextChar = ($isEndOfLine) ? "\0" : $this->render[$nextCharOffset];
if (substr($this->render, $i, $klen) === $k && is_separator($nextChar))
{
array_replace_range($this->hl, $i, $klen, Highlight::KEYWORD2);
$i += $klen - 1;
break;
}
}
}
$prevSep = is_separator($char);
$i++;
}

View File

@ -10,16 +10,22 @@ class Syntax {
public array $filematch = [];
public string $singleLineCommentStart = '//';
public array $keywords1 = [];
public array $keywords2 = [];
public int $flags = 0;
public static function new(string $name, array $extList, string $slcs, int $flags): self
public static function new(string $name, array $extList, array $keywords1, array $keywords2, string $slcs, int $flags): self
{
$self = new self();
$self->filetype = $name;
$self->filematch = $extList;
$self->singleLineCommentStart = '//';
$self->keywords1 = $keywords1;
$self->keywords2 = $keywords2;
$self->singleLineCommentStart = $slcs;
$self->flags = $flags;

View File

@ -283,6 +283,12 @@ function syntax_to_color(int $hl): int
case Highlight::COMMENT:
return 36; // Foreground Cyan
case Highlight::KEYWORD1:
return 33; // Foreground Yellow
case Highlight::KEYWORD2:
return 32; // Foreground Green
case Highlight::STRING:
return 35; // Foreground Magenta

View File

@ -12,30 +12,161 @@ function get_hldb(): array
Syntax::new(
'C',
['.c', '.h', '.cpp'],
[
'continue', 'typedef', 'switch', 'return', 'static', 'while', 'break', 'struct',
'union', 'class', 'else', 'enum', 'for', 'case', 'if',
],
[
'#include', 'unsigned', '#define', '#ifndef', 'double', 'signed', '#endif',
'#ifdef', 'float', '#error', '#undef', 'long', 'char', 'int', 'void', '#if',
],
'//',
Syntax::HIGHLIGHT_NUMBERS | Syntax::HIGHLIGHT_STRINGS,
),
Syntax::new(
'CSS',
['.css', '.less', '.sass', 'scss'],
[],
[],
'',
Syntax::HIGHLIGHT_NUMBERS | Syntax::HIGHLIGHT_STRINGS,
),
Syntax::new(
'JavaScript',
['.js', '.jsx', '.ts', '.tsx', '.jsm', '.es'],
['.js', '.jsx', '.ts', '.tsx', '.jsm', '.mjs', '.es'],
[
'instanceof',
'continue',
'debugger',
'function',
'default',
'extends',
'finally',
'delete',
'export',
'import',
'return',
'switch',
'typeof',
'break',
'catch',
'class',
'const',
'super',
'throw',
'while',
'yield',
'case',
'else',
'this',
'void',
'with',
'from',
'for',
'new',
'try',
'var',
'do',
'if',
'in',
'as',
],
[
'=>', 'Number', 'String', 'Object', 'Math', 'JSON', 'Boolean',
],
'//',
Syntax::HIGHLIGHT_NUMBERS | Syntax::HIGHLIGHT_STRINGS,
),
Syntax::new(
'PHP',
['.php'],
[
'?php', '$this', '__halt_compiler', 'abstract', 'and', 'array', 'as', 'break',
'callable', 'case', 'catch', 'class', 'clone', 'const', 'continue', 'declare',
'default', 'die', 'do', 'echo', 'else', 'elseif', 'empty', 'enddeclare', 'endfor',
'endforeach', 'endif', 'endswitch', 'endwhile', 'eval', 'exit', 'extends',
'final', 'finally', 'for', 'foreach', 'function', 'global', 'goto', 'if', 'implements',
'include', 'include_once', 'instanceof', 'insteadof', 'interface', 'isset', 'list',
'namespace', 'new', 'or', 'print', 'private', 'protected', 'public', 'require', 'require_once',
'return', 'static', 'switch', 'throw', 'trait', 'try', 'unset', 'use', 'var', 'while', 'xor',
'yield', 'yield from', '__CLASS__', '__DIR__', '__FILE__', '__FUNCTION__', '__LINE__',
'__METHOD__', '__NAMESPACE__', '__TRAIT__',
],
[
'int', 'float', 'bool', 'string', 'true', 'TRUE', 'false', 'FALSE', 'null', 'NULL',
'void', 'iterable', 'object', 'strict_types'
],
'//',
Syntax::HIGHLIGHT_NUMBERS | Syntax::HIGHLIGHT_STRINGS,
),
Syntax::new(
'Rust',
['.rs'],
[
'continue', 'return', 'static', 'struct', 'unsafe', 'break', 'const', 'crate',
'extern', 'match', 'super', 'trait', 'where', 'else', 'enum', 'false', 'impl',
'loop', 'move', 'self', 'type', 'while', 'for', 'let', 'mod', 'pub', 'ref', 'true',
'use', 'mut', 'as', 'fn', 'if', 'in',
],
[
'DoubleEndedIterator',
'ExactSizeIterator',
'IntoIterator',
'PartialOrd',
'PartialEq',
'Iterator',
'ToString',
'Default',
'ToOwned',
'Extend',
'FnOnce',
'Option',
'String',
'AsMut',
'AsRef',
'Clone',
'Debug',
'FnMut',
'Sized',
'Unpin',
'array',
'isize',
'usize',
'&str',
'Copy',
'Drop',
'From',
'Into',
'None',
'Self',
'Send',
'Some',
'Sync',
'Sync',
'bool',
'char',
'i128',
'u128',
'Box',
'Err',
'Ord',
'Vec',
'dyn',
'f32',
'f64',
'i16',
'i32',
'i64',
'str',
'u16',
'u32',
'u64',
'Eq',
'Fn',
'Ok',
'i8',
'u8',
],
'//',
Syntax::HIGHLIGHT_NUMBERS | Syntax::HIGHLIGHT_STRINGS,
),