2015-02-14 19:30:25 +01:00
|
|
|
#!/usr/bin/env php
|
2014-04-19 23:12:20 +02:00
|
|
|
<?php
|
|
|
|
|
2015-09-21 08:57:31 +02:00
|
|
|
foreach ([__DIR__ . '/../../../autoload.php', __DIR__ . '/../vendor/autoload.php'] as $file) {
|
2015-09-21 08:39:19 +02:00
|
|
|
if (file_exists($file)) {
|
|
|
|
require $file;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-04-19 23:12:20 +02:00
|
|
|
|
2015-02-13 15:04:00 +01:00
|
|
|
ini_set('xdebug.max_nesting_level', 3000);
|
2014-04-19 23:12:20 +02:00
|
|
|
|
2014-09-28 13:08:59 +02:00
|
|
|
// Disable XDebug var_dump() output truncation
|
|
|
|
ini_set('xdebug.var_display_max_children', -1);
|
|
|
|
ini_set('xdebug.var_display_max_data', -1);
|
|
|
|
ini_set('xdebug.var_display_max_depth', -1);
|
2014-08-31 16:21:21 +02:00
|
|
|
|
2015-04-14 20:31:06 +02:00
|
|
|
list($operations, $files, $attributes) = parseArgs($argv);
|
2014-04-19 23:12:20 +02:00
|
|
|
|
|
|
|
/* Dump nodes by default */
|
|
|
|
if (empty($operations)) {
|
|
|
|
$operations[] = 'dump';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($files)) {
|
|
|
|
showHelp("Must specify at least one file.");
|
|
|
|
}
|
|
|
|
|
2017-08-13 21:58:00 +02:00
|
|
|
$lexer = new PhpParser\Lexer\Emulative(['usedAttributes' => [
|
2016-03-10 12:51:47 +01:00
|
|
|
'startLine', 'endLine', 'startFilePos', 'endFilePos', 'comments'
|
2017-08-13 21:58:00 +02:00
|
|
|
]]);
|
2016-07-25 17:27:12 +02:00
|
|
|
$parser = (new PhpParser\ParserFactory)->create(
|
|
|
|
PhpParser\ParserFactory::PREFER_PHP7,
|
2016-10-16 22:19:33 +02:00
|
|
|
$lexer
|
2016-07-25 17:27:12 +02:00
|
|
|
);
|
2016-12-09 22:41:46 +01:00
|
|
|
$dumper = new PhpParser\NodeDumper([
|
|
|
|
'dumpComments' => true,
|
|
|
|
'dumpPositions' => $attributes['with-positions'],
|
|
|
|
]);
|
2014-04-19 23:12:20 +02:00
|
|
|
$prettyPrinter = new PhpParser\PrettyPrinter\Standard;
|
|
|
|
|
|
|
|
$traverser = new PhpParser\NodeTraverser();
|
|
|
|
$traverser->addVisitor(new PhpParser\NodeVisitor\NameResolver);
|
|
|
|
|
|
|
|
foreach ($files as $file) {
|
2014-09-28 13:14:37 +02:00
|
|
|
if (strpos($file, '<?php') === 0) {
|
|
|
|
$code = $file;
|
2019-04-28 17:34:48 +02:00
|
|
|
fwrite(STDERR, "====> Code $code\n");
|
2014-09-28 13:14:37 +02:00
|
|
|
} else {
|
|
|
|
if (!file_exists($file)) {
|
2019-04-28 17:34:48 +02:00
|
|
|
fwrite(STDERR, "File $file does not exist.\n");
|
|
|
|
exit(1);
|
2014-09-28 13:14:37 +02:00
|
|
|
}
|
2014-04-19 23:12:20 +02:00
|
|
|
|
2014-09-28 13:14:37 +02:00
|
|
|
$code = file_get_contents($file);
|
2019-04-28 17:34:48 +02:00
|
|
|
fwrite(STDERR, "====> File $file:\n");
|
2014-09-28 13:14:37 +02:00
|
|
|
}
|
2014-04-19 23:12:20 +02:00
|
|
|
|
2016-10-16 22:19:33 +02:00
|
|
|
if ($attributes['with-recovery']) {
|
|
|
|
$errorHandler = new PhpParser\ErrorHandler\Collecting;
|
|
|
|
$stmts = $parser->parse($code, $errorHandler);
|
|
|
|
foreach ($errorHandler->getErrors() as $error) {
|
2016-07-25 17:27:12 +02:00
|
|
|
$message = formatErrorMessage($error, $code, $attributes['with-column-info']);
|
2019-04-28 17:34:48 +02:00
|
|
|
fwrite(STDERR, $message . "\n");
|
2015-04-14 20:31:06 +02:00
|
|
|
}
|
2016-07-25 17:27:12 +02:00
|
|
|
if (null === $stmts) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-10-16 22:19:33 +02:00
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
$stmts = $parser->parse($code);
|
|
|
|
} catch (PhpParser\Error $error) {
|
|
|
|
$message = formatErrorMessage($error, $code, $attributes['with-column-info']);
|
2019-04-28 17:34:48 +02:00
|
|
|
fwrite(STDERR, $message . "\n");
|
|
|
|
exit(1);
|
2016-10-16 22:19:33 +02:00
|
|
|
}
|
2014-04-19 23:12:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($operations as $operation) {
|
|
|
|
if ('dump' === $operation) {
|
2019-04-28 17:34:48 +02:00
|
|
|
fwrite(STDERR, "==> Node dump:\n");
|
2016-12-09 22:41:46 +01:00
|
|
|
echo $dumper->dump($stmts, $code), "\n";
|
2014-04-19 23:12:20 +02:00
|
|
|
} elseif ('pretty-print' === $operation) {
|
2019-04-28 17:34:48 +02:00
|
|
|
fwrite(STDERR, "==> Pretty print:\n");
|
2014-04-19 23:12:20 +02:00
|
|
|
echo $prettyPrinter->prettyPrintFile($stmts), "\n";
|
2018-02-08 16:21:25 +01:00
|
|
|
} elseif ('json-dump' === $operation) {
|
2019-04-28 17:34:48 +02:00
|
|
|
fwrite(STDERR, "==> JSON dump:\n");
|
2018-02-08 16:21:25 +01:00
|
|
|
echo json_encode($stmts, JSON_PRETTY_PRINT), "\n";
|
2014-04-19 23:12:20 +02:00
|
|
|
} elseif ('var-dump' === $operation) {
|
2019-04-28 17:34:48 +02:00
|
|
|
fwrite(STDERR, "==> var_dump():\n");
|
2014-04-19 23:12:20 +02:00
|
|
|
var_dump($stmts);
|
|
|
|
} elseif ('resolve-names' === $operation) {
|
2019-04-28 17:34:48 +02:00
|
|
|
fwrite(STDERR, "==> Resolved names.\n");
|
2014-04-19 23:12:20 +02:00
|
|
|
$stmts = $traverser->traverse($stmts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-25 17:27:12 +02:00
|
|
|
function formatErrorMessage(PhpParser\Error $e, $code, $withColumnInfo) {
|
|
|
|
if ($withColumnInfo && $e->hasColumnInfo()) {
|
2016-09-30 18:24:43 +02:00
|
|
|
return $e->getMessageWithColumnInfo($code);
|
2016-07-25 17:27:12 +02:00
|
|
|
} else {
|
|
|
|
return $e->getMessage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-15 14:36:11 +02:00
|
|
|
function showHelp($error = '') {
|
|
|
|
if ($error) {
|
2019-04-28 17:34:48 +02:00
|
|
|
fwrite(STDERR, $error . "\n\n");
|
2016-04-15 14:36:11 +02:00
|
|
|
}
|
2019-04-28 17:34:48 +02:00
|
|
|
fwrite($error ? STDERR : STDOUT, <<<OUTPUT
|
2015-09-21 08:39:19 +02:00
|
|
|
Usage: php-parse [operations] file1.php [file2.php ...]
|
|
|
|
or: php-parse [operations] "<?php code"
|
2014-09-04 22:26:30 +02:00
|
|
|
Turn PHP source code into an abstract syntax tree.
|
2014-09-28 13:14:37 +02:00
|
|
|
|
2014-04-19 23:12:20 +02:00
|
|
|
Operations is a list of the following options (--dump by default):
|
|
|
|
|
2014-09-04 22:26:30 +02:00
|
|
|
-d, --dump Dump nodes using NodeDumper
|
|
|
|
-p, --pretty-print Pretty print file using PrettyPrinter\Standard
|
2018-02-08 16:21:25 +01:00
|
|
|
-j, --json-dump Print json_encode() result
|
2014-09-04 22:26:30 +02:00
|
|
|
--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)
|
2016-12-09 22:41:46 +01:00
|
|
|
-P, --with-positions Show positions in node dumps
|
2016-07-25 17:27:12 +02:00
|
|
|
-r, --with-recovery Use parsing with error recovery
|
2016-04-15 13:58:42 +02:00
|
|
|
-h, --help Display this page
|
2014-04-19 23:12:20 +02:00
|
|
|
|
|
|
|
Example:
|
2015-09-21 08:39:19 +02:00
|
|
|
php-parse -d -p -N -d file.php
|
2014-04-19 23:12:20 +02:00
|
|
|
|
|
|
|
Dumps nodes, pretty prints them, then resolves names and dumps them again.
|
2014-11-26 02:03:35 +01:00
|
|
|
|
|
|
|
|
2014-04-19 23:12:20 +02:00
|
|
|
OUTPUT
|
|
|
|
);
|
2019-04-28 17:34:48 +02:00
|
|
|
exit($error ? 1 : 0);
|
2014-04-19 23:12:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function parseArgs($args) {
|
2017-08-13 21:58:00 +02:00
|
|
|
$operations = [];
|
|
|
|
$files = [];
|
|
|
|
$attributes = [
|
2016-12-09 22:41:46 +01:00
|
|
|
'with-column-info' => false,
|
2017-02-08 23:28:49 +01:00
|
|
|
'with-positions' => false,
|
2016-07-25 17:27:12 +02:00
|
|
|
'with-recovery' => false,
|
2017-08-13 21:58:00 +02:00
|
|
|
];
|
2014-04-19 23:12:20 +02:00
|
|
|
|
|
|
|
array_shift($args);
|
|
|
|
$parseOptions = true;
|
|
|
|
foreach ($args as $arg) {
|
|
|
|
if (!$parseOptions) {
|
|
|
|
$files[] = $arg;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($arg) {
|
|
|
|
case '--dump':
|
|
|
|
case '-d':
|
|
|
|
$operations[] = 'dump';
|
|
|
|
break;
|
|
|
|
case '--pretty-print':
|
|
|
|
case '-p':
|
|
|
|
$operations[] = 'pretty-print';
|
|
|
|
break;
|
2018-02-08 16:21:25 +01:00
|
|
|
case '--json-dump':
|
|
|
|
case '-j':
|
|
|
|
$operations[] = 'json-dump';
|
2014-04-19 23:12:20 +02:00
|
|
|
break;
|
|
|
|
case '--var-dump':
|
|
|
|
$operations[] = 'var-dump';
|
|
|
|
break;
|
|
|
|
case '--resolve-names':
|
|
|
|
case '-N';
|
|
|
|
$operations[] = 'resolve-names';
|
|
|
|
break;
|
2015-04-14 20:31:06 +02:00
|
|
|
case '--with-column-info':
|
|
|
|
case '-c';
|
|
|
|
$attributes['with-column-info'] = true;
|
|
|
|
break;
|
2016-12-09 22:41:46 +01:00
|
|
|
case '--with-positions':
|
|
|
|
case '-P':
|
|
|
|
$attributes['with-positions'] = true;
|
|
|
|
break;
|
2016-07-25 17:27:12 +02:00
|
|
|
case '--with-recovery':
|
|
|
|
case '-r':
|
|
|
|
$attributes['with-recovery'] = true;
|
|
|
|
break;
|
2016-04-15 13:58:42 +02:00
|
|
|
case '--help':
|
|
|
|
case '-h';
|
2016-04-15 14:36:11 +02:00
|
|
|
showHelp();
|
2016-04-15 13:58:42 +02:00
|
|
|
break;
|
2014-04-19 23:12:20 +02:00
|
|
|
case '--':
|
|
|
|
$parseOptions = false;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if ($arg[0] === '-') {
|
|
|
|
showHelp("Invalid operation $arg.");
|
|
|
|
} else {
|
|
|
|
$files[] = $arg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-13 21:58:00 +02:00
|
|
|
return [$operations, $files, $attributes];
|
2014-11-26 02:03:35 +01:00
|
|
|
}
|