mirror of
https://github.com/twigphp/Twig.git
synced 2026-08-30 20:16:45 +00:00
changed arrow syntax
This commit is contained in:
+12
-11
@@ -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 ')
|
||||
|
||||
@@ -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--
|
||||
|
||||
@@ -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--
|
||||
|
||||
@@ -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--
|
||||
|
||||
Reference in New Issue
Block a user