Skip to content

Commit

Permalink
Refactor tokenizer with $firstChar/$secondChar
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jun 11, 2024
1 parent 0bcd33e commit 89c6ee3
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -835,14 +835,17 @@ private function createNextToken(string $string, string $upper, int $offset, Tok
return new Token(Token::TOKEN_TYPE_WHITESPACE, $matches[0]);
}

$firstChar = $string[$offset];
$secondChar = $string[$offset + 1] ?? '';

// Comment
if (
$string[$offset] === '#' ||
(isset($string[$offset + 1]) && ($string[$offset] === '-' && $string[$offset + 1] === '-') ||
(isset($string[$offset + 1]) && $string[$offset] === '/' && $string[$offset + 1] === '*'))
$firstChar === '#' ||
(($firstChar === '-' && $secondChar === '-') ||
($firstChar === '/' && $secondChar === '*'))
) {
// Comment until end of line
if ($string[$offset] === '-' || $string[$offset] === '#') {
if ($firstChar === '-' || $firstChar === '#') {
$last = strpos($string, "\n", $offset);
$type = Token::TOKEN_TYPE_COMMENT;
} else { // Comment until closing comment tag
Expand All @@ -861,26 +864,26 @@ private function createNextToken(string $string, string $upper, int $offset, Tok
}

// Quoted String
if ($string[$offset] === '"' || $string[$offset] === '\'' || $string[$offset] === '`' || $string[$offset] === '[') {
if ($firstChar === '"' || $firstChar === '\'' || $firstChar === '`' || $firstChar === '[') {
return new Token(
($string[$offset] === '`' || $string[$offset] === '['
($firstChar === '`' || $firstChar === '['
? Token::TOKEN_TYPE_BACKTICK_QUOTE
: Token::TOKEN_TYPE_QUOTE),
$this->getNextQuotedString($string, $offset),
);
}

// User-defined Variable
if (($string[$offset] === '@' || $string[$offset] === ':') && isset($string[$offset + 1])) {
if (($firstChar === '@' || $firstChar === ':') && $secondChar !== '') {
$value = null;
$type = Token::TOKEN_TYPE_VARIABLE;

// If the variable name is quoted
if ($string[$offset + 1] === '"' || $string[$offset + 1] === '\'' || $string[$offset + 1] === '`') {
$value = $string[$offset] . $this->getNextQuotedString($string, $offset + 1);
if ($secondChar === '"' || $secondChar === '\'' || $secondChar === '`') {
$value = $firstChar . $this->getNextQuotedString($string, $offset + 1);
} else {
// Non-quoted variable name
preg_match('/\G(' . $string[$offset] . '[\w.$]+)/', $string, $matches, 0, $offset);
preg_match('/\G(' . $firstChar . '[\w.$]+)/', $string, $matches, 0, $offset);
if ($matches) {
$value = $matches[1];
}
Expand Down

0 comments on commit 89c6ee3

Please sign in to comment.