|
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\PP2; |
|
11
|
|
|
|
|
12
|
|
|
use Railt\Io\Readable; |
|
13
|
|
|
use Railt\Parser\Ast\RuleInterface; |
|
14
|
|
|
use Railt\Parser\Configuration; |
|
15
|
|
|
use Railt\Parser\Parser as LLParser; |
|
16
|
|
|
use Railt\Parser\ParserInterface; |
|
17
|
|
|
use Railt\Parser\Rule\Alternation; |
|
18
|
|
|
use Railt\Parser\Rule\Concatenation; |
|
19
|
|
|
use Railt\Parser\Rule\Repetition; |
|
20
|
|
|
use Railt\Parser\Rule\Token; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class Parser |
|
24
|
|
|
*/ |
|
25
|
|
|
class Parser implements ParserInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var ParserInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
private $llk; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Parser constructor. |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct() |
|
36
|
|
|
{ |
|
37
|
|
|
$this->llk = new LLParser(new Lexer(), $this->rules(), $this->options()); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @return array |
|
42
|
|
|
*/ |
|
43
|
|
|
private function rules(): array |
|
44
|
|
|
{ |
|
45
|
|
|
return [ |
|
46
|
|
|
// 0 |
|
47
|
|
|
new Repetition(1, 0, -1, [6], 'Grammar'), |
|
48
|
|
|
new Token(2, 'T_PRAGMA', true), |
|
49
|
|
|
new Token(3, 'T_TOKEN', true), |
|
50
|
|
|
new Token(4, 'T_SKIP', true), |
|
51
|
|
|
new Token(5, 'T_INCLUDE', true), |
|
52
|
|
|
new Alternation(6, [2, 3, 4, 5, 7,], null), |
|
53
|
|
|
new Concatenation(7, [12, 19,], 'Rule'), |
|
54
|
|
|
new Repetition(8, 0, 1, [17,], null), |
|
55
|
|
|
new Token(9, 'T_NAME', true), |
|
56
|
|
|
new Repetition(10, 0, 1, [15,], null), |
|
57
|
|
|
new Token(11, 'T_COLON', false), |
|
58
|
|
|
new Concatenation(12, [8, 9, 10, 11,], 'Name'), |
|
59
|
|
|
new Token(13, 'T_DELEGATE', false), |
|
60
|
|
|
new Token(14, 'T_NAME', true), |
|
61
|
|
|
new Concatenation(15, [13, 14,], 'Delegate'), |
|
62
|
|
|
new Token(16, 'T_KEPT_NAME', false), |
|
63
|
|
|
new Concatenation(17, [16,], 'ShouldKeep'), |
|
64
|
|
|
new Repetition(18, 1, -1, [20,], null), |
|
65
|
|
|
new Concatenation(19, [18,], 'Production'), |
|
66
|
|
|
new Alternation(20, [33, 66, 36, 24, 23,], null), |
|
67
|
|
|
new Token(21, 'T_KEPT_NAME', false), |
|
68
|
|
|
new Token(22, 'T_NAME', true), |
|
69
|
|
|
new Concatenation(23, [21, 22,], 'Rename'), |
|
70
|
|
|
new Alternation(24, [32, 28,], null), |
|
71
|
|
|
new Token(25, 'T_GROUP_OPEN', false), |
|
72
|
|
|
new Repetition(26, 1, -1, [20,], null), |
|
73
|
|
|
new Token(27, 'T_GROUP_CLOSE', false), |
|
74
|
|
|
new Concatenation(28, [25, 26, 27,], null), |
|
75
|
|
|
new Token(29, 'T_KEPT', true), |
|
76
|
|
|
new Token(30, 'T_SKIPPED', true), |
|
77
|
|
|
new Token(31, 'T_INVOKE', true), |
|
78
|
|
|
new Alternation(32, [29, 30, 31,], null), |
|
79
|
|
|
new Repetition(33, 2, -1, [24,], 'Concatenation'), |
|
80
|
|
|
// 34 |
|
81
|
|
|
new Repetition(35, 1, -1, [24,], null), |
|
82
|
|
|
new Concatenation(36, [35, 44,], 'Repetition'), |
|
83
|
|
|
new Token(37, 'T_ZERO_OR_ONE', true), |
|
84
|
|
|
new Concatenation(38, [37,], 'RepetitionInterval'), |
|
85
|
|
|
new Token(39, 'T_ONE_OR_MORE', true), |
|
86
|
|
|
new Concatenation(40, [39,], 'RepetitionInterval'), |
|
87
|
|
|
new Token(41, 'T_ZERO_OR_MORE', true), |
|
88
|
|
|
new Concatenation(42, [41,], 'RepetitionInterval'), |
|
89
|
|
|
new Concatenation(43, [48,], 'RepetitionInterval'), |
|
90
|
|
|
new Alternation(44, [38, 40, 42, 43,], null), |
|
91
|
|
|
new Token(45, 'T_REPETITION_OPEN', false), |
|
92
|
|
|
new Alternation(46, [52, 54,], null), |
|
93
|
|
|
new Token(47, 'T_REPETITION_CLOSE', false), |
|
94
|
|
|
new Concatenation(48, [45, 46, 47,], null), |
|
95
|
|
|
new Repetition(49, 0, 1, [56,], null), |
|
96
|
|
|
new Token(50, 'T_COMMA', false), |
|
97
|
|
|
new Repetition(51, 0, 1, [58,], null), |
|
98
|
|
|
new Concatenation(52, [49, 50, 51,], null), |
|
99
|
|
|
new Token(53, 'T_NUMBER', true), |
|
100
|
|
|
new Concatenation(54, [53,], 'Repeat'), |
|
101
|
|
|
new Token(55, 'T_NUMBER', true), |
|
102
|
|
|
new Concatenation(56, [55,], 'From'), |
|
103
|
|
|
new Token(57, 'T_NUMBER', true), |
|
104
|
|
|
new Concatenation(58, [57,], 'To'), |
|
105
|
|
|
new Concatenation(59, [33,], 'Alternation'), |
|
106
|
|
|
new Concatenation(60, [24,], 'Alternation'), |
|
107
|
|
|
new Alternation(61, [59, 60,], null), |
|
108
|
|
|
new Token(62, 'T_OR', false), |
|
109
|
|
|
new Alternation(63, [33, 24,], null), |
|
110
|
|
|
new Concatenation(64, [62, 63,], null), |
|
111
|
|
|
new Repetition(65, 1, -1, [64,], null), |
|
112
|
|
|
new Concatenation(66, [61, 65,], null), |
|
113
|
|
|
]; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return array |
|
118
|
|
|
*/ |
|
119
|
|
|
private function options(): array |
|
120
|
|
|
{ |
|
121
|
|
|
return [ |
|
122
|
|
|
Configuration::PRAGMA_ROOT => 'Grammar', |
|
123
|
|
|
Configuration::PRAGMA_LOOKAHEAD => 1024, |
|
124
|
|
|
]; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param Readable $input |
|
129
|
|
|
* @return RuleInterface |
|
130
|
|
|
* @throws \LogicException |
|
131
|
|
|
* @throws \Railt\Io\Exception\ExternalFileException |
|
132
|
|
|
* @throws \Railt\Parser\Exception\UnrecognizedRuleException |
|
133
|
|
|
*/ |
|
134
|
|
|
public function parse(Readable $input): RuleInterface |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->llk->parse($input); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: