No Description

php-parse 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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(array('usedAttributes' => array(
  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. $serializer = new PhpParser\Serializer\XML;
  35. $traverser = new PhpParser\NodeTraverser();
  36. $traverser->addVisitor(new PhpParser\NodeVisitor\NameResolver);
  37. foreach ($files as $file) {
  38. if (strpos($file, '<?php') === 0) {
  39. $code = $file;
  40. echo "====> Code $code\n";
  41. } else {
  42. if (!file_exists($file)) {
  43. die("File $file does not exist.\n");
  44. }
  45. $code = file_get_contents($file);
  46. echo "====> 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. echo $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. die($message . "\n");
  64. }
  65. }
  66. foreach ($operations as $operation) {
  67. if ('dump' === $operation) {
  68. echo "==> Node dump:\n";
  69. echo $dumper->dump($stmts, $code), "\n";
  70. } elseif ('pretty-print' === $operation) {
  71. echo "==> Pretty print:\n";
  72. echo $prettyPrinter->prettyPrintFile($stmts), "\n";
  73. } elseif ('serialize-xml' === $operation) {
  74. echo "==> Serialized XML:\n";
  75. echo $serializer->serialize($stmts), "\n";
  76. } elseif ('var-dump' === $operation) {
  77. echo "==> var_dump():\n";
  78. var_dump($stmts);
  79. } elseif ('resolve-names' === $operation) {
  80. echo "==> Resolved names.\n";
  81. $stmts = $traverser->traverse($stmts);
  82. }
  83. }
  84. }
  85. function formatErrorMessage(PhpParser\Error $e, $code, $withColumnInfo) {
  86. if ($withColumnInfo && $e->hasColumnInfo()) {
  87. return $e->getMessageWithColumnInfo($code);
  88. } else {
  89. return $e->getMessage();
  90. }
  91. }
  92. function showHelp($error = '') {
  93. if ($error) {
  94. echo $error . "\n\n";
  95. }
  96. die(<<<OUTPUT
  97. Usage: php-parse [operations] file1.php [file2.php ...]
  98. or: php-parse [operations] "<?php code"
  99. Turn PHP source code into an abstract syntax tree.
  100. Operations is a list of the following options (--dump by default):
  101. -d, --dump Dump nodes using NodeDumper
  102. -p, --pretty-print Pretty print file using PrettyPrinter\Standard
  103. --serialize-xml Serialize nodes using Serializer\XML
  104. --var-dump var_dump() nodes (for exact structure)
  105. -N, --resolve-names Resolve names using NodeVisitor\NameResolver
  106. -c, --with-column-info Show column-numbers for errors (if available)
  107. -P, --with-positions Show positions in node dumps
  108. -r, --with-recovery Use parsing with error recovery
  109. -h, --help Display this page
  110. Example:
  111. php-parse -d -p -N -d file.php
  112. Dumps nodes, pretty prints them, then resolves names and dumps them again.
  113. OUTPUT
  114. );
  115. }
  116. function parseArgs($args) {
  117. $operations = array();
  118. $files = array();
  119. $attributes = array(
  120. 'with-column-info' => false,
  121. 'with-posititions' => false,
  122. 'with-recovery' => false,
  123. );
  124. array_shift($args);
  125. $parseOptions = true;
  126. foreach ($args as $arg) {
  127. if (!$parseOptions) {
  128. $files[] = $arg;
  129. continue;
  130. }
  131. switch ($arg) {
  132. case '--dump':
  133. case '-d':
  134. $operations[] = 'dump';
  135. break;
  136. case '--pretty-print':
  137. case '-p':
  138. $operations[] = 'pretty-print';
  139. break;
  140. case '--serialize-xml':
  141. $operations[] = 'serialize-xml';
  142. break;
  143. case '--var-dump':
  144. $operations[] = 'var-dump';
  145. break;
  146. case '--resolve-names':
  147. case '-N';
  148. $operations[] = 'resolve-names';
  149. break;
  150. case '--with-column-info':
  151. case '-c';
  152. $attributes['with-column-info'] = true;
  153. break;
  154. case '--with-positions':
  155. case '-P':
  156. $attributes['with-positions'] = true;
  157. break;
  158. case '--with-recovery':
  159. case '-r':
  160. $attributes['with-recovery'] = true;
  161. break;
  162. case '--help':
  163. case '-h';
  164. showHelp();
  165. break;
  166. case '--':
  167. $parseOptions = false;
  168. break;
  169. default:
  170. if ($arg[0] === '-') {
  171. showHelp("Invalid operation $arg.");
  172. } else {
  173. $files[] = $arg;
  174. }
  175. }
  176. }
  177. return array($operations, $files, $attributes);
  178. }