Skip to content

Commit c5e0c3d

Browse files
committed
Catch lexer errors in throwOnError=0 mode
1 parent ea47b6e commit c5e0c3d

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

lib/PhpParser/ParserAbstract.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,21 @@ public function getErrors() {
132132
* unable to recover from an error).
133133
*/
134134
public function parse($code) {
135-
$this->lexer->startLexing($code);
136135
$this->errors = array();
137136

137+
// Initialize the lexer
138+
try {
139+
$this->lexer->startLexing($code);
140+
} catch (Error $e) {
141+
$this->errors[] = $e;
142+
if ($this->throwOnError) {
143+
throw $e;
144+
} else {
145+
// Currently can't recover from lexer errors
146+
return null;
147+
}
148+
}
149+
138150
// We start off with no lookahead-token
139151
$symbol = self::SYMBOL_NONE;
140152

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Lexer errors
2+
-----
3+
<?php
4+
5+
$a = 42;
6+
/*
7+
$b = 24;
8+
-----
9+
Unterminated comment on line 4
10+
-----
11+
<?php
12+
13+
$a = 42;
14+
@@{ "\1" }@@
15+
$b = 24;
16+
-----
17+
Unexpected character "@@{ "\1" }@@" (ASCII 1) on unknown line
18+
-----
19+
<?php
20+
21+
$a = 42;
22+
@@{ "\0" }@@
23+
$b = 24;
24+
-----
25+
Unexpected null byte on unknown line

0 commit comments

Comments
 (0)