You are here

public function Twig_ExpressionParser::parseSubscriptExpression in Translation template extractor 6.3

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

File

vendor/Twig/ExpressionParser.php, line 342

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());
      }
      $node = new Twig_Node_Expression_MethodCall($node, 'get' . $arg
        ->getAttribute('value'), $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);
}