Exemple #1
0
 /**
  * Expects a token (like $token->test()) and returns it or throw a syntax error.
  */
 public function expect($primary, $secondary = null)
 {
     $token = $this->current;
     if (!$token->test($primary, $secondary)) {
         throw new Twig_SyntaxError(sprintf('Unexpected token %s (%s expected, value: %s)', Twig_Token::getTypeAsString($token->getType()), Twig_Token::getTypeAsString($primary), $token->getValue()), $this->current->getLine());
     }
     $this->next();
     return $token;
 }
Exemple #2
0
    public function parsePrimaryExpression($assignment = false)
    {
        $token = $this->parser->getCurrentToken();
        switch ($token->getType()) {
            case Twig_Token::NAME_TYPE:
                $this->parser->getStream()->next();
                switch ($token->getValue()) {
                    case 'true':
                        $node = new Twig_Node_Expression_Constant(true, $token->getLine());
                        break;

                    case 'false':
                        $node = new Twig_Node_Expression_Constant(false, $token->getLine());
                        break;

                    case 'none':
                        $node = new Twig_Node_Expression_Constant(null, $token->getLine());
                        break;

                    default:
                        $cls = $assignment ? 'Twig_Node_Expression_AssignName' : 'Twig_Node_Expression_Name';
                        $node = new $cls($token->getValue(), $token->getLine());
                }
                break;

            case Twig_Token::NUMBER_TYPE:
            case Twig_Token::STRING_TYPE:
                $this->parser->getStream()->next();
                $node = new Twig_Node_Expression_Constant($token->getValue(), $token->getLine());
                break;

            default:
                if ($token->test(Twig_Token::OPERATOR_TYPE, '[')) {
                    $node = $this->parseArrayExpression();
                } elseif ($token->test(Twig_Token::OPERATOR_TYPE, '(')) {
                    $this->parser->getStream()->next();
                    $node = $this->parseExpression();
                    $this->parser->getStream()->expect(Twig_Token::OPERATOR_TYPE, ')');
                } else {
                    throw new Twig_SyntaxError(sprintf('Unexpected token "%s" of value "%s"', Twig_Token::getTypeAsString($token->getType()), $token->getValue()), $token->getLine());
                }
        }

        if (!$assignment) {
            $node = $this->parsePostfixExpression($node);
        }

        return $node;
    }
 /**
  * Expects a token (like $token->test()) and returns it or throw a syntax error.
  */
 public function expect($primary, $secondary = null)
 {
     $token = $this->current;
     if (!$token->test($primary, $secondary)) {
         throw new Twig_Error_Syntax(sprintf('Unexpected token "%s" of value "%s" ("%s" expected%s)', Twig_Token::getTypeAsString($token->getType()), $token->getValue(), Twig_Token::getTypeAsString($primary), $secondary ? sprintf(' with value "%s"', $secondary) : ''), $this->current->getLine());
     }
     $this->next();
     return $token;
 }
 public function parseHashExpression()
 {
     $stream = $this->parser->getStream();
     $stream->expect(Twig_Token::PUNCTUATION_TYPE, '{', 'A hash element was expected');
     $elements = array();
     while (!$stream->test(Twig_Token::PUNCTUATION_TYPE, '}')) {
         if (!empty($elements)) {
             $stream->expect(Twig_Token::PUNCTUATION_TYPE, ',', 'A hash value must be followed by a comma (,)');
             // trailing ,?
             if ($stream->test(Twig_Token::PUNCTUATION_TYPE, '}')) {
                 break;
             }
         }
         if (!$stream->test(Twig_Token::STRING_TYPE) && !$stream->test(Twig_Token::NUMBER_TYPE)) {
             throw new Twig_Error_Syntax(sprintf('A hash key must be a quoted string or a number (unexpected token "%s" of value "%s"', Twig_Token::getTypeAsString($stream->getCurrent()->getType()), $stream->getCurrent()->getValue()), $stream->getCurrent()->getLine());
         }
         $key = $stream->next()->getValue();
         $stream->expect(Twig_Token::PUNCTUATION_TYPE, ':', 'A hash key must be followed by a colon (:)');
         $elements[$key] = $this->parseExpression();
     }
     $stream->expect(Twig_Token::PUNCTUATION_TYPE, '}', 'An opened hash is not properly closed');
     return new Twig_Node_Expression_Array($elements, $stream->getCurrent()->getLine());
 }