|
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\Lexer\Factory; |
|
13
|
|
|
use Railt\Lexer\LexerInterface; |
|
14
|
|
|
use Railt\Parser\Driver\Llk; |
|
15
|
|
|
use Railt\Parser\Driver\Stateful; |
|
16
|
|
|
use Railt\Parser\Grammar; |
|
17
|
|
|
use Railt\Parser\GrammarInterface; |
|
18
|
|
|
use Railt\Parser\ParserInterface; |
|
19
|
|
|
use Railt\Parser\Rule\Alternation; |
|
20
|
|
|
use Railt\Parser\Rule\Concatenation; |
|
21
|
|
|
use Railt\Parser\Rule\Repetition; |
|
22
|
|
|
use Railt\Parser\Rule\Terminal; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* --- DO NOT EDIT THIS FILE --- |
|
26
|
|
|
* |
|
27
|
|
|
* Class Parser has been auto-generated. |
|
28
|
|
|
* Generated at: 14-09-2018 17:15:13 |
|
29
|
|
|
* |
|
30
|
|
|
* --- DO NOT EDIT THIS FILE --- |
|
31
|
|
|
*/ |
|
32
|
|
|
class Parser extends Stateful |
|
33
|
|
|
{ |
|
34
|
|
|
public const T_PRAGMA = 'T_PRAGMA'; |
|
35
|
|
|
public const T_INCLUDE = 'T_INCLUDE'; |
|
36
|
|
|
public const T_TOKEN = 'T_TOKEN'; |
|
37
|
|
|
public const T_SKIP = 'T_SKIP'; |
|
38
|
|
|
public const T_OR = 'T_OR'; |
|
39
|
|
|
public const T_TOKEN_SKIPPED = 'T_TOKEN_SKIPPED'; |
|
40
|
|
|
public const T_TOKEN_KEPT = 'T_TOKEN_KEPT'; |
|
41
|
|
|
public const T_TOKEN_STRING = 'T_TOKEN_STRING'; |
|
42
|
|
|
public const T_INVOKE = 'T_INVOKE'; |
|
43
|
|
|
public const T_GROUP_OPEN = 'T_GROUP_OPEN'; |
|
44
|
|
|
public const T_GROUP_CLOSE = 'T_GROUP_CLOSE'; |
|
45
|
|
|
public const T_REPEAT_ZERO_OR_ONE = 'T_REPEAT_ZERO_OR_ONE'; |
|
46
|
|
|
public const T_REPEAT_ONE_OR_MORE = 'T_REPEAT_ONE_OR_MORE'; |
|
47
|
|
|
public const T_REPEAT_ZERO_OR_MORE = 'T_REPEAT_ZERO_OR_MORE'; |
|
48
|
|
|
public const T_REPEAT_N_TO_M = 'T_REPEAT_N_TO_M'; |
|
49
|
|
|
public const T_REPEAT_N_OR_MORE = 'T_REPEAT_N_OR_MORE'; |
|
50
|
|
|
public const T_REPEAT_ZERO_TO_M = 'T_REPEAT_ZERO_TO_M'; |
|
51
|
|
|
public const T_REPEAT_EXACTLY_N = 'T_REPEAT_EXACTLY_N'; |
|
52
|
|
|
public const T_KEPT_NAME = 'T_KEPT_NAME'; |
|
53
|
|
|
public const T_NAME = 'T_NAME'; |
|
54
|
|
|
public const T_EQ = 'T_EQ'; |
|
55
|
|
|
public const T_DELEGATE = 'T_DELEGATE'; |
|
56
|
|
|
public const T_END_OF_RULE = 'T_END_OF_RULE'; |
|
57
|
|
|
public const T_WHITESPACE = 'T_WHITESPACE'; |
|
58
|
|
|
public const T_COMMENT = 'T_COMMENT'; |
|
59
|
|
|
public const T_BLOCK_COMMENT = 'T_BLOCK_COMMENT'; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Lexical tokens list. |
|
63
|
|
|
* |
|
64
|
|
|
* @var string[] |
|
65
|
|
|
*/ |
|
66
|
|
|
protected const LEXER_TOKENS = [ |
|
67
|
|
|
self::T_PRAGMA => '%pragma\\h+([\\w\\.]+)\\h+([^\\s]+)', |
|
68
|
|
|
self::T_INCLUDE => '%include\\h+([^\\s]+)', |
|
69
|
|
|
self::T_TOKEN => '%token\\h+(\\w+)\\h+([^\\s]+)', |
|
70
|
|
|
self::T_SKIP => '%skip\\h+(\\w+)\\h+([^\\s]+)', |
|
71
|
|
|
self::T_OR => '\\|', |
|
72
|
|
|
self::T_TOKEN_SKIPPED => '::(\\w+)::', |
|
73
|
|
|
self::T_TOKEN_KEPT => '<(\\w+)>', |
|
74
|
|
|
self::T_TOKEN_STRING => '("[^"\\\\]+(\\\\.[^"\\\\]*)*"|\'[^\'\\\\]+(\\\\.[^\'\\\\]*)*\')', |
|
75
|
|
|
self::T_INVOKE => '(\\w+)\\(\\)', |
|
76
|
|
|
self::T_GROUP_OPEN => '\\(', |
|
77
|
|
|
self::T_GROUP_CLOSE => '\\)', |
|
78
|
|
|
self::T_REPEAT_ZERO_OR_ONE => '\\?', |
|
79
|
|
|
self::T_REPEAT_ONE_OR_MORE => '\\+', |
|
80
|
|
|
self::T_REPEAT_ZERO_OR_MORE => '\\*', |
|
81
|
|
|
self::T_REPEAT_N_TO_M => '{\\h*(\\-?\\d+)\\h*,\\h*(\\-?\\d+)\\h*}', |
|
82
|
|
|
self::T_REPEAT_N_OR_MORE => '{\\h*(\\-?\\d+)\\h*,\\h*}', |
|
83
|
|
|
self::T_REPEAT_ZERO_TO_M => '{\\h*,\\h*(\\-?\\d+)\\h*}', |
|
84
|
|
|
self::T_REPEAT_EXACTLY_N => '{\\h*(\\-?\\d+)\\h*}', |
|
85
|
|
|
self::T_KEPT_NAME => '#', |
|
86
|
|
|
self::T_NAME => '[a-zA-Z_\\x7f-\\xff\\\\][a-zA-Z0-9_\\x7f-\\xff\\\\]*', |
|
87
|
|
|
self::T_EQ => '(\\:|\\:\\:=|=)', |
|
88
|
|
|
self::T_DELEGATE => '\\->', |
|
89
|
|
|
self::T_END_OF_RULE => ';', |
|
90
|
|
|
self::T_WHITESPACE => '(\\xfe\\xff|\\x20|\\x09|\\x0a|\\x0d)+', |
|
91
|
|
|
self::T_COMMENT => '//[^\\n]*', |
|
92
|
|
|
self::T_BLOCK_COMMENT => '/\\*.*?\\*/', |
|
93
|
|
|
]; |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* List of skipped tokens. |
|
97
|
|
|
* |
|
98
|
|
|
* @var string[] |
|
99
|
|
|
*/ |
|
100
|
|
|
protected const LEXER_SKIPPED_TOKENS = [ |
|
101
|
|
|
'T_WHITESPACE', |
|
102
|
|
|
'T_COMMENT', |
|
103
|
|
|
'T_BLOCK_COMMENT', |
|
104
|
|
|
]; |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @var int |
|
108
|
|
|
*/ |
|
109
|
|
|
protected const LEXER_FLAGS = Factory::LOOKAHEAD; |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* List of rule delegates. |
|
113
|
|
|
* |
|
114
|
|
|
* @var string[] |
|
115
|
|
|
*/ |
|
116
|
|
|
protected const PARSER_DELEGATES = [ |
|
117
|
|
|
'TokenDefinition' => \Railt\Compiler\Grammar\Delegate\TokenDelegate::class, |
|
118
|
|
|
'IncludeDefinition' => \Railt\Compiler\Grammar\Delegate\IncludeDelegate::class, |
|
119
|
|
|
'RuleDefinition' => \Railt\Compiler\Grammar\Delegate\RuleDelegate::class, |
|
120
|
|
|
]; |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Parser root rule name. |
|
124
|
|
|
* |
|
125
|
|
|
* @var string |
|
126
|
|
|
*/ |
|
127
|
|
|
protected const PARSER_ROOT_RULE = 'Grammar'; |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return ParserInterface |
|
131
|
|
|
* @throws \InvalidArgumentException |
|
132
|
|
|
* @throws \Railt\Lexer\Exception\BadLexemeException |
|
133
|
|
|
*/ |
|
134
|
|
|
protected function boot(): ParserInterface |
|
135
|
|
|
{ |
|
136
|
|
|
return new Llk($this->bootLexer(), $this->bootGrammar()); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @return LexerInterface |
|
141
|
|
|
* @throws \InvalidArgumentException |
|
142
|
|
|
* @throws \Railt\Lexer\Exception\BadLexemeException |
|
143
|
|
|
*/ |
|
144
|
|
|
protected function bootLexer(): LexerInterface |
|
145
|
|
|
{ |
|
146
|
|
|
return Factory::create(static::LEXER_TOKENS, static::LEXER_SKIPPED_TOKENS, static::LEXER_FLAGS); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @return GrammarInterface |
|
151
|
|
|
*/ |
|
152
|
|
|
protected function bootGrammar(): GrammarInterface |
|
153
|
|
|
{ |
|
154
|
|
|
return new Grammar([ |
|
155
|
|
|
new Repetition(0, 0, -1, '__definition', null), |
|
156
|
|
|
(new Concatenation('Grammar', [0], 'Grammar'))->setDefaultId('Grammar'), |
|
157
|
|
|
new Concatenation(2, ['RuleDefinition'], null), |
|
158
|
|
|
new Alternation('__definition', ['TokenDefinition', 'PragmaDefinition', 'IncludeDefinition', 2], null), |
|
159
|
|
|
new Terminal(4, 'T_TOKEN', true), |
|
160
|
|
|
new Concatenation(5, [4], 'TokenDefinition'), |
|
161
|
|
|
new Terminal(6, 'T_SKIP', true), |
|
162
|
|
|
new Concatenation(7, [6], 'TokenDefinition'), |
|
163
|
|
|
(new Alternation('TokenDefinition', [5, 7], null))->setDefaultId('TokenDefinition'), |
|
164
|
|
|
new Terminal(9, 'T_PRAGMA', true), |
|
165
|
|
|
(new Concatenation('PragmaDefinition', [9], 'PragmaDefinition'))->setDefaultId('PragmaDefinition'), |
|
166
|
|
|
new Terminal(11, 'T_INCLUDE', true), |
|
167
|
|
|
(new Concatenation('IncludeDefinition', [11], 'IncludeDefinition'))->setDefaultId('IncludeDefinition'), |
|
168
|
|
|
new Repetition(13, 0, 1, 'ShouldKeep', null), |
|
169
|
|
|
new Repetition(14, 0, 1, 'RuleDelegate', null), |
|
170
|
|
|
new Terminal(15, 'T_EQ', false), |
|
171
|
|
|
new Terminal(16, 'T_END_OF_RULE', false), |
|
172
|
|
|
new Repetition(17, 0, 1, 16, null), |
|
173
|
|
|
(new Concatenation('RuleDefinition', [13, 'RuleName', 14, 15, 'RuleProduction', 17], 'RuleDefinition'))->setDefaultId('RuleDefinition'), |
|
174
|
|
|
new Terminal(19, 'T_NAME', true), |
|
175
|
|
|
(new Concatenation('RuleName', [19], 'RuleName'))->setDefaultId('RuleName'), |
|
176
|
|
|
new Terminal(21, 'T_DELEGATE', false), |
|
177
|
|
|
new Terminal(22, 'T_NAME', true), |
|
178
|
|
|
(new Concatenation('RuleDelegate', [21, 22], 'RuleDelegate'))->setDefaultId('RuleDelegate'), |
|
179
|
|
|
new Terminal(24, 'T_KEPT_NAME', false), |
|
180
|
|
|
(new Concatenation('ShouldKeep', [24], 'ShouldKeep'))->setDefaultId('ShouldKeep'), |
|
181
|
|
|
new Concatenation(26, ['__alternation'], null), |
|
182
|
|
|
(new Concatenation('RuleProduction', [26], 'RuleProduction'))->setDefaultId('RuleProduction'), |
|
183
|
|
|
new Concatenation(28, ['Alternation'], null), |
|
184
|
|
|
new Alternation('__alternation', ['__concatenation', 28], null), |
|
185
|
|
|
new Terminal(30, 'T_OR', true), |
|
186
|
|
|
new Concatenation(31, [30, '__concatenation'], 'Alternation'), |
|
187
|
|
|
new Repetition(32, 1, -1, 31, null), |
|
188
|
|
|
(new Concatenation('Alternation', ['__concatenation', 32], null))->setDefaultId('Alternation'), |
|
189
|
|
|
new Concatenation(34, ['Concatenation'], null), |
|
190
|
|
|
new Alternation('__concatenation', ['__repetition', 34], null), |
|
191
|
|
|
new Repetition(36, 1, -1, '__repetition', null), |
|
192
|
|
|
(new Concatenation('Concatenation', ['__repetition', 36], 'Concatenation'))->setDefaultId('Concatenation'), |
|
193
|
|
|
new Alternation(38, ['__simple', 'Repetition'], null), |
|
194
|
|
|
new Repetition(39, 0, 1, 'Rename', null), |
|
195
|
|
|
new Concatenation('__repetition', [38, 39], null), |
|
196
|
|
|
new Concatenation(41, ['Quantifier'], null), |
|
197
|
|
|
(new Concatenation('Repetition', ['__simple', 41], 'Repetition'))->setDefaultId('Repetition'), |
|
198
|
|
|
new Terminal(43, 'T_GROUP_OPEN', true), |
|
199
|
|
|
new Terminal(44, 'T_GROUP_CLOSE', true), |
|
200
|
|
|
new Concatenation(45, [43, '__alternation', 44], null), |
|
201
|
|
|
new Terminal(46, 'T_TOKEN_SKIPPED', true), |
|
202
|
|
|
new Terminal(47, 'T_TOKEN_KEPT', true), |
|
203
|
|
|
new Terminal(48, 'T_TOKEN_STRING', true), |
|
204
|
|
|
new Terminal(49, 'T_INVOKE', true), |
|
205
|
|
|
new Alternation('__simple', [45, 46, 47, 48, 49], null), |
|
206
|
|
|
new Terminal(51, 'T_REPEAT_ZERO_OR_ONE', true), |
|
207
|
|
|
new Concatenation(52, [51], 'Quantifier'), |
|
208
|
|
|
new Terminal(53, 'T_REPEAT_ONE_OR_MORE', true), |
|
209
|
|
|
new Concatenation(54, [53], 'Quantifier'), |
|
210
|
|
|
new Terminal(55, 'T_REPEAT_ZERO_OR_MORE', true), |
|
211
|
|
|
new Concatenation(56, [55], 'Quantifier'), |
|
212
|
|
|
new Terminal(57, 'T_REPEAT_N_TO_M', true), |
|
213
|
|
|
new Concatenation(58, [57], 'Quantifier'), |
|
214
|
|
|
new Terminal(59, 'T_REPEAT_ZERO_OR_MORE', true), |
|
215
|
|
|
new Concatenation(60, [59], 'Quantifier'), |
|
216
|
|
|
new Terminal(61, 'T_REPEAT_ZERO_TO_M', true), |
|
217
|
|
|
new Concatenation(62, [61], 'Quantifier'), |
|
218
|
|
|
new Terminal(63, 'T_REPEAT_N_OR_MORE', true), |
|
219
|
|
|
new Concatenation(64, [63], 'Quantifier'), |
|
220
|
|
|
new Terminal(65, 'T_REPEAT_EXACTLY_N', true), |
|
221
|
|
|
new Concatenation(66, [65], 'Quantifier'), |
|
222
|
|
|
(new Alternation('Quantifier', [52, 54, 56, 58, 60, 62, 64, 66], null))->setDefaultId('Quantifier'), |
|
223
|
|
|
new Terminal(68, 'T_KEPT_NAME', true), |
|
224
|
|
|
new Terminal(69, 'T_NAME', true), |
|
225
|
|
|
(new Concatenation('Rename', [68, 69], 'Rename'))->setDefaultId('Rename'), |
|
226
|
|
|
], static::PARSER_ROOT_RULE, static::PARSER_DELEGATES); |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|