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();
$token = $this->parser->getCurrentToken();
$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;
}
$stream->next();
$stream->next(); // fn
$stream->next(); // (
$names = [];
while (true) {
$token = $this->parser->getCurrentToken();
if (!$token->test(Token::NAME_TYPE)) {
throw new SyntaxError(sprintf('Unexpected token "%s" of value "%s".', Token::typeToEnglish($token->getType()), $token->getValue()), $token->getLine(), $stream->getSourceContext());
}
$names[] = $token->getValue();
$stream->next();
$token = $stream->expect(Token::NAME_TYPE);
$value = $token->getValue();
$names[] = new AssignNameExpression($value, $token->getLine());
if (!$stream->nextIf(Token::PUNCTUATION_TYPE, ',')) {
break;
}
}
$stream->expect(Token::PUNCTUATION_TYPE, '|');
$stream->expect(Token::PUNCTUATION_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()
@@ -12,6 +12,7 @@
namespace Twig\Node\Expression;
use Twig\Compiler;
use Twig\Node\Node;
/**
* Represents an arrow function.
@@ -20,9 +21,9 @@ use Twig\Compiler;
*/
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)
@@ -31,18 +32,28 @@ class ArrowFunctionExpression extends AbstractExpression
->addDebugInfo($this)
->raw('function (')
;
foreach ($this->getAttribute('names') as $i => $name) {
foreach ($this->getNode('names') as $i => $name) {
if ($i) {
$compiler->raw(', ');
}
$compiler->raw('$__'.$name.'__');
$compiler
->raw('$__')
->raw($name->getAttribute('name'))
->raw('__')
;
}
$compiler
->raw(') use ($context) { ')
;
foreach ($this->getAttribute('names') as $name) {
$compiler->raw('$context["'.$name.'"] = $__'.$name.'__; ');
foreach ($this->getNode('names') as $name) {
$compiler
->raw('$context["')
->raw($name->getAttribute('name'))
->raw('"] = $__')
->raw($name->getAttribute('name'))
->raw('__; ')
;
}
$compiler
->raw('return ')
+2 -2
View File
@@ -3,10 +3,10 @@
--TEMPLATE--
{% 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 }}
{% 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 }}
{% endfor %}
--DATA--
+2 -2
View File
@@ -3,10 +3,10 @@
--TEMPLATE--
{% 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 }}
{% 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 }}
{% endfor %}
--DATA--
+1 -1
View File
@@ -3,7 +3,7 @@
--TEMPLATE--
{% 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--
return []
--EXPECT--