Completed
Push — master ( 8a5799...ed0b0a )
by Kirill
07:58
created

Reader   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 8
dl 0
loc 78
ccs 0
cts 31
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A boot() 0 8 1
A getParser() 0 6 1
A addGrammar() 0 12 3
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\Compiler\Grammar;
11
12
use Railt\Compiler\Grammar\Delegate\IncludeDelegate;
13
use Railt\Io\Readable;
14
use Railt\Lexer\Driver\NativeRegex;
15
use Railt\Lexer\LexerInterface;
16
use Railt\Parser\Ast\RuleInterface;
17
use Railt\Parser\Driver\Llk;
18
use Railt\Parser\Grammar;
19
use Railt\Parser\GrammarInterface;
20
use Railt\Parser\ParserInterface;
21
22
/**
23
 * Class Reader
24
 */
25
class Reader
26
{
27
    /**
28
     * @var Readable
29
     */
30
    private $file;
31
32
    /**
33
     * @var ParserInterface
34
     */
35
    private $pp;
36
37
    /**
38
     * @var LexerInterface
39
     */
40
    private $lexer;
41
42
    /**
43
     * @var GrammarInterface
44
     */
45
    private $grammar;
46
47
    /**
48
     * Reader constructor.
49
     * @param Readable $file
50
     */
51
    public function __construct(Readable $file)
52
    {
53
        $this->file = $file;
54
        $this->pp = new Parser();
55
        $this->lexer = new NativeRegex();
56
        $this->grammar = new Grammar();
57
58
        $this->boot();
59
    }
60
61
    /**
62
     * @return void
63
     */
64
    private function boot(): void
65
    {
66
        $env = $this->pp->env();
67
68
        $env->share(LexerInterface::class, $this->lexer);
69
        $env->share(GrammarInterface::class, $this->grammar);
70
        $env->share(self::class, $this);
71
    }
72
73
    /**
74
     * @return ParserInterface
75
     * @throws \Railt\Io\Exception\ExternalFileException
76
     * @throws \Railt\Io\Exception\NotReadableException
77
     */
78
    public function getParser(): ParserInterface
79
    {
80
        $this->addGrammar($this->file);
81
82
        return new Llk($this->lexer, $this->grammar);
83
    }
84
85
    /**
86
     * @param Readable $file
87
     * @throws \Railt\Io\Exception\ExternalFileException
88
     * @throws \Railt\Io\Exception\NotReadableException
89
     */
90
    private function addGrammar(Readable $file): void
91
    {
92
        $ast = $this->pp->parse($file);
93
94
        foreach ($ast->getChildren() as $child) {
95
            switch (true) {
96
                case $child instanceof IncludeDelegate:
97
                    $this->addGrammar($child->getPathname($file));
98
                    break;
99
            }
100
        }
101
    }
102
}
103