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