You are here

public function Twig_TokenParser_For::parse in Translation template extractor 6.3

Same name and namespace in other branches
  1. 7.3 vendor/Twig/TokenParser/For.php \Twig_TokenParser_For::parse()

Parses a token and returns a node.

Parameters

Twig_Token $token A Twig_Token instance:

Return value

Twig_NodeInterface A Twig_NodeInterface instance

Overrides Twig_TokenParserInterface::parse

File

vendor/Twig/TokenParser/For.php, line 33

Class

Twig_TokenParser_For
Loops over each item of a sequence.

Code

public function parse(Twig_Token $token) {
  $lineno = $token
    ->getLine();
  $stream = $this->parser
    ->getStream();
  $targets = $this->parser
    ->getExpressionParser()
    ->parseAssignmentExpression();
  $stream
    ->expect(Twig_Token::OPERATOR_TYPE, 'in');
  $seq = $this->parser
    ->getExpressionParser()
    ->parseExpression();
  $ifexpr = null;
  if ($stream
    ->nextIf(Twig_Token::NAME_TYPE, 'if')) {
    $ifexpr = $this->parser
      ->getExpressionParser()
      ->parseExpression();
  }
  $stream
    ->expect(Twig_Token::BLOCK_END_TYPE);
  $body = $this->parser
    ->subparse(array(
    $this,
    'decideForFork',
  ));
  if ($stream
    ->next()
    ->getValue() == 'else') {
    $stream
      ->expect(Twig_Token::BLOCK_END_TYPE);
    $else = $this->parser
      ->subparse(array(
      $this,
      'decideForEnd',
    ), true);
  }
  else {
    $else = null;
  }
  $stream
    ->expect(Twig_Token::BLOCK_END_TYPE);
  if (count($targets) > 1) {
    $keyTarget = $targets
      ->getNode(0);
    $keyTarget = new Twig_Node_Expression_AssignName($keyTarget
      ->getAttribute('name'), $keyTarget
      ->getLine());
    $valueTarget = $targets
      ->getNode(1);
    $valueTarget = new Twig_Node_Expression_AssignName($valueTarget
      ->getAttribute('name'), $valueTarget
      ->getLine());
  }
  else {
    $keyTarget = new Twig_Node_Expression_AssignName('_key', $lineno);
    $valueTarget = $targets
      ->getNode(0);
    $valueTarget = new Twig_Node_Expression_AssignName($valueTarget
      ->getAttribute('name'), $valueTarget
      ->getLine());
  }
  if ($ifexpr) {
    $this
      ->checkLoopUsageCondition($stream, $ifexpr);
    $this
      ->checkLoopUsageBody($stream, $body);
  }
  return new Twig_Node_For($keyTarget, $valueTarget, $seq, $ifexpr, $body, $else, $lineno, $this
    ->getTag());
}