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