Skip to content

Commit 6ca4d78

Browse files
committed
Split Parser class
1 parent 9ab8ee6 commit 6ca4d78

File tree

3 files changed

+256
-242
lines changed

3 files changed

+256
-242
lines changed

src/JsPhpize/Parser/Parser.php

Lines changed: 1 addition & 242 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@
44

55
use JsPhpize\JsPhpize;
66
use JsPhpize\Lexer\Lexer;
7-
use JsPhpize\Nodes\Assignation;
87
use JsPhpize\Nodes\Block;
98
use JsPhpize\Nodes\BracketsArray;
109
use JsPhpize\Nodes\Constant;
11-
use JsPhpize\Nodes\Dyiade;
12-
use JsPhpize\Nodes\FunctionCall;
1310
use JsPhpize\Nodes\HooksArray;
1411
use JsPhpize\Nodes\Main;
1512
use JsPhpize\Nodes\Node;
@@ -18,7 +15,7 @@
1815
use JsPhpize\Nodes\Value;
1916
use JsPhpize\Nodes\Variable;
2017

21-
class Parser
18+
class Parser extends TokenExtractor
2219
{
2320
/**
2421
* @var JsPhpize
@@ -54,42 +51,6 @@ public function __construct(JsPhpize $engine, $input, $filename)
5451
$this->lexer = new Lexer($engine, $input, $filename);
5552
}
5653

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-
9354
protected function parseLambda(Value $parameters)
9455
{
9556
$lambda = new Block('function');
@@ -175,37 +136,6 @@ protected function parseHooksArray()
175136
throw new Exception('Missing ] to match ' . $exceptionInfos, 6);
176137
}
177138

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-
209139
protected function parseBracketsArray()
210140
{
211141
$array = new BracketsArray();
@@ -236,38 +166,6 @@ protected function parseBracketsArray()
236166
throw new Exception('Missing } to match ' . $exceptionInfos, 7);
237167
}
238168

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-
271169
protected function parseVariable($name)
272170
{
273171
$children = array();
@@ -303,22 +201,6 @@ protected function parseVariable($name)
303201
return $variable;
304202
}
305203

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-
322204
protected function parseTernary(Node $condition)
323205
{
324206
$trueValue = $this->expectValue($this->next());
@@ -370,113 +252,6 @@ protected function parseFunction($token)
370252
return $function;
371253
}
372254

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-
480255
protected function parseKeywordStatement($token)
481256
{
482257
$name = $token->value;
@@ -522,22 +297,6 @@ protected function parseLet($token)
522297
return $letVariable->value;
523298
}
524299

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-
541300
protected function parseInstructions($block)
542301
{
543302
$endToken = $this->getEndTokenFromBlock($block);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace JsPhpize\Parser;
4+
5+
abstract class TokenCrawler
6+
{
7+
protected function retrieveNext()
8+
{
9+
while (($next = $this->lexer->next()) && $next->isNeutral());
10+
11+
return $next;
12+
}
13+
14+
protected function next()
15+
{
16+
return array_shift($this->tokens) ?: $this->retrieveNext();
17+
}
18+
19+
protected function skip()
20+
{
21+
$this->next();
22+
}
23+
24+
protected function get($index)
25+
{
26+
while ($index >= count($this->tokens)) {
27+
$this->tokens[] = $this->retrieveNext();
28+
}
29+
30+
return $this->tokens[$index];
31+
}
32+
33+
protected function exceptionInfos()
34+
{
35+
return $this->lexer->exceptionInfos();
36+
}
37+
38+
protected function unexpected($token)
39+
{
40+
throw new Exception('Unexpected ' . $token->type . rtrim(' ' . ($token->value ?: '')) . $this->exceptionInfos(), 8);
41+
}
42+
}

0 commit comments

Comments
 (0)