) > 1 ? $args->getNode(1) : null, $line); case 'attribute': $args = $this->parseArguments(); if (\count($args) < 2) { throw new SyntaxError('The "attribute" function takes at least two arguments (the variable and the attributes).', $line, $this->parser->getStream()->getSourceContext()); } return new GetAttrExpression($args->getNode(0), $args->getNode(1), \count($args) > 2 ? $args->getNode(2) : null, Template::ANY_CALL, $line); default: if (null !== ($alias = $this->parser->getImportedSymbol('function', $name))) { $arguments = new ArrayExpression([], $line); foreach ($this->parseArguments() as $n) { $arguments->addElement($n); } $node = new MethodCallExpression($alias['node'], $alias['name'], $arguments, $line); $node->setAttribute('safe', \true); return $node; } $args = $this->parseArguments(\true); $class = $this->getFunctionNodeClass($name, $line); return new $class($name, $args, $line); } } public function parseSubscriptExpression($node) { $stream = $this->parser->getStream(); $token = $stream->next(); $lineno = $token->getLine(); $arguments = new ArrayExpression([], $lineno); $type = Template::ANY_CALL; if ('.' == $token->getValue()) { $token = $stream->next(); if (5 == $token->getType() || 6 == $token->getType() || 8 == $token->getType() && \preg_match(Lexer::REGEX_NAME, $token->getValue())) { $arg = new ConstantExpression($token->getValue(), $lineno); if ($stream->test( 9, '(' )) { $type = Template::METHOD_CALL; foreach ($this->parseArguments() as $n) { $arguments->addElement($n); } } } else { throw new SyntaxError(\sprintf('Expected name or number, got value "%s" of type %s.', $token->getValue(), Token::typeToEnglish($token->getType())), $lineno, $stream->getSourceContext()); } if ($node instanceof NameExpression && null !== $this->parser->getImportedSymbol('template', $node->getAttribute('name'))) { if (!$arg instanceof ConstantExpression) { throw new SyntaxError(\sprintf('Dynamic macro names are not supported (called on "%s").', $node->getAttribute('name')), $token->getLine(), $stream->getSourceContext()); } $name = $arg->getAttribute('value'); $node = new MethodCallExpression($node, 'macro_' . $name, $arguments, $lineno); $node->setAttribute('safe', \true); return $node; } } else { $type = Template::ARRAY_CALL; // slice? $slice = \false; if ($stream->test( 9, ':' )) { $slice = \true; $arg = new ConstantExpression(0, $token->getLine()); } else { $arg = $this->parseExpression(); } if ($stream->nextIf( 9, ':' )) { $slice = \true; } if ($slice) { if ($stream->test( 9, ']' )) { $length = new ConstantExpression(null, $token->getLine()); } else { $length = $this->parseExpression(); } $class = $this->getFilterNodeClass('slice', $token->getLine()); $arguments = new Node([$arg, $length]); $filter = new $class($node, new ConstantExpression('slice', $token->getLine()), $arguments, $token->getLine()); $stream->expect( 9, ']' ); return $filter; } $stream->expect( 9, ']' ); } return new GetAttrExpression($node, $arg, $arguments, $type, $lineno); } public function parseFilterExpression($node) { $this->parser->getStream()->next(); return $this->parseFilterExpressionRaw($node); } public function parseFilterExpressionRaw($node, $tag = null) { while (\true) { $token = $this->parser->getStream()->expect( 5 ); $name = new ConstantExpression($token->getValue(), $token->getLine()); if (!$this->parser->getStream()->test( 9, '(' )) { $arguments = new Node(); } else { $arguments = $this->parseArguments(\true, \false, \true); } $class = $this->getFilterNodeClass($name->getAttribute('value'), $token->getLine()); $node = new $class($node, $name, $arguments, $token->getLine(), $tag); if (!$this->parser->getStream()->test( 9, '|' )) { break; } $this->parser->getStream()->next(); } return $node; } public function parseArguments($namedArguments = \false, $definition = \false, $allowArrow = \false) { $args = []; $stream = $this->parser->getStream(); $stream->expect( 9, '(', 'A list of arguments must begin with an opening parenthesis' ); while (!$stream->test( 9, ')' )) { if (!empty($args)) { $stream->expect( 9, ',', 'Arguments must be separated by a comma' ); // if the comma above was a trailing comma, early exit the argument parse loop if ($stream->test( 9, ')' )) { break; } } if ($definition) { $token = $stream->expect( 5, null, 'An argument must be a name' ); $value = new NameExpression($token->getValue(), $this->parser->getCurrentToken()->getLine()); } else { $value = $this->parseExpression(0, $allowArrow); } $name = null; if ($namedArguments && ($token = $stream->nextIf( 8, '=' ))) { if (!$value instanceof NameExpression) { throw new SyntaxError(\sprintf('A parameter name must be a string, "%s" given.', \get_class($value)), $token->getLine(), $stream->getSourceContext()); } $name = $value->getAttribute('name'); if ($definition) { $value = $this->parsePrimaryExpression(); if (!$this->checkConstantExpression($value)) { throw new SyntaxError('A default value for an argument must be a constant (a boolean, a string, a number, or an array).', $token->getLine(), $stream->getSourceContext()); } } else { $value = $this->parseExpression(0, $allowArrow); } } if ($definition) { if (null === $name) { $name = $value->getAttribute('name'); $value = new ConstantExpression(null, $this->parser->getCurrentToken()->getLine()); } $args[$name] = $value; } else { if (null === $name) { $args[] = $value; } else { $args[$name] = $value; } } } $stream->expect( 9, ')', 'A list of arguments must be closed by a parenthesis' ); return new Node($args); } public function parseAssignmentExpression() { $stream = $this->parser->getStream(); $targets = []; while (\true) { $token = $this->parser->getCurrentToken(); if ($stream->test( 8 ) && \preg_match(Lexer::REGEX_NAME, $token->getValue())) { // in this context, string operators are variable names $this->parser->getStream()->next(); } else { $stream->expect( 5, null, 'Only variables can be assigned to' ); } $value = $token->getValue(); if (\in_array(\strtr($value, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), ['true', 'false', 'none', 'null'])) { throw new SyntaxError(\sprintf('You cannot assign a value to "%s".', $value), $token->getLine(), $stream->getSourceContext()); } $targets[] = new AssignNameExpression($value, $token->getLine()); if (!$stream->nextIf( 9, ',' )) { break; } } return new Node($targets); } public function parseMultitargetExpression() { $targets = []; while (\true) { $targets[] = $this->parseExpression(); if (!$this->parser->getStream()->nextIf( 9, ',' )) { break; } } return new Node($targets); } private function parseNotTestExpression(Node $node) : NotUnary { return new NotUnary($this->parseTestExpression($node), $this->parser->getCurrentToken()->getLine()); } private function parseTestExpression(Node $node) : TestExpression { $stream = $this->parser->getStream(); list($name, $test) = $this->getTest($node->getTemplateLine()); $class = $this->getTestNodeClass($test); $arguments = null; if ($stream->test( 9, '(' )) { $arguments = $this->parseArguments(\true); } elseif ($test->hasOneMandatoryArgument()) { $arguments = new Node([0 => $this->parsePrimaryExpression()]); } if ('defined' === $name && $node instanceof NameExpression && null !== ($alias = $this->parser->getImportedSymbol('function', $node->getAttribute('name')))) { $node = new MethodCallExpression($alias['node'], $alias['name'], new ArrayExpression([], $node->getTemplateLine()), $node->getTemplateLine()); $node->setAttribute('safe', \true); } return new $class($node, $name, $arguments, $this->parser->getCurrentToken()->getLine()); } private function getTest(int $line) : array { $stream = $this->parser->getStream(); $name = $stream->expect( 5 )->getValue(); if ($test = $this->env->getTest($name)) { return [$name, $test]; } if ($stream->test( 5 )) { // try 2-words tests $name = $name . ' ' . $this->parser->getCurrentToken()->getValue(); if ($test = $this->env->getTest($name)) { $stream->next(); return [$name, $test]; } } $e = new SyntaxError(\sprintf('Unknown "%s" test.', $name), $line, $stream->getSourceContext()); $e->addSuggestions($name, \array_keys($this->env->getTests())); throw $e; } private function getTestNodeClass(TwigTest $test) : string { if ($test->isDeprecated()) { $stream = $this->parser->getStream(); $message = \sprintf('Twig Test "%s" is deprecated', $test->getName()); if ($test->getDeprecatedVersion()) { $message .= \sprintf(' since version %s', $test->getDeprecatedVersion()); } if ($test->getAlternative()) { $message .= \sprintf('. Use "%s" instead', $test->getAlternative()); } $src = $stream->getSourceContext(); $message .= \sprintf(' in %s at line %d.', $src->getPath() ?: $src->getName(), $stream->getCurrent()->getLine()); @\trigger_error($message, \E_USER_DEPRECATED); } return $test->getNodeClass(); } private function getFunctionNodeClass(string $name, int $line) : string { if (!($function = $this->env->getFunction($name))) { $e = new SyntaxError(\sprintf('Unknown "%s" function.', $name), $line, $this->parser->getStream()->getSourceContext()); $e->addSuggestions($name, \array_keys($this->env->getFunctions())); throw $e; } if ($function->isDeprecated()) { $message = \sprintf('Twig Function "%s" is deprecated', $function->getName()); if ($function->getDeprecatedVersion()) { $message .= \sprintf(' since version %s', $function->getDeprecatedVersion()); } if ($function->getAlternative()) { $message .= \sprintf('. Use "%s" instead', $function->getAlternative()); } $src = $this->parser->getStream()->getSourceContext(); $message .= \sprintf(' in %s at line %d.', $src->getPath() ?: $src->getName(), $line); @\trigger_error($message, \E_USER_DEPRECATED); } return $function->getNodeClass(); } private function getFilterNodeClass(string $name, int $line) : string { if (!($filter = $this->env->getFilter($name))) { $e = new SyntaxError(\sprintf('Unknown "%s" filter.', $name), $line, $this->parser->getStream()->getSourceContext()); $e->addSuggestions($name, \array_keys($this->env->getFilters())); throw $e; } if ($filter->isDeprecated()) { $message = \sprintf('Twig Filter "%s" is deprecated', $filter->getName()); if ($filter->getDeprecatedVersion()) { $message .= \sprintf(' since version %s', $filter->getDeprecatedVersion()); } if ($filter->getAlternative()) { $message .= \sprintf('. Use "%s" instead', $filter->getAlternative()); } $src = $this->parser->getStream()->getSourceContext(); $message .= \sprintf(' in %s at line %d.', $src->getPath() ?: $src->getName(), $line); @\trigger_error($message, \E_USER_DEPRECATED); } return $filter->getNodeClass(); } // checks that the node only contains "constant" elements private function checkConstantExpression(Node $node) : bool { if (!($node instanceof ConstantExpression || $node instanceof ArrayExpression || $node instanceof NegUnary || $node instanceof PosUnary)) { return \false; } foreach ($node as $n) { if (!$this->checkConstantExpression($n)) { return \false; } } return \true; } }