You are here

public function Twig_Parser::subparse in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/twig/twig/lib/Twig/Parser.php \Twig_Parser::subparse()
1 call to Twig_Parser::subparse()
Twig_Parser::parse in vendor/twig/twig/lib/Twig/Parser.php
Converts a token stream to a node tree.

File

vendor/twig/twig/lib/Twig/Parser.php, line 126

Class

Twig_Parser
Default parser implementation.

Code

public function subparse($test, $dropNeedle = false) {
  $lineno = $this
    ->getCurrentToken()
    ->getLine();
  $rv = array();
  while (!$this->stream
    ->isEOF()) {
    switch ($this
      ->getCurrentToken()
      ->getType()) {
      case Twig_Token::TEXT_TYPE:
        $token = $this->stream
          ->next();
        $rv[] = new Twig_Node_Text($token
          ->getValue(), $token
          ->getLine());
        break;
      case Twig_Token::VAR_START_TYPE:
        $token = $this->stream
          ->next();
        $expr = $this->expressionParser
          ->parseExpression();
        $this->stream
          ->expect(Twig_Token::VAR_END_TYPE);
        $rv[] = new Twig_Node_Print($expr, $token
          ->getLine());
        break;
      case Twig_Token::BLOCK_START_TYPE:
        $this->stream
          ->next();
        $token = $this
          ->getCurrentToken();
        if ($token
          ->getType() !== Twig_Token::NAME_TYPE) {
          throw new Twig_Error_Syntax('A block must start with a tag name.', $token
            ->getLine(), $this
            ->getFilename());
        }
        if (null !== $test && call_user_func($test, $token)) {
          if ($dropNeedle) {
            $this->stream
              ->next();
          }
          if (1 === count($rv)) {
            return $rv[0];
          }
          return new Twig_Node($rv, array(), $lineno);
        }
        $subparser = $this->handlers
          ->getTokenParser($token
          ->getValue());
        if (null === $subparser) {
          if (null !== $test) {
            $e = new Twig_Error_Syntax(sprintf('Unexpected "%s" tag', $token
              ->getValue()), $token
              ->getLine(), $this
              ->getFilename());
            if (is_array($test) && isset($test[0]) && $test[0] instanceof Twig_TokenParserInterface) {
              $e
                ->appendMessage(sprintf(' (expecting closing tag for the "%s" tag defined near line %s).', $test[0]
                ->getTag(), $lineno));
            }
          }
          else {
            $e = new Twig_Error_Syntax(sprintf('Unknown "%s" tag.', $token
              ->getValue()), $token
              ->getLine(), $this
              ->getFilename());
            $e
              ->addSuggestions($token
              ->getValue(), array_keys($this->env
              ->getTags()));
          }
          throw $e;
        }
        $this->stream
          ->next();
        $node = $subparser
          ->parse($token);
        if (null !== $node) {
          $rv[] = $node;
        }
        break;
      default:
        throw new Twig_Error_Syntax('Lexer or parser ended up in unsupported state.', 0, $this
          ->getFilename());
    }
  }
  if (1 === count($rv)) {
    return $rv[0];
  }
  return new Twig_Node($rv, array(), $lineno);
}