php-parse 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #!/usr/bin/env php
  2. <?php
  3. foreach ([__DIR__ . '/../../../autoload.php', __DIR__ . '/../vendor/autoload.php'] as $file) {
  4. if (file_exists($file)) {
  5. require $file;
  6. break;
  7. }
  8. }
  9. ini_set('xdebug.max_nesting_level', 3000);
  10. // Disable Xdebug var_dump() output truncation
  11. ini_set('xdebug.var_display_max_children', -1);
  12. ini_set('xdebug.var_display_max_data', -1);
  13. ini_set('xdebug.var_display_max_depth', -1);
  14. list($operations, $files, $attributes) = parseArgs($argv);
  15. /* Dump nodes by default */
  16. if (empty($operations)) {
  17. $operations[] = 'dump';
  18. }
  19. if (empty($files)) {
  20. showHelp("Must specify at least one file.");
  21. }
  22. $parser = (new PhpParser\ParserFactory())->createForVersion($attributes['version']);
  23. $dumper = new PhpParser\NodeDumper([
  24. 'dumpComments' => true,
  25. 'dumpPositions' => $attributes['with-positions'],
  26. ]);
  27. $prettyPrinter = new PhpParser\PrettyPrinter\Standard;
  28. $traverser = new PhpParser\NodeTraverser();
  29. $traverser->addVisitor(new PhpParser\NodeVisitor\NameResolver);
  30. foreach ($files as $file) {
  31. if ($file === '-') {
  32. $code = file_get_contents('php://stdin');
  33. fwrite(STDERR, "====> Stdin:\n");
  34. } else if (strpos($file, '<?php') === 0) {
  35. $code = $file;
  36. fwrite(STDERR, "====> Code $code\n");
  37. } else {
  38. if (!file_exists($file)) {
  39. fwrite(STDERR, "File $file does not exist.\n");
  40. exit(1);
  41. }
  42. $code = file_get_contents($file);
  43. fwrite(STDERR, "====> File $file:\n");
  44. }
  45. if ($attributes['with-recovery']) {
  46. $errorHandler = new PhpParser\ErrorHandler\Collecting;
  47. $stmts = $parser->parse($code, $errorHandler);
  48. foreach ($errorHandler->getErrors() as $error) {
  49. $message = formatErrorMessage($error, $code, $attributes['with-column-info']);
  50. fwrite(STDERR, $message . "\n");
  51. }
  52. if (null === $stmts) {
  53. continue;
  54. }
  55. } else {
  56. try {
  57. $stmts = $parser->parse($code);
  58. } catch (PhpParser\Error $error) {
  59. $message = formatErrorMessage($error, $code, $attributes['with-column-info']);
  60. fwrite(STDERR, $message . "\n");
  61. exit(1);
  62. }
  63. }
  64. foreach ($operations as $operation) {
  65. if ('dump' === $operation) {
  66. fwrite(STDERR, "==> Node dump:\n");
  67. echo $dumper->dump($stmts, $code), "\n";
  68. } elseif ('pretty-print' === $operation) {
  69. fwrite(STDERR, "==> Pretty print:\n");
  70. echo $prettyPrinter->prettyPrintFile($stmts), "\n";
  71. } elseif ('json-dump' === $operation) {
  72. fwrite(STDERR, "==> JSON dump:\n");
  73. echo json_encode($stmts, JSON_PRETTY_PRINT), "\n";
  74. } elseif ('var-dump' === $operation) {
  75. fwrite(STDERR, "==> var_dump():\n");
  76. var_dump($stmts);
  77. } elseif ('resolve-names' === $operation) {
  78. fwrite(STDERR, "==> Resolved names.\n");
  79. $stmts = $traverser->traverse($stmts);
  80. }
  81. }
  82. }
  83. function formatErrorMessage(PhpParser\Error $e, $code, $withColumnInfo) {
  84. if ($withColumnInfo && $e->hasColumnInfo()) {
  85. return $e->getMessageWithColumnInfo($code);
  86. } else {
  87. return $e->getMessage();
  88. }
  89. }
  90. function showHelp($error = '') {
  91. if ($error) {
  92. fwrite(STDERR, $error . "\n\n");
  93. }
  94. fwrite($error ? STDERR : STDOUT, <<<'OUTPUT'
  95. Usage: php-parse [operations] file1.php [file2.php ...]
  96. or: php-parse [operations] "<?php code"
  97. Turn PHP source code into an abstract syntax tree.
  98. Operations is a list of the following options (--dump by default):
  99. -d, --dump Dump nodes using NodeDumper
  100. -p, --pretty-print Pretty print file using PrettyPrinter\Standard
  101. -j, --json-dump Print json_encode() result
  102. --var-dump var_dump() nodes (for exact structure)
  103. -N, --resolve-names Resolve names using NodeVisitor\NameResolver
  104. -c, --with-column-info Show column-numbers for errors (if available)
  105. -P, --with-positions Show positions in node dumps
  106. -r, --with-recovery Use parsing with error recovery
  107. --version=VERSION Target specific PHP version (default: newest)
  108. -h, --help Display this page
  109. Example:
  110. php-parse -d -p -N -d file.php
  111. Dumps nodes, pretty prints them, then resolves names and dumps them again.
  112. OUTPUT
  113. );
  114. exit($error ? 1 : 0);
  115. }
  116. function parseArgs($args) {
  117. $operations = [];
  118. $files = [];
  119. $attributes = [
  120. 'with-column-info' => false,
  121. 'with-positions' => false,
  122. 'with-recovery' => false,
  123. 'version' => PhpParser\PhpVersion::getNewestSupported(),
  124. ];
  125. array_shift($args);
  126. $parseOptions = true;
  127. foreach ($args as $arg) {
  128. if (!$parseOptions) {
  129. $files[] = $arg;
  130. continue;
  131. }
  132. switch ($arg) {
  133. case '--dump':
  134. case '-d':
  135. $operations[] = 'dump';
  136. break;
  137. case '--pretty-print':
  138. case '-p':
  139. $operations[] = 'pretty-print';
  140. break;
  141. case '--json-dump':
  142. case '-j':
  143. $operations[] = 'json-dump';
  144. break;
  145. case '--var-dump':
  146. $operations[] = 'var-dump';
  147. break;
  148. case '--resolve-names':
  149. case '-N';
  150. $operations[] = 'resolve-names';
  151. break;
  152. case '--with-column-info':
  153. case '-c';
  154. $attributes['with-column-info'] = true;
  155. break;
  156. case '--with-positions':
  157. case '-P':
  158. $attributes['with-positions'] = true;
  159. break;
  160. case '--with-recovery':
  161. case '-r':
  162. $attributes['with-recovery'] = true;
  163. break;
  164. case '--help':
  165. case '-h';
  166. showHelp();
  167. break;
  168. case '--':
  169. $parseOptions = false;
  170. break;
  171. default:
  172. if (preg_match('/^--version=(.*)$/', $arg, $matches)) {
  173. $attributes['version'] = PhpParser\PhpVersion::fromString($matches[1]);
  174. } elseif ($arg[0] === '-' && \strlen($arg[0]) > 1) {
  175. showHelp("Invalid operation $arg.");
  176. } else {
  177. $files[] = $arg;
  178. }
  179. }
  180. }
  181. return [$operations, $files, $attributes];
  182. }