4 foreach ([__DIR__ . '/../../../autoload.php', __DIR__ . '/../vendor/autoload.php'] as $file) {
5 if (file_exists($file)) {
11 ini_set('xdebug.max_nesting_level', 3000);
13 // Disable XDebug var_dump() output truncation
14 ini_set('xdebug.var_display_max_children', -1);
15 ini_set('xdebug.var_display_max_data', -1);
16 ini_set('xdebug.var_display_max_depth', -1);
18 list($operations, $files, $attributes) = parseArgs($argv);
20 /* Dump nodes by default */
21 if (empty($operations)) {
22 $operations[] = 'dump';
26 showHelp("Must specify at least one file.");
29 $lexer = new PhpParser\Lexer\Emulative(array('usedAttributes' => array(
30 'startLine', 'endLine', 'startFilePos', 'endFilePos', 'comments'
32 $parser = (new PhpParser\ParserFactory)->create(
33 PhpParser\ParserFactory::PREFER_PHP7,
36 $dumper = new PhpParser\NodeDumper([
37 'dumpComments' => true,
38 'dumpPositions' => $attributes['with-positions'],
40 $prettyPrinter = new PhpParser\PrettyPrinter\Standard;
41 $serializer = new PhpParser\Serializer\XML;
43 $traverser = new PhpParser\NodeTraverser();
44 $traverser->addVisitor(new PhpParser\NodeVisitor\NameResolver);
46 foreach ($files as $file) {
47 if (strpos($file, '<?php') === 0) {
49 echo "====> Code $code\n";
51 if (!file_exists($file)) {
52 die("File $file does not exist.\n");
55 $code = file_get_contents($file);
56 echo "====> File $file:\n";
59 if ($attributes['with-recovery']) {
60 $errorHandler = new PhpParser\ErrorHandler\Collecting;
61 $stmts = $parser->parse($code, $errorHandler);
62 foreach ($errorHandler->getErrors() as $error) {
63 $message = formatErrorMessage($error, $code, $attributes['with-column-info']);
66 if (null === $stmts) {
71 $stmts = $parser->parse($code);
72 } catch (PhpParser\Error $error) {
73 $message = formatErrorMessage($error, $code, $attributes['with-column-info']);
78 foreach ($operations as $operation) {
79 if ('dump' === $operation) {
80 echo "==> Node dump:\n";
81 echo $dumper->dump($stmts, $code), "\n";
82 } elseif ('pretty-print' === $operation) {
83 echo "==> Pretty print:\n";
84 echo $prettyPrinter->prettyPrintFile($stmts), "\n";
85 } elseif ('serialize-xml' === $operation) {
86 echo "==> Serialized XML:\n";
87 echo $serializer->serialize($stmts), "\n";
88 } elseif ('var-dump' === $operation) {
89 echo "==> var_dump():\n";
91 } elseif ('resolve-names' === $operation) {
92 echo "==> Resolved names.\n";
93 $stmts = $traverser->traverse($stmts);
98 function formatErrorMessage(PhpParser\Error $e, $code, $withColumnInfo) {
99 if ($withColumnInfo && $e->hasColumnInfo()) {
100 return $e->getMessageWithColumnInfo($code);
102 return $e->getMessage();
106 function showHelp($error = '') {
108 echo $error . "\n\n";
111 Usage: php-parse [operations] file1.php [file2.php ...]
112 or: php-parse [operations] "<?php code"
113 Turn PHP source code into an abstract syntax tree.
115 Operations is a list of the following options (--dump by default):
117 -d, --dump Dump nodes using NodeDumper
118 -p, --pretty-print Pretty print file using PrettyPrinter\Standard
119 --serialize-xml Serialize nodes using Serializer\XML
120 --var-dump var_dump() nodes (for exact structure)
121 -N, --resolve-names Resolve names using NodeVisitor\NameResolver
122 -c, --with-column-info Show column-numbers for errors (if available)
123 -P, --with-positions Show positions in node dumps
124 -r, --with-recovery Use parsing with error recovery
125 -h, --help Display this page
128 php-parse -d -p -N -d file.php
130 Dumps nodes, pretty prints them, then resolves names and dumps them again.
137 function parseArgs($args) {
138 $operations = array();
141 'with-column-info' => false,
142 'with-positions' => false,
143 'with-recovery' => false,
147 $parseOptions = true;
148 foreach ($args as $arg) {
149 if (!$parseOptions) {
157 $operations[] = 'dump';
159 case '--pretty-print':
161 $operations[] = 'pretty-print';
163 case '--serialize-xml':
164 $operations[] = 'serialize-xml';
167 $operations[] = 'var-dump';
169 case '--resolve-names':
171 $operations[] = 'resolve-names';
173 case '--with-column-info':
175 $attributes['with-column-info'] = true;
177 case '--with-positions':
179 $attributes['with-positions'] = true;
181 case '--with-recovery':
183 $attributes['with-recovery'] = true;
190 $parseOptions = false;
193 if ($arg[0] === '-') {
194 showHelp("Invalid operation $arg.");
201 return array($operations, $files, $attributes);