fixed several bugs in SimpleTokenParser (nested optional grammar, and support for...
authorFabien Potencier <fabien.potencier@gmail.com>
Fri, 26 Nov 2010 17:58:54 +0000 (18:58 +0100)
committerFabien Potencier <fabien.potencier@gmail.com>
Fri, 26 Nov 2010 17:58:54 +0000 (18:58 +0100)
lib/Twig/Grammar/Constant.php
lib/Twig/Grammar/Optional.php
lib/Twig/SimpleTokenParser.php
test/Twig/Tests/SimpleTokenParserTest.php

index 9ea584b..1f523b8 100644 (file)
  */
 class Twig_Grammar_Constant extends Twig_Grammar
 {
+    protected $type;
+
+    public function __construct($name, $type = null)
+    {
+        $this->name = $name;
+        $this->type = null === $type ? Twig_Token::NAME_TYPE : $type;
+    }
+
     public function __toString()
     {
         return $this->name;
@@ -17,8 +25,13 @@ class Twig_Grammar_Constant extends Twig_Grammar
 
     public function parse(Twig_Token $token)
     {
-        $this->parser->getStream()->expect($this->name);
+        $this->parser->getStream()->expect($this->type, $this->name);
 
         return $this->name;
     }
+
+    public function getType()
+    {
+        return $this->type;
+    }
 }
index a16dbfb..3b98426 100644 (file)
@@ -39,7 +39,7 @@ class Twig_Grammar_Optional extends Twig_Grammar
     {
         // test if we have the optional element before consuming it
         if ($this->grammar[0] instanceof Twig_Grammar_Constant) {
-            if (!$this->parser->getStream()->test($this->grammar[0]->getName())) {
+            if (!$this->parser->getStream()->test($this->grammar[0]->getType(), $this->grammar[0]->getName())) {
                 return array();
             }
         } elseif ($this->grammar[0] instanceof Twig_Grammar_Name) {
@@ -56,7 +56,12 @@ class Twig_Grammar_Optional extends Twig_Grammar
         foreach ($this->grammar as $grammar) {
             $grammar->setParser($this->parser);
 
-            $elements[$grammar->getName()] = $grammar->parse($token);
+            $element = $grammar->parse($token);
+            if (is_array($element)) {
+                $elements = array_merge($elements, $element);
+            } else {
+                $elements[$grammar->getName()] = $element;
+            }
         }
 
         return $elements;
index 40c71ba..08c0290 100644 (file)
@@ -109,8 +109,11 @@ abstract class Twig_SimpleTokenParser extends Twig_TokenParser
                 }
                 $grammar->addGrammar(new $class($match[1]));
                 $cursor += strlen($match[0]);
-            } elseif (preg_match('/(\w+|,)/A', $str, $match, null, $cursor)) {
-                $grammar->addGrammar(new Twig_Grammar_Constant($match[1]));
+            } elseif (preg_match('/\w+/A', $str, $match, null, $cursor)) {
+                $grammar->addGrammar(new Twig_Grammar_Constant($match[0]));
+                $cursor += strlen($match[0]);
+            } elseif (preg_match('/,/A', $str, $match, null, $cursor)) {
+                $grammar->addGrammar(new Twig_Grammar_Constant($match[0], Twig_Token::PUNCTUATION_TYPE));
                 $cursor += strlen($match[0]);
             } elseif (preg_match('/\[/A', $str, $match, null, $cursor)) {
                 $cursor += strlen($match[0]);
index a1abbfe..4db10fd 100644 (file)
@@ -102,7 +102,7 @@ class Twig_Tests_SimpleTokenParserTest extends PHPUnit_Framework_TestCase
                     new Twig_Grammar_Constant('with'),
                     new Twig_Grammar_Array('arguments'),
                     new Twig_Grammar_Optional(
-                        new Twig_Grammar_Constant(','),
+                        new Twig_Grammar_Constant(',', Twig_Token::PUNCTUATION_TYPE),
                         new Twig_Grammar_Expression('optional')
                     )
                 )