From da2bec354ad115905a822377ae49fd83652f9c94 Mon Sep 17 00:00:00 2001 From: Timothy J Warren Date: Wed, 6 Nov 2019 13:57:19 -0500 Subject: [PATCH] Ugly progress commit --- src/Row.php | 114 +++++++++++++++++++++++++++++++++++++--------------- test.php | 10 ++++- 2 files changed, 90 insertions(+), 34 deletions(-) diff --git a/src/Row.php b/src/Row.php index 89d3299..3485491 100644 --- a/src/Row.php +++ b/src/Row.php @@ -35,8 +35,9 @@ class Row { T_DNUMBER => Highlight::NUMBER, T_LNUMBER => Highlight::NUMBER, - // Simple string literals + // String literals T_CONSTANT_ENCAPSED_STRING => Highlight::STRING, + T_ENCAPSED_AND_WHITESPACE => Highlight::STRING, // Simple variables T_VARIABLE => Highlight::VARIABLE, @@ -133,6 +134,15 @@ class Row { // Not string literals, but identifiers, keywords, etc. // T_STRING => Highlight::KEYWORD2, + + // Type casts + T_ARRAY_CAST => Highlight::KEYWORD2, + T_BOOL_CAST => Highlight::KEYWORD2, + T_DOUBLE_CAST => Highlight::KEYWORD2, + T_INT_CAST => Highlight::KEYWORD2, + T_OBJECT_CAST => Highlight::KEYWORD2, + T_STRING_CAST => Highlight::KEYWORD2, + T_UNSET_CAST => Highlight::KEYWORD2, ]; private array $phpCharacterHighlightMap = [ @@ -445,10 +455,6 @@ class Row { } $tokens = $this->parent->syntax->tokens[$rowNum]; - if (empty($tokens)) - { - return; - } $inComment = ($this->idx > 0 && $this->parent->rows[$this->idx - 1]->hlOpenComment); @@ -456,6 +462,28 @@ class Row { // multiples of the same tokens can be effectively matched $offset = 0; + // There will be no tokens if a line is empty, or if in a multi-line comment + if (empty($tokens)) + { + if ($inComment && $this->rsize > 0) + { + while ($offset < $this->rsize) + { + if (substr($this->render, $offset, 2) === '*/') + { + array_replace_range($this->hl, $offset, 2, Highlight::ML_COMMENT); + break; + } + + $this->hl[$offset] = Highlight::ML_COMMENT; + $offset++; + continue; + } + } + + return; + } + foreach ($tokens as $token) { if ($offset >= $this->rsize) @@ -463,9 +491,33 @@ class Row { break; } + // A multi-line comment can end in the middle of a line... + while ($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) === '*/') + { + $inComment = FALSE; + array_replace_range($this->hl, $offset, 2, Highlight::ML_COMMENT); + $offset += 2; + } + + $this->hl[$offset] = Highlight::ML_COMMENT; + $offset++; + continue; + } + $char = $token['char']; $charLen = strlen($char); - if ($charLen === 0) + if ($charLen === 0 || $offset >= $this->rsize) { continue; } @@ -474,24 +526,9 @@ class Row { { continue; } - $charEnd = $charStart + $charLen; - // Comments - if ($inComment) - { - if (substr($this->render, $offset, 2) === '*/') - { - $inComment = FALSE; - array_replace_range($this->hl, $offset, 2, Highlight::ML_COMMENT); - $offset += 2; - continue; - } - - $this->hl[$offset] = Highlight::ML_COMMENT; - $offset++; - continue; - } + // Start of multiline comment/single line comment if (in_array($token['type'], [T_DOC_COMMENT, T_COMMENT], TRUE)) { // Single line comments @@ -511,29 +548,40 @@ class Row { { break; } - - continue; } // Highlight specific tokens - if (($token['typeName'] !== 'RAW') && array_key_exists($token['type'], $this->phpTokenHighlightMap)) + if ($token['typeName'] !== 'RAW') { - $hl = $this->phpTokenHighlightMap[$token['type']]; - array_replace_range($this->hl, $charStart, $charLen, $hl); - $offset = $charEnd; - continue; + if (array_key_exists($token['type'], $this->phpTokenHighlightMap)) + { + $hl = $this->phpTokenHighlightMap[$token['type']]; + array_replace_range($this->hl, $charStart, $charLen, $hl); + $offset = $charEnd; + continue; + } + + // Types/identifiers/keywords that don't have their own token + if ($token['type'] === T_STRING) + { + if (in_array($token['char'], $this->parent->syntax->keywords2, TRUE)) + { + array_replace_range($this->hl, $charStart, $charLen, Highlight::KEYWORD2); + $offset = $charEnd; + continue; + } + } } + // Highlight raw characters - if (($token['typeName'] === 'RAW') && array_key_exists($token['char'], $this->phpCharacterHighlightMap)) + if (($token['typeName'] === 'RAW') && array_key_exists(trim($token['char']), $this->phpCharacterHighlightMap)) { - $hl = $this->phpCharacterHighlightMap[$token['char']]; + $hl = $this->phpCharacterHighlightMap[trim($token['char'])]; array_replace_range($this->hl, $charStart, $charLen, $hl); $offset = $charEnd; continue; } - - $offset++; } $changed = $this->hlOpenComment !== $inComment; diff --git a/test.php b/test.php index 8d0db23..2420a4b 100644 --- a/test.php +++ b/test.php @@ -48,7 +48,15 @@ $foobar = new FooBar(); $x = 3; -// Heredoc +$y = [ + 1, + 2, + 3 +]; + +/* +Heredoc +*/ $sql = <<