Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions examples/ternary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
a = {
b: function () {
return 42;
}
};
foo = 9;
bar = "9";
result = foo == bar ? a.b() : null;

return result
1 change: 1 addition & 0 deletions examples/ternary.return
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
42
12 changes: 8 additions & 4 deletions src/JsPhpize/Compiler/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use JsPhpize\Nodes\Instruction;
use JsPhpize\Nodes\Node;
use JsPhpize\Nodes\Parenthesis;
use JsPhpize\Nodes\Ternary;
use JsPhpize\Nodes\Value;
use JsPhpize\Nodes\Variable;

Expand Down Expand Up @@ -218,10 +219,6 @@ public function visitNode(Node $node, $indent)
if ($node instanceof Value) {
$php = $node->getBefore() . $php . $node->getAfter();
}
if (!method_exists($this, $method)) {
var_dump($method);
exit;
}

return $indent . $php;
}
Expand All @@ -231,6 +228,13 @@ protected function visitParenthesis(Parenthesis $parenthesis, $indent)
return '(' . $this->visitNodesArray($parenthesis->nodes, $indent, $parenthesis->separator) . ')';
}

protected function visitTernary(Ternary $ternary, $indent)
{
return $this->visitNode($ternary->condition, $indent) .
' ? ' . $this->visitNode($ternary->trueValue, $indent) .
' : ' . $this->visitNode($ternary->falseValue, $indent);
}

protected function visitVariable(Variable $variable, $indent)
{
$name = $variable->name;
Expand Down
4 changes: 1 addition & 3 deletions src/JsPhpize/Nodes/Assignation.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ public function __construct($operator, Assignable $leftHand, Node $rightHand)
throw new Exception($leftHand->getNonAssignableReason(), 9);
}

if (!($rightHand instanceof Value) && (!($rightHand instanceof Block) || $rightHand->type !== 'function')) {
throw new Exception('Only Value instance or Function block could be assigned.', 19);
}
$rightHand->mustBeAssignable();

$this->operator = $operator;
$this->leftHand = $leftHand;
Expand Down
4 changes: 3 additions & 1 deletion src/JsPhpize/Nodes/BracketsArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

class BracketsArray extends ArrayBase
{
public function addItem(Constant $key, Value $value)
public function addItem(Constant $key, Node $value)
{
$value->mustBeAssignable();

$this->data[] = array($key, $value);
}
}
9 changes: 9 additions & 0 deletions src/JsPhpize/Nodes/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@

namespace JsPhpize\Nodes;

use JsPhpize\Parse\Exception;

abstract class Node
{
public function __get($name)
{
return $this->$name;
}

public function mustBeAssignable()
{
if (!($this instanceof Value) && (!($this instanceof Block) || $this->type !== 'function')) {
throw new Exception('Only Value instance or Function block could be assigned.', 19);
}
}
}
2 changes: 1 addition & 1 deletion src/JsPhpize/Nodes/Ternary.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Ternary extends Value
public function __construct(Value $condition, Value $trueValue, Value $falseValue)
{
$this->condition = $condition;
$this->rueValue = $trueValue;
$this->trueValue = $trueValue;
$this->falseValue = $falseValue;
}
}
64 changes: 50 additions & 14 deletions src/JsPhpize/Parser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
use JsPhpize\Nodes\FunctionCall;
use JsPhpize\Nodes\HooksArray;
use JsPhpize\Nodes\Main;
use JsPhpize\Nodes\Node;
use JsPhpize\Nodes\Parenthesis;
use JsPhpize\Nodes\Ternary;
use JsPhpize\Nodes\Value;
use JsPhpize\Nodes\Variable;

Expand Down Expand Up @@ -296,6 +298,28 @@ protected function expectValue($next, $exception = null)
}
throw new Exception('Value expected before ' . $this->exceptionInfos(), 13);
}
if ($next->is('function')) {
$function = new Block('function');
$next = $this->get(0);
if ($next->is('variable')) {
$this->skip();
$next = $this->get(0);
}
if (!$next->is('(')) {
$this->unexpected($next);
}
$this->skip();
$function->setValue($this->parseParentheses());
$next = $this->get(0);
if (!$next->is('{')) {
$this->unexpected($next);
}
$this->skip();
$this->parseBlock($function);
$this->skip();

return $function;
}
$value = $this->getValueFromToken($next);
if (!$value) {
$this->unexpected($next);
Expand All @@ -304,6 +328,26 @@ protected function expectValue($next, $exception = null)
return $value;
}

protected function parseTernary(Node $condition)
{
$trueValue = $this->expectValue($this->next());
$next = $this->next();
if (!$next) {
throw new Exception("Ternary expression not properly closed after '?' " . $this->exceptionInfos(), 14);
}
if (!$next->is(':')) {
throw new Exception("':' expected but $next given " . $this->exceptionInfos(), 15);
}
$next = $this->next();
if (!$next) {
throw new Exception("Ternary expression not properly closed after ':' " . $this->exceptionInfos(), 16);
}
$falseValue = $this->expectValue($next);
$next = $this->get(0);

return new Ternary($condition, $trueValue, $falseValue);
}

protected function parseValue($token)
{
$debug = ($token->value === 'array_slice');
Expand Down Expand Up @@ -341,27 +385,19 @@ protected function parseValue($token)
}
if ($token->is('?')) {
$this->skip();
$trueValue = $this->expectValue($this->next());
$next = $this->next();
if (!$next) {
throw new Exception("Ternary expression not properly closed after '?' " . $this->exceptionInfos(), 14);
}
if (!$next->is(':')) {
throw new Exception("':' expected but $next given " . $this->exceptionInfos(), 15);
}
$next = $this->next();
if (!$next) {
throw new Exception("Ternary expression not properly closed after ':' " . $this->exceptionInfos(), 16);
}
$falseValue = $this->expectValue($this->next());
$value = new Ternary($value, $trueValue, $falseValue);
$value = $this->parseTernary($value);

continue;
}

$this->skip();
$nextValue = $this->expectValue($this->next());
$value = new Dyiade($token->type, $value, $nextValue);
$token = $this->get(0);
if ($token && $token->is('?')) {
$this->skip();
$value = $this->parseTernary($value);
}

continue;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public function caseProvider()

$examples = __DIR__ . '/../examples';
foreach (scandir($examples) as $file) {
if (substr($file, -7) === '.return' && strpos($file, 'lambda') !== false) {
if (substr($file, -7) === '.return') {
$cases[] = array($file, substr($file, 0, -7) . '.js');
}
}
Expand Down