Test Failed
Push — master ( a0eed4...bb00ac )
by Kirill
149:40
created

Frontend::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
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;
11
12
use Psr\Log\LoggerAwareInterface;
13
use Psr\Log\LoggerAwareTrait;
14
use Psr\Log\LoggerInterface;
15
use Railt\Io\Readable;
16
use Railt\Parser\Ast\RuleInterface;
17
use Railt\Parser\Exception\UnexpectedTokenException;
18
use Railt\Parser\Exception\UnrecognizedTokenException;
19
use Railt\SDL\Exception\CompilerException;
20
use Railt\SDL\Exception\SyntaxException;
21
use Railt\SDL\Frontend\Builder;
22
use Railt\SDL\Frontend\Parser;
23
24
/**
25
 * Class Frontend
26
 */
27
class Frontend implements LoggerAwareInterface
28
{
29
    use LoggerAwareTrait;
30
31
    /**
32
     * @var Parser
33
     */
34
    private $parser;
35
36
    /**
37
     * @var Builder
38
     */
39
    private $builder;
40
41
    /**
42
     * Frontend constructor.
43
     */
44
    public function __construct()
45
    {
46
        $this->parser  = new Parser();
47
        $this->builder = new Builder();
48
    }
49
50
    /**
51
     * @param Readable $readable
52
     * @return mixed|null
53
     * @throws SyntaxException
54
     * @throws CompilerException
55
     * @throws \LogicException
56
     */
57
    public function load(Readable $readable): iterable
58
    {
59
        $ast = $this->parse($readable);
60
61
        yield $this->builder->reduce($readable, $ast);
62
    }
63
64
    /**
65
     * Parse the file using top-down parser and
66
     * return the Abstract Syntax Tree.
67
     *
68
     * @param Readable $file
69
     * @return RuleInterface
70
     * @throws SyntaxException
71
     */
72
    private function parse(Readable $file): RuleInterface
73
    {
74
        try {
75
            return $this->parser->parse($file);
76
        } catch (UnexpectedTokenException | UnrecognizedTokenException $e) {
77
            $error = new SyntaxException($e->getMessage(), $e->getCode());
78
            $error->throwsIn($file, $e->getLine(), $e->getColumn());
79
80
            throw $error;
81
        }
82
    }
83
84
    /**
85
     * @param LoggerInterface $logger
86
     * @return Frontend
87
     */
88
    public function setLogger(LoggerInterface $logger): self
89
    {
90
        $this->logger = $logger;
91
92
        return $this;
93
    }
94
}
95