From 2bc98b03083e1e81638f2058a12b536249ab1e39 Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Fri, 25 Oct 2019 14:49:34 -0400 Subject: [PATCH] Keyword highlighting, completing step 174 --- src/Highlight.php | 8 +-- src/Row.php | 37 +++++++++++++ src/Syntax.php | 10 +++- src/functions.php | 6 +++ src/hldb.php | 133 +++++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 188 insertions(+), 6 deletions(-) diff --git a/src/Highlight.php b/src/Highlight.php index 2358094..a32e441 100644 --- a/src/Highlight.php +++ b/src/Highlight.php @@ -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; } \ No newline at end of file diff --git a/src/Row.php b/src/Row.php index 09a33ec..bdbe3c6 100644 --- a/src/Row.php +++ b/src/Row.php @@ -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++; } diff --git a/src/Syntax.php b/src/Syntax.php index 7959916..8e0f471 100644 --- a/src/Syntax.php +++ b/src/Syntax.php @@ -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; diff --git a/src/functions.php b/src/functions.php index 3b4fae1..2a19ff3 100644 --- a/src/functions.php +++ b/src/functions.php @@ -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 diff --git a/src/hldb.php b/src/hldb.php index 8489bcc..801559a 100644 --- a/src/hldb.php +++ b/src/hldb.php @@ -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, ),