3
require __DIR__ . '/../lib/bootstrap.php';
5
ini_set('xdebug.max_nesting_level', 2000);
7
/* The fancy var_dump function provided by XDebug will cut off the output way too
8
* early to be of use. */
9
ini_set('xdebug.overload_var_dump', 0);
11
list($operations, $files) = parseArgs($argv);
13
/* Dump nodes by default */
14
if (empty($operations)) {
15
$operations[] = 'dump';
19
showHelp("Must specify at least one file.");
22
$parser = new PhpParser\Parser(new PhpParser\Lexer\Emulative);
23
$dumper = new PhpParser\NodeDumper;
24
$prettyPrinter = new PhpParser\PrettyPrinter\Standard;
25
$serializer = new PhpParser\Serializer\XML;
27
$traverser = new PhpParser\NodeTraverser();
28
$traverser->addVisitor(new PhpParser\NodeVisitor\NameResolver);
30
foreach ($files as $file) {
31
if (!file_exists($file)) {
32
die("File $file does not exist.\n");
35
echo "====> File $file:\n";
37
$code = file_get_contents($file);
39
$stmts = $parser->parse($code);
40
} catch (PhpParser\Error $e) {
41
die("==> Parse Error: {$e->getMessage()}\n");
44
foreach ($operations as $operation) {
45
if ('dump' === $operation) {
46
echo "==> Node dump:\n";
47
echo $dumper->dump($stmts), "\n";
48
} elseif ('pretty-print' === $operation) {
49
echo "==> Pretty print:\n";
50
echo $prettyPrinter->prettyPrintFile($stmts), "\n";
51
} elseif ('serialize-xml' === $operation) {
52
echo "==> Serialized XML:\n";
53
echo $serializer->serialize($stmts), "\n";
54
} elseif ('var-dump' === $operation) {
55
echo "==> var_dump():\n";
57
} elseif ('resolve-names' === $operation) {
58
echo "==> Resolved names.\n";
59
$stmts = $traverser->traverse($stmts);
64
function showHelp($error) {
69
php php-parse.php [operations] file1.php [file2.php ...]
71
Operations is a list of the following options (--dump by default):
73
--dump -d Dump nodes using NodeDumper
74
--pretty-print -p Pretty print file using PrettyPrinter\Standard
75
--serialize-xml Serialize nodes using Serializer\XML
76
--var-dump var_dump() nodes (for exact structure)
77
--resolve-names -N Resolve names using NodeVisitor\NameResolver
81
php php-parse.php -d -p -N -d file.php
83
Dumps nodes, pretty prints them, then resolves names and dumps them again.
88
function parseArgs($args) {
89
$operations = array();
94
foreach ($args as $arg) {
103
$operations[] = 'dump';
105
case '--pretty-print':
107
$operations[] = 'pretty-print';
109
case '--serialize-xml':
110
$operations[] = 'serialize-xml';
113
$operations[] = 'var-dump';
115
case '--resolve-names':
117
$operations[] = 'resolve-names';
120
$parseOptions = false;
123
if ($arg[0] === '-') {
124
showHelp("Invalid operation $arg.");
131
return array($operations, $files);
b'\\ No newline at end of file'