|
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\Frontend\Builder\Common; |
|
11
|
|
|
|
|
12
|
|
|
use Railt\Parser\Ast\LeafInterface; |
|
13
|
|
|
use Railt\Parser\Ast\RuleInterface; |
|
14
|
|
|
use Railt\SDL\Frontend\Builder\BaseBuilder; |
|
15
|
|
|
use Railt\SDL\Frontend\Context\ContextInterface; |
|
16
|
|
|
use Railt\SDL\IR\SymbolTable\TypeNamePrimitive; |
|
17
|
|
|
use Railt\SDL\IR\SymbolTable\Value; |
|
18
|
|
|
use Railt\SDL\IR\SymbolTable\ValueInterface; |
|
19
|
|
|
use Railt\SDL\IR\Type; |
|
20
|
|
|
use Railt\SDL\IR\Type\Name; |
|
21
|
|
|
use Railt\SDL\IR\Type\TypeNameInterface; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class TypeNameBuilder |
|
25
|
|
|
*/ |
|
26
|
|
|
class TypeNameBuilder extends BaseBuilder |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @param RuleInterface $rule |
|
30
|
|
|
* @return bool |
|
31
|
|
|
*/ |
|
32
|
|
|
public function match(RuleInterface $rule): bool |
|
33
|
|
|
{ |
|
34
|
|
|
return $rule->getName() === 'TypeName'; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param ContextInterface $ctx |
|
39
|
|
|
* @param RuleInterface $rule |
|
40
|
|
|
* @return TypeNameInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
public function reduce(ContextInterface $ctx, RuleInterface $rule): TypeNameInterface |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->getTypeName($rule); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param RuleInterface $rule |
|
49
|
|
|
* @return TypeNameInterface |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function getTypeName(RuleInterface $rule): TypeNameInterface |
|
52
|
|
|
{ |
|
53
|
|
|
$atRoot = $rule->first('> #AtRoot') instanceof RuleInterface; |
|
54
|
|
|
|
|
55
|
|
|
return Name::fromArray($this->getNameChunks($rule), $atRoot); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param RuleInterface $rule |
|
60
|
|
|
* @return array|string[] |
|
61
|
|
|
*/ |
|
62
|
|
|
private function getNameChunks(RuleInterface $rule): array |
|
63
|
|
|
{ |
|
64
|
|
|
$result = []; |
|
65
|
|
|
|
|
66
|
|
|
/** @var LeafInterface $leaf */ |
|
67
|
|
|
foreach ($rule->find('> :T_NAME') as $leaf) { |
|
68
|
|
|
$result[] = $leaf->getValue(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $result; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|