From 3db3ad7d1ecd731ffdd0c4c04db4bb019f56c0c7 Mon Sep 17 00:00:00 2001 From: nikic Date: Sat, 19 Apr 2014 23:12:20 +0200 Subject: [PATCH] Add experimental php-parse script Script supporting dumping, pretty printing, serializing and resolving names. Intended to help exploring and debugging the node tree. --- bin/php-parse.php | 128 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 bin/php-parse.php diff --git a/bin/php-parse.php b/bin/php-parse.php new file mode 100644 index 0000000..7835106 --- /dev/null +++ b/bin/php-parse.php @@ -0,0 +1,128 @@ +addVisitor(new PhpParser\NodeVisitor\NameResolver); + +foreach ($files as $file) { + if (!file_exists($file)) { + die("File $file does not exist.\n"); + } + + echo "====> File $file:\n"; + + $code = file_get_contents($file); + try { + $stmts = $parser->parse($code); + } catch (PhpParser\Error $e) { + die("==> Parse Error: {$e->getMessage()}\n"); + } + + 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); + } + } +} + +function showHelp($error) { + die($error . "\n\n" . + <<