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.");
|
|
|
|
}
|
|
|
|
|
2015-04-18 13:05:40 +02:00
|
|
|
$lexer = new PhpParser\Lexer\Emulative(array('usedAttributes' => array(
|
2016-03-10 12:51:47 +01:00
|
|
|
'startLine', 'endLine', 'startFilePos', 'endFilePos', 'comments'
|
2015-04-18 13:05:40 +02:00
|
|
|
)));
|
2015-07-14 21:11:54 +02:00
|
|
|
$parser = (new PhpParser\ParserFactory)->create(PhpParser\ParserFactory::PREFER_PHP7, $lexer);
|
2016-03-09 18:57:16 +01:00
|
|
|
$dumper = new PhpParser\NodeDumper(['dumpComments' => true]);
|
2014-04-19 23:12:20 +02:00
|
|
|
$prettyPrinter = new PhpParser\PrettyPrinter\Standard;
|
|
|
|
$serializer = new PhpParser\Serializer\XML;
|
|
|
|
|
|
|
|
$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;
|
|
|
|
echo "====> Code $code\n";
|
|
|
|
} else {
|
|
|
|
if (!file_exists($file)) {
|
|
|
|
die("File $file does not exist.\n");
|
|
|
|
}
|
2014-04-19 23:12:20 +02:00
|
|
|
|
2014-09-28 13:14:37 +02:00
|
|
|
$code = file_get_contents($file);
|
|
|
|
echo "====> File $file:\n";
|
|
|
|
}
|
2014-04-19 23:12:20 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
$stmts = $parser->parse($code);
|
|
|
|
} catch (PhpParser\Error $e) {
|
2015-04-18 13:05:40 +02:00
|
|
|
if ($attributes['with-column-info'] && $e->hasColumnInfo()) {
|
|
|
|
$startLine = $e->getStartLine();
|
|
|
|
$endLine = $e->getEndLine();
|
|
|
|
$startColumn = $e->getStartColumn($code);
|
|
|
|
$endColumn = $e->getEndColumn($code);
|
|
|
|
$message .= $e->getRawMessage() . " from $startLine:$startColumn to $endLine:$endColumn";
|
|
|
|
} else {
|
|
|
|
$message = $e->getMessage();
|
2015-04-14 20:31:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
die($message . "\n");
|
2014-04-19 23:12:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($operations as $operation) {
|
|
|
|
if ('dump' === $operation) {
|
|
|
|
echo "==> Node dump:\n";
|
|
|
|
echo $dumper->dump($stmts), "\n";
|
|
|
|
} elseif ('pretty-print' === $operation) {
|
|
|
|
echo "==> Pretty print:\n";
|
|
|
|
echo $prettyPrinter->prettyPrintFile($stmts), "\n";
|
|
|
|
} elseif ('serialize-xml' === $operation) {
|
|
|
|
echo "==> Serialized XML:\n";
|
|
|
|
echo $serializer->serialize($stmts), "\n";
|
|
|
|
} elseif ('var-dump' === $operation) {
|
|
|
|
echo "==> var_dump():\n";
|
|
|
|
var_dump($stmts);
|
|
|
|
} elseif ('resolve-names' === $operation) {
|
|
|
|
echo "==> Resolved names.\n";
|
|
|
|
$stmts = $traverser->traverse($stmts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-15 14:36:11 +02:00
|
|
|
function showHelp($error = '') {
|
|
|
|
if ($error) {
|
|
|
|
echo $error . "\n\n";
|
|
|
|
}
|
|
|
|
die(<<<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
|
|
|
|
--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)
|
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
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseArgs($args) {
|
|
|
|
$operations = array();
|
|
|
|
$files = array();
|
2015-04-14 20:31:06 +02:00
|
|
|
$attributes = array(
|
|
|
|
'with-column-info' => false,
|
|
|
|
);
|
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;
|
|
|
|
case '--serialize-xml':
|
|
|
|
$operations[] = 'serialize-xml';
|
|
|
|
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-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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-14 20:31:06 +02:00
|
|
|
return array($operations, $files, $attributes);
|
2014-11-26 02:03:35 +01:00
|
|
|
}
|