From: Arnaud Le Blanc Date: Sat, 12 Nov 2011 15:54:35 +0000 (+0100) Subject: parser support for interpolated strings X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=098634e0b7eda9dc1e4d1315bcb1233470bfeb2b;p=web%2Fkonrad%2Ftwig.git parser support for interpolated strings --- diff --git a/lib/Twig/ExpressionParser.php b/lib/Twig/ExpressionParser.php index 33cc0dc..169ffb4 100644 --- a/lib/Twig/ExpressionParser.php +++ b/lib/Twig/ExpressionParser.php @@ -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();