1 | <?php |
||
16 | abstract class Parser { |
||
17 | /** |
||
18 | * @var SymbolTable |
||
19 | */ |
||
20 | protected $symbol_table; |
||
21 | |||
22 | /** |
||
23 | * @var Tokenizer|null |
||
24 | */ |
||
25 | protected $tokenizer = null; |
||
26 | |||
27 | /** |
||
28 | * @var array (Symbol, array $matches) |
||
29 | */ |
||
30 | protected $token; |
||
31 | |||
32 | 37 | public function __construct() { |
|
35 | |||
36 | /** |
||
37 | * Parse the string according to this parser. |
||
38 | * |
||
39 | * @return mixed |
||
40 | */ |
||
41 | 37 | public function parse($source) { |
|
52 | |||
53 | /** |
||
54 | * The root for the parse tree. |
||
55 | * |
||
56 | * @return mixed |
||
57 | */ |
||
58 | abstract protected function root(); |
||
59 | |||
60 | // Factory Methods |
||
61 | |||
62 | /** |
||
63 | * Build the Tokenizer. |
||
64 | * |
||
65 | * @return Tokenizer |
||
66 | */ |
||
67 | 37 | public function create_tokenizer($source) { |
|
71 | |||
72 | /** |
||
73 | * Build the SymbolTable |
||
74 | * |
||
75 | * @return SymbolTable |
||
76 | */ |
||
77 | 37 | public function create_symbol_table() { |
|
83 | |||
84 | // Helpers for defining the grammar. |
||
85 | |||
86 | /** |
||
87 | * Add a symbol to the symbol table. |
||
88 | * |
||
89 | * TODO: This most probably should go to symbol table. |
||
90 | * |
||
91 | * @param string $regexp |
||
92 | * @param int $binding_power |
||
93 | * @throws \InvalidArgumentException if %$regexp% is not a regexp |
||
94 | * @throws \LogicException if there already is a symbol with that $regexp. |
||
95 | * @return Symbol |
||
96 | */ |
||
97 | 37 | protected function symbol($regexp, $binding_power = 0) { |
|
100 | |||
101 | /** |
||
102 | * Add an operator to the symbol table. |
||
103 | * |
||
104 | * TODO: This most probably should go to symbol table. |
||
105 | * |
||
106 | * Convenience, will split the given string and wrap each char in [] |
||
107 | * before passing it to symbol. |
||
108 | * |
||
109 | * @param string $op |
||
110 | * @param int $binding_power |
||
111 | * @throws \InvalidArgumentException if %$regexp% is not a regexp |
||
112 | * @throws \LogicException if there already is a symbol with that $regexp. |
||
113 | * @return Symbol |
||
114 | */ |
||
115 | 37 | protected function operator($op, $binding_power = 0) { |
|
119 | |||
120 | /** |
||
121 | * Add a literal to the symbol table, where the matches are |
||
122 | * transformed using the $converter. |
||
123 | * |
||
124 | * TODO: This most probably should go to symbol table. |
||
125 | * |
||
126 | * @param string $regexp |
||
127 | * @param \Closure $converter |
||
128 | * @throws \InvalidArgumentException if %$regexp% is not a regexp |
||
129 | * @throws \LogicException if there already is a symbol with that $regexp. |
||
130 | * @return Symbol |
||
131 | */ |
||
132 | 37 | protected function literal($regexp, $converter) { |
|
136 | |||
137 | // Helpers for actual parsing. |
||
138 | |||
139 | /** |
||
140 | * Set the current token to the next token from the tokenizer. |
||
141 | * |
||
142 | * @return null |
||
143 | */ |
||
144 | 35 | protected function fetch_next_token() { |
|
150 | |||
151 | /** |
||
152 | * Get the current symbol. |
||
153 | * |
||
154 | * @return Symbol |
||
155 | */ |
||
156 | 31 | protected function current_symbol() { |
|
159 | |||
160 | /** |
||
161 | * Get the current match. |
||
162 | * |
||
163 | * @return string[] |
||
164 | */ |
||
165 | 35 | protected function current_match() { |
|
168 | |||
169 | /** |
||
170 | * Advance the tokenizer to the next token if current token |
||
171 | * was matched by the given regexp. |
||
172 | * |
||
173 | * @param string $regexp |
||
174 | * @return null |
||
175 | */ |
||
176 | 14 | protected function advance($regexp) { |
|
186 | |||
187 | /** |
||
188 | * Advance the tokenizer to the next token if current token |
||
189 | * was matched by the given operator. |
||
190 | * |
||
191 | * @param string $op |
||
192 | * @return null |
||
193 | */ |
||
194 | 10 | protected function advance_operator($op) { |
|
197 | |||
198 | /** |
||
199 | * Is the end of the file reached? |
||
200 | * |
||
201 | * @return bool |
||
202 | */ |
||
203 | 28 | public function is_end_of_file_reached() { |
|
206 | |||
207 | /** |
||
208 | * Check if the current token was matched by the given regexp. |
||
209 | * |
||
210 | * @param string $regexp |
||
211 | * @return bool |
||
212 | */ |
||
213 | 35 | protected function is_current_token_matched_by($regexp) { |
|
217 | |||
218 | /** |
||
219 | * Check if the current token is the given operator. |
||
220 | * |
||
221 | * @param string $operator |
||
222 | * @return bool |
||
223 | */ |
||
224 | 7 | protected function is_current_token_operator($operator) { |
|
227 | |||
228 | // Internal Helpers |
||
229 | /** |
||
230 | * "abc" -> "[a][b][c]" |
||
231 | * |
||
232 | * @param string $op |
||
233 | * @return string |
||
234 | */ |
||
235 | 37 | protected function operator_regexp($op) { |
|
243 | } |
||
244 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..