|
4 | 4 |
|
5 | 5 | use JsPhpize\JsPhpize; |
6 | 6 | use JsPhpize\Lexer\Lexer; |
7 | | -use JsPhpize\Nodes\Assignation; |
8 | 7 | use JsPhpize\Nodes\Block; |
9 | 8 | use JsPhpize\Nodes\BracketsArray; |
10 | 9 | use JsPhpize\Nodes\Constant; |
11 | | -use JsPhpize\Nodes\Dyiade; |
12 | | -use JsPhpize\Nodes\FunctionCall; |
13 | 10 | use JsPhpize\Nodes\HooksArray; |
14 | 11 | use JsPhpize\Nodes\Main; |
15 | 12 | use JsPhpize\Nodes\Node; |
|
18 | 15 | use JsPhpize\Nodes\Value; |
19 | 16 | use JsPhpize\Nodes\Variable; |
20 | 17 |
|
21 | | -class Parser |
| 18 | +class Parser extends TokenExtractor |
22 | 19 | { |
23 | 20 | /** |
24 | 21 | * @var JsPhpize |
@@ -54,42 +51,6 @@ public function __construct(JsPhpize $engine, $input, $filename) |
54 | 51 | $this->lexer = new Lexer($engine, $input, $filename); |
55 | 52 | } |
56 | 53 |
|
57 | | - protected function retrieveNext() |
58 | | - { |
59 | | - while (($next = $this->lexer->next()) && $next->isNeutral()); |
60 | | - |
61 | | - return $next; |
62 | | - } |
63 | | - |
64 | | - protected function next() |
65 | | - { |
66 | | - return array_shift($this->tokens) ?: $this->retrieveNext(); |
67 | | - } |
68 | | - |
69 | | - protected function skip() |
70 | | - { |
71 | | - $this->next(); |
72 | | - } |
73 | | - |
74 | | - protected function get($index) |
75 | | - { |
76 | | - while ($index >= count($this->tokens)) { |
77 | | - $this->tokens[] = $this->retrieveNext(); |
78 | | - } |
79 | | - |
80 | | - return $this->tokens[$index]; |
81 | | - } |
82 | | - |
83 | | - protected function exceptionInfos() |
84 | | - { |
85 | | - return $this->lexer->exceptionInfos(); |
86 | | - } |
87 | | - |
88 | | - protected function unexpected($token) |
89 | | - { |
90 | | - throw new Exception('Unexpected ' . $token->type . rtrim(' ' . ($token->value ?: '')) . $this->exceptionInfos(), 8); |
91 | | - } |
92 | | - |
93 | 54 | protected function parseLambda(Value $parameters) |
94 | 55 | { |
95 | 56 | $lambda = new Block('function'); |
@@ -175,37 +136,6 @@ protected function parseHooksArray() |
175 | 136 | throw new Exception('Missing ] to match ' . $exceptionInfos, 6); |
176 | 137 | } |
177 | 138 |
|
178 | | - protected function getBracketsArrayItemKeyFromToken($token) |
179 | | - { |
180 | | - $type = null; |
181 | | - |
182 | | - if ($token->is('keyword')) { |
183 | | - $type = 'string'; |
184 | | - $value = var_export($token->value, true); |
185 | | - } elseif ($token->isValue()) { |
186 | | - $type = $token->type; |
187 | | - $value = $token->value; |
188 | | - if ($type === 'variable') { |
189 | | - $type = 'string'; |
190 | | - $value = var_export($value, true); |
191 | | - } |
192 | | - } |
193 | | - |
194 | | - if ($type) { |
195 | | - $token = $this->next(); |
196 | | - if (!$token) { |
197 | | - throw new Exception('Missing value after ' . $value . $this->exceptionInfos(), 12); |
198 | | - } |
199 | | - if (!$token->is(':')) { |
200 | | - $this->unexpected($token); |
201 | | - } |
202 | | - $key = new Constant($type, $value); |
203 | | - $value = $this->expectValue($this->next()); |
204 | | - |
205 | | - return array($key, $value); |
206 | | - } |
207 | | - } |
208 | | - |
209 | 139 | protected function parseBracketsArray() |
210 | 140 | { |
211 | 141 | $array = new BracketsArray(); |
@@ -236,38 +166,6 @@ protected function parseBracketsArray() |
236 | 166 | throw new Exception('Missing } to match ' . $exceptionInfos, 7); |
237 | 167 | } |
238 | 168 |
|
239 | | - protected function getVariableChildFromToken($token) |
240 | | - { |
241 | | - if ($token->is('.')) { |
242 | | - $this->skip(); |
243 | | - $token = $this->next(); |
244 | | - |
245 | | - if ($token->is('variable')) { |
246 | | - return new Constant('string', var_export($token->value, true)); |
247 | | - } |
248 | | - |
249 | | - $this->unexpected($token); |
250 | | - } |
251 | | - |
252 | | - if ($token->is('[')) { |
253 | | - $exceptionInfos = $this->exceptionInfos(); |
254 | | - $this->skip(); |
255 | | - $value = $this->expectValue($this->next()); |
256 | | - |
257 | | - $token = $this->next(); |
258 | | - |
259 | | - if (!$token) { |
260 | | - throw new Exception('Missing ] to match ' . $exceptionInfos, 13); |
261 | | - } |
262 | | - |
263 | | - if ($token->is(']')) { |
264 | | - return $value; |
265 | | - } |
266 | | - |
267 | | - $this->unexpected($token); |
268 | | - } |
269 | | - } |
270 | | - |
271 | 169 | protected function parseVariable($name) |
272 | 170 | { |
273 | 171 | $children = array(); |
@@ -303,22 +201,6 @@ protected function parseVariable($name) |
303 | 201 | return $variable; |
304 | 202 | } |
305 | 203 |
|
306 | | - protected function expectValue($next, $token = null) |
307 | | - { |
308 | | - if (!$next) { |
309 | | - if ($token) { |
310 | | - $this->unexpected($token); |
311 | | - } |
312 | | - throw new Exception('Value expected after ' . $this->exceptionInfos(), 20); |
313 | | - } |
314 | | - $value = $this->getValueFromToken($next); |
315 | | - if (!$value) { |
316 | | - $this->unexpected($next); |
317 | | - } |
318 | | - |
319 | | - return $value; |
320 | | - } |
321 | | - |
322 | 204 | protected function parseTernary(Node $condition) |
323 | 205 | { |
324 | 206 | $trueValue = $this->expectValue($this->next()); |
@@ -370,113 +252,6 @@ protected function parseFunction($token) |
370 | 252 | return $function; |
371 | 253 | } |
372 | 254 |
|
373 | | - protected function getInitialValue($token) |
374 | | - { |
375 | | - if ($token->is('function')) { |
376 | | - return $this->parseFunction($token); |
377 | | - } |
378 | | - if ($token->is('(')) { |
379 | | - return $this->parseParentheses(); |
380 | | - } |
381 | | - if ($token->is('[')) { |
382 | | - return $this->parseHooksArray(); |
383 | | - } |
384 | | - if ($token->is('{')) { |
385 | | - return $this->parseBracketsArray(); |
386 | | - } |
387 | | - if ($token->isOperator() && $token->isIn('~', '!', '--', '++', '-', '+', 'delete', 'typeof', 'void')) { |
388 | | - $value = $this->expectValue($this->next(), $token); |
389 | | - $value->prepend($token->type); |
390 | | - |
391 | | - return $value; |
392 | | - } |
393 | | - if ($token->isValue()) { |
394 | | - return $this->parseValue($token); |
395 | | - } |
396 | | - } |
397 | | - |
398 | | - protected function appendFunctionsCalls(&$value) |
399 | | - { |
400 | | - while ($token = $this->get(0)) { |
401 | | - if ($token->is('{') || $token->expectNoLeftMember()) { |
402 | | - $this->unexpected($this->next()); |
403 | | - } |
404 | | - if ($token->is('?')) { |
405 | | - $this->skip(); |
406 | | - $value = $this->parseTernary($value); |
407 | | - |
408 | | - continue; |
409 | | - } |
410 | | - if ($token->is('(')) { |
411 | | - $this->skip(); |
412 | | - $arguments = array(); |
413 | | - $value = new FunctionCall($value, $this->parseParentheses()->nodes); |
414 | | - |
415 | | - continue; |
416 | | - } |
417 | | - if ($token->isOperator()) { |
418 | | - if ($token->isIn('++', '--')) { |
419 | | - $value->append($this->next()->type); |
420 | | - |
421 | | - break; |
422 | | - } |
423 | | - if ($token->isAssignation()) { |
424 | | - $this->skip(); |
425 | | - $arguments = array(); |
426 | | - $valueToAssign = $this->expectValue($this->next()); |
427 | | - $value = new Assignation($token->type, $value, $valueToAssign); |
428 | | - |
429 | | - continue; |
430 | | - } |
431 | | - |
432 | | - $this->skip(); |
433 | | - $nextValue = $this->expectValue($this->next()); |
434 | | - $value = new Dyiade($token->type, $value, $nextValue); |
435 | | - $token = $this->get(0); |
436 | | - |
437 | | - continue; |
438 | | - } |
439 | | - |
440 | | - break; |
441 | | - } |
442 | | - } |
443 | | - |
444 | | - protected function getValueFromToken($token) |
445 | | - { |
446 | | - $value = $this->getInitialValue($token); |
447 | | - if ($value) { |
448 | | - $this->appendFunctionsCalls($value); |
449 | | - } |
450 | | - |
451 | | - return $value; |
452 | | - } |
453 | | - |
454 | | - protected function expectColon($errorMessage, $errorCode) |
455 | | - { |
456 | | - $colon = $this->next(); |
457 | | - if (!$colon || !$colon->is(':')) { |
458 | | - throw new Exception($errorMessage, $errorCode); |
459 | | - } |
460 | | - } |
461 | | - |
462 | | - protected function handleOptionalValue($keyword, $afterKeyword) |
463 | | - { |
464 | | - if (!$afterKeyword->is(';')) { |
465 | | - $value = $this->expectValue($this->next()); |
466 | | - $keyword->setValue($value); |
467 | | - } |
468 | | - } |
469 | | - |
470 | | - protected function handleParentheses($keyword, $afterKeyword) |
471 | | - { |
472 | | - if ($afterKeyword && $afterKeyword->is('(')) { |
473 | | - $this->skip(); |
474 | | - $keyword->setValue($this->parseParentheses()); |
475 | | - } elseif ($keyword->needParenthesis()) { |
476 | | - throw new Exception("'" . $keyword->type . "' block need parentheses.", 17); |
477 | | - } |
478 | | - } |
479 | | - |
480 | 255 | protected function parseKeywordStatement($token) |
481 | 256 | { |
482 | 257 | $name = $token->value; |
@@ -522,22 +297,6 @@ protected function parseLet($token) |
522 | 297 | return $letVariable->value; |
523 | 298 | } |
524 | 299 |
|
525 | | - protected function getInstructionFromToken($token) |
526 | | - { |
527 | | - if ($token->is('keyword')) { |
528 | | - return $this->parseKeyword($token); |
529 | | - } |
530 | | - |
531 | | - if ($value = $this->getValueFromToken($token)) { |
532 | | - return $value; |
533 | | - } |
534 | | - } |
535 | | - |
536 | | - protected function getEndTokenFromBlock($block) |
537 | | - { |
538 | | - return $block->multipleInstructions ? '}' : ';'; |
539 | | - } |
540 | | - |
541 | 300 | protected function parseInstructions($block) |
542 | 301 | { |
543 | 302 | $endToken = $this->getEndTokenFromBlock($block); |
|
0 commit comments