⬇️ Restore php-parser to its original version as tests are failing :/

This commit is contained in:
Pierre Goiffon
2021-03-29 16:51:10 +02:00
parent d62825c99b
commit afd9ddf46d
235 changed files with 13778 additions and 13877 deletions

View File

@@ -26,9 +26,9 @@ if (empty($files)) {
showHelp("Must specify at least one file.");
}
$lexer = new PhpParser\Lexer\Emulative(['usedAttributes' => [
$lexer = new PhpParser\Lexer\Emulative(array('usedAttributes' => array(
'startLine', 'endLine', 'startFilePos', 'endFilePos', 'comments'
]]);
)));
$parser = (new PhpParser\ParserFactory)->create(
PhpParser\ParserFactory::PREFER_PHP7,
$lexer
@@ -38,6 +38,7 @@ $dumper = new PhpParser\NodeDumper([
'dumpPositions' => $attributes['with-positions'],
]);
$prettyPrinter = new PhpParser\PrettyPrinter\Standard;
$serializer = new PhpParser\Serializer\XML;
$traverser = new PhpParser\NodeTraverser();
$traverser->addVisitor(new PhpParser\NodeVisitor\NameResolver);
@@ -45,15 +46,14 @@ $traverser->addVisitor(new PhpParser\NodeVisitor\NameResolver);
foreach ($files as $file) {
if (strpos($file, '<?php') === 0) {
$code = $file;
fwrite(STDERR, "====> Code $code\n");
echo "====> Code $code\n";
} else {
if (!file_exists($file)) {
fwrite(STDERR, "File $file does not exist.\n");
exit(1);
die("File $file does not exist.\n");
}
$code = file_get_contents($file);
fwrite(STDERR, "====> File $file:\n");
echo "====> File $file:\n";
}
if ($attributes['with-recovery']) {
@@ -61,7 +61,7 @@ foreach ($files as $file) {
$stmts = $parser->parse($code, $errorHandler);
foreach ($errorHandler->getErrors() as $error) {
$message = formatErrorMessage($error, $code, $attributes['with-column-info']);
fwrite(STDERR, $message . "\n");
echo $message . "\n";
}
if (null === $stmts) {
continue;
@@ -71,26 +71,25 @@ foreach ($files as $file) {
$stmts = $parser->parse($code);
} catch (PhpParser\Error $error) {
$message = formatErrorMessage($error, $code, $attributes['with-column-info']);
fwrite(STDERR, $message . "\n");
exit(1);
die($message . "\n");
}
}
foreach ($operations as $operation) {
if ('dump' === $operation) {
fwrite(STDERR, "==> Node dump:\n");
echo "==> Node dump:\n";
echo $dumper->dump($stmts, $code), "\n";
} elseif ('pretty-print' === $operation) {
fwrite(STDERR, "==> Pretty print:\n");
echo "==> Pretty print:\n";
echo $prettyPrinter->prettyPrintFile($stmts), "\n";
} elseif ('json-dump' === $operation) {
fwrite(STDERR, "==> JSON dump:\n");
echo json_encode($stmts, JSON_PRETTY_PRINT), "\n";
} elseif ('serialize-xml' === $operation) {
echo "==> Serialized XML:\n";
echo $serializer->serialize($stmts), "\n";
} elseif ('var-dump' === $operation) {
fwrite(STDERR, "==> var_dump():\n");
echo "==> var_dump():\n";
var_dump($stmts);
} elseif ('resolve-names' === $operation) {
fwrite(STDERR, "==> Resolved names.\n");
echo "==> Resolved names.\n";
$stmts = $traverser->traverse($stmts);
}
}
@@ -106,9 +105,9 @@ function formatErrorMessage(PhpParser\Error $e, $code, $withColumnInfo) {
function showHelp($error = '') {
if ($error) {
fwrite(STDERR, $error . "\n\n");
echo $error . "\n\n";
}
fwrite($error ? STDERR : STDOUT, <<<OUTPUT
die(<<<OUTPUT
Usage: php-parse [operations] file1.php [file2.php ...]
or: php-parse [operations] "<?php code"
Turn PHP source code into an abstract syntax tree.
@@ -117,7 +116,7 @@ Operations is a list of the following options (--dump by default):
-d, --dump Dump nodes using NodeDumper
-p, --pretty-print Pretty print file using PrettyPrinter\Standard
-j, --json-dump Print json_encode() result
--serialize-xml Serialize nodes using Serializer\XML
--var-dump var_dump() nodes (for exact structure)
-N, --resolve-names Resolve names using NodeVisitor\NameResolver
-c, --with-column-info Show column-numbers for errors (if available)
@@ -133,17 +132,16 @@ Example:
OUTPUT
);
exit($error ? 1 : 0);
}
function parseArgs($args) {
$operations = [];
$files = [];
$attributes = [
$operations = array();
$files = array();
$attributes = array(
'with-column-info' => false,
'with-positions' => false,
'with-recovery' => false,
];
);
array_shift($args);
$parseOptions = true;
@@ -162,9 +160,8 @@ function parseArgs($args) {
case '-p':
$operations[] = 'pretty-print';
break;
case '--json-dump':
case '-j':
$operations[] = 'json-dump';
case '--serialize-xml':
$operations[] = 'serialize-xml';
break;
case '--var-dump':
$operations[] = 'var-dump';
@@ -201,5 +198,5 @@ function parseArgs($args) {
}
}
return [$operations, $files, $attributes];
return array($operations, $files, $attributes);
}