From 252eddc093b022a0c8debb81b0201ff21a56ddbf Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Fri, 8 Nov 2019 12:02:00 -0500 Subject: [PATCH] Another ugly progress commit --- kilo | 10 ++------ src/Row.php | 60 +++++++++++++++++++++-------------------------- src/constants.php | 10 ++++++++ src/functions.php | 2 ++ test.php | 5 ++-- 5 files changed, 44 insertions(+), 43 deletions(-) create mode 100644 src/constants.php diff --git a/kilo b/kilo index 5b167ba..b382610 100755 --- a/kilo +++ b/kilo @@ -3,12 +3,8 @@ namespace Kilo; -// ----------------------------------------------------------------------------- -// ! App Constants -// ----------------------------------------------------------------------------- -define('KILO_VERSION', '0.1.0'); -define('KILO_TAB_STOP', 4); -define('KILO_QUIT_TIMES', 3); +require_once __DIR__ . '/src/functions.php'; +require_once __DIR__ . '/src/hldb.php'; // Log notices/errors/warnings to file set_error_handler(static function (int $no, $str, $file, $line, $context) { @@ -24,8 +20,6 @@ set_error_handler(static function (int $no, $str, $file, $line, $context) { }, -1); // Set up autoloading -require_once __DIR__ . '/src/functions.php'; -require_once __DIR__ . '/src/hldb.php'; spl_autoload_register(static function ($class) { $parts = explode('\\', $class); $file = __DIR__ . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . $parts[1] . '.php'; diff --git a/src/Row.php b/src/Row.php index 062bc00..29ba48a 100644 --- a/src/Row.php +++ b/src/Row.php @@ -153,6 +153,8 @@ class Row { '}' => Highlight::DELIMITER, '(' => Highlight::DELIMITER, ')' => Highlight::DELIMITER, + '"' => Highlight::DELIMITER, + "'" => Highlight::DELIMITER, // Single character operators ',' => Highlight::OPERATOR, @@ -492,27 +494,22 @@ class Row { } // A multi-line comment can end in the middle of a line... - while ($inComment) + if ($inComment) { - if ($token['type'] === T_WHITESPACE) - { - $char = $token['char']; - $charStart = strpos($this->render, $char, $offset) - 2; - $inComment = FALSE; - array_replace_range($this->hl, $charStart, 2, Highlight::ML_COMMENT); - $offset += 2; - } - - if (substr($this->render, $offset, 2) === '*/') + // Try looking for the end of the comment first + $commentEnd = strpos($this->render, '*/'); + if ($commentEnd !== FALSE) { $inComment = FALSE; - array_replace_range($this->hl, $offset, 2, Highlight::ML_COMMENT); - $offset += 2; + array_replace_range($this->hl, 0, $commentEnd, Highlight::ML_COMMENT); + $offset = $commentEnd + 2; + continue; } + // Otherwise, just set the whole row + $this->hl = array_fill(0, $this->rsize, Highlight::ML_COMMENT); $this->hl[$offset] = Highlight::ML_COMMENT; - $offset++; - continue; + break; } $char = $token['char']; @@ -526,31 +523,26 @@ class Row { { continue; } - - // Probably not great to create a closure in a loop like this, but - // this halves the boilerplate for each type of syntax - $highlightRange = function (int $hl) use ($charLen, $charStart, &$offset) { - array_replace_range($this->hl, $charStart, $charLen, $hl); - - // Update the offset to the end of the current token - $offset = $charStart + $charLen; - }; + $charEnd = $charStart + $charLen; // Start of multiline comment/single line comment if (in_array($token['type'], [T_DOC_COMMENT, T_COMMENT], TRUE)) { // Single line comments - if (strpos($token['char'], '//') !== FALSE) + if (strpos($char, '//') !== FALSE) { - $highlightRange(Highlight::COMMENT); + array_replace_range($this->hl, $charStart, $charLen, Highlight::COMMENT); break; } // Start of multi-line comment $start = strpos($this->render, '/*', $offset); - $inComment = strpos($this->render, '*/', $offset) === FALSE; - array_replace_range($this->hl, $start, strlen($char) - $offset, Highlight::ML_COMMENT); - $offset = $start + strlen($char) - $offset; + if ($start !== FALSE) + { + $inComment = strpos($this->render, '*/', $offset) === FALSE; + array_replace_range($this->hl, $start, $charLen - $offset, Highlight::ML_COMMENT); + $offset = $start + $charLen - $offset; + } if ($inComment) { @@ -564,7 +556,8 @@ class Row { if (array_key_exists($token['type'], $this->phpTokenHighlightMap)) { $hl = $this->phpTokenHighlightMap[$token['type']]; - $highlightRange($hl); + array_replace_range($this->hl, $charStart, $charLen, $hl); + $offset = $charEnd; continue; } @@ -573,18 +566,19 @@ class Row { { if (in_array($token['char'], $this->parent->syntax->keywords2, TRUE)) { - $highlightRange(Highlight::KEYWORD2); + array_replace_range($this->hl, $charStart, $charLen, Highlight::KEYWORD2); + $offset = $charEnd; continue; } } } - // Highlight raw characters if (($token['typeName'] === 'RAW') && array_key_exists(trim($token['char']), $this->phpCharacterHighlightMap)) { $hl = $this->phpCharacterHighlightMap[trim($token['char'])]; - $highlightRange($hl); + array_replace_range($this->hl, $charStart, $charLen, $hl); + $offset = $charEnd; continue; } } diff --git a/src/constants.php b/src/constants.php new file mode 100644 index 0000000..4670d11 --- /dev/null +++ b/src/constants.php @@ -0,0 +1,10 @@ +new('struct termios'); diff --git a/test.php b/test.php index 2420a4b..c2f5c58 100644 --- a/test.php +++ b/test.php @@ -19,7 +19,7 @@ abstract class Foo implements Ifoo { * Docblock comment */ class FooBar extends Foo implements Ifoo { - public function bar(int $a, float $b, array $c, callable $d): string + public function bar(int $a, float $b, array $c, callable $d, string $e = 'default'): string { $cstr = print_r($c, TRUE); $d(); @@ -48,6 +48,7 @@ $foobar = new FooBar(); $x = 3; +# Perl-style comment $y = [ 1, 2, @@ -56,7 +57,7 @@ $y = [ /* Heredoc -*/ +*/$z = $x + $y; $sql = <<