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\Builder; |
11
|
|
|
|
12
|
|
|
use Railt\Io\Readable; |
13
|
|
|
use Railt\Parser\Ast\RuleInterface; |
14
|
|
|
use Railt\Reflection\AbstractDefinition; |
15
|
|
|
use Railt\Reflection\Contracts\Definition; |
16
|
|
|
use Railt\Reflection\Contracts\Document; |
17
|
|
|
use Railt\SDL\Compiler\Process; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class Builder |
21
|
|
|
*/ |
22
|
|
|
abstract class Builder implements BuilderInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var Readable |
26
|
|
|
*/ |
27
|
|
|
protected $file; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Definition |
31
|
|
|
*/ |
32
|
|
|
protected $definition; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Document |
36
|
|
|
*/ |
37
|
|
|
protected $document; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var RuleInterface |
41
|
|
|
*/ |
42
|
|
|
protected $ast; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var Process |
46
|
|
|
*/ |
47
|
|
|
private $process; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Builder constructor. |
51
|
|
|
* @param Context $ctx |
52
|
|
|
* @param RuleInterface $ast |
53
|
|
|
* @param Process $process |
54
|
|
|
*/ |
55
|
|
|
public function __construct(Context $ctx, RuleInterface $ast, Process $process) |
56
|
|
|
{ |
57
|
|
|
$this->ast = $ast; |
58
|
|
|
$this->process = $process; |
59
|
|
|
$this->file = $ctx->getFile(); |
60
|
|
|
$this->definition = $ctx->getDefinition(); |
61
|
|
|
$this->document = $this->definition->getDocument(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param \Closure $then |
66
|
|
|
*/ |
67
|
|
|
public function async(\Closure $then): void |
68
|
|
|
{ |
69
|
|
|
$this->process->async($then); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param Definition|AbstractDefinition $definition |
74
|
|
|
* @return Definition |
75
|
|
|
*/ |
76
|
|
|
protected function bind(Definition $definition): Definition |
77
|
|
|
{ |
78
|
|
|
$definition->withOffset($this->ast->getOffset()); |
79
|
|
|
|
80
|
|
|
return $definition; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return string |
85
|
|
|
* @throws \Railt\SDL\Exception\SyntaxException |
86
|
|
|
*/ |
87
|
|
|
protected function getName(): string |
88
|
|
|
{ |
89
|
|
|
return Utils::getName($this->file, $this->ast); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return string|null |
94
|
|
|
*/ |
95
|
|
|
protected function findName(): ?string |
96
|
|
|
{ |
97
|
|
|
return Utils::findName($this->ast); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|