parser support for interpolated strings
authorArnaud Le Blanc <arnaud.lb@gmail.com>
Sat, 12 Nov 2011 15:54:35 +0000 (16:54 +0100)
committerArnaud Le Blanc <arnaud.lb@gmail.com>
Sat, 12 Nov 2011 15:54:35 +0000 (16:54 +0100)
lib/Twig/ExpressionParser.php

index 33cc0dc..169ffb4 100644 (file)
@@ -143,11 +143,15 @@ class Twig_ExpressionParser
                 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;
 
+            case Twig_Token::STRING_TYPE:
+            case Twig_Token::INTERPOLATION_START_TYPE:
+                $node = $this->parseStringExpression();
+                break;
+
             default:
                 if ($token->test(Twig_Token::PUNCTUATION_TYPE, '[')) {
                     $node = $this->parseArrayExpression();
@@ -161,6 +165,38 @@ class Twig_ExpressionParser
         return $this->parsePostfixExpression($node);
     }
 
+    public function parseStringExpression()
+    {
+        $stream = $this->parser->getStream();
+
+        $nodes = array();
+
+        while (true) {
+            if ($stream->test(Twig_Token::STRING_TYPE)) {
+                $token = $stream->next();
+                $nodes[] = new Twig_Node_Expression_Constant($token->getValue(), $token->getLine());
+            } else if ($stream->test(Twig_Token::INTERPOLATION_START_TYPE)) {
+                $stream->next();
+                $nodes[] = $this->parseExpression();
+                $stream->expect(Twig_Token::INTERPOLATION_END_TYPE);
+            } else {
+                break;
+            }
+        }
+
+        $expr = null;
+
+        foreach(array_reverse($nodes) as $node) {
+            if (null === $expr) {
+                $expr = $node;
+            } else {
+                $expr = new Twig_Node_Expression_Binary_Concat($node, $expr, $node->getLine());
+            }
+        }
+
+        return $expr;
+    }
+
     public function parseArrayExpression()
     {
         $stream = $this->parser->getStream();