You are here

public function Twig_ExpressionParser::parseArguments in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/twig/twig/lib/Twig/ExpressionParser.php \Twig_ExpressionParser::parseArguments()

Parses arguments.

Parameters

bool $namedArguments Whether to allow named arguments or not:

bool $definition Whether we are parsing arguments for a function definition:

Return value

Twig_Node

Throws

Twig_Error_Syntax

3 calls to Twig_ExpressionParser::parseArguments()
Twig_ExpressionParser::getFunctionNode in vendor/twig/twig/lib/Twig/ExpressionParser.php
Twig_ExpressionParser::parseFilterExpressionRaw in vendor/twig/twig/lib/Twig/ExpressionParser.php
Twig_ExpressionParser::parseSubscriptExpression in vendor/twig/twig/lib/Twig/ExpressionParser.php

File

vendor/twig/twig/lib/Twig/ExpressionParser.php, line 482

Class

Twig_ExpressionParser
Parses expressions.

Code

public function parseArguments($namedArguments = false, $definition = false) {
  $args = array();
  $stream = $this->parser
    ->getStream();
  $stream
    ->expect(Twig_Token::PUNCTUATION_TYPE, '(', 'A list of arguments must begin with an opening parenthesis');
  while (!$stream
    ->test(Twig_Token::PUNCTUATION_TYPE, ')')) {
    if (!empty($args)) {
      $stream
        ->expect(Twig_Token::PUNCTUATION_TYPE, ',', 'Arguments must be separated by a comma');
    }
    if ($definition) {
      $token = $stream
        ->expect(Twig_Token::NAME_TYPE, null, 'An argument must be a name');
      $value = new Twig_Node_Expression_Name($token
        ->getValue(), $this->parser
        ->getCurrentToken()
        ->getLine());
    }
    else {
      $value = $this
        ->parseExpression();
    }
    $name = null;
    if ($namedArguments && ($token = $stream
      ->nextIf(Twig_Token::OPERATOR_TYPE, '='))) {
      if (!$value instanceof Twig_Node_Expression_Name) {
        throw new Twig_Error_Syntax(sprintf('A parameter name must be a string, "%s" given.', get_class($value)), $token
          ->getLine(), $this->parser
          ->getFilename());
      }
      $name = $value
        ->getAttribute('name');
      if ($definition) {
        $value = $this
          ->parsePrimaryExpression();
        if (!$this
          ->checkConstantExpression($value)) {
          throw new Twig_Error_Syntax(sprintf('A default value for an argument must be a constant (a boolean, a string, a number, or an array).'), $token
            ->getLine(), $this->parser
            ->getFilename());
        }
      }
      else {
        $value = $this
          ->parseExpression();
      }
    }
    if ($definition) {
      if (null === $name) {
        $name = $value
          ->getAttribute('name');
        $value = new Twig_Node_Expression_Constant(null, $this->parser
          ->getCurrentToken()
          ->getLine());
      }
      $args[$name] = $value;
    }
    else {
      if (null === $name) {
        $args[] = $value;
      }
      else {
        $args[$name] = $value;
      }
    }
  }
  $stream
    ->expect(Twig_Token::PUNCTUATION_TYPE, ')', 'A list of arguments must be closed by a parenthesis');
  return new Twig_Node($args);
}