Browse files

parser support for interpolated strings

  • Loading branch information...
1 parent c8bba24 commit 098634e0b7eda9dc1e4d1315bcb1233470bfeb2b @arnaud-lb arnaud-lb committed Nov 12, 2011
Showing with 37 additions and 1 deletion.
  1. +37 −1 lib/Twig/ExpressionParser.php
View
38 lib/Twig/ExpressionParser.php
@@ -143,11 +143,15 @@ public function parsePrimaryExpression()
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 @@ public function parsePrimaryExpression()
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();

0 comments on commit 098634e

Please sign in to comment.