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; return;
} }
// Check for unterminated comment if (count($this->tokens) > 0) {
$lastToken = $this->tokens[count($this->tokens) - 1]; // Check for unterminated comment
if ($this->isUnterminatedComment($lastToken)) { $lastToken = $this->tokens[count($this->tokens) - 1];
$errorHandler->handleError(new Error('Unterminated comment', [ if ($this->isUnterminatedComment($lastToken)) {
'startLine' => $line - substr_count($lastToken[1], "\n"), $errorHandler->handleError(new Error('Unterminated comment', [
'endLine' => $line, 'startLine' => $line - substr_count($lastToken[1], "\n"),
'startFilePos' => $filePos - \strlen($lastToken[1]), 'endLine' => $line,
'endFilePos' => $filePos, 'startFilePos' => $filePos - \strlen($lastToken[1]),
])); 'endFilePos' => $filePos,
]));
}
} }
} }

View File

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