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