No Description

php-parse 6.1KB

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