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

Parser   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 54
c 0
b 0
f 0
ccs 0
cts 28
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getState() 0 12 4
A process() 0 4 1
A getResult() 0 4 1
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\Reader;
11
12
use Railt\Compiler\ParsingResult;
13
use Railt\Compiler\Reader\Resolver\PragmaResolver;
14
use Railt\Compiler\Reader\Resolver\ResolverInterface;
15
use Railt\Compiler\Reader\Resolver\RuleResolver;
16
use Railt\Compiler\Reader\Resolver\TokenResolver;
17
use Railt\Io\Readable;
18
use Railt\Lexer\TokenInterface;
19
20
/**
21
 * Class Parser
22
 */
23
class Parser
24
{
25
    private const STATE_CONFIGURE = 0x00;
26
    private const STATE_TOKEN = 0x01;
27
    private const STATE_PRODUCTIONS = 0x02;
28
29
    /**
30
     * @var array|ResolverInterface[]
31
     */
32
    private $resolvers;
33
34
    /**
35
     * Parser constructor.
36
     */
37
    public function __construct()
38
    {
39
        $this->resolvers = [
40
            self::STATE_CONFIGURE => new PragmaResolver(),
41
            self::STATE_TOKEN => new TokenResolver(),
42
            self::STATE_PRODUCTIONS => new RuleResolver(),
43
        ];
44
    }
45
46
    /**
47
     * @param TokenInterface $token
48
     * @return int
49
     */
50
    private function getState(TokenInterface $token): int
51
    {
52
        switch ($token->name()) {
53
            case 'T_PRAGMA':
54
                return self::STATE_CONFIGURE;
55
            case 'T_TOKEN':
56
            case 'T_SKIP':
57
                return self::STATE_TOKEN;
58
            default:
59
                return self::STATE_PRODUCTIONS;
60
        }
61
    }
62
63
    /**
64
     * @param Readable $file
65
     * @param TokenInterface $token
66
     */
67
    public function process(Readable $file, TokenInterface $token): void
68
    {
69
        $this->resolvers[$this->getState($token)]->resolve($file, $token);
70
    }
71
72
    public function getResult(): ParsingResult
73
    {
74
        dd($this->resolvers);
75
    }
76
}
77