Another ugly progress commit

This commit is contained in:
Timothy Warren 2019-11-08 12:02:00 -05:00
parent bb10b69e12
commit 252eddc093
5 changed files with 44 additions and 43 deletions

10
kilo
View File

@ -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';

View File

@ -153,6 +153,8 @@ class Row {
'}' => Highlight::DELIMITER,
'(' => Highlight::DELIMITER,
')' => Highlight::DELIMITER,
'"' => Highlight::DELIMITER,
"'" => Highlight::DELIMITER,
// Single character operators
',' => Highlight::OPERATOR,
@ -492,29 +494,24 @@ 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;
}
$this->hl[$offset] = Highlight::ML_COMMENT;
$offset++;
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;
break;
}
$char = $token['char'];
$charLen = strlen($char);
if ($charLen === 0 || $offset >= $this->rsize)
@ -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);
if ($start !== FALSE)
{
$inComment = strpos($this->render, '*/', $offset) === FALSE;
array_replace_range($this->hl, $start, strlen($char) - $offset, Highlight::ML_COMMENT);
$offset = $start + strlen($char) - $offset;
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;
}
}

10
src/constants.php Normal file
View File

@ -0,0 +1,10 @@
<?php declare(strict_types=1);
namespace Kilo;
// -----------------------------------------------------------------------------
// ! App Constants
// -----------------------------------------------------------------------------
define('KILO_VERSION', '0.1.0');
define('KILO_TAB_STOP', 4);
define('KILO_QUIT_TIMES', 3);

View File

@ -4,6 +4,8 @@ namespace Kilo;
use FFI;
require_once __DIR__ . '/constants.php';
/** @var FFI\CData $original_termios */
$original_termios = get_ffi()->new('struct termios');

View File

@ -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 = <<<SQL
SELECT * FROM "foo" WHERE "bar"='baz' AND id={$x};
SQL;