1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Railt package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Railt\Compiler\Console; |
11
|
|
|
|
12
|
|
|
use Railt\Compiler\Compiler; |
13
|
|
|
use Railt\Io\File; |
14
|
|
|
use Symfony\Component\Console\Command\Command; |
15
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
17
|
|
|
use Symfony\Component\Console\Input\InputOption; |
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class AnalyzeCommand |
22
|
|
|
*/ |
23
|
|
|
class AnalyzeCommand extends Command |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @param InputInterface $in |
27
|
|
|
* @param OutputInterface $out |
28
|
|
|
* @throws \Railt\Io\Exception\ExternalFileException |
29
|
|
|
* @throws \Railt\Io\Exception\NotReadableException |
30
|
|
|
* @throws \Throwable |
31
|
|
|
*/ |
32
|
|
|
public function execute(InputInterface $in, OutputInterface $out): void |
33
|
|
|
{ |
34
|
|
|
$compiler = Compiler::load(File::fromPathname($in->getArgument('grammar'))); |
|
|
|
|
35
|
|
|
|
36
|
|
|
$ast = $compiler->parse(File::fromPathname($in->getArgument('source'))); |
|
|
|
|
37
|
|
|
|
38
|
|
|
$ast = $in->getOption('root') ? $ast->first($in->getOption('root')) : $ast; |
39
|
|
|
|
40
|
|
|
$out->write((string)$ast); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return void |
45
|
|
|
* @throws \Symfony\Component\Console\Exception\InvalidArgumentException |
46
|
|
|
* @throws \Symfony\Component\Console\Exception\LogicException |
47
|
|
|
*/ |
48
|
|
|
protected function configure(): void |
49
|
|
|
{ |
50
|
|
|
$this->setName('compiler:analyze'); |
51
|
|
|
$this->setDescription('Analyze source file using selection grammar'); |
52
|
|
|
|
53
|
|
|
$this->addArgument('grammar', InputArgument::REQUIRED, |
54
|
|
|
'Input pp2 grammar file'); |
55
|
|
|
|
56
|
|
|
$this->addArgument('source', InputArgument::REQUIRED, |
57
|
|
|
'Input source file for parsing'); |
58
|
|
|
|
59
|
|
|
$this->addOption('root', 'r', InputOption::VALUE_OPTIONAL, |
60
|
|
|
'Sets an AST root node for dumping'); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.