Fix Lexer errorHandling when there is no tokens

I encounter an issue when no tokens exist. An error `Undefined offset -1` is triggered.
This commit is contained in:
Romain Neutron 2017-05-31 23:54:26 +02:00 committed by Nikita Popov
parent 3da86df48f
commit c28b8556f5
2 changed files with 18 additions and 10 deletions

View File

@ -185,15 +185,17 @@ class Lexer
return;
}
// Check for unterminated comment
$lastToken = $this->tokens[count($this->tokens) - 1];
if ($this->isUnterminatedComment($lastToken)) {
$errorHandler->handleError(new Error('Unterminated comment', [
'startLine' => $line - substr_count($lastToken[1], "\n"),
'endLine' => $line,
'startFilePos' => $filePos - \strlen($lastToken[1]),
'endFilePos' => $filePos,
]));
if (count($this->tokens) > 0) {
// Check for unterminated comment
$lastToken = $this->tokens[count($this->tokens) - 1];
if ($this->isUnterminatedComment($lastToken)) {
$errorHandler->handleError(new Error('Unterminated comment', [
'startLine' => $line - substr_count($lastToken[1], "\n"),
'endLine' => $line,
'startFilePos' => $filePos - \strlen($lastToken[1]),
'endFilePos' => $filePos,
]));
}
}
}

View File

@ -201,7 +201,13 @@ class LexerTest extends \PHPUnit_Framework_TestCase
array(), array()
)
)
)
),
// tests no tokens
array(
'',
array(),
array()
),
);
}