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\SDL\Console; |
11
|
|
|
|
12
|
|
|
use Railt\Compiler\Generator\ParserGenerator; |
13
|
|
|
use Railt\Compiler\Grammar\ParsingResult; |
14
|
|
|
use Railt\Compiler\Grammar\Reader; |
15
|
|
|
use Railt\Io\File; |
16
|
|
|
use Railt\SDL\Parser\Factory; |
17
|
|
|
use Symfony\Component\Console\Command\Command; |
18
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
19
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
20
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class ParserCompileCommand |
24
|
|
|
*/ |
25
|
|
|
class ParserCompileCommand extends Command |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var string Default parser output path |
29
|
|
|
*/ |
30
|
|
|
private const DEFAULT_PATH = __DIR__ . '/../Parser'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string Default generated parser FQN |
34
|
|
|
*/ |
35
|
|
|
private const DEFAULT_PARSER_CLASS_NAME = \Railt\SDL\Parser\SchemaParser::class; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param InputInterface $in |
39
|
|
|
* @param OutputInterface $out |
40
|
|
|
* @return int|null|void |
41
|
|
|
* @throws \LogicException |
42
|
|
|
*/ |
43
|
|
|
public function execute(InputInterface $in, OutputInterface $out) |
44
|
|
|
{ |
45
|
|
|
$grammar = File::fromPathname($in->getArgument('grammar')); |
46
|
|
|
|
47
|
|
|
$this->buildParser($in, (new Reader())->read($grammar)); |
|
|
|
|
48
|
|
|
$out->writeln('<info>OK</info>'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param InputInterface $in |
53
|
|
|
* @param ParsingResult $reader |
54
|
|
|
* @throws \Symfony\Component\Console\Exception\InvalidArgumentException |
55
|
|
|
*/ |
56
|
|
|
private function buildParser(InputInterface $in, ParsingResult $reader): void |
57
|
|
|
{ |
58
|
|
|
[$class, $namespace] = $this->split($in->getArgument('parser')); |
|
|
|
|
59
|
|
|
|
60
|
|
|
(new ParserGenerator($reader)) |
61
|
|
|
->class($class)->namespace($namespace)->build() |
62
|
|
|
->saveTo($in->getArgument('output')); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $class |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
|
|
private function split(string $class): array |
70
|
|
|
{ |
71
|
|
|
$parts = \array_filter(\explode('\\', $class)); |
72
|
|
|
|
73
|
|
|
return [\array_pop($parts), \implode('\\', $parts)]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return void |
78
|
|
|
* @throws \Symfony\Component\Console\Exception\InvalidArgumentException |
79
|
|
|
* @throws \Symfony\Component\Console\Exception\LogicException |
80
|
|
|
*/ |
81
|
|
|
protected function configure(): void |
82
|
|
|
{ |
83
|
|
|
$this->setName('parser:compile'); |
84
|
|
|
$this->setDescription('Builds a new optimised lexer and parser from .pp/.pp2 grammar file.'); |
85
|
|
|
|
86
|
|
|
$this->addArgument( |
87
|
|
|
'output', |
88
|
|
|
InputArgument::OPTIONAL, |
89
|
|
|
'Output generated lexer directory', |
90
|
|
|
self::DEFAULT_PATH |
91
|
|
|
); |
92
|
|
|
|
93
|
|
|
$this->addArgument( |
94
|
|
|
'grammar', |
95
|
|
|
InputArgument::OPTIONAL, |
96
|
|
|
'Input grammar file', |
97
|
|
|
Factory::GRAMMAR_FILE |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
$this->addArgument( |
101
|
|
|
'parser', |
102
|
|
|
InputArgument::OPTIONAL, |
103
|
|
|
'The parser output class name', |
104
|
|
|
self::DEFAULT_PARSER_CLASS_NAME |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.