From 25cfebbd378a5c84097ba26a2c21fc269789fc7e Mon Sep 17 00:00:00 2001 From: Vladimir Razuvaev Date: Wed, 21 Nov 2018 18:09:57 +0700 Subject: [PATCH] Lexer: unify unexpected character errors --- src/Language/Lexer.php | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/Language/Lexer.php b/src/Language/Lexer.php index d3b80ac..fb4157c 100644 --- a/src/Language/Lexer.php +++ b/src/Language/Lexer.php @@ -130,15 +130,6 @@ class Lexer // Read next char and advance string cursor: [, $code, $bytes] = $this->readChar(true); - // SourceCharacter - if ($code < 0x0020 && $code !== 0x0009 && $code !== 0x000A && $code !== 0x000D) { - throw new SyntaxError( - $this->source, - $position, - 'Cannot contain the invalid character ' . Utils::printCharCode($code) - ); - } - switch ($code) { case 33: // ! return new Token(Token::BANG, $position, $position + 1, $line, $col, $prev); @@ -265,17 +256,28 @@ class Lexer ->readString($line, $col, $prev); } - $errMessage = $code === 39 - ? "Unexpected single quote character ('), did you mean to use " . 'a double quote (")?' - : 'Cannot parse the unexpected character ' . Utils::printCharCode($code) . '.'; - throw new SyntaxError( $this->source, $position, - $errMessage + $this->unexpectedCharacterMessage($code) ); } + private function unexpectedCharacterMessage($code) + { + // SourceCharacter + if ($code < 0x0020 && $code !== 0x0009 && $code !== 0x000A && $code !== 0x000D) { + return 'Cannot contain the invalid character ' . Utils::printCharCode($code); + } + + if ($code === 39) { + return "Unexpected single quote character ('), did you mean to use " . + 'a double quote (")?'; + } + + return 'Cannot parse the unexpected character ' . Utils::printCharCode($code) . '.'; + } + /** * Reads an alphanumeric + underscore name from the source. *