You are here

public function Twig_ExpressionParser::parseSubscriptExpression in Zircon Profile 8

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

File

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

Class

Twig_ExpressionParser
Parses expressions.

Code

public function parseSubscriptExpression($node) {
  $stream = $this->parser
    ->getStream();
  $token = $stream
    ->next();
  $lineno = $token
    ->getLine();
  $arguments = new Twig_Node_Expression_Array(array(), $lineno);
  $type = Twig_Template::ANY_CALL;
  if ($token
    ->getValue() == '.') {
    $token = $stream
      ->next();
    if ($token
      ->getType() == Twig_Token::NAME_TYPE || $token
      ->getType() == Twig_Token::NUMBER_TYPE || $token
      ->getType() == Twig_Token::OPERATOR_TYPE && preg_match(Twig_Lexer::REGEX_NAME, $token
      ->getValue())) {
      $arg = new Twig_Node_Expression_Constant($token
        ->getValue(), $lineno);
      if ($stream
        ->test(Twig_Token::PUNCTUATION_TYPE, '(')) {
        $type = Twig_TemplateInterface::METHOD_CALL;
        foreach ($this
          ->parseArguments() as $n) {
          $arguments
            ->addElement($n);
        }
      }
    }
    else {
      throw new Twig_Error_Syntax('Expected name or number', $lineno, $this->parser
        ->getFilename());
    }
    if ($node instanceof Twig_Node_Expression_Name && null !== $this->parser
      ->getImportedSymbol('template', $node
      ->getAttribute('name'))) {
      if (!$arg instanceof Twig_Node_Expression_Constant) {
        throw new Twig_Error_Syntax(sprintf('Dynamic macro names are not supported (called on "%s").', $node
          ->getAttribute('name')), $token
          ->getLine(), $this->parser
          ->getFilename());
      }
      $name = $arg
        ->getAttribute('value');
      if ($this->parser
        ->isReservedMacroName($name)) {
        throw new Twig_Error_Syntax(sprintf('"%s" cannot be called as macro as it is a reserved keyword.', $name), $token
          ->getLine(), $this->parser
          ->getFilename());
      }
      $node = new Twig_Node_Expression_MethodCall($node, 'get' . $name, $arguments, $lineno);
      $node
        ->setAttribute('safe', true);
      return $node;
    }
  }
  else {
    $type = Twig_Template::ARRAY_CALL;

    // slice?
    $slice = false;
    if ($stream
      ->test(Twig_Token::PUNCTUATION_TYPE, ':')) {
      $slice = true;
      $arg = new Twig_Node_Expression_Constant(0, $token
        ->getLine());
    }
    else {
      $arg = $this
        ->parseExpression();
    }
    if ($stream
      ->nextIf(Twig_Token::PUNCTUATION_TYPE, ':')) {
      $slice = true;
    }
    if ($slice) {
      if ($stream
        ->test(Twig_Token::PUNCTUATION_TYPE, ']')) {
        $length = new Twig_Node_Expression_Constant(null, $token
          ->getLine());
      }
      else {
        $length = $this
          ->parseExpression();
      }
      $class = $this
        ->getFilterNodeClass('slice', $token
        ->getLine());
      $arguments = new Twig_Node(array(
        $arg,
        $length,
      ));
      $filter = new $class($node, new Twig_Node_Expression_Constant('slice', $token
        ->getLine()), $arguments, $token
        ->getLine());
      $stream
        ->expect(Twig_Token::PUNCTUATION_TYPE, ']');
      return $filter;
    }
    $stream
      ->expect(Twig_Token::PUNCTUATION_TYPE, ']');
  }
  return new Twig_Node_Expression_GetAttr($node, $arg, $arguments, $type, $lineno);
}