diff options
Diffstat (limited to 'src/Lexer/Lexer.php')
-rw-r--r-- | src/Lexer/Lexer.php | 81 |
1 files changed, 71 insertions, 10 deletions
diff --git a/src/Lexer/Lexer.php b/src/Lexer/Lexer.php index 747b0c9..fba9623 100644 --- a/src/Lexer/Lexer.php +++ b/src/Lexer/Lexer.php @@ -27,7 +27,7 @@ class Lexer "(", ")", "[", "]", "{", "}", - "$", + "$", ".", ]; $lastPosition = -1; @@ -58,6 +58,7 @@ class Lexer // equals if ($nextChar == "=") { $output[] = new Token( + TokenType::Comparator, $currentChar . $nextChar, $currentChar . $nextChar, $this->line, @@ -69,6 +70,7 @@ class Lexer // pipe else if ($nextChar == ">") { $output[] = new Token( + TokenType::Pipe, $currentChar . $nextChar, $currentChar . $nextChar, $this->line, @@ -80,6 +82,7 @@ class Lexer // assign else { $output[] = new Token( + TokenType::Assign, $currentChar, $currentChar, $this->line, @@ -107,6 +110,7 @@ class Lexer // divide else { $output[] = new Token( + TokenType::Operator, $currentChar, $currentChar, $this->line, @@ -124,6 +128,7 @@ class Lexer // exponential if ($nextChar == "*") { $output[] = new Token( + TokenType::Operator, $currentChar . $nextChar, $currentChar . $nextChar, $this->line, @@ -135,6 +140,7 @@ class Lexer // multiply else { $output[] = new Token( + TokenType::Operator, $currentChar, $currentChar, $this->line, @@ -152,6 +158,7 @@ class Lexer // lower equals if ($nextChar == "=") { $output[] = new Token( + TokenType::Comparator, $currentChar . $nextChar, $currentChar . $nextChar, $this->line, @@ -162,6 +169,7 @@ class Lexer else { $output[] = new Token( + TokenType::Comparator, $currentChar, $currentChar, $this->line, @@ -179,6 +187,7 @@ class Lexer // greater equals if ($nextChar == "=") { $output[] = new Token( + TokenType::Comparator, $currentChar . $nextChar, $currentChar . $nextChar, $this->line, @@ -189,6 +198,37 @@ class Lexer else { $output[] = new Token( + TokenType::Comparator, + $currentChar, + $currentChar, + $this->line, + $startColumn, + ); + $this->advance(1); + } + } + + // exclamation mark + else if ($currentChar == "!") { + $nextChar = $this->getNextChar(); + $startColumn = $this->column; + + // not equal + if ($nextChar == "=") { + $output[] = new Token( + TokenType::Comparator, + $currentChar . $nextChar, + $currentChar . $nextChar, + $this->line, + $startColumn, + ); + $this->advance(2); + } + + // not + else { + $output[] = new Token( + TokenType::Operator, $currentChar, $currentChar, $this->line, @@ -201,6 +241,7 @@ class Lexer // single char tokens else if (in_array($currentChar, $singleCharTokens)) { $output[] = new Token( + TokenType::Operator, # TODO $currentChar, $currentChar, $this->line, @@ -225,6 +266,15 @@ class Lexer } } + // add end of file token + $output[] = new Token( + TokenType::EndOfFile, + "", + "", + $this->line, + $this->column + ); + return $output; } @@ -238,13 +288,6 @@ class Lexer return $this->input[$this->position + 1] ?? null; } - /* - public function seek(int $position): void - { - $this->position = $position; - } - */ - public function advance(int $steps): void { $this->position += $steps; @@ -283,7 +326,7 @@ class Lexer $startPosition = $this->position; $endPosition = $this->position; - while ($this->isIdentifierChar($this->getCurrentChar())) { + while ($this->isIdentifierChar($this->getCurrentChar()) or $this->isNumberChar($this->getCurrentChar())) { $endPosition = $this->position; $this->advance(1); } @@ -291,6 +334,7 @@ class Lexer $value = substr($this->input, $startPosition, $endPosition - $startPosition + 1); return new Token( + TokenType::Identifier, $value, $value, $this->line, @@ -322,8 +366,9 @@ class Lexer $value = substr($this->input, $startPosition, $endPosition - $startPosition + 1); return new Token( + TokenType::Number, $value, - $value, + str_replace("_", "", $value), $this->line, $startColumn, ); @@ -357,6 +402,7 @@ class Lexer $rawValue = substr($this->input, $startPosition, $endPosition - $startPosition + 1); return new Token( + TokenType::String, $rawValue, substr($rawValue, 1, strlen($rawValue) - 2), $startLine, @@ -379,6 +425,7 @@ class Lexer $rawValue = substr($this->input, $startPosition, $endPosition - $startPosition + 1); return new Token( + TokenType::Comment, $rawValue, trim(substr($rawValue, 2)), $this->line, @@ -414,6 +461,7 @@ class Lexer $rawValue = substr($this->input, $startPosition, $endPosition - $startPosition + 1); return new Token( + TokenType::Comment, $rawValue, trim(substr($rawValue, 2, strlen($rawValue) - 4)), $startLine, @@ -424,9 +472,22 @@ class Lexer class Token { public function __construct( + public TokenType $type, public string $literal, public string $value, public string $line, public string $column, ) {} } + +enum TokenType { + case Identifier; + case Number; + case String; + case Comment; + case Comparator; + case Operator; + case Assign; + case Pipe; + case EndOfFile; +} |