From b2c8d8eeb10c03e2feb13c24ac0e7ade83d79ee7 Mon Sep 17 00:00:00 2001 From: Andreas Heiberg Date: Fri, 11 Nov 2016 13:19:42 +0000 Subject: [PATCH] [style changes] Lexer --- src/Language/Lexer.php | 117 +++++++++++++++++++++++++++-------------- 1 file changed, 78 insertions(+), 39 deletions(-) diff --git a/src/Language/Lexer.php b/src/Language/Lexer.php index 500f5dc..a100f76 100644 --- a/src/Language/Lexer.php +++ b/src/Language/Lexer.php @@ -110,43 +110,46 @@ class Lexer // SourceCharacter if ($code < 0x0020 && $code !== 0x0009 && $code !== 0x000A && $code !== 0x000D) { - throw new SyntaxError($this->source, $position, 'Invalid character ' . Utils::printCharCode($code)); + throw new SyntaxError( + $this->source, + $position, + 'Invalid character ' . Utils::printCharCode($code) + ); } switch ($code) { - // ! - case 33: return new Token(Token::BANG, $position, $position + 1, $line, $col, $prev); - // # - case 35: return $this->readComment($position, $line, $col, $prev); - // $ - case 36: return new Token(Token::DOLLAR, $position, $position + 1, $line, $col, $prev); - // ( - case 40: return new Token(Token::PAREN_L, $position, $position + 1, $line, $col, $prev); - // ) - case 41: return new Token(Token::PAREN_R, $position, $position + 1, $line, $col, $prev); - // . - case 46: + case 33: // ! + return new Token(Token::BANG, $position, $position + 1, $line, $col, $prev); + case 35: // # + return $this->readComment($position, $line, $col, $prev); + case 36: // $ + return new Token(Token::DOLLAR, $position, $position + 1, $line, $col, $prev); + case 40: // ( + return new Token(Token::PAREN_L, $position, $position + 1, $line, $col, $prev); + case 41: // ) + return new Token(Token::PAREN_R, $position, $position + 1, $line, $col, $prev); + case 46: // . if (Utils::charCodeAt($body, $position+1) === 46 && Utils::charCodeAt($body, $position+2) === 46) { return new Token(Token::SPREAD, $position, $position + 3, $line, $col, $prev); } break; - // : - case 58: return new Token(Token::COLON, $position, $position + 1, $line, $col, $prev); - // = - case 61: return new Token(Token::EQUALS, $position, $position + 1, $line, $col, $prev); - // @ - case 64: return new Token(Token::AT, $position, $position + 1, $line, $col, $prev); - // [ - case 91: return new Token(Token::BRACKET_L, $position, $position + 1, $line, $col, $prev); - // ] - case 93: return new Token(Token::BRACKET_R, $position, $position + 1, $line, $col, $prev); - // { - case 123: return new Token(Token::BRACE_L, $position, $position + 1, $line, $col, $prev); - // | - case 124: return new Token(Token::PIPE, $position, $position + 1, $line, $col, $prev); - // } - case 125: return new Token(Token::BRACE_R, $position, $position + 1, $line, $col, $prev); + case 58: // : + return new Token(Token::COLON, $position, $position + 1, $line, $col, $prev); + case 61: // = + return new Token(Token::EQUALS, $position, $position + 1, $line, $col, $prev); + case 64: // @ + return new Token(Token::AT, $position, $position + 1, $line, $col, $prev); + case 91: // [ + return new Token(Token::BRACKET_L, $position, $position + 1, $line, $col, $prev); + case 93: // ] + return new Token(Token::BRACKET_R, $position, $position + 1, $line, $col, $prev); + case 123: // { + return new Token(Token::BRACE_L, $position, $position + 1, $line, $col, $prev); + case 124: // | + return new Token(Token::PIPE, $position, $position + 1, $line, $col, $prev); + case 125: // } + return new Token(Token::BRACE_R, $position, $position + 1, $line, $col, $prev); // A-Z case 65: case 66: case 67: case 68: case 69: case 70: case 71: case 72: case 73: case 74: case 75: case 76: case 77: case 78: case 79: case 80: @@ -159,18 +162,23 @@ class Lexer case 105: case 106: case 107: case 108: case 109: case 110: case 111: case 112: case 113: case 114: case 115: case 116: case 117: case 118: case 119: case 120: case 121: case 122: - return $this->readName($position, $line, $col, $prev); + return $this->readName($position, $line, $col, $prev); // - case 45: // 0-9 case 48: case 49: case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57: - return $this->readNumber($position, $code, $line, $col, $prev); + return $this->readNumber($position, $code, $line, $col, $prev); // " - case 34: return $this->readString($position, $line, $col, $prev); + case 34: + return $this->readString($position, $line, $col, $prev); } - throw new SyntaxError($this->source, $position, 'Unexpected character ' . Utils::printCharCode($code)); + throw new SyntaxError( + $this->source, + $position, + 'Unexpected character ' . Utils::printCharCode($code) + ); } /** @@ -239,6 +247,7 @@ class Lexer $code = Utils::charCodeAt($body, ++$position); } + // guard against leading zero's if ($code === 48) { // 0 $code = Utils::charCodeAt($body, ++$position); @@ -267,6 +276,7 @@ class Lexer } $position = $this->readDigits($position, $code); } + return new Token( $isFloat ? Token::FLOAT : Token::INT, $start, @@ -286,6 +296,7 @@ class Lexer $body = $this->source->body; $position = $start; $code = $firstCode; + if ($code >= 48 && $code <= 57) { // 0 - 9 do { $code = Utils::charCodeAt($body, ++$position); @@ -293,10 +304,16 @@ class Lexer return $position; } + if ($position > $this->source->length - 1) { $code = null; } - throw new SyntaxError($this->source, $position, "Invalid number, expected digit but got: " . Utils::printCharCode($code)); + + throw new SyntaxError( + $this->source, + $position, + 'Invalid number, expected digit but got: ' . Utils::printCharCode($code) + ); } /** @@ -343,7 +360,11 @@ class Lexer case 117: $hex = mb_substr($body, $position + 1, 4, 'UTF-8'); if (!preg_match('/[0-9a-fA-F]{4}/', $hex)) { - throw new SyntaxError($this->source, $position, 'Invalid character escape sequence: \\u' . $hex); + throw new SyntaxError( + $this->source, + $position, + 'Invalid character escape sequence: \\u' . $hex + ); } $code = hexdec($hex); $this->assertValidStringCharacterCode($code, $position - 1); @@ -351,7 +372,11 @@ class Lexer $position += 4; break; default: - throw new SyntaxError($this->source, $position, 'Invalid character escape sequence: \\' . Utils::chr($code)); + throw new SyntaxError( + $this->source, + $position, + 'Invalid character escape sequence: \\' . Utils::chr($code) + ); } ++$position; $chunkStart = $position; @@ -359,11 +384,24 @@ class Lexer } if ($code !== 34) { - throw new SyntaxError($this->source, $position, 'Unterminated string'); + throw new SyntaxError( + $this->source, + $position, + 'Unterminated string' + ); } $value .= mb_substr($body, $chunkStart, $position - $chunkStart, 'UTF-8'); - return new Token(Token::STRING, $start, $position + 1, $line, $col, $prev, $value); + + return new Token( + Token::STRING, + $start, + $position + 1, + $line, + $col, + $prev, + $value + ); } private function assertValidStringCharacterCode($code, $position) @@ -373,7 +411,7 @@ class Lexer throw new SyntaxError( $this->source, $position, - "Invalid character within String: " . Utils::printCharCode($code) + 'Invalid character within String: ' . Utils::printCharCode($code) ); } } @@ -415,6 +453,7 @@ class Lexer break; } } + return $position; }