1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-12-02 09:17:58 +01:00

Add error recovery mode to php-parse script

This commit is contained in:
Nikita Popov 2016-07-25 17:27:12 +02:00
parent 977cbab8e7
commit 2b209aaaf0

View File

@ -29,7 +29,11 @@ if (empty($files)) {
$lexer = new PhpParser\Lexer\Emulative(array('usedAttributes' => array( $lexer = new PhpParser\Lexer\Emulative(array('usedAttributes' => array(
'startLine', 'endLine', 'startFilePos', 'endFilePos', 'comments' 'startLine', 'endLine', 'startFilePos', 'endFilePos', 'comments'
))); )));
$parser = (new PhpParser\ParserFactory)->create(PhpParser\ParserFactory::PREFER_PHP7, $lexer); $parser = (new PhpParser\ParserFactory)->create(
PhpParser\ParserFactory::PREFER_PHP7,
$lexer,
array('throwOnError' => !$attributes['with-recovery'])
);
$dumper = new PhpParser\NodeDumper(['dumpComments' => true]); $dumper = new PhpParser\NodeDumper(['dumpComments' => true]);
$prettyPrinter = new PhpParser\PrettyPrinter\Standard; $prettyPrinter = new PhpParser\PrettyPrinter\Standard;
$serializer = new PhpParser\Serializer\XML; $serializer = new PhpParser\Serializer\XML;
@ -52,17 +56,15 @@ foreach ($files as $file) {
try { try {
$stmts = $parser->parse($code); $stmts = $parser->parse($code);
} catch (PhpParser\Error $e) { foreach ($parser->getErrors() as $error) {
if ($attributes['with-column-info'] && $e->hasColumnInfo()) { $message = formatErrorMessage($error, $code, $attributes['with-column-info']);
$startLine = $e->getStartLine(); echo $message . "\n";
$endLine = $e->getEndLine();
$startColumn = $e->getStartColumn($code);
$endColumn = $e->getEndColumn($code);
$message .= $e->getRawMessage() . " from $startLine:$startColumn to $endLine:$endColumn";
} else {
$message = $e->getMessage();
} }
if (null === $stmts) {
continue;
}
} catch (PhpParser\Error $error) {
$message = formatErrorMessage($error, $code, $attributes['with-column-info']);
die($message . "\n"); die($message . "\n");
} }
@ -86,6 +88,18 @@ foreach ($files as $file) {
} }
} }
function formatErrorMessage(PhpParser\Error $e, $code, $withColumnInfo) {
if ($withColumnInfo && $e->hasColumnInfo()) {
$startLine = $e->getStartLine();
$endLine = $e->getEndLine();
$startColumn = $e->getStartColumn($code);
$endColumn = $e->getEndColumn($code);
return $e->getRawMessage() . " from $startLine:$startColumn to $endLine:$endColumn";
} else {
return $e->getMessage();
}
}
function showHelp($error = '') { function showHelp($error = '') {
if ($error) { if ($error) {
echo $error . "\n\n"; echo $error . "\n\n";
@ -103,6 +117,7 @@ Operations is a list of the following options (--dump by default):
--var-dump var_dump() nodes (for exact structure) --var-dump var_dump() nodes (for exact structure)
-N, --resolve-names Resolve names using NodeVisitor\NameResolver -N, --resolve-names Resolve names using NodeVisitor\NameResolver
-c, --with-column-info Show column-numbers for errors (if available) -c, --with-column-info Show column-numbers for errors (if available)
-r, --with-recovery Use parsing with error recovery
-h, --help Display this page -h, --help Display this page
Example: Example:
@ -119,7 +134,8 @@ function parseArgs($args) {
$operations = array(); $operations = array();
$files = array(); $files = array();
$attributes = array( $attributes = array(
'with-column-info' => false, 'with-column-info' => false,
'with-recovery' => false,
); );
array_shift($args); array_shift($args);
@ -153,6 +169,10 @@ function parseArgs($args) {
case '-c'; case '-c';
$attributes['with-column-info'] = true; $attributes['with-column-info'] = true;
break; break;
case '--with-recovery':
case '-r':
$attributes['with-recovery'] = true;
break;
case '--help': case '--help':
case '-h'; case '-h';
showHelp(); showHelp();