Skip to content

Commit 2b209aa

Browse files
committed
Add error recovery mode to php-parse script
1 parent 977cbab commit 2b209aa

File tree

1 file changed

+32
-12
lines changed

1 file changed

+32
-12
lines changed

bin/php-parse

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ if (empty($files)) {
2929
$lexer = new PhpParser\Lexer\Emulative(array('usedAttributes' => array(
3030
'startLine', 'endLine', 'startFilePos', 'endFilePos', 'comments'
3131
)));
32-
$parser = (new PhpParser\ParserFactory)->create(PhpParser\ParserFactory::PREFER_PHP7, $lexer);
32+
$parser = (new PhpParser\ParserFactory)->create(
33+
PhpParser\ParserFactory::PREFER_PHP7,
34+
$lexer,
35+
array('throwOnError' => !$attributes['with-recovery'])
36+
);
3337
$dumper = new PhpParser\NodeDumper(['dumpComments' => true]);
3438
$prettyPrinter = new PhpParser\PrettyPrinter\Standard;
3539
$serializer = new PhpParser\Serializer\XML;
@@ -52,17 +56,15 @@ foreach ($files as $file) {
5256

5357
try {
5458
$stmts = $parser->parse($code);
55-
} catch (PhpParser\Error $e) {
56-
if ($attributes['with-column-info'] && $e->hasColumnInfo()) {
57-
$startLine = $e->getStartLine();
58-
$endLine = $e->getEndLine();
59-
$startColumn = $e->getStartColumn($code);
60-
$endColumn = $e->getEndColumn($code);
61-
$message .= $e->getRawMessage() . " from $startLine:$startColumn to $endLine:$endColumn";
62-
} else {
63-
$message = $e->getMessage();
59+
foreach ($parser->getErrors() as $error) {
60+
$message = formatErrorMessage($error, $code, $attributes['with-column-info']);
61+
echo $message . "\n";
6462
}
65-
63+
if (null === $stmts) {
64+
continue;
65+
}
66+
} catch (PhpParser\Error $error) {
67+
$message = formatErrorMessage($error, $code, $attributes['with-column-info']);
6668
die($message . "\n");
6769
}
6870

@@ -86,6 +88,18 @@ foreach ($files as $file) {
8688
}
8789
}
8890

91+
function formatErrorMessage(PhpParser\Error $e, $code, $withColumnInfo) {
92+
if ($withColumnInfo && $e->hasColumnInfo()) {
93+
$startLine = $e->getStartLine();
94+
$endLine = $e->getEndLine();
95+
$startColumn = $e->getStartColumn($code);
96+
$endColumn = $e->getEndColumn($code);
97+
return $e->getRawMessage() . " from $startLine:$startColumn to $endLine:$endColumn";
98+
} else {
99+
return $e->getMessage();
100+
}
101+
}
102+
89103
function showHelp($error = '') {
90104
if ($error) {
91105
echo $error . "\n\n";
@@ -103,6 +117,7 @@ Operations is a list of the following options (--dump by default):
103117
--var-dump var_dump() nodes (for exact structure)
104118
-N, --resolve-names Resolve names using NodeVisitor\NameResolver
105119
-c, --with-column-info Show column-numbers for errors (if available)
120+
-r, --with-recovery Use parsing with error recovery
106121
-h, --help Display this page
107122
108123
Example:
@@ -119,7 +134,8 @@ function parseArgs($args) {
119134
$operations = array();
120135
$files = array();
121136
$attributes = array(
122-
'with-column-info' => false,
137+
'with-column-info' => false,
138+
'with-recovery' => false,
123139
);
124140

125141
array_shift($args);
@@ -153,6 +169,10 @@ function parseArgs($args) {
153169
case '-c';
154170
$attributes['with-column-info'] = true;
155171
break;
172+
case '--with-recovery':
173+
case '-r':
174+
$attributes['with-recovery'] = true;
175+
break;
156176
case '--help':
157177
case '-h';
158178
showHelp();

0 commit comments

Comments
 (0)