Passed
Push — master ( e44dc3...5dec7d )
by Kirill
07:30
created

ParsingResult::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
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\Compiler;
11
12
use Railt\Compiler\Builder\Buildable;
13
use Railt\Compiler\Reader\PragmaParser;
14
use Railt\Compiler\Reader\ProductionParser;
15
use Railt\Compiler\Reader\RuleAnalyzer;
16
use Railt\Compiler\Reader\TokenParser;
17
use Railt\Compiler\Lexer\NativeStateless;
18
use Railt\Compiler\Lexer\Stateless;
19
use Railt\Compiler\Parser;
20
use Railt\Compiler\Parser\Runtime as ParserRuntime;
21
use Railt\Compiler\ParserInterface;
22
23
/**
24
 * Class Result
25
 */
26
class ParsingResult
27
{
28
    /**
29
     * @var PragmaParser
30
     */
31
    private $pragma;
32
33
    /**
34
     * @var TokenParser
35
     */
36
    private $tokens;
37
38
    /**
39
     * @var ProductionParser
40
     */
41
    private $productions;
42
43
    /**
44
     * @var Stateless
45
     */
46
    private $lexer;
47
48
    /**
49
     * @var ParserInterface|ParserRuntime
50
     */
51
    private $parser;
52
53
    /**
54
     * Result constructor.
55
     * @param PragmaParser $pragma
56
     * @param TokenParser $tokens
57
     * @param ProductionParser $productions
58
     */
59
    public function __construct(PragmaParser $pragma, TokenParser $tokens, ProductionParser $productions)
60
    {
61
        $this->pragma      = $pragma;
62
        $this->tokens      = $tokens;
63
        $this->productions = $productions;
64
    }
65
66
    /**
67
     * @return ParserInterface|Parser
68
     */
69
    public function getParser(): ParserInterface
70
    {
71
        if ($this->parser === null) {
72
            $this->parser = new Parser($this->getLexer(), $this->getRules());
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Railt\Compiler\Pars...r(), $this->getRules()) of type object<Railt\Compiler\Parser> is incompatible with the declared type object<Railt\Compiler\Pa...ompiler\Parser\Runtime> of property $parser.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
73
        }
74
75
        return $this->parser;
76
    }
77
78
    /**
79
     * @return Stateless
80
     */
81
    public function getLexer(): Stateless
82
    {
83
        if ($this->lexer === null) {
84
            $this->lexer = new NativeStateless();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Railt\Compiler\Lexer\NativeStateless() of type object<Railt\Compiler\Lexer\NativeStateless> is incompatible with the declared type object<Railt\Compiler\Lexer\Stateless> of property $lexer.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
85
86
            foreach ($this->tokens->getTokens() as $name => $pcre) {
87
                $this->lexer->add($name, $pcre);
88
            }
89
90
            foreach ($this->tokens->getSkippedTokens() as $name) {
91
                $this->lexer->skip($name);
92
            }
93
        }
94
95
        return $this->lexer;
96
    }
97
98
    /**
99
     * @return array
100
     */
101
    public function getRules(): array
102
    {
103
        $result = [];
104
105
        foreach ($this->getBuilders() as $key => $builder) {
106
            $result[$key] = $builder->toRule();
107
        }
108
109
        return $result;
110
    }
111
112
    /**
113
     * @return array|Buildable[]
114
     */
115
    public function getBuilders(): iterable
116
    {
117
        $analyzer = new RuleAnalyzer($this->getLexer());
118
119
        return $analyzer->analyze($this->productions->getRules());
120
    }
121
}
122