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\Definition; |
11
|
|
|
|
12
|
|
|
use Railt\Parser\Ast\RuleInterface; |
13
|
|
|
use Railt\SDL\Frontend\Builder\BaseBuilder; |
14
|
|
|
use Railt\SDL\Frontend\Context\ContextInterface; |
15
|
|
|
use Railt\SDL\Frontend\Deferred\NamedDeferred; |
16
|
|
|
use Railt\SDL\Frontend\Definition\DefinitionInterface; |
17
|
|
|
use Railt\SDL\IR\Definition\InterfaceDefinitionValueObject; |
18
|
|
|
use Railt\SDL\IR\Definition\ObjectDefinitionValueObject; |
19
|
|
|
use Railt\SDL\IR\DefinitionValueObject; |
20
|
|
|
use Railt\SDL\IR\SymbolTable\ValueInterface; |
21
|
|
|
use Railt\SDL\IR\Type; |
22
|
|
|
use Railt\SDL\IR\ValueObject; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class ObjectDefinitionBuilder |
26
|
|
|
*/ |
27
|
|
|
class ObjectDefinitionBuilder extends BaseBuilder |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @param RuleInterface $rule |
31
|
|
|
* @return bool |
32
|
|
|
*/ |
33
|
|
|
public function match(RuleInterface $rule): bool |
34
|
|
|
{ |
35
|
|
|
return $rule->getName() === 'ObjectDefinition'; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param ContextInterface $ctx |
40
|
|
|
* @param RuleInterface $rule |
41
|
|
|
* @return mixed|\Generator|void |
42
|
|
|
*/ |
43
|
|
|
public function reduce(ContextInterface $ctx, RuleInterface $rule) |
44
|
|
|
{ |
45
|
|
|
/** @var DefinitionInterface $definition */ |
46
|
|
|
$definition = yield $rule->first('> #TypeDefinition'); |
47
|
|
|
|
48
|
|
|
yield new NamedDeferred($definition, $ctx, function (ContextInterface $local) use ($definition, $rule) { |
49
|
|
|
$struct = new ObjectDefinitionValueObject(); |
50
|
|
|
|
51
|
|
|
$struct->type = Type::object($definition->getName()->getFullyQualifiedName()); |
52
|
|
|
$struct->name = $definition->getName(); |
53
|
|
|
$struct->file = $local->getFile(); |
54
|
|
|
|
55
|
|
|
yield from $this->loadInterfaces($local, $struct, $rule); |
56
|
|
|
|
57
|
|
|
return $struct; |
58
|
|
|
}); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param ContextInterface $local |
63
|
|
|
* @param ObjectDefinitionValueObject $vo |
64
|
|
|
* @param RuleInterface $rule |
65
|
|
|
* @return \Generator |
66
|
|
|
* @throws \Railt\SDL\Exception\NotFoundException |
67
|
|
|
*/ |
68
|
|
|
private function loadInterfaces(ContextInterface $local, ObjectDefinitionValueObject $vo, RuleInterface $rule): \Generator |
69
|
|
|
{ |
70
|
|
|
foreach ($rule->find('> #TypeDefinitionImplements') as $impl) { |
71
|
|
|
/** @var ValueInterface $result */ |
72
|
|
|
$result = yield $impl->getChild(0); |
73
|
|
|
|
74
|
|
|
yield from $iterator = $this->invoke($result->getValue(), $local); |
75
|
|
|
|
76
|
|
|
/** @var InterfaceDefinitionValueObject $interface */ |
77
|
|
|
$interface = $this->loadInterface($iterator->getReturn()); |
78
|
|
|
|
79
|
|
|
$vo->implements[] = $interface->name; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
private function loadInterface(DefinitionValueObject $dto) |
84
|
|
|
{ |
85
|
|
|
return $dto; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|