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\Exception\UnknownPragmaException; |
13
|
|
|
use Railt\Io\Readable; |
14
|
|
|
use Railt\Lexer\TokenInterface; |
15
|
|
|
use Railt\Parser\Configuration; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class BasePragmas |
19
|
|
|
*/ |
20
|
|
|
abstract class BasePragmas implements ProvidePragmas |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
public const GROUP_PARSER = 'parser'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
public const GROUP_LEXER = 'lexer'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
public const GROUP_GRAMMAR = 'grammar'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array|Config[] |
39
|
|
|
*/ |
40
|
|
|
private $resolvers; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
private $configs = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* PragmaResolver constructor. |
49
|
|
|
*/ |
50
|
|
|
public function __construct() |
51
|
|
|
{ |
52
|
|
|
$this->bootResolvers(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
|
|
private function bootResolvers(): void |
59
|
|
|
{ |
60
|
|
|
$this->resolvers[self::GROUP_PARSER] = $this->getParserResolver(); |
61
|
|
|
$this->resolvers[self::GROUP_LEXER] = $this->getLexerResolver(); |
62
|
|
|
$this->resolvers[self::GROUP_GRAMMAR] = $this->getGrammarResolver(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return Config |
67
|
|
|
*/ |
68
|
|
|
private function getParserResolver(): Config |
69
|
|
|
{ |
70
|
|
|
return new Config(self::GROUP_PARSER, [ |
71
|
|
|
Configuration::PRAGMA_ROOT, |
72
|
|
|
Configuration::PRAGMA_LOOKAHEAD, |
73
|
|
|
Configuration::PRAGMA_RUNTIME, |
74
|
|
|
]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return Config |
79
|
|
|
*/ |
80
|
|
|
private function getLexerResolver(): Config |
81
|
|
|
{ |
82
|
|
|
return new Config(self::GROUP_LEXER, [ |
83
|
|
|
// TODO |
84
|
|
|
]); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return Config |
89
|
|
|
*/ |
90
|
|
|
private function getGrammarResolver(): Config |
91
|
|
|
{ |
92
|
|
|
return new Config(self::GROUP_GRAMMAR, [ |
93
|
|
|
// TODO |
94
|
|
|
]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return iterable|Config[] |
99
|
|
|
*/ |
100
|
|
|
protected function getResolvers(): iterable |
101
|
|
|
{ |
102
|
|
|
return $this->resolvers; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
public function parser(): array |
109
|
|
|
{ |
110
|
|
|
return $this->resolvers[self::GROUP_GRAMMAR] ?? []; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
|
|
public function lexer(): array |
117
|
|
|
{ |
118
|
|
|
return $this->resolvers[self::GROUP_LEXER] ?? []; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
|
|
public function grammar(): array |
125
|
|
|
{ |
126
|
|
|
return $this->resolvers[self::GROUP_GRAMMAR] ?? []; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string $group |
131
|
|
|
* @param string $name |
132
|
|
|
* @param string $value |
133
|
|
|
*/ |
134
|
|
|
protected function set(string $group, string $name, string $value): void |
135
|
|
|
{ |
136
|
|
|
if (! \array_key_exists($group, $this->configs)) { |
137
|
|
|
$this->configs[$group] = []; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$this->configs[$group][$name] = $value; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param Readable $readable |
145
|
|
|
* @param TokenInterface $token |
146
|
|
|
* @param null|string $name |
147
|
|
|
* @return string |
148
|
|
|
* @throws \Railt\Io\Exception\ExternalFileException |
149
|
|
|
*/ |
150
|
|
|
protected function verifyPragmaName(Readable $readable, TokenInterface $token, ?string $name): string |
151
|
|
|
{ |
152
|
|
|
if ($name === null) { |
153
|
|
|
$error = \vsprintf('Unknown configuration pragma rule "%s" with value "%s"', [ |
154
|
|
|
$token->value(1), |
155
|
|
|
$token->value(2), |
156
|
|
|
]); |
157
|
|
|
|
158
|
|
|
throw (new UnknownPragmaException($error))->throwsIn($readable, $token->offset()); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $name; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|