From: Fabien Potencier Date: Fri, 26 Nov 2010 17:58:54 +0000 (+0100) Subject: fixed several bugs in SimpleTokenParser (nested optional grammar, and support for... X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=a511b9bd64f2f5931822106194a1ee678569acb0;p=konrad%2Ftwig.git fixed several bugs in SimpleTokenParser (nested optional grammar, and support for , as a separator) --- diff --git a/lib/Twig/Grammar/Constant.php b/lib/Twig/Grammar/Constant.php index 9ea584b..1f523b8 100644 --- a/lib/Twig/Grammar/Constant.php +++ b/lib/Twig/Grammar/Constant.php @@ -10,6 +10,14 @@ */ 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; + } } diff --git a/lib/Twig/Grammar/Optional.php b/lib/Twig/Grammar/Optional.php index a16dbfb..3b98426 100644 --- a/lib/Twig/Grammar/Optional.php +++ b/lib/Twig/Grammar/Optional.php @@ -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; diff --git a/lib/Twig/SimpleTokenParser.php b/lib/Twig/SimpleTokenParser.php index 40c71ba..08c0290 100644 --- a/lib/Twig/SimpleTokenParser.php +++ b/lib/Twig/SimpleTokenParser.php @@ -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]); diff --git a/test/Twig/Tests/SimpleTokenParserTest.php b/test/Twig/Tests/SimpleTokenParserTest.php index a1abbfe..4db10fd 100644 --- a/test/Twig/Tests/SimpleTokenParserTest.php +++ b/test/Twig/Tests/SimpleTokenParserTest.php @@ -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') ) )