Skip to content
This repository has been archived by the owner on Apr 24, 2018. It is now read-only.

Commit

Permalink
🔨 Return exception and throw in place
Browse files Browse the repository at this point in the history
This makes it obvious for static analysis tools.
  • Loading branch information
hansott committed Sep 16, 2017
1 parent e98c119 commit e5185fd
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/Query/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ private function emit($type, $value)
$this->tokens[] = new Token($type, $value, $line, $column);
}

private function error($message)
private function getError($message)
{
$line = $this->scanner->getLine();
$column = $this->scanner->getColumn();
throw new SyntaxError($message . " (line {$line}, column {$column})");

return new SyntaxError($message . " (line {$line}, column {$column})");
}

private function name()
Expand Down Expand Up @@ -78,14 +79,14 @@ private function spread()
$next = $this->scanner->peek();

if ($next !== '.') {
$this->error("Expected \".\" but instead found \"{$next}\"");
throw $this->getError("Expected \".\" but instead found \"{$next}\"");
}

$points .= $this->scanner->next();
$next = $this->scanner->peek();

if ($next !== '.') {
$this->error("Expected \".\" but instead found \"{$next}\"");
throw $this->getError("Expected \".\" but instead found \"{$next}\"");
}

$points .= $this->scanner->next();
Expand All @@ -100,7 +101,7 @@ private function str()

while (true) {
if ($this->scanner->eof()) {
$this->error('Unclosed quote');
throw $this->getError('Unclosed quote');
}
$next = $this->scanner->peek();
if ($previousChar !== '\\' && $next === '"') {
Expand All @@ -120,11 +121,11 @@ private function integerPart()
$number = $this->scanner->next();
if ($number === '-') {
if ($this->scanner->eof()) {
$this->error('Expected a digit but instead reached end');
throw $this->getError('Expected a digit but instead reached end');
}
$next = $this->scanner->peek();
if (ctype_digit($next) === false) {
$this->error("Expected a digit but instead found \"{$next}\"");
throw $this->getError("Expected a digit but instead found \"{$next}\"");
}
}

Expand All @@ -148,12 +149,12 @@ private function fractionalPart()
$part = $this->scanner->next();

if ($this->scanner->eof()) {
$this->error('Expected a digit but instead reached end');
throw $this->getError('Expected a digit but instead reached end');
}

$next = $this->scanner->peek();
if (ctype_digit($next) === false) {
$this->error("Expected a digit but instead found \"{$next}\"");
throw $this->getError("Expected a digit but instead found \"{$next}\"");
}

$next = $this->scanner->peek();
Expand All @@ -170,7 +171,7 @@ private function exponentPart()
$part = $this->scanner->next();

if ($this->scanner->eof()) {
$this->error('Expected a digit but instead reached end');
throw $this->getError('Expected a digit but instead reached end');
}

$next = $this->scanner->peek();
Expand All @@ -180,7 +181,7 @@ private function exponentPart()

$next = $this->scanner->peek();
if (ctype_digit($next) === false) {
$this->error("Expected a digit but instead found \"{$next}\"");
throw $this->getError("Expected a digit but instead found \"{$next}\"");
}

$next = $this->scanner->peek();
Expand Down Expand Up @@ -284,7 +285,7 @@ public function lex($query)
continue;
}

$this->error("Unknown character: \"{$next}\"");
throw $this->getError("Unknown character: \"{$next}\"");
}

return $this->tokens;
Expand Down

0 comments on commit e5185fd

Please sign in to comment.