break;
default:
- $node = new Twig_Node_Expression_Name($token->getValue(), $token->getLine());
+ if ('(' === $this->parser->getCurrentToken()->getValue()) {
+ $node = $this->getFunctionNode($token->getValue(), $token->getLine());
+ } else {
+ $node = new Twig_Node_Expression_Name($token->getValue(), $token->getLine());
+ }
}
break;
public function parsePostfixExpression($node)
{
- $firstPass = true;
while (true) {
$token = $this->parser->getCurrentToken();
if ($token->getType() == Twig_Token::PUNCTUATION_TYPE) {
$node = $this->parseSubscriptExpression($node);
} elseif ('|' == $token->getValue()) {
$node = $this->parseFilterExpression($node);
- } elseif ($firstPass && $node instanceof Twig_Node_Expression_Name && '(' == $token->getValue()) {
- $node = $this->getFunctionNode($node);
} else {
break;
}
} else {
break;
}
-
- $firstPass = false;
}
return $node;
}
- public function getFunctionNode(Twig_Node_Expression_Name $node)
+ public function getFunctionNode($name, $line)
{
$args = $this->parseArguments();
- if ('parent' === $node->getAttribute('name')) {
+ if ('parent' === $name) {
if (!count($this->parser->getBlockStack())) {
- throw new Twig_Error_Syntax('Calling "parent" outside a block is forbidden', $node->getLine());
+ throw new Twig_Error_Syntax('Calling "parent" outside a block is forbidden', $line);
}
if (!$this->parser->getParent()) {
- throw new Twig_Error_Syntax('Calling "parent" on a template that does not extend another one is forbidden', $node->getLine());
+ throw new Twig_Error_Syntax('Calling "parent" on a template that does not extend another one is forbidden', $line);
}
- return new Twig_Node_Expression_Parent($this->parser->peekBlockStack(), $node->getLine());
+ return new Twig_Node_Expression_Parent($this->parser->peekBlockStack(), $line);
}
- if ('block' === $node->getAttribute('name')) {
- return new Twig_Node_Expression_BlockReference($args->getNode(0), false, $node->getLine());
+ if ('block' === $name) {
+ return new Twig_Node_Expression_BlockReference($args->getNode(0), false, $line);
}
- if (null !== $alias = $this->parser->getImportedFunction($node->getAttribute('name'))) {
- return new Twig_Node_Expression_GetAttr($alias['node'], new Twig_Node_Expression_Constant($alias['name'], $node->getLine()), $args, Twig_TemplateInterface::METHOD_CALL, $node->getLine());
+ if (null !== $alias = $this->parser->getImportedFunction($name)) {
+ return new Twig_Node_Expression_GetAttr($alias['node'], new Twig_Node_Expression_Constant($alias['name'], $line), $args, Twig_TemplateInterface::METHOD_CALL, $line);
}
- return new Twig_Node_Expression_Function($node, $args, $node->getLine());
+ return new Twig_Node_Expression_Function($name, $args, $line);
}
public function parseSubscriptExpression($node)
*/
class Twig_Node_Expression_Function extends Twig_Node_Expression
{
- public function __construct(Twig_Node_Expression_Name $name, Twig_NodeInterface $arguments, $lineno)
+ public function __construct($name, Twig_NodeInterface $arguments, $lineno)
{
- parent::__construct(array('name' => $name, 'arguments' => $arguments), array(), $lineno);
+ parent::__construct(array('arguments' => $arguments), array('name' => $name), $lineno);
}
public function compile(Twig_Compiler $compiler)
{
- $function = $compiler->getEnvironment()->getFunction($this->getNode('name')->getAttribute('name'));
+ $function = $compiler->getEnvironment()->getFunction($this->getAttribute('name'));
if (false === $function) {
- throw new Twig_Error_Syntax(sprintf('The function "%s" does not exist', $this->getNode('name')->getAttribute('name')), $this->getLine());
+ throw new Twig_Error_Syntax(sprintf('The function "%s" does not exist', $this->getAttribute('name')), $this->getLine());
}
$compiler
*/
public function testConstructor()
{
- $name = new Twig_Node_Expression_Name('function', 0);
+ $name = 'function';
$args = new Twig_Node();
$node = new Twig_Node_Expression_Function($name, $args, 0);
- $this->assertEquals($name, $node->getNode('name'));
+ $this->assertEquals($name, $node->getAttribute('name'));
$this->assertEquals($args, $node->getNode('arguments'));
}
protected function createFunction($name, array $arguments = array())
{
- $name = new Twig_Node_Expression_Name($name, 0);
$arguments = new Twig_Node($arguments);
return new Twig_Node_Expression_Function($name, $arguments, 0);
}