Passed
Push — master ( a7212f...b9d836 )
by Kirill
01:47
created

Builder::make()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
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\Definition\TypeDefinition;
17
use Railt\Reflection\Contracts\Document;
18
use Railt\Reflection\Exception\TypeNotFoundException;
19
use Railt\SDL\Compiler\Backend;
20
use Railt\SDL\Compiler\Process;
21
use Railt\SDL\Exception\TypeNotFoundException as SDLTypeNotFoundException;
22
23
/**
24
 * Class Builder
25
 */
26
abstract class Builder implements BuilderInterface
27
{
28
    /**
29
     * @var Readable
30
     */
31
    protected $file;
32
33
    /**
34
     * @var Definition
35
     */
36
    protected $definition;
37
38
    /**
39
     * @var Document
40
     */
41
    protected $document;
42
43
    /**
44
     * @var RuleInterface
45
     */
46
    protected $ast;
47
48
    /**
49
     * @var Backend
50
     */
51
    private $backend;
52
53
    /**
54
     * Builder constructor.
55
     * @param Context $ctx
56
     * @param RuleInterface $ast
57
     * @param Backend $backend
58
     */
59
    public function __construct(Context $ctx, RuleInterface $ast, Backend $backend)
60
    {
61
        $this->ast = $ast;
62
        $this->backend = $backend;
63
        $this->file = $ctx->getFile();
64
        $this->definition = $ctx->getDefinition();
65
        $this->document = $this->definition->getDocument();
66
    }
67
68
    /**
69
     * @param string $type
70
     * @param Definition $from
71
     * @param int $offset
72
     * @return TypeDefinition
73
     * @throws SDLTypeNotFoundException
74
     */
75
    protected function load(string $type, Definition $from, int $offset): TypeDefinition
76
    {
77
        try {
78
            return $from->getDictionary()->get($type, $from);
79
        } catch (TypeNotFoundException $e) {
80
            $exception = new SDLTypeNotFoundException($e->getMessage());
81
            $exception->throwsIn($from->getFile(), $offset);
82
83
            throw $exception;
84
        }
85
    }
86
87
    /**
88
     * @param \Closure $then
89
     */
90
    protected function async(\Closure $then): void
91
    {
92
        $this->backend->async($then);
93
    }
94
95
    /**
96
     * @param Definition $context
97
     * @param RuleInterface $ast
98
     * @return Definition
99
     * @throws \Railt\SDL\Exception\TypeException
100
     */
101
    protected function make(Definition $context, RuleInterface $ast): Definition
102
    {
103
        return $this->backend->exec(new Context($this->file, $context), $ast);
104
    }
105
106
    /**
107
     * @param Definition|AbstractDefinition $definition
108
     * @return Definition
109
     */
110
    protected function bind(Definition $definition): Definition
111
    {
112
        $definition->withOffset($this->ast->getOffset());
113
114
        return $definition;
115
    }
116
117
    /**
118
     * @return string
119
     * @throws \Railt\SDL\Exception\SyntaxException
120
     */
121
    protected function getName(): string
122
    {
123
        return Utils::getName($this->file, $this->ast);
124
    }
125
126
    /**
127
     * @return string|null
128
     */
129
    protected function findName(): ?string
130
    {
131
        return Utils::findName($this->ast);
132
    }
133
}
134