Catch lexer errors in throwOnError=0 mode

This commit is contained in:
Nikita Popov 2016-09-30 13:49:34 +02:00
parent ea47b6e0d6
commit c5e0c3d7e2
2 changed files with 38 additions and 1 deletions

View File

@ -132,9 +132,21 @@ abstract class ParserAbstract implements Parser
* unable to recover from an error).
*/
public function parse($code) {
$this->lexer->startLexing($code);
$this->errors = array();
// Initialize the lexer
try {
$this->lexer->startLexing($code);
} catch (Error $e) {
$this->errors[] = $e;
if ($this->throwOnError) {
throw $e;
} else {
// Currently can't recover from lexer errors
return null;
}
}
// We start off with no lookahead-token
$symbol = self::SYMBOL_NONE;

View File

@ -0,0 +1,25 @@
Lexer errors
-----
<?php
$a = 42;
/*
$b = 24;
-----
Unterminated comment on line 4
-----
<?php
$a = 42;
@@{ "\1" }@@
$b = 24;
-----
Unexpected character "@@{ "\1" }@@" (ASCII 1) on unknown line
-----
<?php
$a = 42;
@@{ "\0" }@@
$b = 24;
-----
Unexpected null byte on unknown line