changed arrow syntax

This commit is contained in:
Fabien Potencier
2019-05-03 15:52:49 +02:00
parent b30bce1139
commit dc2776359c
5 changed files with 34 additions and 22 deletions
+12 -11
View File
@@ -111,28 +111,29 @@ class ExpressionParser
$stream = $this->parser->getStream(); $stream = $this->parser->getStream();
$token = $this->parser->getCurrentToken(); $token = $this->parser->getCurrentToken();
$line = $token->getLine(); $line = $token->getLine();
if (!$token->test(Token::PUNCTUATION_TYPE, '|')) { if (!$token->test(Token::NAME_TYPE, 'fn')) {
return null;
}
if ($stream->look(2)->test(Token::PUNCTUATION_TYPE, '(')) {
return null; return null;
} }
$stream->next(); $stream->next(); // fn
$stream->next(); // (
$names = []; $names = [];
while (true) { while (true) {
$token = $this->parser->getCurrentToken(); $token = $stream->expect(Token::NAME_TYPE);
if (!$token->test(Token::NAME_TYPE)) { $value = $token->getValue();
throw new SyntaxError(sprintf('Unexpected token "%s" of value "%s".', Token::typeToEnglish($token->getType()), $token->getValue()), $token->getLine(), $stream->getSourceContext()); $names[] = new AssignNameExpression($value, $token->getLine());
}
$names[] = $token->getValue();
$stream->next();
if (!$stream->nextIf(Token::PUNCTUATION_TYPE, ',')) { if (!$stream->nextIf(Token::PUNCTUATION_TYPE, ',')) {
break; break;
} }
} }
$stream->expect(Token::PUNCTUATION_TYPE, ')');
$stream->expect(Token::PUNCTUATION_TYPE, '|');
$stream->expect(Token::ARROW_TYPE); $stream->expect(Token::ARROW_TYPE);
return new ArrowFunctionExpression($this->parseExpression(0), $names, $line); return new ArrowFunctionExpression($this->parseExpression(0), new Node($names), $line);
} }
protected function getPrimary() protected function getPrimary()
@@ -12,6 +12,7 @@
namespace Twig\Node\Expression; namespace Twig\Node\Expression;
use Twig\Compiler; use Twig\Compiler;
use Twig\Node\Node;
/** /**
* Represents an arrow function. * Represents an arrow function.
@@ -20,9 +21,9 @@ use Twig\Compiler;
*/ */
class ArrowFunctionExpression extends AbstractExpression class ArrowFunctionExpression extends AbstractExpression
{ {
public function __construct(AbstractExpression $expr, array $names, $lineno, $tag = null) public function __construct(AbstractExpression $expr, Node $names, $lineno, $tag = null)
{ {
parent::__construct(['expr' => $expr], ['names' => $names], $lineno, $tag); parent::__construct(['expr' => $expr, 'names' => $names], [], $lineno, $tag);
} }
public function compile(Compiler $compiler) public function compile(Compiler $compiler)
@@ -31,18 +32,28 @@ class ArrowFunctionExpression extends AbstractExpression
->addDebugInfo($this) ->addDebugInfo($this)
->raw('function (') ->raw('function (')
; ;
foreach ($this->getAttribute('names') as $i => $name) { foreach ($this->getNode('names') as $i => $name) {
if ($i) { if ($i) {
$compiler->raw(', '); $compiler->raw(', ');
} }
$compiler->raw('$__'.$name.'__'); $compiler
->raw('$__')
->raw($name->getAttribute('name'))
->raw('__')
;
} }
$compiler $compiler
->raw(') use ($context) { ') ->raw(') use ($context) { ')
; ;
foreach ($this->getAttribute('names') as $name) { foreach ($this->getNode('names') as $name) {
$compiler->raw('$context["'.$name.'"] = $__'.$name.'__; '); $compiler
->raw('$context["')
->raw($name->getAttribute('name'))
->raw('"] = $__')
->raw($name->getAttribute('name'))
->raw('__; ')
;
} }
$compiler $compiler
->raw('return ') ->raw('return ')
+2 -2
View File
@@ -3,10 +3,10 @@
--TEMPLATE-- --TEMPLATE--
{% set offset = 3 %} {% set offset = 3 %}
{% for k, v in [1, 5, 3, 4, 5]|filter(|v| => v > offset) -%} {% for k, v in [1, 5, 3, 4, 5]|filter(fn(v) => v > offset) -%}
{{ k }} = {{ v }} {{ k }} = {{ v }}
{% endfor %} {% endfor %}
{% for k, v in {a: 1, b: 2, c: 5, d: 2}|filter(|v| => v > offset) -%} {% for k, v in {a: 1, b: 2, c: 5, d: 2}|filter(fn(v) => v > offset) -%}
{{ k }} = {{ v }} {{ k }} = {{ v }}
{% endfor %} {% endfor %}
--DATA-- --DATA--
+2 -2
View File
@@ -3,10 +3,10 @@
--TEMPLATE-- --TEMPLATE--
{% set offset = 3 %} {% set offset = 3 %}
{% for k, v in [1, 2]|map(|item| => item + 2 ) -%} {% for k, v in [1, 2]|map(fn(item) => item + 2 ) -%}
{{ k }} = {{ v }} {{ k }} = {{ v }}
{% endfor %} {% endfor %}
{% for k, v in {a: 1, b: 2}|map(|item| => item ~ "*" ) -%} {% for k, v in {a: 1, b: 2}|map(fn(item) => item ~ "*" ) -%}
{{ k }} = {{ v }} {{ k }} = {{ v }}
{% endfor %} {% endfor %}
--DATA-- --DATA--
+1 -1
View File
@@ -3,7 +3,7 @@
--TEMPLATE-- --TEMPLATE--
{% set offset = 3 %} {% set offset = 3 %}
{{ [1, -1, 4]|reduce(|carry, item| => carry + item + offset, 10) }} {{ [1, -1, 4]|reduce(fn(carry, item) => carry + item + offset, 10) }}
--DATA-- --DATA--
return [] return []
--EXPECT-- --EXPECT--