public function Twig_ExpressionParser::parseSubscriptExpression

1 call to Twig_ExpressionParser::parseSubscriptExpression()
Twig_ExpressionParser::parsePostfixExpression in drupal/core/vendor/twig/twig/lib/Twig/ExpressionParser.php

File

drupal/core/vendor/twig/twig/lib/Twig/ExpressionParser.php, line 340

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_TemplateInterface::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 !== ($alias = $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());
      }
      $node = new Twig_Node_Expression_MethodCall($node, 'get' . $arg
        ->getAttribute('value'), $arguments, $lineno);
      $node
        ->setAttribute('safe', true);
      return $node;
    }
  }
  else {
    $type = Twig_TemplateInterface::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
      ->test(Twig_Token::PUNCTUATION_TYPE, ':')) {
      $slice = true;
      $stream
        ->next();
    }
    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);
}