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\Compiler\Ast; |
11
|
|
|
|
12
|
|
|
use Railt\Parser\Ast\Rule; |
13
|
|
|
use Railt\Parser\Ast\RuleInterface; |
14
|
|
|
use Railt\Reflection\Contracts\Definition\Behaviour\ProvidesTypeIndication; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class TypeHintNode |
18
|
|
|
*/ |
19
|
|
|
class TypeHintNode extends Rule |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private const HINT_LIST = 'ListTypeHint'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private const HINT_SINGULAR = 'SingularTypeHint'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private const HINT_NOT_NULL = 'NonNull'; |
35
|
|
|
/** |
36
|
|
|
* @var int |
37
|
|
|
*/ |
38
|
|
|
protected $modifiers = 0; |
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $definition; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* TypeHintNode constructor. |
46
|
|
|
* @param string $name |
47
|
|
|
* @param array $children |
48
|
|
|
* @param int $offset |
49
|
|
|
*/ |
50
|
|
|
public function __construct(string $name, $children = [], int $offset = 0) |
51
|
|
|
{ |
52
|
|
|
parent::__construct($name, $children, $offset); |
53
|
|
|
|
54
|
|
|
$inner = \reset($children); |
55
|
|
|
|
56
|
|
|
if ($this->isListTypeHint($inner)) { |
57
|
|
|
$this->parseListTypeHint($inner); |
58
|
|
|
} else { |
59
|
|
|
$this->parseInnerTypeHint($inner); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param RuleInterface $rule |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
private function isListTypeHint(RuleInterface $rule): bool |
68
|
|
|
{ |
69
|
|
|
return $rule->getName() === self::HINT_LIST; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param RuleInterface $rule |
74
|
|
|
*/ |
75
|
|
|
private function parseListTypeHint(RuleInterface $rule): void |
76
|
|
|
{ |
77
|
|
|
$this->modifiers |= ProvidesTypeIndication::IS_LIST; |
78
|
|
|
|
79
|
|
|
foreach ($rule->getChildren() as $child) { |
80
|
|
|
switch ($child->getName()) { |
81
|
|
|
case self::HINT_SINGULAR: |
82
|
|
|
$this->parseInnerTypeHint($child); |
83
|
|
|
break; |
84
|
|
|
|
85
|
|
|
case self::HINT_NOT_NULL: |
86
|
|
|
$this->modifiers |= ProvidesTypeIndication::IS_LIST_OF_NOT_NULL; |
87
|
|
|
break; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param RuleInterface $rule |
94
|
|
|
*/ |
95
|
|
|
private function parseInnerTypeHint(RuleInterface $rule): void |
96
|
|
|
{ |
97
|
|
|
foreach ($rule->getChildren() as $child) { |
98
|
|
|
switch ($child->getName()) { |
99
|
|
|
case self::HINT_NOT_NULL: |
100
|
|
|
$this->modifiers |= ProvidesTypeIndication::IS_NOT_NULL; |
101
|
|
|
break; |
102
|
|
|
|
103
|
|
|
case 'TypeName': |
104
|
|
|
/** @var TypeNameNode $child */ |
105
|
|
|
$this->definition = $child->getTypeName(); |
106
|
|
|
break; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
public function getTypeName(): string |
115
|
|
|
{ |
116
|
|
|
return $this->definition; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return int |
121
|
|
|
*/ |
122
|
|
|
public function getModifiers(): int |
123
|
|
|
{ |
124
|
|
|
return $this->modifiers; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|