diff --git a/examples/new.js b/examples/new.js new file mode 100644 index 0000000..53afcd3 --- /dev/null +++ b/examples/new.js @@ -0,0 +1 @@ +new Date(); diff --git a/examples/new.php b/examples/new.php new file mode 100644 index 0000000..53afcd3 --- /dev/null +++ b/examples/new.php @@ -0,0 +1 @@ +new Date(); diff --git a/src/JsPhpize/Compiler/Compiler.php b/src/JsPhpize/Compiler/Compiler.php index 9b5bcdd..f08d5d6 100644 --- a/src/JsPhpize/Compiler/Compiler.php +++ b/src/JsPhpize/Compiler/Compiler.php @@ -189,6 +189,7 @@ protected function visitFunctionCall(FunctionCall $functionCall, $indent) { $function = $functionCall->function; $arguments = $functionCall->arguments; + $applicant = $functionCall->applicant; $arguments = $this->visitNodesArray($arguments, $indent, ', '); $dynamicCall = 'call_user_func(' . $this->visitNode($function, $indent) . @@ -199,6 +200,10 @@ protected function visitFunctionCall(FunctionCall $functionCall, $indent) $name = $function->name; $staticCall = $name . '(' . $arguments . ')'; + if ($applicant === 'new') { + return $staticCall; + } + if (in_array($name, array( 'array', 'echo', diff --git a/src/JsPhpize/Nodes/FunctionCall.php b/src/JsPhpize/Nodes/FunctionCall.php index a2b21e0..65307d7 100644 --- a/src/JsPhpize/Nodes/FunctionCall.php +++ b/src/JsPhpize/Nodes/FunctionCall.php @@ -14,9 +14,15 @@ class FunctionCall extends Value */ protected $arguments; - public function __construct(Value $function, array $arguments) + /** + * @var null|string + */ + protected $applicant; + + public function __construct(Value $function, array $arguments, $applicant = null) { $this->function = $function; $this->arguments = $arguments; + $this->applicant = $applicant; } } diff --git a/src/JsPhpize/Parser/Parser.php b/src/JsPhpize/Parser/Parser.php index f896289..d9849f7 100644 --- a/src/JsPhpize/Parser/Parser.php +++ b/src/JsPhpize/Parser/Parser.php @@ -247,10 +247,12 @@ protected function parseKeywordStatement($token) $name = $token->value; $keyword = new Block($name); switch ($name) { + case 'new': + case 'clone': case 'return': case 'continue': case 'break': - $this->handleOptionalValue($keyword, $this->get(0)); + $this->handleOptionalValue($keyword, $this->get(0), $name); break; case 'case': $value = $this->expectValue($this->next()); diff --git a/src/JsPhpize/Parser/TokenExtractor.php b/src/JsPhpize/Parser/TokenExtractor.php index f3d71e6..31395b1 100644 --- a/src/JsPhpize/Parser/TokenExtractor.php +++ b/src/JsPhpize/Parser/TokenExtractor.php @@ -78,20 +78,20 @@ protected function getInstructionFromToken($token) } } - protected function getValueFromToken($token) + protected function getValueFromToken($token, $previousToken = null, $applicant = null) { $value = $this->getInitialValue($token); if ($value) { - $this->appendFunctionsCalls($value); + $this->appendFunctionsCalls($value, $previousToken, $applicant); } return $value; } - protected function handleOptionalValue($keyword, $afterKeyword) + protected function handleOptionalValue($keyword, $afterKeyword, $applicant) { if (!$afterKeyword->is(';')) { - $value = $this->expectValue($this->next()); + $value = $this->expectValue($this->next(), $keyword, $applicant); $keyword->setValue($value); } } @@ -131,7 +131,7 @@ protected function getInitialValue($token) } } - protected function appendFunctionsCalls(&$value) + protected function appendFunctionsCalls(&$value, $previousToken = null, $applicant = null) { while ($token = $this->get(0)) { if ($token->is('{') || $token->expectNoLeftMember()) { @@ -145,8 +145,7 @@ protected function appendFunctionsCalls(&$value) } if ($token->is('(')) { $this->skip(); - $arguments = array(); - $value = new FunctionCall($value, $this->parseParentheses()->nodes); + $value = new FunctionCall($value, $this->parseParentheses()->nodes, $applicant); continue; } @@ -158,17 +157,16 @@ protected function appendFunctionsCalls(&$value) } if ($token->isAssignation()) { $this->skip(); - $arguments = array(); - $valueToAssign = $this->expectValue($this->next()); + $valueToAssign = $this->expectValue($this->next(), $previousToken); $value = new Assignation($token->type, $value, $valueToAssign); continue; } $this->skip(); - $nextValue = $this->expectValue($this->next()); + $nextValue = $this->expectValue($this->next(), $previousToken); $value = new Dyiade($token->type, $value, $nextValue); - $token = $this->get(0); + $this->get(0); continue; } @@ -177,7 +175,7 @@ protected function appendFunctionsCalls(&$value) } } - protected function expectValue($next, $token = null) + protected function expectValue($next, $token = null, $applicant = null) { if (!$next) { if ($token) { @@ -185,7 +183,7 @@ protected function expectValue($next, $token = null) } throw new Exception('Value expected after ' . $this->exceptionInfos(), 20); } - $value = $this->getValueFromToken($next); + $value = $this->getValueFromToken($next, $token, $applicant); if (!$value) { throw $this->unexpected($next); }