annotate vendor/psy/psysh/src/Command/ParseCommand.php @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents c75dbcec494b
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of Psy Shell.
Chris@0 5 *
Chris@0 6 * (c) 2012-2018 Justin Hileman
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Psy\Command;
Chris@0 13
Chris@0 14 use PhpParser\Node;
Chris@0 15 use PhpParser\Parser;
Chris@0 16 use Psy\Context;
Chris@0 17 use Psy\ContextAware;
Chris@0 18 use Psy\Input\CodeArgument;
Chris@0 19 use Psy\ParserFactory;
Chris@0 20 use Psy\VarDumper\Presenter;
Chris@0 21 use Psy\VarDumper\PresenterAware;
Chris@0 22 use Symfony\Component\Console\Input\InputInterface;
Chris@0 23 use Symfony\Component\Console\Input\InputOption;
Chris@0 24 use Symfony\Component\Console\Output\OutputInterface;
Chris@0 25 use Symfony\Component\VarDumper\Caster\Caster;
Chris@0 26
Chris@0 27 /**
Chris@0 28 * Parse PHP code and show the abstract syntax tree.
Chris@0 29 */
Chris@0 30 class ParseCommand extends Command implements ContextAware, PresenterAware
Chris@0 31 {
Chris@0 32 /**
Chris@0 33 * Context instance (for ContextAware interface).
Chris@0 34 *
Chris@0 35 * @var Context
Chris@0 36 */
Chris@0 37 protected $context;
Chris@0 38
Chris@0 39 private $presenter;
Chris@0 40 private $parserFactory;
Chris@0 41 private $parsers;
Chris@0 42
Chris@0 43 /**
Chris@0 44 * {@inheritdoc}
Chris@0 45 */
Chris@0 46 public function __construct($name = null)
Chris@0 47 {
Chris@0 48 $this->parserFactory = new ParserFactory();
Chris@0 49 $this->parsers = [];
Chris@0 50
Chris@0 51 parent::__construct($name);
Chris@0 52 }
Chris@0 53
Chris@0 54 /**
Chris@0 55 * ContextAware interface.
Chris@0 56 *
Chris@0 57 * @param Context $context
Chris@0 58 */
Chris@0 59 public function setContext(Context $context)
Chris@0 60 {
Chris@0 61 $this->context = $context;
Chris@0 62 }
Chris@0 63
Chris@0 64 /**
Chris@0 65 * PresenterAware interface.
Chris@0 66 *
Chris@0 67 * @param Presenter $presenter
Chris@0 68 */
Chris@0 69 public function setPresenter(Presenter $presenter)
Chris@0 70 {
Chris@0 71 $this->presenter = clone $presenter;
Chris@0 72 $this->presenter->addCasters([
Chris@0 73 'PhpParser\Node' => function (Node $node, array $a) {
Chris@0 74 $a = [
Chris@0 75 Caster::PREFIX_VIRTUAL . 'type' => $node->getType(),
Chris@0 76 Caster::PREFIX_VIRTUAL . 'attributes' => $node->getAttributes(),
Chris@0 77 ];
Chris@0 78
Chris@0 79 foreach ($node->getSubNodeNames() as $name) {
Chris@0 80 $a[Caster::PREFIX_VIRTUAL . $name] = $node->$name;
Chris@0 81 }
Chris@0 82
Chris@0 83 return $a;
Chris@0 84 },
Chris@0 85 ]);
Chris@0 86 }
Chris@0 87
Chris@0 88 /**
Chris@0 89 * {@inheritdoc}
Chris@0 90 */
Chris@0 91 protected function configure()
Chris@0 92 {
Chris@0 93 $definition = [
Chris@0 94 new CodeArgument('code', CodeArgument::REQUIRED, 'PHP code to parse.'),
Chris@0 95 new InputOption('depth', '', InputOption::VALUE_REQUIRED, 'Depth to parse.', 10),
Chris@0 96 ];
Chris@0 97
Chris@0 98 if ($this->parserFactory->hasKindsSupport()) {
Chris@0 99 $msg = 'One of PhpParser\\ParserFactory constants: '
Chris@4 100 . \implode(', ', ParserFactory::getPossibleKinds())
Chris@0 101 . " (default is based on current interpreter's version).";
Chris@0 102 $defaultKind = $this->parserFactory->getDefaultKind();
Chris@0 103
Chris@0 104 $definition[] = new InputOption('kind', '', InputOption::VALUE_REQUIRED, $msg, $defaultKind);
Chris@0 105 }
Chris@0 106
Chris@0 107 $this
Chris@0 108 ->setName('parse')
Chris@0 109 ->setDefinition($definition)
Chris@0 110 ->setDescription('Parse PHP code and show the abstract syntax tree.')
Chris@0 111 ->setHelp(
Chris@0 112 <<<'HELP'
Chris@0 113 Parse PHP code and show the abstract syntax tree.
Chris@0 114
Chris@0 115 This command is used in the development of PsySH. Given a string of PHP code,
Chris@0 116 it pretty-prints the PHP Parser parse tree.
Chris@0 117
Chris@0 118 See https://github.com/nikic/PHP-Parser
Chris@0 119
Chris@0 120 It prolly won't be super useful for most of you, but it's here if you want to play.
Chris@0 121 HELP
Chris@0 122 );
Chris@0 123 }
Chris@0 124
Chris@0 125 /**
Chris@0 126 * {@inheritdoc}
Chris@0 127 */
Chris@0 128 protected function execute(InputInterface $input, OutputInterface $output)
Chris@0 129 {
Chris@0 130 $code = $input->getArgument('code');
Chris@4 131 if (\strpos('<?', $code) === false) {
Chris@0 132 $code = '<?php ' . $code;
Chris@0 133 }
Chris@0 134
Chris@0 135 $parserKind = $this->parserFactory->hasKindsSupport() ? $input->getOption('kind') : null;
Chris@0 136 $depth = $input->getOption('depth');
Chris@0 137 $nodes = $this->parse($this->getParser($parserKind), $code);
Chris@0 138 $output->page($this->presenter->present($nodes, $depth));
Chris@0 139
Chris@0 140 $this->context->setReturnValue($nodes);
Chris@0 141 }
Chris@0 142
Chris@0 143 /**
Chris@0 144 * Lex and parse a string of code into statements.
Chris@0 145 *
Chris@0 146 * @param Parser $parser
Chris@0 147 * @param string $code
Chris@0 148 *
Chris@0 149 * @return array Statements
Chris@0 150 */
Chris@0 151 private function parse(Parser $parser, $code)
Chris@0 152 {
Chris@0 153 try {
Chris@0 154 return $parser->parse($code);
Chris@0 155 } catch (\PhpParser\Error $e) {
Chris@4 156 if (\strpos($e->getMessage(), 'unexpected EOF') === false) {
Chris@0 157 throw $e;
Chris@0 158 }
Chris@0 159
Chris@0 160 // If we got an unexpected EOF, let's try it again with a semicolon.
Chris@0 161 return $parser->parse($code . ';');
Chris@0 162 }
Chris@0 163 }
Chris@0 164
Chris@0 165 /**
Chris@0 166 * Get (or create) the Parser instance.
Chris@0 167 *
Chris@0 168 * @param string|null $kind One of Psy\ParserFactory constants (only for PHP parser 2.0 and above)
Chris@0 169 *
Chris@0 170 * @return Parser
Chris@0 171 */
Chris@0 172 private function getParser($kind = null)
Chris@0 173 {
Chris@4 174 if (!\array_key_exists($kind, $this->parsers)) {
Chris@0 175 $this->parsers[$kind] = $this->parserFactory->createParser($kind);
Chris@0 176 }
Chris@0 177
Chris@0 178 return $this->parsers[$kind];
Chris@0 179 }
Chris@0 180 }