Example #1
0
 public function subparse($test, $drop_needle = false)
 {
     $lineno = $this->stream->current->lineno;
     $rv = array();
     while (!$this->stream->eof) {
         switch ($this->stream->current->type) {
             case Twig_Token::TEXT_TYPE:
                 $token = $this->stream->next();
                 $rv[] = new Twig_Text($token->value, $token->lineno);
                 break;
             case Twig_Token::VAR_START_TYPE:
                 $token = $this->stream->next();
                 $expr = $this->parseExpression();
                 $this->stream->expect(Twig_Token::VAR_END_TYPE);
                 $rv[] = new Twig_Print($expr, $token->lineno);
                 break;
             case Twig_Token::BLOCK_START_TYPE:
                 $this->stream->next();
                 $token = $this->stream->current;
                 if ($token->type !== Twig_Token::NAME_TYPE) {
                     throw new Twig_SyntaxError('expected directive', $token->lineno);
                 }
                 if (!is_null($test) && call_user_func($test, $token)) {
                     if ($drop_needle) {
                         $this->stream->next();
                     }
                     return Twig_NodeList::fromArray($rv, $lineno);
                 }
                 if (!isset($this->handlers[$token->value])) {
                     throw new Twig_SyntaxError('unknown directive', $token->lineno);
                 }
                 $this->stream->next();
                 $node = call_user_func($this->handlers[$token->value], $token);
                 if (!is_null($node)) {
                     $rv[] = $node;
                 }
                 break;
             default:
                 assert(false, 'Lexer or parser ended up in ' . 'unsupported state.');
         }
     }
     return Twig_NodeList::fromArray($rv, $lineno);
 }